Skip to content

Commit

Permalink
chore(package.json): git message,eslint 加入git hook
Browse files Browse the repository at this point in the history
  • Loading branch information
BoBoooooo committed Jan 5, 2020
1 parent c6e28b9 commit 0ca4a55
Show file tree
Hide file tree
Showing 14 changed files with 545 additions and 1 deletion.
3 changes: 3 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['./lib/lint'],
};
112 changes: 112 additions & 0 deletions lib/cz/engine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* eslint-disable */
'format cjs';

const wrap = require('word-wrap');
const longest = require('longest');
const rightPad = require('right-pad');
const chalk = require('chalk');

const filter = array => array.filter(x => x);

// 获取选择列表
const getList = (obj) => {
const objLeng = longest(Object.keys(obj)).length * 2 + 1;

return Object.keys(obj).map(key => ({
name: `${rightPad(`${key}:`, objLeng / 2, ' ')} ${obj[key].description}`,
value: key,
}));
};

module.exports = function (options) {
const typeList = getList(options.types);

return {
prompter(cz, commit) {
console.log(chalk.yellow('\n标题会在100个字符后进行裁剪。 主体内容每行会在100个字符后自动换行,手动换行请直接输入"\\n"。\n'));
cz.prompt([
{
type: 'list',
name: 'type',
message: '选择你提交的信息类型:',
choices: typeList,
}, {
type: 'string',
name: 'scope',
message: '(非必填)本次提交的改变所影响的范围?',
}, {
type: 'input',
name: 'subject',
validate(str) {
const charLen = 3;
if (str.length > charLen) {
return str.length > charLen;
}
console.log(chalk.yellow(`字符长度大于${charLen}`));
},
message: '(必填)写一个简短的变化描述:',
}, {
type: 'input',
name: 'body',
message: '(非必填)提供更详细的变更描述:',
}, {
type: 'confirm',
name: 'isBreaking',
message: '是否存在不兼容变更?',
default: false,
}, {
type: 'input',
name: 'breaking',
message: '列出所有的不兼容变更:\n',
when(answers) {
return answers.isBreaking;
},
}, {
type: 'confirm',
name: 'isIssueAffected',
message: '此次变更是否影响某些打开的 issue ?',
default: false,
}, {
type: 'input',
name: 'issues',
message: '列出此次改动引用的所有 issues (如:"fix #123", "Closes #123, #124"):\n',
when(answers) {
return answers.isIssueAffected;
},
},
])
.then((answers) => {
const maxLineWidth = 100;

const wrapOptions = {
trim: true,
newline: '\n',
indent: '',
width: maxLineWidth,
};

// 判断影响范围是否输入
const scope = answers.scope ? `(${answers.scope.trim()})` : '';

// 限制短描述为 100 个字符
const head = (`${answers.type + scope}: ${answers.subject.trim()}`).slice(0, maxLineWidth);

// 限制详细描述最长宽度为 100 个字符串
const body = wrap(answers.body, wrapOptions);

// Apply breaking change prefix, removing it if already present
let breaking = answers.breaking ? answers.breaking.trim() : '';

// 如果手动输入了 不兼容变更,则过滤掉,最后进行长度限制
breaking = breaking ? `不兼容变更: ${breaking.replace(/^不兼容变更: /, '')}` : '';
breaking = wrap(breaking, wrapOptions);

const issues = answers.issues ? wrap(answers.issues, wrapOptions) : '';

const footer = filter([breaking, issues]).join('\n\n');

commit(`${head}\n\n${body}\n\n${footer}`);
});
},
};
};
70 changes: 70 additions & 0 deletions lib/cz/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'format cjs';

const engine = require('./engine');

module.exports = engine({
types: {
feat: {
description: '🌟 一个新功能',
title: '🌟 新功能',
},
fix: {
description: '🐛 一个 bug 修复',
title: '🐛 Bug 修复',
},
perf: {
description: '🚀 提升性能的代码更改',
title: '🚀 性能优化',
},
style: {
description: '🎨 对代码含义无影响的改动(空格,格式化,等,非 UI 变动)',
title: '🎨 代码样式',
},
docs: {
description: '📝 只有文档发生改变',
title: '📝 文档',
},
test: {
description: '🔧 添加一些缺失的测试或者修正已存在的测试',
title: '🔧 测试',
},
refactor: {
description: '🔨 既不是修复 bug 也不是添加新功能的代码更改',
title: '🔨 代码重构',
},
chore: {
description: '🏠 影响构建系统或外部依赖的更改(例如:gulp,npm,webpack)',
title: '🏠 构建',
},
ci: {
description: '📦 持续集成的配置文件和脚本的改变(例如: Travis, Circle)',
title: '📦 持续集成',
},
revert: {
description: '🔙 撤销上一次的提交',
title: '🔙 撤销',
},
},
scopes: {
global: {
description: '影响整个项目',
title: 'global',
},
ui: {
description: 'UI 界面',
title: 'ui',
},
data: {
description: '数据变化',
title: 'data',
},
component: {
description: '影响公共组件使用',
title: 'component',
},
unknown: {
description: '不知道影响范围',
title: 'unknown',
},
},
});
55 changes: 55 additions & 0 deletions lib/lint/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module.exports = {
parserPreset: {
parserOpts: {
headerPattern: /^(\w*|[\u4e00-\u9fa5]*)(?:[\(\(](.*)[\)\)])?[\:\:] (.*)/,
headerCorrespondence: [
'type',
'scope',
'subject'
],
referenceActions: [
'close',
'closes',
'closed',
'fix',
'fixes',
'fixed',
'resolve',
'resolves',
'resolved'
],
issuePrefixes: ['#'],
noteKeywords: ['BREAKING CHANGE', '不兼容变更'],
fieldPattern: /^-(.*?)-$/,
revertPattern: /^Revert\s"([\s\S]*)"\s*This reverts commit (\w*)\./,
revertCorrespondence: ['header', 'hash'],
warn() {},
mergePattern: null,
mergeCorrespondence: null
},
},
rules: {
'body-leading-blank': [2, 'always'],
'footer-leading-blank': [1, 'always'],
'header-max-length': [2, 'always', 108],
'subject-empty': [2, 'never'],
'type-empty': [2, 'never'],
'type-enum': [
2,
'always',
[
'feat',
'fix',
'perf',
'style',
'docs',
'test',
'refactor',
'build',
'ci',
'chore',
'revert'
],
],
},
};
8 changes: 8 additions & 0 deletions lib/log/conventional-changelog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@


/* eslint-disable */
const parserOpts = require('./parser-opts');
const writerOpts = require('./writer-opts');

module.exports = Q.all([parserOpts, writerOpts])
.spread((parserOpts, writerOpts) => ({ parserOpts, writerOpts }));
33 changes: 33 additions & 0 deletions lib/log/conventional-recommended-bump.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@


const parserOpts = require('./parser-opts');

module.exports = {
parserOpts,

whatBump: (commits) => {
let level = 2;
let breakings = 0;
let features = 0;

commits.forEach((commit) => {
console.log(commit);
if (commit.notes.length > 0) {
breakings += commit.notes.length;
level = 0;
} else if (commit.type === 'feat' || commit.type === '新功能') {
features += 1;
if (level === 2) {
level = 1;
}
}
});

return {
level,
reason: breakings === 1
? `There is ${breakings} BREAKING CHANGE and ${features} features`
: `There are ${breakings} BREAKING CHANGES and ${features} features`,
};
},
};
11 changes: 11 additions & 0 deletions lib/log/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable */
const Q = require('q');
const conventionalChangelog = require('./conventional-changelog');
const parserOpts = require('./parser-opts');
const recommendedBumpOpts = require('./conventional-recommended-bump');
const writerOpts = require('./writer-opts');

module.exports = Q.all([conventionalChangelog, parserOpts, recommendedBumpOpts, writerOpts])
.spread((conventionalChangelog, parserOpts, recommendedBumpOpts, writerOpts) => ({
conventionalChangelog, parserOpts, recommendedBumpOpts, writerOpts,
}));
14 changes: 14 additions & 0 deletions lib/log/parser-opts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


/* eslint-disable */
module.exports = {
headerPattern: /^(\w*|[\u4e00-\u9fa5]*)(?:[\(\(](.*)[\)\)])?[\:\:] (.*)$/,
headerCorrespondence: [
'type',
'scope',
'subject',
],
noteKeywords: ['BREAKING CHANGE', '不兼容变更'],
revertPattern: /^revert:\s([\s\S]*?)\s*This reverts commit (\w*)\./,
revertCorrespondence: ['header', 'hash'],
};
62 changes: 62 additions & 0 deletions lib/log/templates/commit.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{#if scope}} {{scope}} |
{{~else}} - |
{{~/if}} {{#if subject}}
{{~subject}}
{{~else}}
{{~header}}
{{~/if}}
|
{{~!-- commit link --}} {{#if @root.linkReferences~}}
[{{hash}}](
{{~#if @root.repository}}
{{~#if @root.host}}
{{~@root.host}}/
{{~/if}}
{{~#if @root.owner}}
{{~@root.owner}}/
{{~/if}}
{{~@root.repository}}
{{~else}}
{{~@root.repoUrl}}
{{~/if}}/
{{~@root.commit}}/{{hash}})
{{~else}}
{{~hash}}
{{~/if}}

{{~!-- commit references --}}
{{~#if references~}}
, closes
{{~#each references}} {{#if @root.linkReferences~}}
[
{{~#if this.owner}}
{{~this.owner}}/
{{~/if}}
{{~this.repository}}#{{this.issue}}](
{{~#if @root.repository}}
{{~#if @root.host}}
{{~@root.host}}/
{{~/if}}
{{~#if this.repository}}
{{~#if this.owner}}
{{~this.owner}}/
{{~/if}}
{{~this.repository}}
{{~else}}
{{~#if @root.owner}}
{{~@root.owner}}/
{{~/if}}
{{~@root.repository}}
{{~/if}}
{{~else}}
{{~@root.repoUrl}}
{{~/if}}/
{{~@root.issue}}/{{this.issue}})
{{~else}}
{{~#if this.owner}}
{{~this.owner}}/
{{~/if}}
{{~this.repository}}#{{this.issue}}
{{~/if}}{{/each}}
{{~/if}}

11 changes: 11 additions & 0 deletions lib/log/templates/footer.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{#if noteGroups}}
{{#each noteGroups}}

### {{title}}

{{#each notes}}
* {{#if commit.scope}}**{{commit.scope}}:** {{/if}}{{text}}
{{/each}}
{{/each}}

{{/if}}
Loading

0 comments on commit 0ca4a55

Please sign in to comment.