From 516c0bafd8108bebaffa907aa8e065b3112d7953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?impart=E6=88=91=E7=9A=84impart=E5=91=A2?= Date: Sat, 9 Mar 2024 02:30:26 +0800 Subject: [PATCH] chore: do not rebuild if code is not changed --- .gitignore | 3 + packages/engine-chronocat-api/package.json | 2 +- packages/engine-chronocat-event/package.json | 2 +- packages/red/package.json | 2 +- scripts/runOnChanged.cts | 58 ++++++++++++++++++++ 5 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 scripts/runOnChanged.cts diff --git a/.gitignore b/.gitignore index f785edf..f3c38b8 100644 --- a/.gitignore +++ b/.gitignore @@ -133,3 +133,6 @@ dist/ # Temp files tmp/ + +# Run on change hash cache +.runOnChanged.hashes.json diff --git a/packages/engine-chronocat-api/package.json b/packages/engine-chronocat-api/package.json index b2ea601..e9507a4 100644 --- a/packages/engine-chronocat-api/package.json +++ b/packages/engine-chronocat-api/package.json @@ -14,7 +14,7 @@ "lib" ], "scripts": { - "build": "concurrently -n cjs,dts yarn:build:cjs yarn:build:dts", + "build": "node -r esbuild-register ../../scripts/runOnChanged.cts ./src concurrently -n cjs,dts yarn:build:cjs yarn:build:dts", "build:cjs": "node -r esbuild-register ../../scripts/build.cts", "build:dts": "tsc -p tsconfig.json" }, diff --git a/packages/engine-chronocat-event/package.json b/packages/engine-chronocat-event/package.json index 7362136..22d7a10 100644 --- a/packages/engine-chronocat-event/package.json +++ b/packages/engine-chronocat-event/package.json @@ -14,7 +14,7 @@ "lib" ], "scripts": { - "build": "concurrently -n cjs,dts yarn:build:cjs yarn:build:dts", + "build": "node -r esbuild-register ../../scripts/runOnChanged.cts ./src concurrently -n cjs,dts yarn:build:cjs yarn:build:dts", "build:cjs": "node -r esbuild-register ../../scripts/build.cts", "build:dts": "tsc -p tsconfig.json" }, diff --git a/packages/red/package.json b/packages/red/package.json index 7da1c07..79b8569 100644 --- a/packages/red/package.json +++ b/packages/red/package.json @@ -23,7 +23,7 @@ "lib" ], "scripts": { - "build": "tsc -p tsconfig.json" + "build": "node -r esbuild-register ../../scripts/runOnChanged.cts ./src tsc -p tsconfig.json" }, "devDependencies": { "concurrently": "^8.2.1", diff --git a/scripts/runOnChanged.cts b/scripts/runOnChanged.cts new file mode 100644 index 0000000..ba5e81f --- /dev/null +++ b/scripts/runOnChanged.cts @@ -0,0 +1,58 @@ +import * as fs from 'node:fs' +import * as crypto from 'node:crypto' + + +const watchFolder = process.argv[2] +if (!watchFolder) { + console.error('Usage: node -r esbuild-register runOnChanged ') + process.exit(1) +} +const command = process.argv.slice(3).join(' ') + +// create or compare hash of files in the folder +let hasChanged = false + +let hashes = new Map() + +const sha1 = (data: string) => crypto.createHash('sha1').update(data).digest('hex') + + +const hashFile = (file: string) => { + const hash = sha1(fs.readFileSync(file, 'utf8')) + if (hashes.get(file) !== hash) { + hashes.set(file, hash) + hasChanged = true + } +} + +const hashFilepath = `${watchFolder}/.runOnChanged.hashes.json` +const hashFolder = (folder: string) => { + const files = fs.readdirSync(folder) + files.forEach((file: any) => { + const path = `${folder}/${file}` + + if (file.endsWith('.runOnChanged.hashes.json')) return + + if (fs.statSync(path).isDirectory()) { + hashFolder(path) + } else { + hashFile(path) + } + }) +} + +if (fs.existsSync(hashFilepath)) { + const oldHashes = JSON.parse(fs.readFileSync(hashFilepath, 'utf8')) + hashes = new Map(Object.entries(oldHashes)) +} + +hashFolder(watchFolder) + +if (hasChanged) { + console.log('[+] Files have changed, rebuilding...') + fs.writeFileSync(hashFilepath, JSON.stringify(Object.fromEntries(hashes))) + require('child_process').execSync(command, { stdio: 'inherit' }) +} else { + console.log('[-] Files not changed :)') +} +