preserve.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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, run } 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 run('npm', ['run', 'bootstrap']);
  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. }