Skip to content

Commit 389a3d2

Browse files
committed
feat(scripts): support current dir(.)
``` $ npm init textlint-rule . ```
1 parent ec4bc2b commit 389a3d2

File tree

6 files changed

+510
-321
lines changed

6 files changed

+510
-321
lines changed

bin/cmd.js

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/usr/bin/env node
2-
'use strict';
3-
const meow = require('meow');
4-
const chalk = require('chalk');
5-
const updateNotifier = require('update-notifier');
6-
const pkg = require('../package.json');
7-
const cliHandler = require('../lib/cli-handler');
8-
const cli = meow(`
2+
"use strict";
3+
const meow = require("meow");
4+
const chalk = require("chalk");
5+
const updateNotifier = require("update-notifier");
6+
const pkg = require("../package.json");
7+
const cliHandler = require("../lib/cli-handler");
8+
const cli = meow(
9+
`
910
Usage
1011
$ create-textlint-rule rule-name
1112
@@ -15,12 +16,17 @@ const cli = meow(`
1516
--yes Pass --yes all for initializing process
1617
1718
Examples
18-
$ create-textlint-rule awesome-rule
19-
`, {
20-
alias: {
21-
h: 'help'
19+
# create textlint-rule-example directory and install
20+
$ create-textlint-rule example
21+
# install to current directory
22+
$ create-textlint-rule .
23+
`,
24+
{
25+
alias: {
26+
h: "help"
27+
}
2228
}
23-
});
29+
);
2430
/*
2531
{
2632
input: ['unicorns'],
@@ -36,12 +42,18 @@ updateNotifier({
3642
if (cli.input.length === 0 || cli.flags.help) {
3743
cli.showHelp();
3844
} else {
39-
cliHandler(cli.input[0], cli.flags).then(() => {
40-
process.exit(0);
41-
}).catch(error => {
42-
console.log(chalk.red(`✗ Error: ${error.message}`));
43-
console.log();
44-
console.error(error);
45-
process.exit(1);
46-
});
45+
cliHandler(cli.input[0], {
46+
yes: cli.flags.yes,
47+
yarn: cli.flags.yarn,
48+
cwd: process.cwd()
49+
})
50+
.then(() => {
51+
process.exit(0);
52+
})
53+
.catch(error => {
54+
console.log(chalk.red(`✗ Error: ${error.message}`));
55+
console.log();
56+
console.error(error);
57+
process.exit(1);
58+
});
4759
}

lib/cli-handler.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
// MIT © 2016 azu
22
"use strict";
3-
const chalk = require('chalk');
3+
const path = require("path");
4+
const chalk = require("chalk");
45
const createTextlintRule = require("./scripts/create-textlint-rule");
5-
module.exports = function(projectName, flags) {
6-
const ruleName = `textlint-rule-${projectName.replace(/^textlint-rule-/, '')}`;
7-
return createTextlintRule(ruleName, flags).then(() => {
8-
console.log(chalk.green(`✔ Complete: Let's create textlint rule`));
6+
/**
7+
* @param {string} projectName
8+
* @param {{
9+
* yes: boolean
10+
* yarn: boolean
11+
* cwd: string
12+
* }} options
13+
* @returns {Promise<any | never>}
14+
*/
15+
module.exports = function(projectName, options = {}) {
16+
return createTextlintRule(projectName, options).then(() => {
17+
console.log(chalk.green(`✔ Complete: Let's write textlint rule`));
918
});
10-
};
19+
};

lib/scripts/create-textlint-rule.js

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
11
// MIT © 2016 azu
22
"use strict";
3-
const spawn = require('cross-spawn-promise');
3+
const spawn = require("cross-spawn-promise");
44
const path = require("path");
55
const 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(/^textlint-rule-/, "")}`;
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+
};

package.json

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"create-textlint-rule": "bin/cmd.js"
1717
},
1818
"scripts": {
19-
"test": "./test/test.sh"
19+
"test": "./test/test.sh",
20+
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\""
2021
},
2122
"keywords": [
2223
"textlint",
@@ -33,11 +34,32 @@
3334
},
3435
"homepage": "https://github.com/textlint/create-textlint-rule",
3536
"dependencies": {
36-
"chalk": "^1.1.3",
37-
"cross-spawn": "^5.0.1",
37+
"chalk": "^2.4.1",
38+
"cross-spawn": "^6.0.5",
3839
"cross-spawn-promise": "^0.10.1",
39-
"meow": "^3.7.0",
40-
"rimraf": "^2.5.4",
41-
"update-notifier": "^2.1.0"
40+
"meow": "^5.0.0",
41+
"rimraf": "^2.6.3",
42+
"update-notifier": "^2.5.0"
43+
},
44+
"devDependencies": {
45+
"husky": "^1.3.1",
46+
"lint-staged": "^8.1.0",
47+
"prettier": "^1.15.3"
48+
},
49+
"prettier": {
50+
"singleQuote": false,
51+
"printWidth": 120,
52+
"tabWidth": 4
53+
},
54+
"husky": {
55+
"hooks": {
56+
"precommit": "lint-staged"
57+
}
58+
},
59+
"lint-staged": {
60+
"*.{js,jsx,ts,tsx,css}": [
61+
"prettier --write",
62+
"git add"
63+
]
4264
}
43-
}
65+
}

test/test.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
#!/bin/bash
22
set -eux
3+
# test rule-name
34
declare currentDir=$(cd $(dirname $0);pwd)
45
declare dirName=$(basename "${currentDir}")
56
declare parentDir=$(dirname "${currentDir}")
67
declare testRuleName="test-rule-name";
7-
# test
8+
echo "Run: create-textlint-rule ${test-rule-name}"
89
cd ${currentDir}
910
node ${parentDir}/bin/cmd.js "${testRuleName}" --yes
1011
rm -rf "${currentDir}/textlint-rule-${testRuleName}"
12+
# test .
13+
echo "Run: create-textlint-rule ."
14+
mkdir "textlint-rule-${testRuleName}"
15+
cd "textlint-rule-${testRuleName}"
16+
node ${parentDir}/bin/cmd.js "." --yes
17+
rm -rf "${currentDir}/textlint-rule-${testRuleName}"

0 commit comments

Comments
 (0)