preserve.ts 2.4 KB

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