handleGiteeJenkins.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // WARN 该文件只是方便我将当前项目复制一份到我电脑的另一个位置(gitee私有仓库的位置),其他人不需要管这个文件~
  2. const fs = require('fs');
  3. const path = require('path');
  4. const { execSync } = require('child_process');
  5. const allFile = [];
  6. const ignore = ['.DS_Store', '.git', 'node_modules', 'dist'];
  7. const localDir = path.resolve(__dirname);
  8. const giteeDir = path.resolve(__dirname, '../../jenkins/billd-live');
  9. const dir = fs.readdirSync(localDir).filter((item) => {
  10. if (ignore.includes(item)) {
  11. return false;
  12. }
  13. return true;
  14. });
  15. function findFile(inputDir) {
  16. for (let i = 0; i < inputDir.length; i += 1) {
  17. const file = inputDir[i];
  18. const filePath = `${localDir}/${file}`;
  19. const stat = fs.statSync(filePath);
  20. const isDir = stat.isDirectory();
  21. if (!isDir) {
  22. allFile.push(filePath);
  23. } else {
  24. findFile(fs.readdirSync(filePath).map((key) => `${file}/${key}`));
  25. }
  26. }
  27. }
  28. function putFile() {
  29. for (let i = 0; i < allFile.length; i += 1) {
  30. const file = allFile[i];
  31. const arr = [];
  32. const githubFile = file.replace(localDir, '');
  33. const githubFileArr = githubFile.split('/').filter((item) => item !== '');
  34. githubFileArr.forEach((item) => {
  35. if (arr.length) {
  36. arr.push(path.resolve(arr[arr.length - 1], item));
  37. } else {
  38. arr.push(path.resolve(giteeDir, item));
  39. }
  40. });
  41. arr.forEach((item, index) => {
  42. // 数组的最后一个一定是文件,因此不需要判断它是不是目录
  43. if (index !== arr.length - 1) {
  44. const flag = fs.existsSync(item);
  45. // eslint-disable-next-line
  46. !flag && fs.mkdirSync(item);
  47. }
  48. });
  49. fs.copyFileSync(
  50. file,
  51. path.join(giteeDir, './', file.replace(localDir, ''))
  52. );
  53. }
  54. }
  55. if (path.resolve(__dirname) === giteeDir) {
  56. // eslint-disable-next-line
  57. console.log('当前在gitee文件目录,直接退出!');
  58. } else {
  59. findFile(dir);
  60. putFile();
  61. execSync(`pnpm i`, { cwd: giteeDir });
  62. execSync(`git add .`, { cwd: giteeDir });
  63. execSync(`git commit -m 'feat: ${new Date().toLocaleString()}'`, {
  64. cwd: giteeDir,
  65. });
  66. execSync(`git push`, { cwd: giteeDir });
  67. }