forked from serverless/serverless-plugin-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchFiles.ts
41 lines (34 loc) · 1.16 KB
/
watchFiles.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import * as typescript from './typescript'
import { watchFile, unwatchFile, Stats } from 'fs'
export function watchFiles(
rootFileNames: string[],
originalServicePath: string,
serverless: Serverless.Instance,
cb: () => void
) {
const tsConfig = typescript.getTypescriptConfig(originalServicePath, serverless)
let watchedFiles = typescript.getSourceFiles(rootFileNames, tsConfig)
watchedFiles.forEach(fileName => {
watchFile(fileName, { persistent: true, interval: 250 }, watchCallback)
})
function watchCallback(curr: Stats, prev: Stats) {
// Check timestamp
if (+curr.mtime <= +prev.mtime) {
return
}
cb()
// use can reference not watched yet file or remove reference to already watched
const newWatchFiles = typescript.getSourceFiles(rootFileNames, tsConfig)
watchedFiles.forEach(fileName => {
if (newWatchFiles.indexOf(fileName) < 0) {
unwatchFile(fileName, watchCallback)
}
})
newWatchFiles.forEach(fileName => {
if (watchedFiles.indexOf(fileName) < 0) {
watchFile(fileName, { persistent: true, interval: 250 }, watchCallback)
}
})
watchedFiles = newWatchFiles
}
}