You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.4 KiB

3 months ago
  1. import fs from 'node:fs';
  2. import path, { dirname } from 'node:path';
  3. import { fileURLToPath } from 'node:url';
  4. import semver from 'semver';
  5. import { execSync } from 'child_process'; // 引入 child_process 模块用于执行 Git 命令
  6. const __filename = fileURLToPath(import.meta.url);
  7. const __dirname = dirname(__filename);
  8. const packagePath = path.resolve(__dirname, 'package.json');
  9. const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf-8'));
  10. // 读取命令行参数(默认使用 'patch')
  11. const versionType = process.argv[2] || 'patch';
  12. // 递增版本
  13. const newVersion = semver.inc(packageJson.version, versionType);
  14. if (!newVersion) {
  15. throw new Error(`Invalid version type: ${versionType}`);
  16. }
  17. packageJson.version = newVersion;
  18. fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2));
  19. console.log(`Version updated to: ${newVersion}`);
  20. // 新增:自动提交 package.json 到远程仓库
  21. try {
  22. // 将 package.json 添加到暂存区
  23. execSync('git add package.json');
  24. console.log('Added package.json to staging area.');
  25. // 提交更改
  26. execSync(`git commit -m "fix: Update version to V${newVersion}"`);
  27. console.log(`Committed changes with message: Update version to ${newVersion}`);
  28. // 推送到远程仓库
  29. execSync('git push');
  30. console.log('Pushed changes to remote repository.');
  31. } catch (error) {
  32. console.error('Failed to commit and push changes:', error.message);
  33. }