Add pre-serve build step to check if node_modules are up-to-date

This commit is contained in:
Andrei Andreev 2022-03-28 18:18:34 +03:00
parent e23124bb1a
commit 14bd19f722
3 changed files with 41 additions and 1 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
dist
.tmp
.vscode
.idea

39
build/check-npm.js Normal file
View File

@ -0,0 +1,39 @@
/* eslint-disable no-bitwise */
const fs = require("fs");
const path = require("path");
const proc = require("child_process");
function getHash(string) {
let hash = 0;
for (let i = 0; i < string.length; i++) {
const char = string.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
// Convert to 32bit integer
hash &= hash;
}
return hash.toString();
}
const hashPath = path.resolve(__dirname, "../.tmp/package-lock.json.hash");
let currentHash = undefined;
if (fs.existsSync(hashPath)) {
currentHash = fs.readFileSync(hashPath, { encoding: "utf-8" });
}
const packageLockPath = path.resolve(__dirname, "../package-lock.json");
const packageLockContents = fs.readFileSync(packageLockPath, { encoding: "utf-8" });
const newHash = getHash(packageLockContents);
if (newHash !== currentHash) {
const tmpPath = path.resolve(__dirname, "../.tmp");
if (!fs.existsSync(tmpPath)) {
fs.mkdirSync(tmpPath);
}
// eslint-disable-next-line no-console
console.log("package-lock.json changes were detected");
console.log("Running 'npm ci' (this might take a while)...");
proc.execSync("npm ci");
fs.writeFileSync(hashPath, newHash, {});
}

View File

@ -1,7 +1,7 @@
{
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"serve": "node build/check-npm.js && vue-cli-service serve",
"build": "vue-cli-service build && node build/post-build.js",
"lint": "vue-cli-service lint"
},