vue-vben-admin/build/script/preserve.ts

75 lines
2.4 KiB
TypeScript
Raw Normal View History

// Do you need to update the dependencies to prevent package.json from updating the dependencies, and no install after others get the code
2020-09-28 20:19:10 +08:00
import path from 'path';
import fs from 'fs-extra';
import { isEqual } from 'lodash';
import { sh } from 'tasksfile';
2020-10-26 21:45:43 +08:00
import { successConsole, errorConsole } from '../utils';
2020-09-28 20:19:10 +08:00
const resolve = (dir: string) => {
return path.resolve(process.cwd(), dir);
};
2020-10-26 21:45:43 +08:00
const reg = /[\u4E00-\u9FA5\uF900-\uFA2D]/;
2020-09-28 20:19:10 +08:00
let NEED_INSTALL = false;
export async function runPreserve() {
2020-10-24 01:52:37 +08:00
// rc.6 fixed
2020-10-26 21:45:43 +08:00
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);
}
2020-09-28 20:19:10 +08:00
2020-11-12 22:20:15 +08:00
await 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');
2020-09-28 20:19:10 +08:00
if (!isEqual(depsJson, { dependencies, devDependencies })) {
NEED_INSTALL = true;
}
}
checkPkgUpdate();
2020-09-28 20:19:10 +08:00
if (NEED_INSTALL) {
// 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)'
2020-09-28 20:19:10 +08:00
);
try {
await sh('npm run bootstrap ', {
async: true,
nopipe: true,
});
successConsole('Dependency installation is successful, start running the project');
2020-09-28 20:19:10 +08:00
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) {}
}
}
2020-10-16 22:28:44 +08:00
runPreserve();