11// MIT © 2016 azu
22"use strict" ;
3- const spawn = require ( ' cross-spawn-promise' ) ;
3+ const spawn = require ( " cross-spawn-promise" ) ;
44const path = require ( "path" ) ;
55const rimraf = require ( "rimraf" ) ;
6- const chalk = require ( ' chalk' ) ;
6+ const chalk = require ( " chalk" ) ;
77
88/**
99 * @see https://github.com/textlint/textlint-rule-template
10- * @param {string } ruleName
11- * @param {Object } [options]
10+ * @param {string } projectName
11+ * @param {{
12+ * yes: boolean
13+ * yarn: boolean
14+ * cwd: string
15+ * }} [options]
1216 * @returns {Promise }
1317 */
14- module . exports = function ( ruleName , options = { } ) {
18+ module . exports = function ( projectName , options = { } ) {
1519 const useYarn = options . yarn !== undefined ;
1620 const useYes = options . yes !== undefined ;
17- const cwd = process . cwd ( ) ;
18- const ruleDir = path . join ( cwd , ruleName ) ;
21+ const isInitInCurrentDir = projectName === "." ;
22+ const ruleName = isInitInCurrentDir
23+ ? path . dirname ( projectName )
24+ : `textlint-rule-${ projectName . replace ( / ^ t e x t l i n t - r u l e - / , "" ) } ` ;
25+ const ruleDir = isInitInCurrentDir ? options . cwd : path . join ( options . cwd , ruleName ) ;
1926 return spawn ( `git` , [
20- "clone" ,
21- "--depth=1" ,
22- "https://github.com/textlint/textlint-rule-template.git" ,
23- ruleName
24- ] , { stdio : 'inherit' } ) . then ( ( ) => {
25- console . log ( chalk . green ( `cd ${ ruleDir } ` ) ) ;
26- process . chdir ( ruleDir ) ;
27- } ) . then ( ( ) => {
28- const gitDir = path . join ( ruleDir , ".git" ) ;
29- rimraf . sync ( gitDir )
30- } ) . then ( ( ) => {
31- const README = path . join ( ruleDir , "README.md" ) ;
32- rimraf . sync ( README ) ;
33- } ) . then ( ( ) => {
34- return spawn ( "git" , [
35- "init"
36- ] , { stdio : 'inherit' } ) ;
37- } ) . then ( ( ) => {
38- console . log ( chalk . green ( `Input information about your textlint rule` ) ) ;
39- return spawn ( "npm" ,
40- [ "init" ] . concat ( useYes ? [ "--yes" ] : [ ] ) ,
41- { stdio : 'inherit' } ) ;
42- } ) . then ( ( ) => {
43- console . log ( chalk . green ( `Wait... Installing npm packages for development` ) ) ;
44- if ( useYarn ) {
45- return spawn ( "yarn" ,
46- [
47- "install"
48- ] ,
49- { stdio : 'inherit' } ) ;
50-
51- } else {
52- return spawn ( "npm" ,
53- [
54- "install"
55- ] ,
56- { stdio : 'inherit' } ) ;
57-
58- }
59- } ) . then ( ( ) => {
60- console . log ( chalk . green ( `Setup your README!` ) ) ;
61- return spawn ( `./node_modules/.bin/textlint-scripts` , [
62- "init"
63- ] , { stdio : 'inherit' } ) ;
64- } ) ;
65- } ;
27+ "clone" , "--depth=1" , "https://github.com/textlint/textlint-rule-template.git" ,
28+ isInitInCurrentDir ? "." : ruleName
29+ ] , {
30+ stdio : "inherit"
31+ } )
32+ . then ( ( ) => {
33+ if ( ! isInitInCurrentDir ) {
34+ console . log ( chalk . green ( `cd ${ ruleDir } ` ) ) ;
35+ process . chdir ( ruleDir ) ;
36+ }
37+ } )
38+ . then ( ( ) => {
39+ const gitDir = path . join ( ruleDir , ".git" ) ;
40+ rimraf . sync ( gitDir ) ;
41+ } )
42+ . then ( ( ) => {
43+ const README = path . join ( ruleDir , "README.md" ) ;
44+ rimraf . sync ( README ) ;
45+ } )
46+ . then ( ( ) => {
47+ return spawn ( "git" , [ "init" ] , { stdio : "inherit" } ) ;
48+ } )
49+ . then ( ( ) => {
50+ console . log ( chalk . green ( `Input information about your textlint rule` ) ) ;
51+ return spawn ( "npm" , [ "init" ] . concat ( useYes ? [ "--yes" ] : [ ] ) , { stdio : "inherit" } ) ;
52+ } )
53+ . then ( ( ) => {
54+ console . log ( chalk . green ( `Wait... Installing npm packages for development` ) ) ;
55+ if ( useYarn ) {
56+ return spawn ( "yarn" , [ "install" ] , { stdio : "inherit" } ) ;
57+ } else {
58+ return spawn ( "npm" , [ "install" ] , { stdio : "inherit" } ) ;
59+ }
60+ } )
61+ . then ( ( ) => {
62+ console . log ( chalk . green ( `Initialize your README!` ) ) ;
63+ return spawn ( `./node_modules/.bin/textlint-scripts` , [ "init" ] , { stdio : "inherit" } ) ;
64+ } ) ;
65+ } ;
0 commit comments