-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.ts
168 lines (151 loc) · 4.36 KB
/
config.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
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
import { parse, stringify } from "./deps.ts";
import { ensureDir } from "./deps.ts";
import { dirname, isAbsolute, join } from "./deps.ts";
import { normalizeURL, withTrailingSlash } from "./deps.ts";
export interface SiteConfig {
title: string;
description: string;
rootName: string;
url: string;
author: { name: string; email: string; url: string };
}
export interface TerConfig {
inputPath: string;
outputPath: string;
pageView: string;
feedView: string;
style: string;
assetsPath: string;
viewsPath: string;
siteConfigPath: string;
ignoreKeys: string[];
staticExts: string[];
site: SiteConfig;
quiet: boolean;
renderDrafts: boolean;
}
const defaultSiteConfig: SiteConfig = {
title: "Your Blog Name",
rootName: "index",
description: "I am writing about my experiences as a naval navel-gazer",
url: "https://example.com/",
author: {
name: "Your Name Here",
email: "youremailaddress@example.com",
url: "https://example.com/about-me/",
},
};
const defaultConfig: TerConfig = {
inputPath: Deno.cwd(),
outputPath: "_site",
assetsPath: ".ter/assets",
viewsPath: ".ter/views",
pageView: "",
feedView: "",
style: "",
siteConfigPath: ".ter/config.yml",
ignoreKeys: ["draft"],
staticExts: [
"png",
"jpg",
"jpeg",
"gif",
"webp",
"pdf",
"ico",
"webm",
"mp4",
],
site: defaultSiteConfig,
quiet: false,
renderDrafts: false,
};
async function checkSiteConfig(configPath: string): Promise<boolean> {
const filepath = isAbsolute(configPath)
? configPath
: join(Deno.cwd(), configPath);
await Deno.stat(filepath).catch(() => Promise.reject(filepath));
return Promise.resolve(true);
}
async function initSiteConfig(config: SiteConfig, configPath: string) {
const yaml = stringify(
config as unknown as Record<string, unknown>,
);
await ensureDir(dirname(configPath));
await Deno.writeTextFile(configPath, yaml);
}
async function parseSiteConfig(path: string): Promise<SiteConfig | undefined> {
try {
const decoder = new TextDecoder("utf-8");
const data = decoder.decode(await Deno.readFile(path));
const conf = parse(data) as SiteConfig;
return conf;
} catch {
return undefined;
}
}
interface CreateConfigOpts {
configPath: string | undefined;
inputPath: string | undefined;
outputPath: string | undefined;
pageView: string;
feedView: string;
style: string;
quiet: boolean;
renderDrafts: boolean;
}
export async function createConfig(opts: CreateConfigOpts): Promise<TerConfig> {
const conf = defaultConfig;
if (opts.configPath && opts.configPath != "") {
conf.siteConfigPath = isAbsolute(opts.configPath)
? opts.configPath
: join(Deno.cwd(), opts.configPath);
}
if (opts.inputPath && opts.inputPath != "") {
conf.inputPath = isAbsolute(opts.inputPath)
? opts.inputPath
: join(Deno.cwd(), opts.inputPath);
}
if (opts.outputPath && opts.outputPath != "") {
conf.outputPath = isAbsolute(opts.outputPath)
? opts.outputPath
: join(Deno.cwd(), opts.outputPath);
}
conf.pageView = opts.pageView;
conf.feedView = opts.feedView;
conf.style = opts.style;
conf.quiet = opts.quiet;
conf.renderDrafts = opts.renderDrafts;
await checkSiteConfig(conf.siteConfigPath)
.catch(async () => {
console.log(
`Config file missing, initializing default config at ${conf.siteConfigPath}`,
);
await initSiteConfig(conf.site, conf.siteConfigPath);
});
const siteConf = await parseSiteConfig(conf.siteConfigPath);
if (siteConf) {
if (typeof siteConf.title === "string") {
conf.site.title = siteConf.title;
}
if (typeof siteConf.rootName === "string") {
conf.site.rootName = siteConf.rootName;
}
if (typeof siteConf.description === "string") {
conf.site.description = siteConf.description;
}
if (typeof siteConf.url === "string") {
conf.site.url = withTrailingSlash(normalizeURL(siteConf.url));
}
if (typeof siteConf.author?.name === "string") {
conf.site.author = { ...conf.site.author, name: siteConf.author.name };
}
if (typeof siteConf.author?.email === "string") {
conf.site.author = { ...conf.site.author, email: siteConf.author.email };
}
if (typeof siteConf.author?.url === "string") {
conf.site.author = { ...conf.site.author, url: siteConf.author.url };
}
}
return conf;
}