-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·169 lines (147 loc) · 3.29 KB
/
cli.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
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
#!/usr/bin/env node
import { parseArgs } from 'node:util';
import { glob, readFile } from 'node:fs/promises';
import { sep, resolve, join } from 'node:path';
import nunjucks from 'nunjucks';
import { load } from 'js-yaml';
import pkg from './package.json' with { type: 'json' };
const DEFAULT_CONFIG = {
"datadir": "data",
"templatesdir": "templates",
"contentdir": "content",
"outdir": "dist"
};
const AS_JSON = {
with: {
type: 'json'
}
};
let config = { ...DEFAULT_CONFIG };
let args;
try {
args = parseArgs({
options: {
help: {
type: 'boolean',
short: 'h'
},
version: {
type: 'boolean',
short: 'V'
},
config: {
type: 'string',
short: 'c'
},
datadir: {
type: 'string'
},
templatesdir: {
type: 'string'
},
contentdir: {
type: 'string'
},
outdir: {
type: 'string'
}
}
});
} catch(err) {
console.error(err.message);
outputHelp();
process.exit(1);
}
const { values, positionals } = args;
if (values.help) {
outputHelp();
process.exit(0);
}
if (values.version) {
console.log(`${pkg.name} ${pkg.version}`);
process.exit(0);
}
if (values.datadir) config.datadir = values.datadir;
if (values.contentdir) config.contentdir = values.contentdir;
if (values.templatesdir) config.templatesdir = values.templatesdir;
if (values.outdir) config.outdir = values.outdir;
if (values.config) {
try {
let userConfig = (await import(resolve(values.config))).default;
config = {
...config,
...(typeof userConfig === 'function' ? await userConfig() : userConfig)
};
} catch(err) {
console.log(err);
process.exit(1);
}
}
const datafiles = await Array.fromAsync(
glob(
'**/*.{js,json}',
{
cwd: config.datadir
}
)
);
const globalData = await datafiles.reduce(async (acc, file) => {
const res = await acc;
const attrs = /\.json$/.test(file) ? AS_JSON : undefined;
let value = (await import(resolve(config.datadir, file), attrs)).default;
if (typeof value === 'function') value = await value();
const key = file.replace(/\.js(on)?$/, '').split(sep);
setDeepValue(res, key, value);
return res;
}, {});
const NunjucksEnv = nunjucks.configure(config.templatesdir, {
autoescape: true
});
const templatefiles = await Array.fromAsync(
glob(
'**/*.{njk,son}',
{
cwd: config.templatesdir
}
)
);
const sourcefiles = await Array.fromAsync(
glob(
'**/*.{txt,md,html,njk,son}',
{
cwd: config.contentdir
}
)
);
// TODO: create collections
sourcefiles.forEach(async file => {
const filecontent = await readFile(join(config.contentdir, file), 'utf8');
const { data, content } = extractFrontmatter(filecontent);
const processedContent = await nunjucks.renderString(content, {
page: data,
data: globalData
});
});
function extractFrontmatter(filecontent, delim = /(^-{3,}$)/m) {
const [ws, startDelim, frontmatter, endDelim, ...after] = filecontent.split(delim);
if (ws.trim() || !after) {
return { data: {}, content: filecontent };
}
return {
data: load(frontmatter),
content: after.join(delim)
};
}
function setDeepValue(obj, keypath, val) {
const parts = keypath.slice(0, -1);
let o = obj, k;
while (k = parts.shift()) {
if (!Object.hasOwn(o, k)) o[k] = {};
o = o[k];
}
o[keypath.at(-1)] = val;
}
function outputHelp() {
console.log(`${pkg.name} ${pkg.version}`);
console.log('Help info');
}