preserve.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Do you need to update the dependencies to prevent package.json from updating the dependencies, and no install after others get the code
  2. import path from 'path';
  3. import fs from 'fs-extra';
  4. import { isEqual } from 'lodash';
  5. import { sh } from 'tasksfile';
  6. import { successConsole, errorConsole } from '../utils';
  7. const resolve = (dir: string) => {
  8. return path.resolve(process.cwd(), dir);
  9. };
  10. const reg = /[\u4E00-\u9FA5\uF900-\uFA2D]/;
  11. let NEED_INSTALL = false;
  12. export async function runPreserve() {
  13. // rc.6 fixed
  14. const cwdPath = process.cwd();
  15. if (reg.test(cwdPath)) {
  16. errorConsole(
  17. 'Do not include Chinese, Japanese or Korean in the full path of the project directory, please modify the directory name and run again!'
  18. );
  19. errorConsole('项目目录全路径请勿包含中文、日文、韩文,请修改目录名后再次重新运行!');
  20. process.exit(1);
  21. }
  22. await fs.mkdirp(resolve('build/.cache'));
  23. function checkPkgUpdate() {
  24. const pkg = require('../../package.json');
  25. const { dependencies, devDependencies } = pkg;
  26. const depsFile = resolve('build/.cache/deps.json');
  27. if (!fs.pathExistsSync(depsFile)) {
  28. NEED_INSTALL = true;
  29. return;
  30. }
  31. const depsJson = require('../.cache/deps.json');
  32. if (!isEqual(depsJson, { dependencies, devDependencies })) {
  33. NEED_INSTALL = true;
  34. }
  35. }
  36. checkPkgUpdate();
  37. if (NEED_INSTALL) {
  38. // no error
  39. successConsole(
  40. 'A dependency change is detected, and the dependency is being installed to ensure that the dependency is consistent! (Tip: The project will be executed for the first time)!'
  41. );
  42. try {
  43. await sh('npm run bootstrap ', {
  44. async: true,
  45. nopipe: true,
  46. });
  47. successConsole('Dependency installation is successful, start running the project!');
  48. const pkg = require('../../package.json');
  49. const { dependencies, devDependencies } = pkg;
  50. const depsFile = resolve('build/.cache/deps.json');
  51. const deps = { dependencies, devDependencies };
  52. if (!fs.pathExistsSync(depsFile)) {
  53. fs.writeFileSync(depsFile, JSON.stringify(deps));
  54. } else {
  55. const depsFile = resolve('build/.cache/deps.json');
  56. const depsJson = require('../.cache/deps.json');
  57. if (!isEqual(depsJson, deps)) {
  58. fs.writeFileSync(depsFile, JSON.stringify(deps));
  59. }
  60. }
  61. } catch (error) {}
  62. }
  63. }
  64. runPreserve();