Skip to content

Commit

Permalink
feat(scripts): support current dir(.)
Browse files Browse the repository at this point in the history
```
$ npm init textlint-rule .
```
  • Loading branch information
azu committed Jan 3, 2019
1 parent ec4bc2b commit 389a3d2
Show file tree
Hide file tree
Showing 6 changed files with 510 additions and 321 deletions.
52 changes: 32 additions & 20 deletions bin/cmd.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const chalk = require('chalk');
const updateNotifier = require('update-notifier');
const pkg = require('../package.json');
const cliHandler = require('../lib/cli-handler');
const cli = meow(`
"use strict";
const meow = require("meow");
const chalk = require("chalk");
const updateNotifier = require("update-notifier");
const pkg = require("../package.json");
const cliHandler = require("../lib/cli-handler");
const cli = meow(
`
Usage
$ create-textlint-rule rule-name
Expand All @@ -15,12 +16,17 @@ const cli = meow(`
--yes Pass --yes all for initializing process
Examples
$ create-textlint-rule awesome-rule
`, {
alias: {
h: 'help'
# create textlint-rule-example directory and install
$ create-textlint-rule example
# install to current directory
$ create-textlint-rule .
`,
{
alias: {
h: "help"
}
}
});
);
/*
{
input: ['unicorns'],
Expand All @@ -36,12 +42,18 @@ updateNotifier({
if (cli.input.length === 0 || cli.flags.help) {
cli.showHelp();
} else {
cliHandler(cli.input[0], cli.flags).then(() => {
process.exit(0);
}).catch(error => {
console.log(chalk.red(`✗ Error: ${error.message}`));
console.log();
console.error(error);
process.exit(1);
});
cliHandler(cli.input[0], {
yes: cli.flags.yes,
yarn: cli.flags.yarn,
cwd: process.cwd()
})
.then(() => {
process.exit(0);
})
.catch(error => {
console.log(chalk.red(`✗ Error: ${error.message}`));
console.log();
console.error(error);
process.exit(1);
});
}
21 changes: 15 additions & 6 deletions lib/cli-handler.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
// MIT © 2016 azu
"use strict";
const chalk = require('chalk');
const path = require("path");
const chalk = require("chalk");
const createTextlintRule = require("./scripts/create-textlint-rule");
module.exports = function(projectName, flags) {
const ruleName = `textlint-rule-${projectName.replace(/^textlint-rule-/, '')}`;
return createTextlintRule(ruleName, flags).then(() => {
console.log(chalk.green(`✔ Complete: Let's create textlint rule`));
/**
* @param {string} projectName
* @param {{
* yes: boolean
* yarn: boolean
* cwd: string
* }} options
* @returns {Promise<any | never>}
*/
module.exports = function(projectName, options = {}) {
return createTextlintRule(projectName, options).then(() => {
console.log(chalk.green(`✔ Complete: Let's write textlint rule`));
});
};
};
106 changes: 53 additions & 53 deletions lib/scripts/create-textlint-rule.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
// MIT © 2016 azu
"use strict";
const spawn = require('cross-spawn-promise');
const spawn = require("cross-spawn-promise");
const path = require("path");
const rimraf = require("rimraf");
const chalk = require('chalk');
const chalk = require("chalk");

/**
* @see https://github.com/textlint/textlint-rule-template
* @param {string} ruleName
* @param {Object} [options]
* @param {string} projectName
* @param {{
* yes: boolean
* yarn: boolean
* cwd: string
* }} [options]
* @returns {Promise}
*/
module.exports = function(ruleName, options = {}) {
module.exports = function(projectName, options = {}) {
const useYarn = options.yarn !== undefined;
const useYes = options.yes !== undefined;
const cwd = process.cwd();
const ruleDir = path.join(cwd, ruleName);
const isInitInCurrentDir = projectName === ".";
const ruleName = isInitInCurrentDir
? path.dirname(projectName)
: `textlint-rule-${projectName.replace(/^textlint-rule-/, "")}`;
const ruleDir = isInitInCurrentDir ? options.cwd : path.join(options.cwd, ruleName);
return spawn(`git`, [
"clone",
"--depth=1",
"https://github.com/textlint/textlint-rule-template.git",
ruleName
], {stdio: 'inherit'}).then(() => {
console.log(chalk.green(`cd ${ruleDir}`));
process.chdir(ruleDir);
}).then(() => {
const gitDir = path.join(ruleDir, ".git");
rimraf.sync(gitDir)
}).then(() => {
const README = path.join(ruleDir, "README.md");
rimraf.sync(README);
}).then(() => {
return spawn("git", [
"init"
], {stdio: 'inherit'});
}).then(() => {
console.log(chalk.green(`Input information about your textlint rule`));
return spawn("npm",
["init"].concat(useYes ? ["--yes"] : []),
{stdio: 'inherit'});
}).then(() => {
console.log(chalk.green(`Wait... Installing npm packages for development`));
if (useYarn) {
return spawn("yarn",
[
"install"
],
{stdio: 'inherit'});

} else {
return spawn("npm",
[
"install"
],
{stdio: 'inherit'});

}
}).then(() => {
console.log(chalk.green(`Setup your README!`));
return spawn(`./node_modules/.bin/textlint-scripts`, [
"init"
], {stdio: 'inherit'});
});
};
"clone", "--depth=1", "https://github.com/textlint/textlint-rule-template.git",
isInitInCurrentDir ? "." : ruleName
], {
stdio: "inherit"
})
.then(() => {
if (!isInitInCurrentDir) {
console.log(chalk.green(`cd ${ruleDir}`));
process.chdir(ruleDir);
}
})
.then(() => {
const gitDir = path.join(ruleDir, ".git");
rimraf.sync(gitDir);
})
.then(() => {
const README = path.join(ruleDir, "README.md");
rimraf.sync(README);
})
.then(() => {
return spawn("git", ["init"], { stdio: "inherit" });
})
.then(() => {
console.log(chalk.green(`Input information about your textlint rule`));
return spawn("npm", ["init"].concat(useYes ? ["--yes"] : []), { stdio: "inherit" });
})
.then(() => {
console.log(chalk.green(`Wait... Installing npm packages for development`));
if (useYarn) {
return spawn("yarn", ["install"], { stdio: "inherit" });
} else {
return spawn("npm", ["install"], { stdio: "inherit" });
}
})
.then(() => {
console.log(chalk.green(`Initialize your README!`));
return spawn(`./node_modules/.bin/textlint-scripts`, ["init"], { stdio: "inherit" });
});
};
36 changes: 29 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"create-textlint-rule": "bin/cmd.js"
},
"scripts": {
"test": "./test/test.sh"
"test": "./test/test.sh",
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\""
},
"keywords": [
"textlint",
Expand All @@ -33,11 +34,32 @@
},
"homepage": "https://github.com/textlint/create-textlint-rule",
"dependencies": {
"chalk": "^1.1.3",
"cross-spawn": "^5.0.1",
"chalk": "^2.4.1",
"cross-spawn": "^6.0.5",
"cross-spawn-promise": "^0.10.1",
"meow": "^3.7.0",
"rimraf": "^2.5.4",
"update-notifier": "^2.1.0"
"meow": "^5.0.0",
"rimraf": "^2.6.3",
"update-notifier": "^2.5.0"
},
"devDependencies": {
"husky": "^1.3.1",
"lint-staged": "^8.1.0",
"prettier": "^1.15.3"
},
"prettier": {
"singleQuote": false,
"printWidth": 120,
"tabWidth": 4
},
"husky": {
"hooks": {
"precommit": "lint-staged"
}
},
"lint-staged": {
"*.{js,jsx,ts,tsx,css}": [
"prettier --write",
"git add"
]
}
}
}
9 changes: 8 additions & 1 deletion test/test.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
#!/bin/bash
set -eux
# test rule-name
declare currentDir=$(cd $(dirname $0);pwd)
declare dirName=$(basename "${currentDir}")
declare parentDir=$(dirname "${currentDir}")
declare testRuleName="test-rule-name";
# test
echo "Run: create-textlint-rule ${test-rule-name}"
cd ${currentDir}
node ${parentDir}/bin/cmd.js "${testRuleName}" --yes
rm -rf "${currentDir}/textlint-rule-${testRuleName}"
# test .
echo "Run: create-textlint-rule ."
mkdir "textlint-rule-${testRuleName}"
cd "textlint-rule-${testRuleName}"
node ${parentDir}/bin/cmd.js "." --yes
rm -rf "${currentDir}/textlint-rule-${testRuleName}"
Loading

0 comments on commit 389a3d2

Please sign in to comment.