handleSyncPublic.mjs 3.5 KB

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