-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmain.ts
78 lines (76 loc) · 2.04 KB
/
main.ts
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
import log from "./log.ts";
import fetchSources from "./fetch-sources.ts";
import build from "./build.ts";
import serverMarkdown from "./serve-markdown.ts";
import servePublic from "./serve-public.ts";
import { getConfig, getFormatedSource, getSqlitePath, isDev } from "./util.ts";
import { CliOptions, RunOptions } from "./interface.ts";
import initDb from "./init-db.ts";
import buildHtml from "./build-html.ts";
// import db init meta json
export default async function main(cliOptions: CliOptions, ...args: string[]) {
if (cliOptions.debug) {
log.setLevel("debug");
}
const config = await getConfig();
let sourceIdentifiers: string[] = args.length > 0
? args
: Object.keys(config.sources);
if (
cliOptions.limit && cliOptions.limit > 0
) {
sourceIdentifiers = sourceIdentifiers.slice(0, cliOptions.limit);
}
// check if source exists
for (const sourceIdentifier of sourceIdentifiers) {
if (config.sources[sourceIdentifier] === undefined) {
config.sources[sourceIdentifier] = getFormatedSource(
sourceIdentifier,
null,
);
}
}
const isBuildHtml = cliOptions.html || false;
const autoInit = cliOptions.autoInit;
if (autoInit || (isDev())) {
await initDb();
}
// init sqlite db
// te
// Open a database
const runOptions: RunOptions = {
config: config,
sourceIdentifiers: args,
...cliOptions,
};
log.info(
`run options: ${
JSON.stringify({ sourceIdentifiers: args, ...cliOptions }, null, 2)
}`,
);
if (cliOptions.fetch) {
await fetchSources(runOptions);
} else {
log.info("skip fetch sources");
}
// 2. build markdowns, and htmls
await build(runOptions);
// 3. build html
//
// if (isBuildHtml) {
// await buildHtml(runOptions);
// }
// 3. serve site
if (runOptions.serve) {
log.info("serve site");
// check is there is html
if (isBuildHtml) {
servePublic();
} else {
// serve to markdown preview files
await serverMarkdown(runOptions);
}
} else {
log.info("skip serve site");
}
}