From 35db07aa4a013ca5f02980786bf5d1b5df94d4b9 Mon Sep 17 00:00:00 2001 From: linpengteng Date: Fri, 16 Dec 2022 16:07:50 +0800 Subject: [PATCH] feat: Add cz-message-helper config in package.json --- .cz-message.en.js | 3 + .cz-message.js | 2 +- index.js | 201 ++-------------------------------------- package.json | 11 ++- pnpm-lock.yaml | 8 +- rollup.config.cjs | 8 +- src/config.cn.js | 2 +- src/config.en.js | 2 +- src/lib/megre-config.ts | 4 +- src/lib/read-config.ts | 5 + tsconfig.json | 6 +- 11 files changed, 42 insertions(+), 210 deletions(-) create mode 100644 .cz-message.en.js diff --git a/.cz-message.en.js b/.cz-message.en.js new file mode 100644 index 0000000..2458f55 --- /dev/null +++ b/.cz-message.en.js @@ -0,0 +1,3 @@ +module.exports = { + language: 'en' // 支持 cn | en +} \ No newline at end of file diff --git a/.cz-message.js b/.cz-message.js index 33179b7..2d1bfae 100644 --- a/.cz-message.js +++ b/.cz-message.js @@ -1,3 +1,3 @@ module.exports = { - language: 'en' // 支持 en | cn + language: 'cn' // 支持 cn | en } \ No newline at end of file diff --git a/index.js b/index.js index 7025058..0e60533 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,8 @@ var rimraf = require('rimraf'); var node_child_process = require('node:child_process'); var findConfig = require('find-config'); var wordWrap = require('word-wrap'); +var enConfig = require('./src/config.en.js'); +var cnConfig = require('./src/config.cn.js'); var log = console; @@ -93,7 +95,11 @@ function editor (file, cb) { } var readConfig = (config = '.cz-message.js') => { + const pkg = findConfig.require('package.json', { home: false }); const czConfig = findConfig.require(config, { home: false }); + if (typeof pkg === 'object' && typeof pkg.config === 'object' && typeof pkg.config['cz-message-helper'] === 'object' && typeof pkg.config['cz-message-helper'].config === 'string') { + return findConfig.require(pkg.config['cz-message-helper'].config, { home: false }) || czConfig; + } if (czConfig) { return czConfig; } @@ -111,201 +117,6 @@ var buildCommit = (config, answers) => { return initialize(config.templater?.(answers, wrap) || ''); }; -var enConfig = { - questions: [ - { - type: 'list', - name: 'type', - message: 'Please select the type of change that you\'re committing:', - choices: [ - { value: 'fix', name: 'fix: -------- A bug fix' }, - { value: 'feat', name: 'feat: ------- A new feature' }, - { value: 'begin', name: 'begin: ------ Begin new repository' }, - { value: 'docs', name: 'docs: ------- Documentation only changes' }, - { value: 'style', name: 'style: ------ Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)' }, - { value: 'chore', name: 'chore: ------ Changes to the build process or auxiliary tools and libraries such as documentation generation' }, - { value: 'refactor', name: 'refactor: --- A code change that neither fixes a bug nor adds a feature' }, - { value: 'perf', name: 'perf: ------- A code change that improves performance' }, - { value: 'test', name: 'test: ------- Add Test Unit' }, - { value: 'revert', name: 'revert: ----- Revert to a commit' }, - { value: 'merge', name: 'merge: ------ Merge from branches' }, - { value: 'wip', name: 'wip: -------- Work in progress' } - ] - }, - - { - type: 'list', - name: 'scope', - message: 'Please select the SCOPE of this change (optional):', - choices() { - return [ - { name: 'empty', value: false }, - { name: 'custom', value: 'custom' } - ] - } - }, - - { - type: 'input', - name: 'customScope', - message: 'Please input the custom SCOPE of this change:', - when(answers) { - return answers.scope === 'custom' - }, - filter(value, answers) { - answers.scope = value || ''; - return value || '' - } - }, - - { - type: 'input', - name: 'subject', - message: 'Please write a SHORT tense description of the change(word number less than 50):', - validate(value) { - return value.length > 50 - ? `Exceed limit: 50` - : true - } - }, - - { - type: 'input', - name: 'body', - message: 'Please provide a LONGER description of the change (optional). Use "\\n" to break new line:' - }, - - { - type: 'input', - name: 'breaking', - message: 'Please list any BREAKING CHANGES (optional):', - when(answers) { - return ['feat', 'fix'].includes(answers.type.toLowerCase()) - } - }, - - { - type: 'input', - name: 'footer', - message: 'Please list any ISSUES CLOSED by this change (optional). Eg: #31, #34:' - } - ], - - templater: (answers, wrap) => { - let template = ''; - - template += answers.type ? `${answers.type}` : ``; - template += answers.scope ? `(${answers.scope})` : ``; - template += answers.subject ? `: ${answers.subject}` : ``; - template += answers.body ? `\n\n${wrap(answers.body)}` : ``; - template += answers.breaking ? `\n\nBREAKING CHANGE: ${wrap(answers.breaking)}` : ``; - template += answers.footer ? `\n\nISSUES CLOSED: ${wrap(answers.footer)}` : ``; - - return template - }, - - language: 'en' -}; - -var cnConfig = { - questions: [ - { - type: 'list', - name: 'type', - message: '请选择要提交的更改类型:', - choices: [ - { value: 'fix', name: 'fix: -------- 修复BUG' }, - { value: 'feat', name: 'feat: ------- 新功能' }, - { value: 'begin', name: 'begin: ------ 创建新存储库' }, - { value: 'docs', name: 'docs: ------- 仅文档更改' }, - { value: 'style', name: 'style: ------ 不影响代码运行的更改(调整空白、格式、缺少分号等)' }, - { value: 'chore', name: 'chore: ------ 对构建过程或辅助工具的更改以及文档生成等库' }, - { value: 'refactor', name: 'refactor: --- 重构架构或代码' }, - { value: 'perf', name: 'perf: ------ 改进性能的代码更改' }, - { value: 'test', name: 'test: ------ 添加测试单元' }, - { value: 'revert', name: 'revert: ----- 回退至某一个版本' }, - { value: 'merge', name: 'merge: ------ 合并一个分支, 解决冲突分支' }, - { value: 'wip', name: 'wip: -------- 正在进行中的工作' } - ] - }, - - { - type: 'list', - name: 'scope', - message: '请选择更改的范围:', - choices() { - return [ - { name: '无', value: false }, - { name: '自定义', value: 'custom' } - ] - }, - filter(value, answers) { - return value || '' - } - }, - - { - type: 'input', - name: 'customScope', - message: '请输入自定义的变更的范围(可选):', - when(answers) { - return answers.scope === 'custom' - }, - filter(value, answers) { - answers.scope = value || ''; - return value || '' - } - }, - - { - type: 'input', - name: 'subject', - message: '请简明扼要的摘要描述(建议字数在50字内):', - validate(value) { - return value.length > 50 - ? `[subject] Exceed limit: 50` - : true - } - }, - - { - type: 'input', - name: 'body', - message: '请提供更详细的变更说明(可选), 使用“\\n”换行:' - }, - - { - type: 'input', - name: 'breaking', - message: '请列出任何重大变化(可选)\n', - when(answers) { - return ['feat', 'fix'].includes(answers.type.toLowerCase()) - } - }, - - { - type: 'input', - name: 'footer', - message: '请列出此更改关闭的任何问题(可选), 例如: #31,#34:' - } - ], - - templater: (answers, wrap) => { - let template = ''; - - template += answers.type ? `${answers.type}` : ``; - template += answers.scope ? `(${answers.scope})` : ``; - template += answers.subject ? `: ${answers.subject}` : ``; - template += answers.body ? `\n\n${wrap(answers.body)}` : ``; - template += answers.breaking ? `\n\nBREAKING CHANGE: ${wrap(answers.breaking)}` : ``; - template += answers.footer ? `\n\nISSUES CLOSED: ${wrap(answers.footer)}` : ``; - - return template - }, - - language: 'cn' -}; - var megreConfig = (cfg) => { const def = cfg?.language === 'cn' ? cnConfig diff --git a/package.json b/package.json index 6aa39f1..8aaa88c 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,17 @@ { "name": "cz-message-helper", - "version": "1.1.6", + "version": "1.1.7", "description": "A commit message helper for commitizen", "license": "MIT", "author": "lin pengteng ", "files": [ + "src/config.en.js", + "src/config.cn.js", "index.js" ], "keywords": [ - "@commitlint/config-conventional", "commit message customizable", + "commit message helper", "commit lint", "commitizen" ], @@ -32,7 +34,7 @@ "devDependencies": { "@commitlint/cli": "^17.3.0", "@commitlint/config-conventional": "^17.3.0", - "@rollup/plugin-commonjs": "^23.0.4", + "@rollup/plugin-commonjs": "^23.0.5", "@rollup/plugin-node-resolve": "^15.0.1", "@rollup/plugin-typescript": "^10.0.1", "@rollup/pluginutils": "^5.0.2", @@ -53,6 +55,9 @@ "typescript": "^4.9.4" }, "config": { + "cz-message-helper": { + "config": ".cz-message.en.js" + }, "commitizen": { "path": "./index.js" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7ba1f6a..02b0a54 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3,7 +3,7 @@ lockfileVersion: 5.4 specifiers: '@commitlint/cli': ^17.3.0 '@commitlint/config-conventional': ^17.3.0 - '@rollup/plugin-commonjs': ^23.0.4 + '@rollup/plugin-commonjs': ^23.0.5 '@rollup/plugin-node-resolve': ^15.0.1 '@rollup/plugin-typescript': ^10.0.1 '@rollup/pluginutils': ^5.0.2 @@ -36,7 +36,7 @@ dependencies: devDependencies: '@commitlint/cli': 17.3.0 '@commitlint/config-conventional': 17.3.0 - '@rollup/plugin-commonjs': 23.0.4_rollup@3.7.4 + '@rollup/plugin-commonjs': 23.0.5_rollup@3.7.4 '@rollup/plugin-node-resolve': 15.0.1_rollup@3.7.4 '@rollup/plugin-typescript': 10.0.1_fhibmf72xnv5tve6nwed265eae '@rollup/pluginutils': 5.0.2_rollup@3.7.4 @@ -328,8 +328,8 @@ packages: fastq: 1.14.0 dev: true - /@rollup/plugin-commonjs/23.0.4_rollup@3.7.4: - resolution: {integrity: sha512-bOPJeTZg56D2MCm+TT4psP8e8Jmf1Jsi7pFUMl8BN5kOADNzofNHe47+84WVCt7D095xPghC235/YKuNDEhczg==} + /@rollup/plugin-commonjs/23.0.5_rollup@3.7.4: + resolution: {integrity: sha512-IwI51j5kCmLMYsErEvZAID/pg7Z1qgyVJ+QIPDDIg1AOPbIGbdTCjuHDWIBCtoF1dvMkXfQv7B2eTadjnLRbmA==} engines: {node: '>=14.0.0'} peerDependencies: rollup: ^2.68.0||^3.0.0 diff --git a/rollup.config.cjs b/rollup.config.cjs index 9d9eb11..c4eb61f 100644 --- a/rollup.config.cjs +++ b/rollup.config.cjs @@ -17,7 +17,9 @@ module.exports = defineConfig([ exports: 'auto', sourcemap: false, entryFileNames: '[name].js', - chunkFileNames: '[name]-[hash].js' + chunkFileNames: '[name]-[hash].js', + paths: id => /.+(\/config\.(cn|en)\.(js|ts))/.test(id) ? id.replace(/.+(\/config\.(cn|en)\.(js|ts))/, './src/$1') : undefined + } ], plugins: [ @@ -31,9 +33,11 @@ module.exports = defineConfig([ 'node:path', 'node:fs', 'node:os', - 'inquirer', + '../config.cn.js', + '../config.en.js', 'find-config', 'word-wrap', + 'inquirer', 'rimraf' ] } diff --git a/src/config.cn.js b/src/config.cn.js index b624b07..85b88d0 100644 --- a/src/config.cn.js +++ b/src/config.cn.js @@ -1,4 +1,4 @@ -export default { +module.exports = { questions: [ { type: 'list', diff --git a/src/config.en.js b/src/config.en.js index 581ac5c..431787f 100644 --- a/src/config.en.js +++ b/src/config.en.js @@ -1,4 +1,4 @@ -export default { +module.exports = { questions: [ { type: 'list', diff --git a/src/lib/megre-config.ts b/src/lib/megre-config.ts index fd11aa0..cfa528a 100644 --- a/src/lib/megre-config.ts +++ b/src/lib/megre-config.ts @@ -1,7 +1,7 @@ import type { Answers, ListQuestion, InputQuestion, NumberQuestion, ExpandQuestion, ConfirmQuestion } from 'inquirer' import buildCommit from './build-commit' -import enConfig from '../config.en' -import cnConfig from '../config.cn' +import enConfig from '../config.en.js' +import cnConfig from '../config.cn.js' import log from '../util/logger' diff --git a/src/lib/read-config.ts b/src/lib/read-config.ts index cadcbca..c11fa2d 100644 --- a/src/lib/read-config.ts +++ b/src/lib/read-config.ts @@ -2,8 +2,13 @@ import findConfig from 'find-config' import log from '../util/logger' export default (config = '.cz-message.js') => { + const pkg = findConfig.require('package.json', { home: false }) const czConfig = findConfig.require(config, { home: false }) + if (typeof pkg === 'object' && typeof pkg.config === 'object' && typeof pkg.config['cz-message-helper'] === 'object' && typeof pkg.config['cz-message-helper'].config === 'string') { + return findConfig.require(pkg.config['cz-message-helper'].config, { home: false }) || czConfig + } + if (czConfig) { return czConfig } diff --git a/tsconfig.json b/tsconfig.json index 7bf0f6d..1edc7c1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -27,7 +27,11 @@ "*.ts", "*.js", "*.cjs", - "*.mjs" + "*.mjs", + "src/.config.en.js", + "src/.config.en.js", + "src/.config.cn.js", + "src/.config.cn.js" ], "exclude": [ "dist/**/*.ts",