handleGiteeJenkins.mjs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 =
  9. '/Users/huangshuisheng/Desktop/hss/billd-project/billd-live-pro';
  10. const giteeDir = '/Users/huangshuisheng/Desktop/hss/jenkins/billd-live';
  11. const dir = fs.readdirSync(localDir).filter((item) => {
  12. if (ignore.includes(item)) {
  13. return false;
  14. }
  15. return true;
  16. });
  17. function findFile(inputDir) {
  18. for (let i = 0; i < inputDir.length; i += 1) {
  19. const file = inputDir[i];
  20. const filePath = `${localDir}/${file}`;
  21. const stat = fs.statSync(filePath);
  22. const isDir = stat.isDirectory();
  23. if (!isDir) {
  24. allFile.push(filePath);
  25. } else {
  26. findFile(fs.readdirSync(filePath).map((key) => `${file}/${key}`));
  27. }
  28. }
  29. }
  30. function putFile() {
  31. for (let i = 0; i < allFile.length; i += 1) {
  32. const file = allFile[i];
  33. const arr = [];
  34. const githubFile = file.replace(localDir, '');
  35. const githubFileArr = githubFile.split('/').filter((item) => item !== '');
  36. githubFileArr.forEach((item) => {
  37. if (arr.length) {
  38. arr.push(path.resolve(arr[arr.length - 1], item));
  39. } else {
  40. arr.push(path.resolve(giteeDir, item));
  41. }
  42. });
  43. arr.forEach((item, index) => {
  44. // 数组的最后一个一定是文件,因此不需要判断它是不是目录
  45. if (index !== arr.length - 1) {
  46. const flag = fs.existsSync(item);
  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. if (process.cwd().indexOf('jenkins') !== -1) {
  68. console.log('当前目录错误');
  69. } else {
  70. clearOld().then(() => {
  71. findFile(dir);
  72. putFile();
  73. const gitignoreTxt =
  74. 'node_modules\ndist\ncomponents.d.ts\n.eslintcache\n.DS_Store\n';
  75. fs.writeFileSync(path.resolve(giteeDir, './.gitignore'), gitignoreTxt);
  76. execSync(`git rm -r --cached .`, { cwd: giteeDir });
  77. execSync(`pnpm i`, { cwd: giteeDir });
  78. execSync(`git add .`, { cwd: giteeDir });
  79. execSync(`git commit -m 'feat: ${new Date().toLocaleString()}'`, {
  80. cwd: giteeDir,
  81. });
  82. execSync(`git push`, { cwd: giteeDir });
  83. });
  84. }