Skip to content

Commit

Permalink
feat: Add cz-message-helper config in package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
linpengteng committed Dec 16, 2022
1 parent 6ee8d1f commit 35db07a
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 210 deletions.
3 changes: 3 additions & 0 deletions .cz-message.en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
language: 'en' // 支持 cn | en
}
2 changes: 1 addition & 1 deletion .cz-message.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
language: 'en' // 支持 en | cn
language: 'cn' // 支持 cn | en
}
201 changes: 6 additions & 195 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <china.linpengteng@gmail.com>",
"files": [
"src/config.en.js",
"src/config.cn.js",
"index.js"
],
"keywords": [
"@commitlint/config-conventional",
"commit message customizable",
"commit message helper",
"commit lint",
"commitizen"
],
Expand All @@ -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",
Expand All @@ -53,6 +55,9 @@
"typescript": "^4.9.4"
},
"config": {
"cz-message-helper": {
"config": ".cz-message.en.js"
},
"commitizen": {
"path": "./index.js"
}
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions rollup.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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'
]
}
Expand Down
2 changes: 1 addition & 1 deletion src/config.cn.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
module.exports = {
questions: [
{
type: 'list',
Expand Down
2 changes: 1 addition & 1 deletion src/config.en.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
module.exports = {
questions: [
{
type: 'list',
Expand Down
4 changes: 2 additions & 2 deletions src/lib/megre-config.ts
Original file line number Diff line number Diff line change
@@ -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'


Expand Down
5 changes: 5 additions & 0 deletions src/lib/read-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 35db07a

Please sign in to comment.