Skip to content

Commit

Permalink
➕ update: add generation scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Mar 8, 2019
1 parent d35001d commit 2b0ff7d
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 0 deletions.
38 changes: 38 additions & 0 deletions scripts/lib/rules.js
Original file line number Diff line number Diff line change
@@ -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
)
})
)
35 changes: 35 additions & 0 deletions scripts/lib/utils.js
Original file line number Diff line number Diff line change
@@ -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
}
45 changes: 45 additions & 0 deletions scripts/update-docs-headers.js
Original file line number Diff line number Diff line change
@@ -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'))
)
}
36 changes: 36 additions & 0 deletions scripts/update-docs-index.js
Original file line number Diff line number Diff line change
@@ -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/<wbr>${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')}
`
)
27 changes: 27 additions & 0 deletions scripts/update-recommended-rules.js
Original file line number Diff line number Diff line change
@@ -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 ')}
},
}`)
)
26 changes: 26 additions & 0 deletions scripts/update.js
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 2b0ff7d

Please sign in to comment.