commit-pack 1.0.5 → 1.0.9

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.
@@ -1,16 +1,28 @@
1
+ #!/usr/bin/env node
2
+
1
3
  /* eslint-env node */
2
4
 
3
5
  import fs from 'fs'
4
6
  import chalk from 'chalk'
5
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}`))
6
18
 
7
19
  // 检测包管理器
8
20
  function detectPackageManager() {
9
- if (fs.existsSync('pnpm-lock.yaml')) {
21
+ if (fs.existsSync(path.join(projectRoot, 'pnpm-lock.yaml'))) {
10
22
  return 'pnpm'
11
- } else if (fs.existsSync('yarn.lock')) {
23
+ } else if (fs.existsSync(path.join(projectRoot, 'yarn.lock'))) {
12
24
  return 'yarn'
13
- } else if (fs.existsSync('bun.lockb')) {
25
+ } else if (fs.existsSync(path.join(projectRoot, 'bun.lockb'))) {
14
26
  return 'bun'
15
27
  } else {
16
28
  return 'npm'
@@ -20,7 +32,7 @@ function detectPackageManager() {
20
32
  const packageManager = detectPackageManager()
21
33
  console.log(chalk.green(`检测到使用的包管理器:${packageManager}`))
22
34
 
23
- const packageJsonPath = './package.json'
35
+ const packageJsonPath = path.join(projectRoot, 'package.json')
24
36
  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
25
37
 
26
38
  // 确保 devDependencies 存在
@@ -84,7 +96,7 @@ if (dependenciesToInstall.length > 0) {
84
96
 
85
97
  console.log(chalk.green(`正在安装开发依赖:${dependenciesToInstall.join(', ')}`))
86
98
  console.log(chalk.green(`执行命令:${installCommand}`))
87
- execSync(installCommand, { stdio: 'inherit' })
99
+ execSync(installCommand, { stdio: 'inherit', cwd: projectRoot })
88
100
  } else {
89
101
  console.log(chalk.yellow('所有开发依赖已安装,无需安装'))
90
102
  }
@@ -93,43 +105,51 @@ if (dependenciesToInstall.length > 0) {
93
105
  console.log(chalk.green('初始化 Git 仓库'))
94
106
  try {
95
107
  // 检查是否已经是 Git 仓库
96
- execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' })
108
+ execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore', cwd: projectRoot })
97
109
  console.log(chalk.yellow('当前已是一个 Git 仓库'))
98
110
  } catch (error) {
99
111
  console.error(chalk.red('未检测到 Git 仓库,正在初始化...'), error)
100
112
  // 初始化 Git 仓库
101
- execSync('git init', { stdio: 'inherit' })
113
+ execSync('git init', { stdio: 'inherit', cwd: projectRoot })
102
114
  }
103
115
 
104
116
  // 根据包管理器,执行对应的 Husky 初始化命令
105
117
  let huskyInitCommand = ''
106
118
  switch (packageManager) {
107
119
  case 'pnpm':
108
- huskyInitCommand = 'pnpm exec husky init'
120
+ huskyInitCommand = 'pnpm husky install'
109
121
  break
110
122
  case 'yarn':
111
- huskyInitCommand = 'yarn dlx husky-init'
123
+ huskyInitCommand = 'yarn husky install'
112
124
  break
113
125
  case 'bun':
114
- huskyInitCommand = 'bunx husky init'
126
+ huskyInitCommand = 'bunx husky install'
115
127
  break
116
128
  default:
117
- huskyInitCommand = 'npx husky init'
129
+ huskyInitCommand = 'npx husky install'
118
130
  break
119
131
  }
120
132
 
121
133
  console.log(chalk.green(`执行 Husky 初始化命令:${huskyInitCommand}`))
122
- execSync(huskyInitCommand, { stdio: 'inherit' })
134
+ execSync(huskyInitCommand, { stdio: 'inherit', cwd: projectRoot })
123
135
 
124
136
  // 执行 setup-script 中的所有文件
125
137
  console.log(chalk.green('执行 setup-script 中的所有文件'))
126
138
  try {
127
- execSync('sh ./prettier.sh', { stdio: 'inherit' })
128
- execSync('sh ./lintstagedrc.sh', { stdio: 'inherit' })
129
- execSync('sh ./eslint.sh', { stdio: 'inherit' })
130
- execSync('sh ./czrc.sh', { stdio: 'inherit' })
131
- execSync('sh ./husky.sh', { stdio: 'inherit' })
132
- execSync('sh ./commitlintrc.sh', { stdio: 'inherit' })
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
+ }
133
153
  console.log(chalk.green('所有 setup-script 已执行完毕'))
134
154
  } catch (error) {
135
155
  console.error(chalk.red('执行 setup-script 时出错'), error)
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,8 +1,16 @@
1
1
  {
2
2
  "name": "commit-pack",
3
- "version": "1.0.5",
3
+ "version": "1.0.9",
4
4
  "description": "A setup package to automatly check project's style and commit configuration",
5
- "main": "index.js",
5
+ "main": "lib/index.js",
6
+ "bin": {
7
+ "commit-pack": "./index.js"
8
+ },
9
+ "files": [
10
+ "bin/",
11
+ "setup-script/",
12
+ "lib/"
13
+ ],
6
14
  "publishConfig": {
7
15
  "registry": "https://registry.npmjs.org/"
8
16
  },
@@ -48,11 +56,16 @@
48
56
  "shelljs": "^0.8.5",
49
57
  "standard-version": "^9.5.0"
50
58
  },
59
+ "vscode_custom_css.imports": [
60
+ "file:///Users/jett/code/Res/custom/custom.css",
61
+ "file:///Users/jett/code/Res/custom/custom.js"
62
+ ],
51
63
  "scripts": {
64
+ "postinstall": "node bin/index.mjs",
52
65
  "lint": "eslint ./ --ext .ts,.tsx,.json --max-warnings=0",
53
66
  "format": "prettier --config .prettierrc '.' --write",
54
67
  "commit": "cz",
55
- "build": "babel bin/index.js --out-file lib/index.js",
68
+ "build": "babel bin/index.mjs --out-file lib/index.mjs",
56
69
  "release": "standard-version"
57
70
  }
58
71
  }
package/.babelrc DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "presets": [
3
- [
4
- "@babel/preset-env",
5
- {
6
- "targets": {
7
- "node": "current"
8
- },
9
- "modules": "commonjs",
10
- "exclude": ["transform-regenerator"]
11
- }
12
- ]
13
- ]
14
- }
@@ -1,30 +0,0 @@
1
- {
2
- "extends": ["@commitlint/config-conventional"],
3
- "parserPreset": {
4
- "parserOpts": {
5
- "headerPattern": "^(.+?)\\((.+?)\\): (.+)$",
6
- "headerCorrespondence": ["type", "scope", "subject"]
7
- }
8
- },
9
- "rules": {
10
- "scope-empty": [2, "never"],
11
- "type-enum": [
12
- 2,
13
- "always",
14
- [
15
- "✨ feat",
16
- "🐛 fix",
17
- "🎉 init",
18
- "✏️ docs",
19
- "💄 style",
20
- "♻️ refactor",
21
- "⚡️ perf",
22
- "✅ test",
23
- "⏪️ revert",
24
- "📦 build",
25
- "🚀 chore",
26
- "👷 ci"
27
- ]
28
- ]
29
- }
30
- }
package/.cz-config.js DELETED
@@ -1,41 +0,0 @@
1
- module.exports = {
2
- types: [
3
- { value: '✨ feat', name: ' ✨ feat: 新功能' },
4
- { value: '🐛 fix', name: ' 🐛 fix: 修复bug' },
5
- { value: '🎉 init', name: ' 🎉 init: 初始化' },
6
- { value: '✏️ docs', name: ' ✏️ docs: 文档变更' },
7
- { value: '💄 style', name: ' 💄 style: 更改样式' },
8
- { value: '♻️ refactor', name: ' ♻️ refactor: 重构' },
9
- { value: '⚡️ perf', name: ' ⚡️ perf: 性能优化' },
10
- { value: '✅ test', name: ' ✅ test: 测试' },
11
- { value: '⏪️ revert', name: ' ⏪️ revert: 回退' },
12
- { value: '📦 build', name: ' 📦 build: 打包' },
13
- { value: '🚀 chore', name: ' 🚀 chore: 构建/工程依赖/工具' },
14
- { value: '👷 ci', name: ' 👷 ci: CI related changes' }
15
- ],
16
-
17
- scopes: [
18
- { name: 'components' },
19
- { name: 'page' },
20
- { name: 'css' },
21
- { name: 'api' },
22
- { name: 'README.md' },
23
- { name: 'custom' }
24
- ],
25
-
26
- messages: {
27
- type: '请选择提交类型(必填)',
28
- scope: '请选择文件修改范围(必填):',
29
- customScope: '请输自定义文件修改范围(必填)',
30
- subject: '请简要描述提交(必填)',
31
- body: '请输入详细描述(可选)',
32
- breaking: '列出任何breaking changes(可选)',
33
- footer: '请输入要关闭的issue(可选)',
34
- confirmCommit: '确定提交吗'
35
- },
36
-
37
- allowCustomScopes: true,
38
- allowBreakingChanges: ['✨ feat', '🐛 fix'],
39
- subjectLimit: 49
40
-
41
- }
package/.czrc DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "path": "cz-customizable"
3
- }
package/.eslintignore DELETED
@@ -1,3 +0,0 @@
1
- node_modules/
2
-
3
- README.md
package/.eslintrc DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "root": true,
3
- "parser": "@typescript-eslint/parser",
4
- "extends": [
5
- "eslint:recommended",
6
- "plugin:@typescript-eslint/recommended",
7
- "prettier"
8
- ],
9
- "plugins": ["@typescript-eslint", "prettier"],
10
- "rules": {
11
- "prettier/prettier": "error",
12
- "arrow-body-style": "off",
13
- "prefer-arrow-callback": "off"
14
- }
15
- }
@@ -1,73 +0,0 @@
1
- name: Publish to npm
2
-
3
- on:
4
- push:
5
- branches:
6
- - main
7
- paths-ignore:
8
- - 'pnpm-lock.yaml'
9
-
10
- permissions:
11
- contents: write
12
- jobs:
13
- release:
14
- runs-on: ubuntu-latest
15
-
16
- steps:
17
- # 1. 检出代码
18
- - name: Checkout code
19
- uses: actions/checkout@v4
20
- with:
21
- fetch-depth: 0
22
-
23
- # 2. 设置 Node.js 环境
24
- - name: Sync node version and setup cache
25
- uses: actions/setup-node@v4
26
- with:
27
- node-version: 20
28
-
29
- # 3. 安装 pnpm
30
- - name: Install PNPM
31
- run: npm i -g pnpm
32
-
33
- # 4. 验证 pnpm 安装
34
- - name: Verify pnpm installation
35
- run: pnpm --version
36
-
37
- # 5. 安装依赖
38
- - name: Install dependencies
39
- run: pnpm install
40
-
41
- # 6. 配置 Git 信息
42
- - name: Configure Git
43
- run: |
44
- git config user.name "github-actions[bot]"
45
- git config user.email "github-actions[bot]@users.noreply.github.com"
46
-
47
- # 7. 更新版本号并创建标签
48
- - name: Bump version and create tag
49
- run: pnpm run release
50
- env:
51
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
52
-
53
- # 8. 配置 Git 身份验证
54
- - name: 配置 Git 身份验证
55
- run: |
56
- git remote set-url origin https://${GITHUB_ACTOR}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
57
-
58
- # 9. 推送更改
59
- - name: Push changes
60
- run: |
61
- git push origin main --follow-tags
62
- env:
63
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
64
-
65
- # 10. 运行构建脚本
66
- - name: Run build
67
- run: pnpm run build
68
-
69
- # 11. 发布到 npm
70
- - name: Publish to npm
71
- run: pnpm publish --access public
72
- env:
73
- NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/.husky/commit-msg DELETED
@@ -1 +0,0 @@
1
- npx --no -- commitlint --edit "$1"
package/.husky/pre-commit DELETED
@@ -1 +0,0 @@
1
- npx lint-staged
package/.lintstagedrc DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "*.{js,jsx,ts,tsx}": ["eslint --fix", "prettier --write"]
3
- }
package/.prettierignore DELETED
@@ -1,5 +0,0 @@
1
- node_modules/
2
-
3
- README.md
4
-
5
- .cz-config.js
package/.prettierrc DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "singleQuote": true,
3
- "printWidth": 88,
4
- "jsxSingleQuote": true,
5
- "bracketSameLine": true,
6
- "semi": false,
7
- "tabWidth": 2,
8
- "bracketSpacing": true,
9
- "trailingComma": "none"
10
- }
package/CHANGELOG.md DELETED
@@ -1,21 +0,0 @@
1
- # Changelog
2
-
3
- All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
-
5
- ## [1.0.5](https://github.com/Jett191/commit-pack/compare/v1.0.4...v1.0.5) (2024-11-05)
6
-
7
-
8
-
9
- ## [1.0.4](https://github.com/Jett191/commit-pack/compare/v1.0.3...v1.0.4) (2024-11-05)
10
-
11
-
12
-
13
- ## [1.0.3](https://github.com/Jett191/commit-pack/compare/v1.0.2...v1.0.3) (2024-11-05)
14
-
15
-
16
-
17
- ## [1.0.2](https://github.com/Jett191/commit-pack/compare/v1.0.1...v1.0.2) (2024-11-05)
18
-
19
-
20
-
21
- ## 1.0.1 (2024-11-05)