-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (73 loc) · 1.85 KB
/
index.js
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
#!/usr/bin/env node
const process = require('process')
const pfs = require('fs/promises')
const fs = require('fs')
const path = require('path')
const http = require('http')
const pkg = fs.readFileSync(path.join(__dirname, 'package.json'))
const version = JSON.parse(pkg).version
let rootPath = __dirname
let configPath = path.join(rootPath, 'kds-config.json')
let proxyPath = ''
let fileName = 'index.html'
// check argv
function checkArgv() {
const breakArgvList = new Set(['--help', '-h', '--version', '-v'])
const noBreakArgvList = new Set(['--file', '--config', '--proxy'])
const argvList = process.argv.slice(2)
if (argvList.length <= 0) { // if no argv, go default
main()
} else {
// if there has a break operation, break all
for (let i of breakArgvList) {
if (argvList.indexOf(i) >= 0) {
switch(i) {
case '--help':
case '-h':
consoleHelp()
break
case '--version':
case '-v':
console.log(`k-dev-server v${version}`)
break
default:
consoleHelp()
}
return
}
}
// or have some params to change
argvList.forEach((i, index) => {
if (noBreakArgvList.has(i)) {
switch(i) {
case '--config':
configPath = argvList[index + 1]
break
case '--file':
fileName = argvList[index + 1]
break
case '--proxy':
proxyPath = argvList[index + 1]
break
default:
console.log('?')
}
}
})
// go main task
main()
}
}
function consoleHelp() {
console.log('help!')
}
// main function
async function main() {
try {
const dir = await pfs.readdir(process.cwd())
console.log(dir.indexOf(configPath))
} catch (err) {
console.error(err)
}
}
checkArgv()