handleGiteeJenkins.mjs 2.6 KB

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