-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.mjs
executable file
·103 lines (90 loc) · 3.65 KB
/
cli.mjs
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
#!/usr/bin/env node
import { spawn } from "child_process";
import { readFileSync } from "fs";
import path from "path";
import { fileURLToPath } from "url";
import yargs from "yargs/yargs";
import journal from "./index.mjs";
import { color, colors, dim } from "./src/colors.mjs";
const { log } = console;
const dirname = path.dirname(fileURLToPath(import.meta.url));
const packageJSON = JSON.parse(readFileSync(path.resolve(dirname, "./package.json")));
const titleTemplate = `${color(` _ _
| | ___ _ _ _ __ _ __ __ _| |
_ | |/ _ \\| | | | '__| '_ \\ / _\` | |
| |_| | (_) | |_| | | | | | | (_| | |
\\___/ \\___/ \\__,_|_| |_| |_|\\__,_|_|`, `${colors.FgGreen}`)}
Journal v${packageJSON.version}. ${color("A static blog generator", colors.FgCyan)}.
`;
function getConfig(args){
log(`Loading journal config from ${color(args.config, colors.FgBlue)}`);
let config = {};
try {
config = JSON.parse(readFileSync(path.resolve(args.config)));
} catch (err) {
log(`${color("ERROR: Could not load config, make sure the file exists!", colors.FgRed)}\n`);
process.exit(1); // eslint-disable-line no-process-exit
}
config.inputPath = path.resolve("./", config.inputPath || args.input);
config.static = path.resolve("./", config.static || args.static);
config.publish = path.resolve("./", config.publish || args.output);
return config;
}
/*
* The CLI commands
*/
function command(argv){
log(titleTemplate);
return getConfig(argv);
}
async function build(argv){
const config = command(argv);
log(`Building ${color(config.title, colors.FgGreen)}`);
const buildStats = await journal(config);
log(`
Built:
- ${color(buildStats.postCount, colors.FgGreen)} Posts
- ${color(buildStats.pageCount, colors.FgGreen)} Pages
in ${color(buildStats.time, colors.FgGreen)} seconds
The site was exported to: ${color(`${config.publish}/`, colors.FgGreen)}
`);
return buildStats;
}
function staticServer(argv){
const config = command(argv);
log("Starting local static html server.");
spawn("npx", ["http-server", config.publish, "-gbo", "-c10"], { stdio: "inherit" });
}
/* eslint-disable-next-line no-unused-vars */
const args = yargs(process.argv.slice(2)) // We have to have a var or node exits
.scriptName("journal")
.version(packageJSON.version)
.usage("Usage: $0 [command] [args]")
.help("h")
.alias("h", "help")
.alias("v", "version")
.example("$0", "Basic usage, uses defaults")
.example("$0 --config myconfig.json", "Use a custom config file")
.example("$0 -i '../custom/dir'", "Use a custom content directory")
.example("$0 view", "Start a server for built content")
.example("$0 view --config myconfig.json", "Start server with a custom config")
.group(["c"], "Journal Config: (can be overriden)")
.default("c", "journal.json", `${dim("<current dir>")}/journal.json`)
.alias("c", "config")
.group(["i", "o", "s"], "Journal Directories: (overrides config)")
.default("i", "content", `${dim("<current dir>")}/content/`)
.alias("i", "input")
.alias("i", "content")
.describe("i", "Location of markdown to be parsed")
.default("o", "build", `${dim("<current dir>")}/build/`)
.alias("o", "output")
.describe("o", "Location to output generated html")
.default("s", "static", `${dim("<current dir>")}/static/`)
.alias("s", "static")
.describe("s", "Location of static files (css, images, etc)")
.group(["v", "h"], "Other:")
.command(["build", "$0"], "build a journal", {}, build)
.command(["serve", "view"], "start a httpserver locally to serve any content", {}, staticServer)
.epilog(`Documentation available at: https://github.com/abritinthebay/journal/
${dim(`© 2013–${new Date().getFullYear()} Gregory Wild-Smith`)}`)
.argv;