-
Notifications
You must be signed in to change notification settings - Fork 6
/
files.ts
189 lines (168 loc) · 4.1 KB
/
files.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { copy, ensureDir, minify, MinifyOpts, WalkEntry } from "./deps.ts";
import { basename, dirname, join, relative } from "./deps.ts";
import { buildFeed, buildPage, buildTagPage } from "./build.ts";
import { SiteConfig } from "./config.ts";
import { getTags } from "./attributes.ts";
import {
getBacklinkPages,
getChildPages,
getChildTags,
getPagesByTag,
Page,
TagPage,
} from "./pages.ts";
export interface OutputFile {
inputPath?: string;
filePath: string;
fileContent?: string;
}
interface BuildOpts {
outputPath: string;
view: string;
head: string;
conf: SiteConfig;
style: string;
includeRefresh: boolean;
}
const minifyOpts: MinifyOpts = {
collapseWhitespace: true,
collapseBooleanAttributes: true,
html5: true,
minifyCSS: true,
minifyJS: true,
};
export async function buildContentFiles(
pages: Array<Page>,
opts: BuildOpts,
): Promise<OutputFile[]> {
const files: Array<OutputFile> = [];
for (const page of pages) {
const filePath = join(
opts.outputPath,
page.url.pathname,
"index.html",
);
const tags = getTags(page.attrs);
const pagesByTag: { [tag: string]: Array<Page> } = {};
tags.forEach((tag: string) => {
pagesByTag[tag] = getPagesByTag(pages, tag);
});
const html = await buildPage(page, {
headInclude: opts.head,
includeRefresh: opts.includeRefresh,
childPages: page.isIndex ? getChildPages(pages, page) : [],
backlinkPages: getBacklinkPages(pages, page),
taggedPages: pagesByTag,
childTags: page.isIndex ? getChildTags(pages, page) : [],
view: opts.view,
siteConf: opts.conf,
style: opts.style,
});
if (typeof html === "string") {
const minified = await minify(html, minifyOpts);
files.push({
filePath,
fileContent: minified,
});
}
}
return files;
}
export async function buildTagFiles(
tagPages: Array<TagPage>,
opts: BuildOpts,
): Promise<OutputFile[]> {
const files: Array<OutputFile> = [];
for (const tag of tagPages) {
const filePath = join(
opts.outputPath,
"tag",
tag.name,
"index.html",
);
const html = await buildTagPage(tag.name, {
taggedPages: tag.pages,
view: opts.view,
headInclude: opts.head,
includeRefresh: opts.includeRefresh,
siteConf: opts.conf,
style: opts.style,
});
if (typeof html === "string") {
const minified = await minify(html, minifyOpts);
files.push({
inputPath: "",
filePath,
fileContent: minified,
});
}
}
return files;
}
export async function buildFeedFile(
pages: Array<Page>,
view: string,
outputPath: string,
siteConf: SiteConfig,
): Promise<OutputFile | undefined> {
const xml = await buildFeed(
pages,
view,
siteConf,
);
if (typeof xml === "string") {
return {
inputPath: "",
filePath: outputPath,
fileContent: xml,
};
}
}
export function getStaticFiles(
entries: Array<WalkEntry>,
inputPath: string,
outputPath: string,
): OutputFile[] {
const files: Array<OutputFile> = [];
for (const entry of entries) {
const relPath = relative(inputPath, entry.path);
const filePath = join(
outputPath,
dirname(relPath),
basename(relPath),
);
files.push({
inputPath: entry.path,
filePath,
});
}
return files;
}
export async function writeFiles(
files: OutputFile[],
quiet = false,
) {
quiet && console.log(`writing files...`);
for (const file of files) {
if (file.fileContent) {
quiet ||
console.log(`write\t${relative(Deno.cwd(), file.filePath)}`);
await ensureDir(dirname(file.filePath));
await Deno.writeTextFile(file.filePath, file.fileContent);
}
}
}
export async function copyFiles(
files: OutputFile[],
quiet = false,
) {
quiet && console.log(`copying files...`);
for (const file of files) {
if (file.inputPath) {
quiet ||
console.log(`copy\t${relative(Deno.cwd(), file.filePath)}`);
await ensureDir(dirname(file.filePath));
await copy(file.inputPath, file.filePath);
}
}
}