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/packages/shell/package.json b/packages/shell/package.json index 2ab25cf..d34fbf8 100644 --- a/packages/shell/package.json +++ b/packages/shell/package.json @@ -15,7 +15,7 @@ ], "scripts": { "codegen": "node -r esbuild-register scripts/codegen.cts", - "build": "concurrently -n codegen yarn:codegen && concurrently -n cjs,dts yarn:build:cjs yarn:build:dts", + "build": "node -r esbuild-register ../../scripts/runOnChanged.cts ./src concurrently -n codegen yarn:codegen && 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/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 :)') +} +