-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.ts
112 lines (99 loc) · 2.95 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
import { deepmerge } from "./deps/deepmerge.ts";
import { path, fs } from "./deps/std.ts";
import { BuildConfig, UserConfig } from "./types.d.ts";
const defaultUserConfig: UserConfig = {
title: "Your Blog Name",
description: "I am writing about my experiences as a naval navel-gazer",
url: "https://example.com/",
rootCrumb: "~",
authorName: "Your Name Here",
authorEmail: "[email protected]",
authorUrl: "https://example.com/about-me/",
lang: "en",
codeHighlight: false,
head: "",
};
export const defaultBuildConfig: BuildConfig = {
inputPath: Deno.cwd(),
outputPath: path.join(Deno.cwd(), "_site"),
userConfigPath: path.join(Deno.cwd(), ".ter/config.json"),
ignoreKeys: ["draft"],
staticExts: [
"png",
"jpg",
"jpeg",
"gif",
"svg",
"webp",
"pdf",
"ico",
"webm",
"mp4",
],
userConfig: defaultUserConfig,
renderDrafts: false,
};
const checkUserConfig = async (configPath: string): Promise<boolean> => {
const filepath = path.isAbsolute(configPath)
? configPath
: path.join(Deno.cwd(), configPath);
await Deno.stat(filepath).catch(() => Promise.reject(filepath));
return Promise.resolve(true);
};
const initUserConfig = (config: UserConfig, configPath: string) => {
fs.ensureDirSync(path.dirname(configPath));
Deno.writeTextFileSync(configPath, JSON.stringify(config, null, 2));
};
interface CreateConfigOpts {
configPath: string | undefined;
inputPath: string | undefined;
outputPath: string | undefined;
renderDrafts: boolean;
}
export const createConfig = async (
opts: CreateConfigOpts
): Promise<BuildConfig> => {
const { isAbsolute, join } = path;
if (opts.configPath && opts.configPath != "") {
defaultBuildConfig.userConfigPath = isAbsolute(opts.configPath)
? opts.configPath
: join(Deno.cwd(), opts.configPath);
}
if (opts.inputPath && opts.inputPath != "") {
defaultBuildConfig.inputPath = isAbsolute(opts.inputPath)
? opts.inputPath
: join(Deno.cwd(), opts.inputPath);
}
if (opts.outputPath && opts.outputPath != "") {
defaultBuildConfig.outputPath = isAbsolute(opts.outputPath)
? opts.outputPath
: join(Deno.cwd(), opts.outputPath);
}
defaultBuildConfig.renderDrafts = opts.renderDrafts;
await checkUserConfig(defaultBuildConfig.userConfigPath).catch(() => {
console.warn(
`Config file missing, initializing default config at ${defaultBuildConfig.userConfigPath}`
);
initUserConfig(
defaultBuildConfig.userConfig,
defaultBuildConfig.userConfigPath
);
});
try {
const parsedConf = JSON.parse(
await Deno.readTextFile(defaultBuildConfig.userConfigPath)
);
defaultBuildConfig.userConfig = deepmerge(
defaultBuildConfig.userConfig,
parsedConf
);
} catch (err) {
console.error(
"Possibly error in config file:",
defaultBuildConfig.userConfigPath
);
console.error(err);
Deno.exit(1);
}
return defaultBuildConfig;
};