preserve.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. const cwdPath = process.cwd();
  14. if (reg.test(cwdPath)) {
  15. errorConsole(
  16. 'Do not include Chinese, Japanese or Korean in the full path of the project directory, please modify the directory name and run again!'
  17. );
  18. errorConsole('项目目录全路径请勿包含中文、日文、韩文,请修改目录名后再次重新运行!');
  19. process.exit(1);
  20. }
  21. fs.mkdirp(resolve('build/.cache'));
  22. function checkPkgUpdate() {
  23. const pkg = require('../../package.json');
  24. const { dependencies, devDependencies } = pkg;
  25. const depsFile = resolve('build/.cache/deps.json');
  26. if (!fs.pathExistsSync(depsFile)) {
  27. NEED_INSTALL = true;
  28. return;
  29. }
  30. const depsJson = require('../.cache/deps.json');
  31. if (!isEqual(depsJson, { dependencies, devDependencies })) {
  32. NEED_INSTALL = true;
  33. }
  34. }
  35. checkPkgUpdate();
  36. if (NEED_INSTALL) {
  37. // no error
  38. successConsole(
  39. '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)!'
  40. );
  41. try {
  42. await sh('npm run bootstrap ', {
  43. async: true,
  44. nopipe: true,
  45. });
  46. successConsole('Dependency installation is successful, start running the project!');
  47. const pkg = require('../../package.json');
  48. const { dependencies, devDependencies } = pkg;
  49. const depsFile = resolve('build/.cache/deps.json');
  50. const deps = { dependencies, devDependencies };
  51. if (!fs.pathExistsSync(depsFile)) {
  52. fs.writeFileSync(depsFile, JSON.stringify(deps));
  53. } else {
  54. const depsFile = resolve('build/.cache/deps.json');
  55. const depsJson = require('../.cache/deps.json');
  56. if (!isEqual(depsJson, deps)) {
  57. fs.writeFileSync(depsFile, JSON.stringify(deps));
  58. }
  59. }
  60. } catch (error) {}
  61. }
  62. }