preserve.ts 2.4 KB
Newer Older
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 3 4 5

import path from 'path';
import fs from 'fs-extra';
import { isEqual } from 'lodash';
6
import { sh } from 'tasksfile';
V
vben 已提交
7 8 9 10
import {
  successConsole,
  //  errorConsole
} from '../utils';
陈文彬 已提交
11 12 13 14 15

const resolve = (dir: string) => {
  return path.resolve(process.cwd(), dir);
};

V
vben 已提交
16
// const reg = /[\u4E00-\u9FA5\uF900-\uFA2D]/;
17

陈文彬 已提交
18 19
let NEED_INSTALL = false;

20
export async function runPreserve() {
V
vben 已提交
21 22 23 24 25 26 27 28 29
  // rc.6 fixed
  // const cwdPath = process.cwd();
  // if (reg.test(cwdPath)) {
  //   errorConsole(
  //     'Do not include Chinese, Japanese or Korean in the full path of the project directory, please modify the directory name and run again!'
  //   );
  //   errorConsole('项目目录全路径请勿包含中文、日文、韩文,请修改目录名后再次重新运行!');
  //   process.exit(1);
  // }
陈文彬 已提交
30

31 32 33 34 35 36 37 38 39 40
  fs.mkdirp(resolve('build/.cache'));
  function checkPkgUpdate() {
    const pkg = require('../../package.json');
    const { dependencies, devDependencies } = pkg;
    const depsFile = resolve('build/.cache/deps.json');
    if (!fs.pathExistsSync(depsFile)) {
      NEED_INSTALL = true;
      return;
    }
    const depsJson = require('../.cache/deps.json');
陈文彬 已提交
41

42 43 44 45 46
    if (!isEqual(depsJson, { dependencies, devDependencies })) {
      NEED_INSTALL = true;
    }
  }
  checkPkgUpdate();
陈文彬 已提交
47
  if (NEED_INSTALL) {
48 49 50
    // no error
    successConsole(
      '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)!'
陈文彬 已提交
51 52
    );
    try {
53 54 55 56
      await sh('npm run bootstrap ', {
        async: true,
        nopipe: true,
      });
57 58

      successConsole('Dependency installation is successful, start running the project!');
陈文彬 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

      const pkg = require('../../package.json');
      const { dependencies, devDependencies } = pkg;
      const depsFile = resolve('build/.cache/deps.json');
      const deps = { dependencies, devDependencies };
      if (!fs.pathExistsSync(depsFile)) {
        fs.writeFileSync(depsFile, JSON.stringify(deps));
      } else {
        const depsFile = resolve('build/.cache/deps.json');
        const depsJson = require('../.cache/deps.json');
        if (!isEqual(depsJson, deps)) {
          fs.writeFileSync(depsFile, JSON.stringify(deps));
        }
      }
    } catch (error) {}
  }
75
}
V
vben 已提交
76 77

runPreserve();