handleGiteeJenkins.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/galaxy-s10/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. 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(`pnpm i`, { cwd: giteeDir });
  77. execSync(`git add .`, { cwd: giteeDir });
  78. execSync(`git commit -m 'feat: ${new Date().toLocaleString()}'`, {
  79. cwd: giteeDir,
  80. });
  81. execSync(`git push`, { cwd: giteeDir });
  82. });
  83. }