From 2b0ff7d9226db4af1deaebc1fe4271f7dcf1c50d Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Fri, 8 Mar 2019 15:41:37 +0900 Subject: [PATCH] :heavy_plus_sign: update: add generation scripts --- scripts/lib/rules.js | 38 ++++++++++++++++++++++++ scripts/lib/utils.js | 35 ++++++++++++++++++++++ scripts/update-docs-headers.js | 45 +++++++++++++++++++++++++++++ scripts/update-docs-index.js | 36 +++++++++++++++++++++++ scripts/update-recommended-rules.js | 27 +++++++++++++++++ scripts/update.js | 26 +++++++++++++++++ 6 files changed, 207 insertions(+) create mode 100644 scripts/lib/rules.js create mode 100644 scripts/lib/utils.js create mode 100644 scripts/update-docs-headers.js create mode 100644 scripts/update-docs-index.js create mode 100644 scripts/update-recommended-rules.js create mode 100644 scripts/update.js diff --git a/scripts/lib/rules.js b/scripts/lib/rules.js new file mode 100644 index 00000000..027a2df2 --- /dev/null +++ b/scripts/lib/rules.js @@ -0,0 +1,38 @@ +/** + * @fileoverview Rules loading script library + * @author kazuya kawaguchi (a.k.a. kazupon) + * Forked by https://github.com/mysticatea/eslint-plugin-eslint-comments/tree/master/scripts/lib/rules.js + */ +'use strict' + +const { readdirSync } = require('fs') +const { resolve, basename } = require('path') + +const rules = readdirSync(resolve(__dirname, '../../lib/rules')) + .map(fileName => basename(fileName, '.js')) + .map(name => { + const meta = require(`../../lib/rules/${name}`).meta + return { + id: `vue-i18n/${name}`, + name, + category: String(meta.docs.category), + description: String(meta.docs.description), + recommended: Boolean(meta.docs.recommended), + fixable: Boolean(meta.fixable), + deprecated: Boolean(meta.deprecated), + replacedBy: meta.docs.replacedBy || null + } + }) + +module.exports = rules +module.exports.withCategories = [ + 'Best Practices', + 'Stylistic Issues' +].map( + category => ({ + category, + rules: rules.filter( + rule => rule.category === category && !rule.deprecated + ) + }) +) diff --git a/scripts/lib/utils.js b/scripts/lib/utils.js new file mode 100644 index 00000000..ce12ec5b --- /dev/null +++ b/scripts/lib/utils.js @@ -0,0 +1,35 @@ +/** + * @fileoverview Utility script library + * @author kazuya kawaguchi (a.k.a. kazupon) + * Forked by https://github.com/mysticatea/eslint-plugin-eslint-comments/tree/master/scripts/lib/utils.js + */ +'use strict' + +const { readdirSync } = require('fs') +const { basename } = require('path') +const { CLIEngine } = require('eslint') +const linter = new CLIEngine({ fix: true }) + +function format (text) { + const lintResult = linter.executeOnText(text) + return lintResult.results[0].output || text +} + +function createIndex (dirPath) { + const dirName = basename(dirPath) + return format(`/** DON'T EDIT THIS FILE; was created by scripts. */ + 'use strict' + + module.exports = { + ${readdirSync(dirPath) + .map(file => basename(file, '.js')) + .map(id => `'${id}': require('./${dirName}/${id}'),`) + .join('\n ')} + } + `) +} + +module.exports = { + createIndex, + format +} diff --git a/scripts/update-docs-headers.js b/scripts/update-docs-headers.js new file mode 100644 index 00000000..01fff208 --- /dev/null +++ b/scripts/update-docs-headers.js @@ -0,0 +1,45 @@ +/** + * @fileoverview Update docs headers script + * @author kazuya kawaguchi (a.k.a. kazupon) + * Forked by https://github.com/mysticatea/eslint-plugin-eslint-comments/tree/master/scripts/update-docs-headers.js + */ +'use strict' + +const { writeFileSync, readFileSync } = require('fs') +const { join } = require('path') +const rules = require('./lib/rules') +const PLACE_HOLDER = /^#[^\n]*\n+> .+\n+(?:- .+\n)*\n*/u + +for (const rule of rules) { + const filePath = join(__dirname, `../docs/rules/${rule.name}.md`) + const headerLines = [`# ${rule.id}`, '', `> ${rule.description}`] + + if (rule.recommended || rule.deprecated || rule.fixable) { + headerLines.push('') + } + + if (rule.deprecated) { + headerLines.push( + `- :warning:️ This rule was **deprecated** and replaced by ${rule.replacedBy + .map(id => `[${id}](${id}.md) rule`) + .join(', ')}.` + ) + } else if (rule.recommended) { + headerLines.push( + '- :star: The `"extends": "plugin:vue-i18n/recommended"` property in a configuration file enables this rule.' + ) + } + + if (rule.fixable) { + headerLines.push( + '- :black_nib:️ The `--fix` option on the [command line](http://eslint.org/docs/user-guide/command-line-interface#fix) can automatically fix some of the problems reported by this rule.' + ) + } + headerLines.push('', '') + + writeFileSync( + filePath, + readFileSync(filePath, 'utf8') + .replace(PLACE_HOLDER, headerLines.join('\n')) + ) +} diff --git a/scripts/update-docs-index.js b/scripts/update-docs-index.js new file mode 100644 index 00000000..9d1ad330 --- /dev/null +++ b/scripts/update-docs-index.js @@ -0,0 +1,36 @@ +/** + * @fileoverview Update docs index script + * @author kazuya kawaguchi (a.k.a. kazupon) + * Forked by https://github.com/mysticatea/eslint-plugin-eslint-comments/tree/master/scripts/update-docs-index.js + */ +'use strict' + +const { writeFileSync } = require('fs') +const { resolve } = require('path') +const { withCategories } = require('./lib/rules') + +function toTableRow (rule) { + const mark = `${rule.recommended ? '🌟' : ''}${rule.fixable ? '✒️' : ''}` + const link = `[vue-i18n/${rule.name}](./${rule.name}.html)` + const description = rule.description || '(no description)' + return `| ${link} | ${description} | ${mark} |` +} + +function toCategorySection ({ category, rules }) { + return `## ${category} + +| Rule ID | Description | | +|:--------|:------------|:---| +${rules.map(toTableRow).join('\n')} +` +} + +writeFileSync( + resolve(__dirname, '../docs/rules/README.md'), `# Available Rules + +- :star: mark: the rule which is enabled by \`vue-i18n/recommended\` preset. +- :black_nib: mark: the rule which is fixable by \`eslint --fix\` command. + +${withCategories.map(toCategorySection).join('\n')} +` +) diff --git a/scripts/update-recommended-rules.js b/scripts/update-recommended-rules.js new file mode 100644 index 00000000..9b7edc7e --- /dev/null +++ b/scripts/update-recommended-rules.js @@ -0,0 +1,27 @@ +/** + * @fileoverview Update recommended rules + * @author kazuya kawaguchi (a.k.a. kazupon) + * Forked by https://github.com/mysticatea/eslint-plugin-eslint-comments/tree/master/scripts/update-recommended-rules.js + */ +'use stricut' + +const { writeFileSync } = require('fs') +const { resolve } = require('path') +const rules = require('./lib/rules') +const { format } = require('./lib/utils') + +// recommended.js +writeFileSync( + resolve(__dirname, '../lib/configs/recommended.js'), + format(`/** DON'T EDIT THIS FILE; was created by scripts. */ +'use strict' + +module.exports = { + plugins: ['vue-i18n'], + rules: { + ${rules.filter(rule => rule.recommended) + .map(rule => `'${rule.id}': 'error',`) + .join('\n ')} + }, +}`) +) diff --git a/scripts/update.js b/scripts/update.js new file mode 100644 index 00000000..a8d9c91b --- /dev/null +++ b/scripts/update.js @@ -0,0 +1,26 @@ +/** + * @fileoverview Update script + * @author kazuya kawaguchi (a.k.a. kazupon) + * Forked by https://github.com/mysticatea/eslint-plugin-eslint-comments/tree/master/scripts/update.js + */ +'use stricut' + +const { writeFileSync } = require('fs') +const { resolve } = require('path') +const { createIndex } = require('./lib/utils') + +// docs. +require('./update-docs-headers') +require('./update-docs-index') + +// recommended rules. +require('./update-recommended-rules') + +// indices. +for (const dirPath of [ + resolve(__dirname, '../lib/configs'), + resolve(__dirname, '../lib/rules'), + resolve(__dirname, '../lib/utils') +]) { + writeFileSync(`${dirPath}.js`, createIndex(dirPath)) +}