Skip to content

Commit

Permalink
⚒ add 'npm run new' command
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Sep 4, 2019
1 parent 11d396f commit abbfb27
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -34,11 +34,12 @@
"rimraf": "^2.6.3"
},
"scripts": {
"build": "node scripts/update.js",
"build": "node scripts/update",
"clean": "rimraf .nyc_output coverage",
"codecov": "nyc report --reporter text-lcov | codecov --pipe --disable=gcov -t $CODECOV_TOKEN",
"coverage": "opener ./coverage/lcov-report/index.html",
"lint": "eslint lib scripts tests/lib .eslintrc.js",
"new": "node scripts/new-rule",
"pretest": "npm run -s lint",
"test": "nyc npm run -s test:_mocha",
"test:ci": "nyc npm run -s test:_mocha",
Expand Down
108 changes: 108 additions & 0 deletions scripts/new-rule.js
@@ -0,0 +1,108 @@
/**
* @author Toru Nagashima <https://github.com/mysticatea>
* See LICENSE file in root directory for full license.
*/
"use strict"

const cp = require("child_process")
const fs = require("fs")
const path = require("path")
const logger = console

// main
;(ruleId => {
if (ruleId == null) {
logger.error("Usage: npm run new <RuleID>")
process.exitCode = 1
return
}
if (!/^[\w-]+$/u.test(ruleId)) {
logger.error("Invalid RuleID '%s'.", ruleId)
process.exitCode = 1
return
}

const ruleFile = path.resolve(__dirname, `../lib/rules/${ruleId}.js`)
const testFile = path.resolve(__dirname, `../tests/lib/rules/${ruleId}.js`)
const docFile = path.resolve(__dirname, `../docs/rules/${ruleId}.md`)

fs.writeFileSync(
ruleFile,
`/**
* @author Toru Nagashima <https://github.com/mysticatea>
* See LICENSE file in root directory for full license.
*/
"use strict"
module.exports = {
meta: {
docs: {
description: "",
category: "",
recommended: false,
url: "",
},
fixable: null,
messages: {
},
schema: [],
type: "problem",
},
create(context) {
return {}
},
}
`
)
fs.writeFileSync(
testFile,
`/**
* @author Toru Nagashima <https://github.com/mysticatea>
* See LICENSE file in root directory for full license.
*/
"use strict"
const { RuleTester } = require("eslint")
const rule = require("../../../lib/rules/${ruleId}.js")
new RuleTester().run("${ruleId}", rule, {
valid: [],
invalid: [],
})
`
)
fs.writeFileSync(
docFile,
`# (es/${ruleId})
(TODO: Why this rule is useful.)
## 📖 Rule Details
(TODO: How this rule will report code.)
👍 Examples of **correct** code for this rule:
\`\`\`js
/*eslint node/${ruleId}: error */
\`\`\`
👎 Examples of **incorrect** code for this rule:
\`\`\`js
/*eslint node/${ruleId}: error */
\`\`\`
## ⚙ Options
\`\`\`json
{
"node/${ruleId}": ["error", ...]
}
\`\`\`
`
)

cp.execSync(`code "${ruleFile}"`)
cp.execSync(`code "${testFile}"`)
cp.execSync(`code "${docFile}"`)
})(process.argv[2])

0 comments on commit abbfb27

Please sign in to comment.