-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.js
executable file
·73 lines (62 loc) · 1.62 KB
/
cmd.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
#!/usr/bin/env node
const fs = require("fs")
const path = require("path")
const jsmsg = require("./lib")
function printUsageAndExit(code) {
console.log("Available args:")
console.log(" --help print usage and exit")
console.log(" --plugin plugin to use, default is 'blog'")
console.log(" --config user config file, default is '.js-msg.js'")
console.log(" srcDir source dir")
console.log(" dstDir destination dir")
process.exit(code)
}
function checkArgs() {
let args = process.argv.slice(2)
let config = null
let plugins = []
let leftover = []
for (var i = 0; i < args.length; i++) {
if (args[i] === "--plugin") {
plugins.push(args[++i])
} else if (args[i] === "--config") {
config = args[++i]
} else if (args[i] === "--help") {
printUsageAndExit(0)
} else {
leftover.push(args[i])
}
}
if (leftover.length != 2) {
console.log("Invalid args.")
printUsageAndExit(-1)
}
if (plugins.length === 0) {
plugins.push("blog")
}
for (const plugin of plugins) {
if (plugin !== "null") {
jsmsg.runConfig(require("./plugins/" + plugin))
}
}
if (config !== null) {
jsmsg.runConfig(path.resolve(process.cwd(), config))
} else {
config = path.resolve(process.cwd(), ".js-msg.js")
if (fs.existsSync(config)) {
jsmsg.runConfig(require(config))
}
}
let srcDir = leftover[0]
let dstDir = leftover[1]
return {srcDir, dstDir}
}
async function main() {
let {srcDir, dstDir} = checkArgs()
try {
await jsmsg.genSite(srcDir, dstDir)
} catch (e) {
console.log(e)
}
}
main()