chore: do not rebuild if code is not changed

This commit is contained in:
impart我的impart呢 2024-03-09 02:30:26 +08:00
parent 41b0eba4ce
commit 67968c685c
No known key found for this signature in database
GPG Key ID: 68ED75B1D060D166
6 changed files with 65 additions and 4 deletions

3
.gitignore vendored
View File

@ -133,3 +133,6 @@ dist/
# Temp files
tmp/
# Run on change hash cache
.runOnChanged.hashes.json

View File

@ -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"
},

View File

@ -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"
},

View File

@ -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",

View File

@ -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"
},

58
scripts/runOnChanged.cts Normal file
View File

@ -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 <folder> <command>')
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 :)')
}