Repository navigation
Expand file tree
/
Copy pathWatchCommand.ts
More file actions
93 lines (77 loc) · 2.52 KB
/
Copy pathWatchCommand.ts
File metadata and controls
93 lines (77 loc) · 2.52 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { fs } from '@nofrills/fs'
import { Arguments, CommandBuilder } from 'yargs'
import { watch, WatchOptions as FileWatcherOptions } from 'chokidar'
import { BaseCommand } from './BaseCommand'
import { MediaScanner } from '../MediaScanner'
import { WatchOptions } from '../Options/WatchOptions'
function exit(cleanup: boolean, exit: boolean = false, exitCode: number = 0) {
if (cleanup) {
console.log('terminated')
}
if (exitCode !== 0) {
process.exit(exitCode)
}
if (exit) {
process.exit()
}
}
export class WatchCommand extends BaseCommand<WatchOptions> {
aliases = ['watch']
command = 'watch [cwd] [extensions..]'
builder: CommandBuilder<{}, WatchOptions> = {
extensions: {
default: MediaScanner.extensions.map((ext) => `.${ext}`),
string: true,
type: 'array',
},
}
handler = (args: Arguments<WatchOptions>) => {
const options: FileWatcherOptions = {
atomic: true,
cwd: args.cwd,
followSymlinks: false,
persistent: true,
}
const watcher = watch('.', options)
console.log(`watching "${args.cwd}"`)
watcher.on('ready', () => {
console.log(`watching for file changes: ${args.extensions.join(', ')}...`)
watcher.on('add', (path) => this.added(args, path))
watcher.on('addDir', (path) => this.addedDir(args, path))
watcher.on('change', (path) => this.changed(args, path))
watcher.on('unlink', (path) => this.removed(args, path))
watcher.on('unlinkDir', (path) => this.removedDir(args, path))
})
process.on('SIGINT', () => {
console.log('')
console.log('terminating...')
exit(true, true, process.exitCode)
})
}
private added(args: WatchOptions, path: string): Promise<void> {
if (args.extensions.includes(fs.ext(path))) {
this.log.debug('[added]', path)
}
return Promise.resolve()
}
private addedDir(args: WatchOptions, path: string): Promise<void> {
this.log.debug('[addedDir]', path)
return Promise.resolve()
}
private changed(args: WatchOptions, path: string): Promise<void> {
if (args.extensions.includes(fs.ext(path))) {
this.log.debug('[changed]', path)
}
return Promise.resolve()
}
private removed(args: WatchOptions, path: string): Promise<void> {
if (args.extensions.includes(fs.ext(path))) {
this.log.debug('[removed]', path)
}
return Promise.resolve()
}
private removedDir(args: WatchOptions, path: string): Promise<void> {
this.log.debug('[removedDir]', path)
return Promise.resolve()
}
}