Skip to content

Commit abbfb27

Browse files
committed
⚒ add 'npm run new' command
1 parent 11d396f commit abbfb27

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
"rimraf": "^2.6.3"
3535
},
3636
"scripts": {
37-
"build": "node scripts/update.js",
37+
"build": "node scripts/update",
3838
"clean": "rimraf .nyc_output coverage",
3939
"codecov": "nyc report --reporter text-lcov | codecov --pipe --disable=gcov -t $CODECOV_TOKEN",
4040
"coverage": "opener ./coverage/lcov-report/index.html",
4141
"lint": "eslint lib scripts tests/lib .eslintrc.js",
42+
"new": "node scripts/new-rule",
4243
"pretest": "npm run -s lint",
4344
"test": "nyc npm run -s test:_mocha",
4445
"test:ci": "nyc npm run -s test:_mocha",

scripts/new-rule.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @author Toru Nagashima <https://github.com/mysticatea>
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
"use strict"
6+
7+
const cp = require("child_process")
8+
const fs = require("fs")
9+
const path = require("path")
10+
const logger = console
11+
12+
// main
13+
;(ruleId => {
14+
if (ruleId == null) {
15+
logger.error("Usage: npm run new <RuleID>")
16+
process.exitCode = 1
17+
return
18+
}
19+
if (!/^[\w-]+$/u.test(ruleId)) {
20+
logger.error("Invalid RuleID '%s'.", ruleId)
21+
process.exitCode = 1
22+
return
23+
}
24+
25+
const ruleFile = path.resolve(__dirname, `../lib/rules/${ruleId}.js`)
26+
const testFile = path.resolve(__dirname, `../tests/lib/rules/${ruleId}.js`)
27+
const docFile = path.resolve(__dirname, `../docs/rules/${ruleId}.md`)
28+
29+
fs.writeFileSync(
30+
ruleFile,
31+
`/**
32+
* @author Toru Nagashima <https://github.com/mysticatea>
33+
* See LICENSE file in root directory for full license.
34+
*/
35+
"use strict"
36+
37+
module.exports = {
38+
meta: {
39+
docs: {
40+
description: "",
41+
category: "",
42+
recommended: false,
43+
url: "",
44+
},
45+
fixable: null,
46+
messages: {
47+
},
48+
schema: [],
49+
type: "problem",
50+
},
51+
create(context) {
52+
return {}
53+
},
54+
}
55+
`
56+
)
57+
fs.writeFileSync(
58+
testFile,
59+
`/**
60+
* @author Toru Nagashima <https://github.com/mysticatea>
61+
* See LICENSE file in root directory for full license.
62+
*/
63+
"use strict"
64+
const { RuleTester } = require("eslint")
65+
const rule = require("../../../lib/rules/${ruleId}.js")
66+
67+
new RuleTester().run("${ruleId}", rule, {
68+
valid: [],
69+
invalid: [],
70+
})
71+
`
72+
)
73+
fs.writeFileSync(
74+
docFile,
75+
`# (es/${ruleId})
76+
77+
(TODO: Why this rule is useful.)
78+
79+
## 📖 Rule Details
80+
81+
(TODO: How this rule will report code.)
82+
83+
👍 Examples of **correct** code for this rule:
84+
85+
\`\`\`js
86+
/*eslint node/${ruleId}: error */
87+
\`\`\`
88+
89+
👎 Examples of **incorrect** code for this rule:
90+
91+
\`\`\`js
92+
/*eslint node/${ruleId}: error */
93+
\`\`\`
94+
95+
## ⚙ Options
96+
97+
\`\`\`json
98+
{
99+
"node/${ruleId}": ["error", ...]
100+
}
101+
\`\`\`
102+
`
103+
)
104+
105+
cp.execSync(`code "${ruleFile}"`)
106+
cp.execSync(`code "${testFile}"`)
107+
cp.execSync(`code "${docFile}"`)
108+
})(process.argv[2])

0 commit comments

Comments
 (0)