mirror of
https://github.com/chrononeko/chronocat.git
synced 2024-11-21 14:48:23 +00:00
chore: do not rebuild if code is not changed
This commit is contained in:
parent
41b0eba4ce
commit
67968c685c
3
.gitignore
vendored
3
.gitignore
vendored
@ -133,3 +133,6 @@ dist/
|
||||
|
||||
# Temp files
|
||||
tmp/
|
||||
|
||||
# Run on change hash cache
|
||||
.runOnChanged.hashes.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"
|
||||
},
|
||||
|
@ -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"
|
||||
},
|
||||
|
@ -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",
|
||||
|
@ -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
58
scripts/runOnChanged.cts
Normal 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 :)')
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user