Skip to content

Commit

Permalink
chore: generator script
Browse files Browse the repository at this point in the history
  • Loading branch information
veritem committed Nov 4, 2023
1 parent c420dec commit 6e3c5c4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 164 deletions.
73 changes: 42 additions & 31 deletions scripts/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function generate() {
rules.map(async (rule) => {
const ruleName = rule.replace(/\.ts$/, '')
const content = await import(
path.resolve(__dirname, `../src/rules/${ruleName}.ts`)
path.resolve(__dirname, `../src/rules/${ruleName}`)
)

if (content.default.meta.docs.recommended === 'strict') {
Expand All @@ -32,44 +32,55 @@ async function generate() {
})
)

const allRulesNames = allRules.reduce((acc, { name }) => {
return {
...acc,
[name]: 'warn'
}
}, {})
function capitalizeLetterAfterDash(inputString) {
return inputString.replace(/-([a-zA-Z])/g, function (match, letter) {
return letter.toUpperCase()
})
}

const allRecommendedRulesNames = recommendedRules.reduce((acc, { name }) => {
const allImports = allRules.map((item) => {
return {
...acc,
[name]: 'error'
name: item.name,
isRecommended: item.rule.meta.docs.recommended === 'strict',
import: `import ${capitalizeLetterAfterDash(item.name)} from './rules/${
item.name
}'`
}
}, {})
})

const allImports = allRules.reduce((acc, { name }) => {
const namesWithoutDash = name.replace(/-/g, '')
return {
...acc,
name,
import: `import ${namesWithoutDash} from './rules/${name}.ts'`
const createConfig = (rules) => {
const config = {
plugins: ['vitest'],
rules: {}
}
}, {})

const createConfig = (rules: Record<string, string>) => ({
plugins: ['vitest'],
rules: Object.keys(rules).reduce((acc, ruleName) => {
return {
...acc,
[`vitest/${ruleName}`]: rules[ruleName]
}
}, {})
})
rules.forEach((rule) => {
config.rules[`vitest/${rule.name}`] = rule.ruleImportName
})
}

console.log(createConfig(allImports.map((item) => ({
name: item.name,
ruleImportName: capitalizeLetterAfterDash(item.name)
}))))

const returnObj = {
rules: allRulesNames,
rules: allImports.map((item) => item.name),
configs: {
all: createConfig(allRulesNames),
recommended: createConfig(allRecommendedRulesNames)
all: createConfig(
allImports.map((item) => ({
name: item.name,
ruleImportName: capitalizeLetterAfterDash(item.name)
}))
),
recommended: createConfig(
allImports
.filter((item) => item.isRecommended)
.map((item) => ({
name: item.name,
ruleImportName: capitalizeLetterAfterDash(item.name)
}))
)
},
environments: {
env: {
Expand All @@ -93,7 +104,6 @@ async function generate() {

const date = new Date()

// write all Object conflig to index file
fs.writeFileSync(
path.resolve(__dirname, '../src/index.ts'),
`// This file was last auto generated by scripts/generate.ts at: ${
Expand All @@ -103,6 +113,7 @@ async function generate() {
' ' +
Intl.DateTimeFormat().resolvedOptions().timeZone
}
${allImports.map((item) => item.import).join('\n')} \n\n
export default ${JSON.stringify(returnObj)}`,
'utf8'
)
Expand Down
136 changes: 3 additions & 133 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,133 +1,3 @@
// This file was last auto generated by scripts/generate.ts at: 11/3/2023 8:10:03 PM America/New_York
export default {
rules: {
'consistent-test-filename': 'warn',
'max-expect': 'warn',
'max-expects': 'warn',
'max-nested-describe': 'warn',
'no-alias-methods': 'warn',
'no-commented-out-tests': 'warn',
'no-conditional-expect': 'warn',
'no-conditional-in-test': 'warn',
'no-conditional-tests': 'warn',
'no-disabled-tests': 'warn',
'no-done-callback': 'warn',
'no-duplicate-hooks': 'warn',
'no-focused-tests': 'warn',
'no-hooks': 'warn',
'consistent-test-it': 'warn',
'no-interpolation-in-snapshots': 'warn',
'no-large-snapshots': 'warn',
'no-mocks-import': 'warn',
'no-restricted-matchers': 'warn',
'no-restricted-vi-methods': 'warn',
'no-standalone-expect': 'warn',
'no-test-prefixes': 'warn',
'no-test-return-statement': 'warn',
'prefer-called-with': 'warn',
'prefer-comparison-matcher': 'warn',
'prefer-each': 'warn',
'prefer-equality-matcher': 'warn',
'prefer-expect-resolves': 'warn',
'prefer-hooks-in-order': 'warn',
'prefer-hooks-on-top': 'warn',
'prefer-lowercase-title': 'warn',
'prefer-mock-promise-shorthand': 'warn',
'prefer-snapshot-hint': 'warn',
'prefer-spy-on': 'warn',
'prefer-strict-equal': 'warn',
'prefer-to-be-falsy': 'warn',
'prefer-to-be-object': 'warn',
'prefer-to-be-truthy': 'warn',
'prefer-to-contain': 'warn',
'prefer-to-have-length': 'warn',
'prefer-todo': 'warn',
'require-hook': 'warn',
'require-to-throw-message': 'warn',
'require-top-level-describe': 'warn',
'unbound-method': 'warn',
'valid-expect': 'warn',
'valid-title': 'warn'
},
configs: {
all: {
plugins: ['vitest'],
rules: {
'vitest/consistent-test-filename': 'warn',
'vitest/max-expect': 'warn',
'vitest/max-expects': 'warn',
'vitest/max-nested-describe': 'warn',
'vitest/no-alias-methods': 'warn',
'vitest/no-commented-out-tests': 'warn',
'vitest/no-conditional-expect': 'warn',
'vitest/no-conditional-in-test': 'warn',
'vitest/no-conditional-tests': 'warn',
'vitest/no-disabled-tests': 'warn',
'vitest/no-done-callback': 'warn',
'vitest/no-duplicate-hooks': 'warn',
'vitest/no-focused-tests': 'warn',
'vitest/no-hooks': 'warn',
'vitest/consistent-test-it': 'warn',
'vitest/no-interpolation-in-snapshots': 'warn',
'vitest/no-large-snapshots': 'warn',
'vitest/no-mocks-import': 'warn',
'vitest/no-restricted-matchers': 'warn',
'vitest/no-restricted-vi-methods': 'warn',
'vitest/no-standalone-expect': 'warn',
'vitest/no-test-prefixes': 'warn',
'vitest/no-test-return-statement': 'warn',
'vitest/prefer-called-with': 'warn',
'vitest/prefer-comparison-matcher': 'warn',
'vitest/prefer-each': 'warn',
'vitest/prefer-equality-matcher': 'warn',
'vitest/prefer-expect-resolves': 'warn',
'vitest/prefer-hooks-in-order': 'warn',
'vitest/prefer-hooks-on-top': 'warn',
'vitest/prefer-lowercase-title': 'warn',
'vitest/prefer-mock-promise-shorthand': 'warn',
'vitest/prefer-snapshot-hint': 'warn',
'vitest/prefer-spy-on': 'warn',
'vitest/prefer-strict-equal': 'warn',
'vitest/prefer-to-be-falsy': 'warn',
'vitest/prefer-to-be-object': 'warn',
'vitest/prefer-to-be-truthy': 'warn',
'vitest/prefer-to-contain': 'warn',
'vitest/prefer-to-have-length': 'warn',
'vitest/prefer-todo': 'warn',
'vitest/require-hook': 'warn',
'vitest/require-to-throw-message': 'warn',
'vitest/require-top-level-describe': 'warn',
'vitest/unbound-method': 'warn',
'vitest/valid-expect': 'warn',
'vitest/valid-title': 'warn'
}
},
recommended: {
plugins: ['vitest'],
rules: {
'vitest/expect-expect': 'error',
'vitest/no-identical-title': 'error',
'vitest/prefer-to-be': 'error',
'vitest/valid-describe-callback': 'error'
}
}
},
environments: {
env: {
globals: {
suite: true,
test: true,
describe: true,
it: true,
expect: true,
assert: true,
vitest: true,
vi: true,
beforeAll: true,
afterAll: true,
beforeEach: true,
afterEach: true
}
}
}
}
// This file was last auto generated by scripts/generate.ts at: 11/3/2023 8:51:50 PM America/New_York

export default { rules: ['no-conditional-tests', 'no-done-callback', 'no-duplicate-hooks', 'no-disabled-tests', 'no-conditional-expect', 'no-conditional-in-test', 'consistent-test-filename', 'consistent-test-it', 'no-alias-methods', 'no-commented-out-tests', 'max-expect', 'max-expects', 'max-nested-describe', 'no-focused-tests', 'no-interpolation-in-snapshots', 'no-large-snapshots', 'no-mocks-import', 'no-hooks', 'no-restricted-matchers', 'no-restricted-vi-methods', 'no-standalone-expect', 'no-test-prefixes', 'no-test-return-statement', 'prefer-called-with', 'prefer-comparison-matcher', 'prefer-each', 'prefer-equality-matcher', 'prefer-expect-resolves', 'prefer-hooks-in-order', 'prefer-hooks-on-top', 'prefer-lowercase-title', 'prefer-mock-promise-shorthand', 'prefer-snapshot-hint', 'prefer-spy-on', 'prefer-strict-equal', 'prefer-to-be-falsy', 'prefer-to-be-object', 'prefer-to-contain', 'prefer-to-be-truthy', 'prefer-to-have-length', 'prefer-todo', 'require-hook', 'require-top-level-describe', 'require-to-throw-message', 'unbound-method', 'valid-title', 'valid-expect'], configs: {}, environments: { env: { globals: { suite: true, test: true, describe: true, it: true, expect: true, assert: true, vitest: true, vi: true, beforeAll: true, afterAll: true, beforeEach: true, afterEach: true } } } }

0 comments on commit 6e3c5c4

Please sign in to comment.