handleSyncPublic.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // WARN 该文件只是方便我将当前项目同步到开源仓库,其他人不需要管这个文件~
  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. let allFile = [];
  7. const proDir = '/Users/huangshuisheng/Desktop/hss/billd-project/billd-live-pro';
  8. const freeDir = '/Users/huangshuisheng/Desktop/hss/galaxy-s10/billd-live';
  9. const ignoreRootDir = ['.DS_Store', '.git', 'node_modules', 'dist'];
  10. const ignoreIndexOf = [
  11. path.resolve(proDir, 'src/hooks'),
  12. path.resolve(proDir, 'src/store'),
  13. path.resolve(proDir, 'src/types'),
  14. path.resolve(proDir, 'src/utils'),
  15. path.resolve(proDir, 'src/views'),
  16. ];
  17. const dir = fs.readdirSync(proDir).filter((item) => {
  18. if (ignoreRootDir.includes(item)) {
  19. return false;
  20. }
  21. return true;
  22. });
  23. function findFile(inputDir) {
  24. for (let i = 0; i < inputDir.length; i += 1) {
  25. const file = inputDir[i];
  26. const filePath = `${proDir}/${file}`;
  27. const stat = fs.statSync(filePath);
  28. const isDir = stat.isDirectory();
  29. if (!isDir) {
  30. allFile.push(filePath);
  31. } else {
  32. findFile(fs.readdirSync(filePath).map((key) => `${file}/${key}`));
  33. }
  34. }
  35. }
  36. function putFile() {
  37. for (let i = 0; i < allFile.length; i += 1) {
  38. const file = allFile[i];
  39. const arr = [];
  40. const githubFile = file.replace(proDir, '');
  41. const githubFileArr = githubFile.split('/').filter((item) => item !== '');
  42. githubFileArr.forEach((item) => {
  43. if (arr.length) {
  44. arr.push(path.resolve(arr[arr.length - 1], item));
  45. } else {
  46. arr.push(path.resolve(freeDir, item));
  47. }
  48. });
  49. arr.forEach((item, index) => {
  50. // 数组的最后一个一定是文件,因此不需要判断它是不是目录
  51. if (index !== arr.length - 1) {
  52. const flag = fs.existsSync(item);
  53. !flag && fs.mkdirSync(item);
  54. }
  55. });
  56. fs.copyFileSync(file, path.join(freeDir, './', file.replace(proDir, '')));
  57. }
  58. }
  59. async function clearOld() {
  60. const freeDirAllFile = fs.readdirSync(freeDir);
  61. const queue = [];
  62. freeDirAllFile.forEach((url) => {
  63. const fullurl = `${freeDir}/${url}`;
  64. if (!['node_modules', '.git'].includes(url)) {
  65. queue.push(trash(fullurl));
  66. }
  67. });
  68. await Promise.all(queue);
  69. }
  70. if (process.cwd().indexOf('galaxy-s10') !== -1) {
  71. console.log('当前目录错误');
  72. } else {
  73. clearOld().then(() => {
  74. findFile(dir);
  75. allFile = allFile.filter((x) => {
  76. let flag = true;
  77. ignoreIndexOf.forEach((y) => {
  78. if (x.indexOf(y) === 0) {
  79. flag = false;
  80. }
  81. });
  82. return flag;
  83. });
  84. putFile();
  85. const currentPkgStr = fs.readFileSync(
  86. path.resolve(proDir, 'package.json'),
  87. 'utf-8'
  88. );
  89. const newPkg = JSON.parse(currentPkgStr);
  90. delete newPkg['private'];
  91. newPkg['name'] = 'billd-live';
  92. newPkg['repository']['url'] = 'https://github.com/galaxy-s10/billd-live';
  93. newPkg['bugs']['url'] = 'https://github.com/galaxy-s10/billd-live/issues';
  94. newPkg['devDependencies'] = {};
  95. newPkg['dependencies'] = {};
  96. fs.writeFileSync(
  97. path.resolve(freeDir, 'package.json'),
  98. // @ts-ignore
  99. JSON.stringify({ ...newPkg }, {}, 2)
  100. );
  101. execSync(`pnpm i`, { cwd: freeDir });
  102. execSync(`git add .`, { cwd: freeDir });
  103. execSync(`git commit -m 'feat: 优化-${new Date().toLocaleString()}'`, {
  104. cwd: freeDir,
  105. });
  106. execSync(`git push`, { cwd: freeDir });
  107. });
  108. }