commit-pack 1.1.1 → 1.1.3

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.
@@ -0,0 +1,7 @@
1
+ import chalk from 'chalk'
2
+
3
+ export const log = {
4
+ success: chalk.hex('#CCFF99'),
5
+ error: chalk.hex('#FF6666'),
6
+ warn: chalk.hex('#FFFF66')
7
+ }
package/bin/index.mjs CHANGED
@@ -7,6 +7,8 @@ import chalk from 'chalk'
7
7
  import { execSync } from 'child_process'
8
8
  import path from 'path'
9
9
  import { fileURLToPath } from 'url'
10
+ import { log } from './chalkColor.js'
11
+ import { installWithProgress } from './installWithProgress.js'
10
12
 
11
13
  // 模拟 CommonJS 的 __dirname
12
14
  const __filename = fileURLToPath(import.meta.url)
@@ -30,7 +32,6 @@ function findProjectRootWithLockFile() {
30
32
  }
31
33
 
32
34
  const projectRoot = findProjectRootWithLockFile()
33
- console.log(chalk.green(`检测到项目根目录:${projectRoot}`))
34
35
 
35
36
  function detectPackageManager() {
36
37
  if (fs.existsSync(path.join(projectRoot, 'pnpm-lock.yaml'))) {
@@ -44,10 +45,10 @@ function detectPackageManager() {
44
45
  }
45
46
  }
46
47
 
47
- console.log('执行 postinstall 脚本:bin/index.js')
48
-
49
48
  const packageManager = detectPackageManager()
50
- console.log(chalk.green(`检测到使用的包管理器:${packageManager}`))
49
+ console.log('')
50
+ console.log(`🍀 包管理器:${packageManager}`)
51
+ console.log(`📁 根目录:${projectRoot}`)
51
52
 
52
53
  const packageJsonPath = path.join(projectRoot, 'package.json')
53
54
  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
@@ -55,7 +56,6 @@ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
55
56
  // 检查是否已经初始化过
56
57
  const initFlagPath = path.join(projectRoot, '.commit-pack-init')
57
58
  if (fs.existsSync(initFlagPath)) {
58
- console.log(chalk.yellow('已检测到初始化标志文件,跳过初始化'))
59
59
  process.exit(0)
60
60
  }
61
61
 
@@ -81,7 +81,8 @@ const devDependencies = [
81
81
  '@commitlint/config-conventional',
82
82
  'commitlint-config-cz',
83
83
  'cz-customizable',
84
- 'cz-custom'
84
+ 'cz-custom',
85
+ 'prettier-plugin-tailwindcss'
85
86
  ]
86
87
 
87
88
  let dependenciesToInstall = []
@@ -99,31 +100,7 @@ for (const [dep, version] of Object.entries(devDependenciesWithVersion)) {
99
100
  }
100
101
  }
101
102
 
102
- // 安装缺失的依赖
103
- if (dependenciesToInstall.length > 0) {
104
- let installCommand = ''
105
-
106
- switch (packageManager) {
107
- case 'pnpm':
108
- installCommand = `pnpm add -D ${dependenciesToInstall.join(' ')}`
109
- break
110
- case 'yarn':
111
- installCommand = `yarn add ${dependenciesToInstall.join(' ')} --dev`
112
- break
113
- case 'bun':
114
- installCommand = `bun add -d ${dependenciesToInstall.join(' ')}`
115
- break
116
- default:
117
- installCommand = `npm install ${dependenciesToInstall.join(' ')} --save-dev`
118
- break
119
- }
120
-
121
- console.log(chalk.green(`正在安装开发依赖:${dependenciesToInstall.join(', ')}`))
122
- console.log(chalk.green(`执行命令:${installCommand}`))
123
- execSync(installCommand, { stdio: 'inherit', cwd: projectRoot })
124
- } else {
125
- console.log(chalk.yellow('所有开发依赖已安装,无需安装'))
126
- }
103
+ installWithProgress(dependenciesToInstall, packageManager, projectRoot)
127
104
 
128
105
  let isGitRepo = false
129
106
 
@@ -143,9 +120,9 @@ try {
143
120
  }
144
121
 
145
122
  if (isGitRepo) {
146
- console.log(chalk.yellow('当前已是一个 Git 仓库'))
123
+ console.log(log.success('👍 Git仓库已存在'))
147
124
  } else {
148
- console.log(chalk.red('未检测到 Git 仓库,正在初始化...'))
125
+ console.log(log.warn('👌 Git仓库初始化...'))
149
126
  execSync('git init', { stdio: 'inherit', cwd: projectRoot })
150
127
  }
151
128
 
@@ -166,11 +143,11 @@ switch (packageManager) {
166
143
  break
167
144
  }
168
145
 
169
- console.log(chalk.green(`执行 Husky 初始化命令:${huskyInitCommand}`))
146
+ console.log(chalk.green(`🐶 Husky初始化...`))
170
147
  execSync(huskyInitCommand, { stdio: 'inherit', cwd: projectRoot })
171
148
 
172
149
  // 执行 setup-script 中的所有文件
173
- console.log(chalk.green('执行 setup-script 中的所有文件'))
150
+ console.log('🚀 创建配置文件...')
174
151
  try {
175
152
  const setupScripts = [
176
153
  'prettier.sh',
@@ -184,12 +161,11 @@ try {
184
161
 
185
162
  for (const script of setupScripts) {
186
163
  const scriptPath = path.join(__dirname, '..', 'setup-script', script)
187
- console.log(chalk.green(`执行脚本:${scriptPath}`))
188
164
  execSync(`sh ${scriptPath}`, { stdio: 'inherit', cwd: projectRoot })
189
165
  }
190
- console.log(chalk.green('所有 setup-script 已执行完毕'))
166
+ console.log(chalk.green(' 文件创建完毕'))
191
167
  } catch (error) {
192
- console.error(chalk.red('执行 setup-script 时出错'), error)
168
+ console.error(log.warn(' 文件创建出错'), error)
193
169
  }
194
170
 
195
171
  // 创建或更新脚本
@@ -201,23 +177,23 @@ let modified = false
201
177
 
202
178
  if (!packageJson.scripts.lint) {
203
179
  packageJson.scripts.lint = 'eslint ./ --ext .ts,.tsx,.json --max-warnings=0'
204
- console.log(chalk.green('已添加 "lint" 脚本到 package.json'))
180
+ console.log(log.success('已添加 "lint" package.json'))
205
181
  modified = true
206
182
  } else {
207
- console.log(chalk.yellow('package.json 中已存在 "lint" 脚本,未作修改'))
183
+ console.log(log.warn('package.json 中已存在 "lint" 未作修改'))
208
184
  }
209
185
 
210
186
  if (!packageJson.scripts.format) {
211
187
  packageJson.scripts.format = "prettier --config .prettierrc '.' --write"
212
- console.log(chalk.green('已添加 "format" 脚本到 package.json'))
188
+ console.log(log.success('已添加 "format" package.json'))
213
189
  modified = true
214
190
  } else {
215
- console.log(chalk.yellow('package.json 中已存在 "format" 脚本,未作修改'))
191
+ console.log(log.warn('package.json 中已存在 "format" 未作修改'))
216
192
  }
217
193
 
218
194
  // 添加或更新 "commit" 脚本
219
195
  packageJson.scripts.commit = 'cz'
220
- console.log(chalk.green('已添加或更新 "commit" 脚本到 package.json'))
196
+ console.log(log.success('已添加或更新 "commit" 脚本到 package.json'))
221
197
  modified = true
222
198
 
223
199
  // 添加或更新 "config.commitizen" 配置
@@ -228,15 +204,18 @@ if (!packageJson.config) {
228
204
  packageJson.config.commitizen = {
229
205
  path: 'node_modules/cz-customizable'
230
206
  }
231
- console.log(chalk.green('已添加或更新 "config.commitizen" 到 package.json'))
232
207
  modified = true
233
208
 
234
209
  // 写入修改后的 package.json
235
210
  if (modified) {
236
211
  fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf8')
237
212
  console.log(chalk.green('已更新 package.json'))
213
+ console.log('')
214
+ console.log('')
238
215
  }
239
216
 
240
217
  // 创建初始化标志文件
241
- fs.writeFileSync(initFlagPath, 'initialized', 'utf8')
242
- console.log(chalk.green('已创建初始化标志文件'))
218
+ // fs.writeFileSync(initFlagPath, 'initialized', 'utf8')
219
+ console.log(log.success('🎉🎉🎉 完成啦!'))
220
+ console.log('')
221
+ console.log('运行 git add 后 | 运行 ${packageManager} run commit 即可')
@@ -0,0 +1,52 @@
1
+ /* eslint-env node */
2
+ import chalk from 'chalk'
3
+ import { execSync } from 'child_process'
4
+ import cliProgress from 'cli-progress'
5
+
6
+ export function installWithProgress(dependencies, packageManager, projectRoot) {
7
+ if (!dependencies || dependencies.length === 0) {
8
+ console.log(chalk.yellow('✅ 所有开发依赖已安装,跳过安装'))
9
+ return
10
+ }
11
+
12
+ console.log('')
13
+ console.log(chalk.cyan(`⬇️ 开始安装依赖...`))
14
+
15
+ const bar = new cliProgress.SingleBar({
16
+ format: `${chalk.green('📦 安装中')} {bar} {percentage}% | {value}/{total} | 正在安装: {dep}`,
17
+ barCompleteChar: '█',
18
+ barIncompleteChar: '░',
19
+ hideCursor: true
20
+ })
21
+
22
+ bar.start(dependencies.length, 0, { dep: '' })
23
+
24
+ try {
25
+ for (let i = 0; i < dependencies.length; i++) {
26
+ const dep = dependencies[i]
27
+
28
+ const installCommand = {
29
+ pnpm: `pnpm add -D ${dep}`,
30
+ yarn: `yarn add ${dep} --dev`,
31
+ bun: `bun add -d ${dep}`,
32
+ npm: `npm install ${dep} --save-dev`
33
+ }[packageManager]
34
+
35
+ bar.update(i, { dep })
36
+
37
+ execSync(installCommand, { cwd: projectRoot, stdio: 'ignore' })
38
+
39
+ bar.update(i + 1, { dep })
40
+ }
41
+
42
+ bar.stop()
43
+ console.log('')
44
+ console.log(chalk.green('✅ 所有开发依赖安装完成'))
45
+ console.log('')
46
+ console.log('')
47
+ } catch (err) {
48
+ bar.stop()
49
+ console.log(chalk.red('❌ 安装过程中出错'), err)
50
+ throw err
51
+ }
52
+ }
package/lib/index.mjs CHANGED
@@ -8,6 +8,8 @@ var _chalk = _interopRequireDefault(require("chalk"));
8
8
  var _child_process = require("child_process");
9
9
  var _path = _interopRequireDefault(require("path"));
10
10
  var _url = require("url");
11
+ var _chalkColor = require("./chalkColor.js");
12
+ var _installWithProgress = require("./installWithProgress.js");
11
13
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
12
14
  // 模拟 CommonJS 的 __dirname
13
15
  const _filename = (0, _url.fileURLToPath)(import.meta.url);
@@ -28,7 +30,6 @@ function findProjectRootWithLockFile() {
28
30
  return process.cwd(); // fallback
29
31
  }
30
32
  const projectRoot = findProjectRootWithLockFile();
31
- console.log(_chalk.default.green(`检测到项目根目录:${projectRoot}`));
32
33
  function detectPackageManager() {
33
34
  if (_fs.default.existsSync(_path.default.join(projectRoot, 'pnpm-lock.yaml'))) {
34
35
  return 'pnpm';
@@ -40,16 +41,16 @@ function detectPackageManager() {
40
41
  return 'npm';
41
42
  }
42
43
  }
43
- console.log('执行 postinstall 脚本:bin/index.js');
44
44
  const packageManager = detectPackageManager();
45
- console.log(_chalk.default.green(`检测到使用的包管理器:${packageManager}`));
45
+ console.log('');
46
+ console.log(`🍀 包管理器:${packageManager}`);
47
+ console.log(`📁 根目录:${projectRoot}`);
46
48
  const packageJsonPath = _path.default.join(projectRoot, 'package.json');
47
49
  const packageJson = JSON.parse(_fs.default.readFileSync(packageJsonPath, 'utf8'));
48
50
 
49
51
  // 检查是否已经初始化过
50
52
  const initFlagPath = _path.default.join(projectRoot, '.commit-pack-init');
51
53
  if (_fs.default.existsSync(initFlagPath)) {
52
- console.log(_chalk.default.yellow('已检测到初始化标志文件,跳过初始化'));
53
54
  process.exit(0);
54
55
  }
55
56
 
@@ -61,7 +62,7 @@ const devDependenciesWithVersion = {
61
62
  commitizen: '4.2.4',
62
63
  eslint: '8.57.1'
63
64
  };
64
- 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'];
65
+ 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', 'prettier-plugin-tailwindcss'];
65
66
  let dependenciesToInstall = [];
66
67
 
67
68
  // 检查并收集需要安装的依赖
@@ -75,33 +76,7 @@ for (const [dep, version] of Object.entries(devDependenciesWithVersion)) {
75
76
  dependenciesToInstall.push(`${dep}@${version}`);
76
77
  }
77
78
  }
78
-
79
- // 安装缺失的依赖
80
- if (dependenciesToInstall.length > 0) {
81
- let installCommand = '';
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
- console.log(_chalk.default.green(`正在安装开发依赖:${dependenciesToInstall.join(', ')}`));
97
- console.log(_chalk.default.green(`执行命令:${installCommand}`));
98
- (0, _child_process.execSync)(installCommand, {
99
- stdio: 'inherit',
100
- cwd: projectRoot
101
- });
102
- } else {
103
- console.log(_chalk.default.yellow('所有开发依赖已安装,无需安装'));
104
- }
79
+ (0, _installWithProgress.installWithProgress)(dependenciesToInstall, packageManager, projectRoot);
105
80
  let isGitRepo = false;
106
81
  try {
107
82
  // 获取 Git 仓库的顶级目录
@@ -118,9 +93,9 @@ try {
118
93
  isGitRepo = false;
119
94
  }
120
95
  if (isGitRepo) {
121
- console.log(_chalk.default.yellow('当前已是一个 Git 仓库'));
96
+ console.log(_chalkColor.log.success('👍 Git仓库已存在'));
122
97
  } else {
123
- console.log(_chalk.default.red('未检测到 Git 仓库,正在初始化...'));
98
+ console.log(_chalkColor.log.warn('👌 Git仓库初始化...'));
124
99
  (0, _child_process.execSync)('git init', {
125
100
  stdio: 'inherit',
126
101
  cwd: projectRoot
@@ -143,27 +118,26 @@ switch (packageManager) {
143
118
  huskyInitCommand = 'npx husky init';
144
119
  break;
145
120
  }
146
- console.log(_chalk.default.green(`执行 Husky 初始化命令:${huskyInitCommand}`));
121
+ console.log(_chalk.default.green(`🐶 Husky初始化...`));
147
122
  (0, _child_process.execSync)(huskyInitCommand, {
148
123
  stdio: 'inherit',
149
124
  cwd: projectRoot
150
125
  });
151
126
 
152
127
  // 执行 setup-script 中的所有文件
153
- console.log(_chalk.default.green('执行 setup-script 中的所有文件'));
128
+ console.log('🚀 创建配置文件...');
154
129
  try {
155
130
  const setupScripts = ['prettier.sh', 'lintstagedrc.sh', 'eslint.sh', 'czrc.sh', 'husky.sh', 'cz-config.sh', 'commitlintrc.sh'];
156
131
  for (const script of setupScripts) {
157
132
  const scriptPath = _path.default.join(_dirname, '..', 'setup-script', script);
158
- console.log(_chalk.default.green(`执行脚本:${scriptPath}`));
159
133
  (0, _child_process.execSync)(`sh ${scriptPath}`, {
160
134
  stdio: 'inherit',
161
135
  cwd: projectRoot
162
136
  });
163
137
  }
164
- console.log(_chalk.default.green('所有 setup-script 已执行完毕'));
138
+ console.log(_chalk.default.green(' 文件创建完毕'));
165
139
  } catch (error) {
166
- console.error(_chalk.default.red('执行 setup-script 时出错'), error);
140
+ console.error(_chalkColor.log.warn(' 文件创建出错'), error);
167
141
  }
168
142
 
169
143
  // 创建或更新脚本
@@ -173,22 +147,22 @@ if (!packageJson.scripts) {
173
147
  let modified = false;
174
148
  if (!packageJson.scripts.lint) {
175
149
  packageJson.scripts.lint = 'eslint ./ --ext .ts,.tsx,.json --max-warnings=0';
176
- console.log(_chalk.default.green('已添加 "lint" 脚本到 package.json'));
150
+ console.log(_chalkColor.log.success('已添加 "lint" package.json'));
177
151
  modified = true;
178
152
  } else {
179
- console.log(_chalk.default.yellow('package.json 中已存在 "lint" 脚本,未作修改'));
153
+ console.log(_chalkColor.log.warn('package.json 中已存在 "lint" 未作修改'));
180
154
  }
181
155
  if (!packageJson.scripts.format) {
182
156
  packageJson.scripts.format = "prettier --config .prettierrc '.' --write";
183
- console.log(_chalk.default.green('已添加 "format" 脚本到 package.json'));
157
+ console.log(_chalkColor.log.success('已添加 "format" package.json'));
184
158
  modified = true;
185
159
  } else {
186
- console.log(_chalk.default.yellow('package.json 中已存在 "format" 脚本,未作修改'));
160
+ console.log(_chalkColor.log.warn('package.json 中已存在 "format" 未作修改'));
187
161
  }
188
162
 
189
163
  // 添加或更新 "commit" 脚本
190
164
  packageJson.scripts.commit = 'cz';
191
- console.log(_chalk.default.green('已添加或更新 "commit" 脚本到 package.json'));
165
+ console.log(_chalkColor.log.success('已添加或更新 "commit" 脚本到 package.json'));
192
166
  modified = true;
193
167
 
194
168
  // 添加或更新 "config.commitizen" 配置
@@ -198,15 +172,18 @@ if (!packageJson.config) {
198
172
  packageJson.config.commitizen = {
199
173
  path: 'node_modules/cz-customizable'
200
174
  };
201
- console.log(_chalk.default.green('已添加或更新 "config.commitizen" 到 package.json'));
202
175
  modified = true;
203
176
 
204
177
  // 写入修改后的 package.json
205
178
  if (modified) {
206
179
  _fs.default.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf8');
207
180
  console.log(_chalk.default.green('已更新 package.json'));
181
+ console.log('');
182
+ console.log('');
208
183
  }
209
184
 
210
185
  // 创建初始化标志文件
211
- _fs.default.writeFileSync(initFlagPath, 'initialized', 'utf8');
212
- console.log(_chalk.default.green('已创建初始化标志文件'));
186
+ // fs.writeFileSync(initFlagPath, 'initialized', 'utf8')
187
+ console.log(_chalkColor.log.success('🎉🎉🎉 完成啦!'));
188
+ console.log('');
189
+ console.log('运行 git add 后 | 运行 ${packageManager} run commit 即可');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "commit-pack",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "A setup package to automatly check project's style and commit configuration",
5
5
  "main": "lib/index.js",
6
6
  "bin": {
@@ -34,18 +34,19 @@
34
34
  "author": "jett191",
35
35
  "license": "MIT",
36
36
  "dependencies": {
37
- "@typescript-eslint/parser": "^8.12.2",
37
+ "@commitlint/cli": "^19.5.0",
38
+ "@commitlint/config-conventional": "^19.5.0",
38
39
  "@typescript-eslint/eslint-plugin": "^8.12.2",
39
- "prettier": "^3.3.3",
40
+ "@typescript-eslint/parser": "^8.12.2",
41
+ "cli-progress": "^3.12.0",
42
+ "commitlint-config-cz": "^0.13.3",
43
+ "cz-custom": "^0.0.2",
44
+ "cz-customizable": "^7.2.1",
40
45
  "eslint-config-prettier": "^9.1.0",
41
46
  "eslint-plugin-prettier": "^5.2.1",
42
47
  "husky": "^9.1.6",
43
48
  "lint-staged": "^15.2.10",
44
- "@commitlint/cli": "^19.5.0",
45
- "@commitlint/config-conventional": "^19.5.0",
46
- "commitlint-config-cz": "^0.13.3",
47
- "cz-custom": "^0.0.2",
48
- "cz-customizable": "^7.2.1"
49
+ "prettier": "^3.3.3"
49
50
  },
50
51
  "devDependencies": {
51
52
  "@babel/cli": "^7.25.9",
@@ -7,7 +7,8 @@ echo '
7
7
  "semi": false,
8
8
  "tabWidth": 2,
9
9
  "bracketSpacing": true,
10
- "trailingComma": "none"
10
+ "trailingComma": "none",
11
+ "plugins": ["prettier-plugin-tailwindcss"]
11
12
  }
12
13
  ' > .prettierrc
13
14