-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (92 loc) · 2.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const Commander = require('commander');
const spawn = require('child_process').spawn;
const fs = require('fs-extra');
const path = require('path');
const _ = require('lodash');
const chalk = require('chalk');
const commander = new Commander.Command();
const log = console.log;
const projectRoot = process.cwd();
const pkgRoot = __dirname;
/**
* 执行安装命令
* @param {string} packageManager
* @param {string} packageName
*/
async function InstallPkgs(packageManager = 'npm', packageName) {
const installArgs = [];
switch (packageManager) {
case 'cnpm':
case 'pnpm':
case 'npm':
installArgs.push('install');
break;
case 'yarn':
installArgs.push('add');
break;
default:
packageManager = 'npm';
installArgs.push('install');
break;
}
installArgs.push(packageName);
await new Promise((resolve, reject) => {
spawn(packageManager, installArgs, { stdio: 'inherit', shell: true })
.on('close', (code) => {
if (code === 0) {
log(chalk.green(`Installed packages for tooling via ${packageManager}.`));
resolve();
} else {
log(chalk.red('Package install failed, see above.'));
reject('Package install failed');
}
},
);
});
}
/**
* 修改package.json的相关配置
*/
function editPkgJson() {
const pkgJsonPath = path.join(projectRoot, 'package.json');
const pkgJson = fs.readJsonSync(pkgJsonPath);
_.merge(pkgJson, {
config: {
commitizen: {
path: "./node_modules/cz-conventional-changelog"
}
},
husky: {
hooks: {
'commit-msg': "commitlint -E HUSKY_GIT_PARAMS"
}
},
scripts: {
'std-cm': 'git-cz',
changelog: "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"
}
})
fs.writeJSONSync(pkgJsonPath, pkgJson, { spaces: '\t' });
}
/**
* 将commit规则写入项目根目录
*/
function copyRulesToProject() {
fs.copySync(path.join(pkgRoot, 'commitlint.config.js'), path.join(projectRoot, 'commitlint.config.js'));
}
// 自定义包管理器
commander.version('0.1.1')
.option('-p, --package-manager <pkg>', 'package manager with npm in default')
.action(async function (pkg) {
// 需要安装的依赖包
const pkgs = 'commitizen cz-conventional-changelog @commitlint/cli husky conventional-changelog-cli';
await InstallPkgs(commander.packageManager, pkgs);
editPkgJson();
log(chalk.green(`Has changed package.json`));
copyRulesToProject();
log(chalk.green(`
commitlint.config.js for commit rules has putted in the project
All has done! Now you can test 'npm run changelog' or 'git commit'
`));
})
.parse(process.argv);