commit-pack 1.0.8 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.mjs +201 -0
- package/lib/index.mjs +183 -0
- package/package.json +5 -4
package/bin/index.mjs
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/* eslint-env node */
|
|
4
|
+
|
|
5
|
+
import fs from 'fs'
|
|
6
|
+
import chalk from 'chalk'
|
|
7
|
+
import { execSync } from 'child_process'
|
|
8
|
+
import path from 'path'
|
|
9
|
+
import { fileURLToPath } from 'url'
|
|
10
|
+
|
|
11
|
+
// 获取当前文件的目录名
|
|
12
|
+
const __filename = fileURLToPath(import.meta.url)
|
|
13
|
+
const __dirname = path.dirname(__filename)
|
|
14
|
+
|
|
15
|
+
// 获取用户项目的根目录
|
|
16
|
+
const projectRoot = process.cwd()
|
|
17
|
+
console.log(chalk.green(`当前工作目录:${projectRoot}`))
|
|
18
|
+
|
|
19
|
+
// 检测包管理器
|
|
20
|
+
function detectPackageManager() {
|
|
21
|
+
if (fs.existsSync(path.join(projectRoot, 'pnpm-lock.yaml'))) {
|
|
22
|
+
return 'pnpm'
|
|
23
|
+
} else if (fs.existsSync(path.join(projectRoot, 'yarn.lock'))) {
|
|
24
|
+
return 'yarn'
|
|
25
|
+
} else if (fs.existsSync(path.join(projectRoot, 'bun.lockb'))) {
|
|
26
|
+
return 'bun'
|
|
27
|
+
} else {
|
|
28
|
+
return 'npm'
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const packageManager = detectPackageManager()
|
|
33
|
+
console.log(chalk.green(`检测到使用的包管理器:${packageManager}`))
|
|
34
|
+
|
|
35
|
+
const packageJsonPath = path.join(projectRoot, 'package.json')
|
|
36
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
|
|
37
|
+
|
|
38
|
+
// 确保 devDependencies 存在
|
|
39
|
+
if (!packageJson.devDependencies) {
|
|
40
|
+
packageJson.devDependencies = {}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const devDependenciesWithVersion = {
|
|
44
|
+
commitizen: '4.2.4',
|
|
45
|
+
eslint: '8.57.1'
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const devDependencies = [
|
|
49
|
+
'@typescript-eslint/parser',
|
|
50
|
+
'@typescript-eslint/eslint-plugin',
|
|
51
|
+
'prettier',
|
|
52
|
+
'eslint-config-prettier',
|
|
53
|
+
'eslint-plugin-prettier',
|
|
54
|
+
'husky',
|
|
55
|
+
'lint-staged',
|
|
56
|
+
'@commitlint/cli',
|
|
57
|
+
'@commitlint/config-conventional',
|
|
58
|
+
'commitlint-config-cz',
|
|
59
|
+
'cz-customizable',
|
|
60
|
+
'cz-custom'
|
|
61
|
+
]
|
|
62
|
+
|
|
63
|
+
let dependenciesToInstall = []
|
|
64
|
+
|
|
65
|
+
// 检查并收集需要安装的依赖
|
|
66
|
+
for (const dep of devDependencies) {
|
|
67
|
+
if (!packageJson.devDependencies[dep]) {
|
|
68
|
+
dependenciesToInstall.push(dep)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
for (const [dep, version] of Object.entries(devDependenciesWithVersion)) {
|
|
73
|
+
if (!packageJson.devDependencies[dep]) {
|
|
74
|
+
dependenciesToInstall.push(`${dep}@${version}`)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// 安装缺失的依赖
|
|
79
|
+
if (dependenciesToInstall.length > 0) {
|
|
80
|
+
let installCommand = ''
|
|
81
|
+
|
|
82
|
+
switch (packageManager) {
|
|
83
|
+
case 'pnpm':
|
|
84
|
+
installCommand = `pnpm add -D ${dependenciesToInstall.join(' ')}`
|
|
85
|
+
break
|
|
86
|
+
case 'yarn':
|
|
87
|
+
installCommand = `yarn add ${dependenciesToInstall.join(' ')} --dev`
|
|
88
|
+
break
|
|
89
|
+
case 'bun':
|
|
90
|
+
installCommand = `bun add -d ${dependenciesToInstall.join(' ')}`
|
|
91
|
+
break
|
|
92
|
+
default:
|
|
93
|
+
installCommand = `npm install ${dependenciesToInstall.join(' ')} --save-dev`
|
|
94
|
+
break
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
console.log(chalk.green(`正在安装开发依赖:${dependenciesToInstall.join(', ')}`))
|
|
98
|
+
console.log(chalk.green(`执行命令:${installCommand}`))
|
|
99
|
+
execSync(installCommand, { stdio: 'inherit', cwd: projectRoot })
|
|
100
|
+
} else {
|
|
101
|
+
console.log(chalk.yellow('所有开发依赖已安装,无需安装'))
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// 初始化 Git 仓库
|
|
105
|
+
console.log(chalk.green('初始化 Git 仓库'))
|
|
106
|
+
try {
|
|
107
|
+
// 检查是否已经是 Git 仓库
|
|
108
|
+
execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore', cwd: projectRoot })
|
|
109
|
+
console.log(chalk.yellow('当前已是一个 Git 仓库'))
|
|
110
|
+
} catch (error) {
|
|
111
|
+
console.error(chalk.red('未检测到 Git 仓库,正在初始化...'), error)
|
|
112
|
+
// 初始化 Git 仓库
|
|
113
|
+
execSync('git init', { stdio: 'inherit', cwd: projectRoot })
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 根据包管理器,执行对应的 Husky 初始化命令
|
|
117
|
+
let huskyInitCommand = ''
|
|
118
|
+
switch (packageManager) {
|
|
119
|
+
case 'pnpm':
|
|
120
|
+
huskyInitCommand = 'pnpm husky install'
|
|
121
|
+
break
|
|
122
|
+
case 'yarn':
|
|
123
|
+
huskyInitCommand = 'yarn husky install'
|
|
124
|
+
break
|
|
125
|
+
case 'bun':
|
|
126
|
+
huskyInitCommand = 'bunx husky install'
|
|
127
|
+
break
|
|
128
|
+
default:
|
|
129
|
+
huskyInitCommand = 'npx husky install'
|
|
130
|
+
break
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
console.log(chalk.green(`执行 Husky 初始化命令:${huskyInitCommand}`))
|
|
134
|
+
execSync(huskyInitCommand, { stdio: 'inherit', cwd: projectRoot })
|
|
135
|
+
|
|
136
|
+
// 执行 setup-script 中的所有文件
|
|
137
|
+
console.log(chalk.green('执行 setup-script 中的所有文件'))
|
|
138
|
+
try {
|
|
139
|
+
const setupScripts = [
|
|
140
|
+
'prettier.sh',
|
|
141
|
+
'lintstagedrc.sh',
|
|
142
|
+
'eslint.sh',
|
|
143
|
+
'czrc.sh',
|
|
144
|
+
'husky.sh',
|
|
145
|
+
'commitlintrc.sh'
|
|
146
|
+
]
|
|
147
|
+
|
|
148
|
+
for (const script of setupScripts) {
|
|
149
|
+
const scriptPath = path.join(__dirname, '..', 'setup-script', script)
|
|
150
|
+
console.log(chalk.green(`执行脚本:${scriptPath}`))
|
|
151
|
+
execSync(`sh ${scriptPath}`, { stdio: 'inherit', cwd: projectRoot })
|
|
152
|
+
}
|
|
153
|
+
console.log(chalk.green('所有 setup-script 已执行完毕'))
|
|
154
|
+
} catch (error) {
|
|
155
|
+
console.error(chalk.red('执行 setup-script 时出错'), error)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 创建或更新脚本
|
|
159
|
+
if (!packageJson.scripts) {
|
|
160
|
+
packageJson.scripts = {}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
let modified = false
|
|
164
|
+
|
|
165
|
+
if (!packageJson.scripts.lint) {
|
|
166
|
+
packageJson.scripts.lint = 'eslint ./ --ext .ts,.tsx,.json --max-warnings=0'
|
|
167
|
+
console.log(chalk.green('已添加 "lint" 脚本到 package.json'))
|
|
168
|
+
modified = true
|
|
169
|
+
} else {
|
|
170
|
+
console.log(chalk.yellow('package.json 中已存在 "lint" 脚本,未作修改'))
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (!packageJson.scripts.format) {
|
|
174
|
+
packageJson.scripts.format = "prettier --config .prettierrc '.' --write"
|
|
175
|
+
console.log(chalk.green('已添加 "format" 脚本到 package.json'))
|
|
176
|
+
modified = true
|
|
177
|
+
} else {
|
|
178
|
+
console.log(chalk.yellow('package.json 中已存在 "format" 脚本,未作修改'))
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// 添加或更新 "commit" 脚本
|
|
182
|
+
packageJson.scripts.commit = 'cz'
|
|
183
|
+
console.log(chalk.green('已添加或更新 "commit" 脚本到 package.json'))
|
|
184
|
+
modified = true
|
|
185
|
+
|
|
186
|
+
// 添加或更新 "config.commitizen" 配置
|
|
187
|
+
if (!packageJson.config) {
|
|
188
|
+
packageJson.config = {}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
packageJson.config.commitizen = {
|
|
192
|
+
path: 'node_modules/cz-customizable'
|
|
193
|
+
}
|
|
194
|
+
console.log(chalk.green('已添加或更新 "config.commitizen" 到 package.json'))
|
|
195
|
+
modified = true
|
|
196
|
+
|
|
197
|
+
// 写入修改后的 package.json
|
|
198
|
+
if (modified) {
|
|
199
|
+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf8')
|
|
200
|
+
console.log(chalk.green('已更新 package.json'))
|
|
201
|
+
}
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/* eslint-env node */
|
|
4
|
+
"use strict";
|
|
5
|
+
|
|
6
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
7
|
+
var _chalk = _interopRequireDefault(require("chalk"));
|
|
8
|
+
var _child_process = require("child_process");
|
|
9
|
+
var _path = _interopRequireDefault(require("path"));
|
|
10
|
+
var _url = require("url");
|
|
11
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
|
+
// 获取当前文件的目录名
|
|
13
|
+
const _filename = (0, _url.fileURLToPath)(import.meta.url);
|
|
14
|
+
const _dirname = _path.default.dirname(_filename);
|
|
15
|
+
|
|
16
|
+
// 获取用户项目的根目录
|
|
17
|
+
const projectRoot = process.cwd();
|
|
18
|
+
console.log(_chalk.default.green(`当前工作目录:${projectRoot}`));
|
|
19
|
+
|
|
20
|
+
// 检测包管理器
|
|
21
|
+
function detectPackageManager() {
|
|
22
|
+
if (_fs.default.existsSync(_path.default.join(projectRoot, 'pnpm-lock.yaml'))) {
|
|
23
|
+
return 'pnpm';
|
|
24
|
+
} else if (_fs.default.existsSync(_path.default.join(projectRoot, 'yarn.lock'))) {
|
|
25
|
+
return 'yarn';
|
|
26
|
+
} else if (_fs.default.existsSync(_path.default.join(projectRoot, 'bun.lockb'))) {
|
|
27
|
+
return 'bun';
|
|
28
|
+
} else {
|
|
29
|
+
return 'npm';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const packageManager = detectPackageManager();
|
|
33
|
+
console.log(_chalk.default.green(`检测到使用的包管理器:${packageManager}`));
|
|
34
|
+
const packageJsonPath = _path.default.join(projectRoot, 'package.json');
|
|
35
|
+
const packageJson = JSON.parse(_fs.default.readFileSync(packageJsonPath, 'utf8'));
|
|
36
|
+
|
|
37
|
+
// 确保 devDependencies 存在
|
|
38
|
+
if (!packageJson.devDependencies) {
|
|
39
|
+
packageJson.devDependencies = {};
|
|
40
|
+
}
|
|
41
|
+
const devDependenciesWithVersion = {
|
|
42
|
+
commitizen: '4.2.4',
|
|
43
|
+
eslint: '8.57.1'
|
|
44
|
+
};
|
|
45
|
+
const devDependencies = ['@typescript-eslint/parser', '@typescript-eslint/eslint-plugin', 'prettier', 'eslint-config-prettier', 'eslint-plugin-prettier', 'husky', 'lint-staged', '@commitlint/cli', '@commitlint/config-conventional', 'commitlint-config-cz', 'cz-customizable', 'cz-custom'];
|
|
46
|
+
let dependenciesToInstall = [];
|
|
47
|
+
|
|
48
|
+
// 检查并收集需要安装的依赖
|
|
49
|
+
for (const dep of devDependencies) {
|
|
50
|
+
if (!packageJson.devDependencies[dep]) {
|
|
51
|
+
dependenciesToInstall.push(dep);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
for (const [dep, version] of Object.entries(devDependenciesWithVersion)) {
|
|
55
|
+
if (!packageJson.devDependencies[dep]) {
|
|
56
|
+
dependenciesToInstall.push(`${dep}@${version}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 安装缺失的依赖
|
|
61
|
+
if (dependenciesToInstall.length > 0) {
|
|
62
|
+
let installCommand = '';
|
|
63
|
+
switch (packageManager) {
|
|
64
|
+
case 'pnpm':
|
|
65
|
+
installCommand = `pnpm add -D ${dependenciesToInstall.join(' ')}`;
|
|
66
|
+
break;
|
|
67
|
+
case 'yarn':
|
|
68
|
+
installCommand = `yarn add ${dependenciesToInstall.join(' ')} --dev`;
|
|
69
|
+
break;
|
|
70
|
+
case 'bun':
|
|
71
|
+
installCommand = `bun add -d ${dependenciesToInstall.join(' ')}`;
|
|
72
|
+
break;
|
|
73
|
+
default:
|
|
74
|
+
installCommand = `npm install ${dependenciesToInstall.join(' ')} --save-dev`;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
console.log(_chalk.default.green(`正在安装开发依赖:${dependenciesToInstall.join(', ')}`));
|
|
78
|
+
console.log(_chalk.default.green(`执行命令:${installCommand}`));
|
|
79
|
+
(0, _child_process.execSync)(installCommand, {
|
|
80
|
+
stdio: 'inherit',
|
|
81
|
+
cwd: projectRoot
|
|
82
|
+
});
|
|
83
|
+
} else {
|
|
84
|
+
console.log(_chalk.default.yellow('所有开发依赖已安装,无需安装'));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// 初始化 Git 仓库
|
|
88
|
+
console.log(_chalk.default.green('初始化 Git 仓库'));
|
|
89
|
+
try {
|
|
90
|
+
// 检查是否已经是 Git 仓库
|
|
91
|
+
(0, _child_process.execSync)('git rev-parse --is-inside-work-tree', {
|
|
92
|
+
stdio: 'ignore',
|
|
93
|
+
cwd: projectRoot
|
|
94
|
+
});
|
|
95
|
+
console.log(_chalk.default.yellow('当前已是一个 Git 仓库'));
|
|
96
|
+
} catch (error) {
|
|
97
|
+
console.error(_chalk.default.red('未检测到 Git 仓库,正在初始化...'), error);
|
|
98
|
+
// 初始化 Git 仓库
|
|
99
|
+
(0, _child_process.execSync)('git init', {
|
|
100
|
+
stdio: 'inherit',
|
|
101
|
+
cwd: projectRoot
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// 根据包管理器,执行对应的 Husky 初始化命令
|
|
106
|
+
let huskyInitCommand = '';
|
|
107
|
+
switch (packageManager) {
|
|
108
|
+
case 'pnpm':
|
|
109
|
+
huskyInitCommand = 'pnpm husky install';
|
|
110
|
+
break;
|
|
111
|
+
case 'yarn':
|
|
112
|
+
huskyInitCommand = 'yarn husky install';
|
|
113
|
+
break;
|
|
114
|
+
case 'bun':
|
|
115
|
+
huskyInitCommand = 'bunx husky install';
|
|
116
|
+
break;
|
|
117
|
+
default:
|
|
118
|
+
huskyInitCommand = 'npx husky install';
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
console.log(_chalk.default.green(`执行 Husky 初始化命令:${huskyInitCommand}`));
|
|
122
|
+
(0, _child_process.execSync)(huskyInitCommand, {
|
|
123
|
+
stdio: 'inherit',
|
|
124
|
+
cwd: projectRoot
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// 执行 setup-script 中的所有文件
|
|
128
|
+
console.log(_chalk.default.green('执行 setup-script 中的所有文件'));
|
|
129
|
+
try {
|
|
130
|
+
const setupScripts = ['prettier.sh', 'lintstagedrc.sh', 'eslint.sh', 'czrc.sh', 'husky.sh', 'commitlintrc.sh'];
|
|
131
|
+
for (const script of setupScripts) {
|
|
132
|
+
const scriptPath = _path.default.join(_dirname, '..', 'setup-script', script);
|
|
133
|
+
console.log(_chalk.default.green(`执行脚本:${scriptPath}`));
|
|
134
|
+
(0, _child_process.execSync)(`sh ${scriptPath}`, {
|
|
135
|
+
stdio: 'inherit',
|
|
136
|
+
cwd: projectRoot
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
console.log(_chalk.default.green('所有 setup-script 已执行完毕'));
|
|
140
|
+
} catch (error) {
|
|
141
|
+
console.error(_chalk.default.red('执行 setup-script 时出错'), error);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// 创建或更新脚本
|
|
145
|
+
if (!packageJson.scripts) {
|
|
146
|
+
packageJson.scripts = {};
|
|
147
|
+
}
|
|
148
|
+
let modified = false;
|
|
149
|
+
if (!packageJson.scripts.lint) {
|
|
150
|
+
packageJson.scripts.lint = 'eslint ./ --ext .ts,.tsx,.json --max-warnings=0';
|
|
151
|
+
console.log(_chalk.default.green('已添加 "lint" 脚本到 package.json'));
|
|
152
|
+
modified = true;
|
|
153
|
+
} else {
|
|
154
|
+
console.log(_chalk.default.yellow('package.json 中已存在 "lint" 脚本,未作修改'));
|
|
155
|
+
}
|
|
156
|
+
if (!packageJson.scripts.format) {
|
|
157
|
+
packageJson.scripts.format = "prettier --config .prettierrc '.' --write";
|
|
158
|
+
console.log(_chalk.default.green('已添加 "format" 脚本到 package.json'));
|
|
159
|
+
modified = true;
|
|
160
|
+
} else {
|
|
161
|
+
console.log(_chalk.default.yellow('package.json 中已存在 "format" 脚本,未作修改'));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// 添加或更新 "commit" 脚本
|
|
165
|
+
packageJson.scripts.commit = 'cz';
|
|
166
|
+
console.log(_chalk.default.green('已添加或更新 "commit" 脚本到 package.json'));
|
|
167
|
+
modified = true;
|
|
168
|
+
|
|
169
|
+
// 添加或更新 "config.commitizen" 配置
|
|
170
|
+
if (!packageJson.config) {
|
|
171
|
+
packageJson.config = {};
|
|
172
|
+
}
|
|
173
|
+
packageJson.config.commitizen = {
|
|
174
|
+
path: 'node_modules/cz-customizable'
|
|
175
|
+
};
|
|
176
|
+
console.log(_chalk.default.green('已添加或更新 "config.commitizen" 到 package.json'));
|
|
177
|
+
modified = true;
|
|
178
|
+
|
|
179
|
+
// 写入修改后的 package.json
|
|
180
|
+
if (modified) {
|
|
181
|
+
_fs.default.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf8');
|
|
182
|
+
console.log(_chalk.default.green('已更新 package.json'));
|
|
183
|
+
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "commit-pack",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
4
4
|
"description": "A setup package to automatly check project's style and commit configuration",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"bin": {
|
|
7
|
-
"commit-pack": "
|
|
7
|
+
"commit-pack": "bin/index.mjs"
|
|
8
8
|
},
|
|
9
9
|
"files": [
|
|
10
|
-
"
|
|
11
|
-
"setup-script/"
|
|
10
|
+
"bin/",
|
|
11
|
+
"setup-script/",
|
|
12
|
+
"lib/"
|
|
12
13
|
],
|
|
13
14
|
"publishConfig": {
|
|
14
15
|
"registry": "https://registry.npmjs.org/"
|