-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotload
182 lines (174 loc) · 6.53 KB
/
hotload
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env node
const { join, extname } = require('path')
const { access, statSync } = require('fs')
const { exec } = require('child_process')
const { flag, output } = require('@alanchenchen/commandlineflag')
const chalk = require('chalk')
const watch = require('../utils/watch')
const { defaultDoc, paramDoc, loadDoc, cmdDoc } = require('../utils/doc')
const ROOTPATH = process.cwd()
const version = require('../package.json').version
const doc = output()
const program = flag()
// CLI contents that contain Usage and Options
doc.write(defaultDoc)
const renderDoc = doc.render(false)
/**
* @function check check whether path exists
* @param path file or directory absolute path
* @param succeed while path exist, trigger callback
* @param fail while path doesn't exist, trigger callback
*/
const checkExist = (path, succeed, fail) => {
return new Promise((resolve, reject) => {
access(path, (err) => {
if(err) {
// console.error(err)
fail && fail()
resolve(false)
}
else {
const stat = statSync(path)
let type, ext
if(stat.isFile()) {
type = 'file'
}
else if(stat.isDirectory()) {
type = 'directory'
}
ext = extname(path)
succeed && succeed(type, ext)
resolve(true)
}
})
})
}
/**
* global cli params
*
* @cmd {Array} command list
* @argument {Array} process argument list
* @load {String} nodejs that will be reloading
*/
let watchArgs = {
cmd: [],
argument: [],
load: ''
}
program
.inject(doc)
// handle the flag -h | --help
.option('-h | --help', ({index, param}, args) => {
const helpArg = args[index-1]
if(helpArg) {
// output the proper doc
const flagMap = {
'--param | -p': paramDoc,
'--load | -l': loadDoc,
'--cmd | -c': cmdDoc
}
for(key of Object.keys(flagMap)) {
if(key.split(' | ').includes(helpArg)) {
console.log(flagMap[key])
}
}
}
else {
console.log(renderDoc)
}
})
// handle the flag --param, when you watch one nodejs with some arguments
.option('--param | -p', ({index, param}, args) => {
// argments that will be reloading
watchArgs.argument = args.slice(index+1) || []
})
// handle the flag --cmd, when you watch one nodejs and reload a command
.option('--cmd | -c', ({index, param}, args) => {
// command that will be reloading
const cmd = args.slice(index+1) || []
if(cmd.length >= 1) {
const needHelpForCMD = args[index+1] == '-h'
|| args[index+1] == '--help'
if(!needHelpForCMD) {
watchArgs.cmd = cmd
}
}
else {
console.log(chalk.red('if option --cmd | -c is provided , must input the commands'))
}
})
// handle the flag --load, when you watch one nodejs and reload another nodejs
.option('--load | -l', async ({index, param}, args) => {
// file that will be reloading
const loadFile = args[index+1]?
join(ROOTPATH, args[index+1])
: ''
const needHelpForCMD = args[index+1] == '-h'
|| args[index+1] == '--help'
if(!needHelpForCMD) {
await checkExist(loadFile, (type, ext) => {
if(type == 'file' && ext == '.js') {
watchArgs.load = loadFile
}
else {
console.log(chalk.red('if option --load | -l is provided, the load file must be nodejs'))
}
}, () => {
console.log(chalk.red(`the load file could't be found`))
})
}
})
// when you watch a file or a directory and reload something
.param({
index: 0,
async action({index, param}, args) {
// file that is being wathced
const file = join(ROOTPATH, param)
// command that will be reloading
const commandsNeedReload = watchArgs.cmd.length >= 1
&& watchArgs.cmd
// another file that will be reloading
const fileNeedReload = watchArgs.load
// argments that will be reloading
const fileArgs = watchArgs.argument
// options list exclude --param | -p
const optList = ['--load', '-l', '--cmd', '-c']
await checkExist(file, (type, ext) => {
const onlyReloadNodejsItself = args.every(item => !optList.includes(item))
if(fileNeedReload) {
watch(file, {target: fileNeedReload, params: fileArgs})
}
else if(commandsNeedReload) {
watch(file, {
target: () => {
// in order to make the npm CLI compatible, must use exec to execute .bat or .cmd with shell mode
// 为了兼容npm包的全局命令,只能使用exec衍生shell来运行bat或cmd文件
return exec(commandsNeedReload.join(' '), (err, stdout, stderr) => {
if(err) {
console.log(err)
}
console.log(stdout)
})
}
})
}
else if(onlyReloadNodejsItself) {
// you could only watch and reload itself for nodejs
if(type == 'file' && ext == '.js') {
watch(file, {target: file, params: fileArgs})
}
else {
console.log(chalk.yellow('when you watch a directory or not nodejs file, you must reload a nodejs or cmd'))
}
}
}, () => {
const list = ['-h', '--help', '-V', '--version']
if(!list.includes(param)) {
console.log(chalk.yellow('hotload must be before a valid file or directory path'))
}
})
}
})
// handle the version flag
.version(version)
.run()