-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
246 lines (225 loc) · 7.25 KB
/
index.js
File metadata and controls
246 lines (225 loc) · 7.25 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import {minimist, fs, fsConstants, path,
glob, mkdirp, hb, require} from "./deps.js"
function usage() {console.log(`wiki-ssg build --from <dir> --dest <dir>
generates a static wiki site
Options:
--from <folder> ... copy wiki pages and assets from the specified folder
--dest <folder> ... generate the static site in the specified folder
`)}
export async function main() {
await parseArgv()
await Promise.all([
copyWikiClientCode(),
createFactories(),
copyPlugins(),
copySecurityFriends(),
copyDefaultData()
])
await copyWikiData()
}
let DATA, BASE, ownedBy
let CLIENT = path.resolve(require.resolve("wiki-client/package.json"), "..")
let SERVER = path.resolve(require.resolve("wiki-server/package.json"), "..")
let DEPS = path.join(CLIENT, "..")
const htmlTemplate = await createTemplate("wiki-client/views/static.html")
async function parseArgv() {
let args = minimist(process.argv.slice(2), {
string: ["from", "dest"], boolean: ["help", "version"], alias: {
h: "help", v: "version"
}
})
let [cmd] = args._
switch (true) {
case args.help || cmd == "help":
usage()
process.exit(1)
case args.version || cmd == "version":
console.log(await version())
process.exit(1)
case cmd == "build":
DATA = args.from || path.resolve("./data")
BASE = args.dest || path.resolve(".", "docs")
ownedBy = await owner()
break
default:
usage()
process.exit(1)
}
}
async function version() {
let pkg = JSON.parse(
await fs.readFile(new URL("package.json", import.meta.url) , "utf8"))
return pkg.version
}
const writers = new Map()
function guard(filename, fn) { // serialize writes to the same file
let p = writers.has(filename)
? writers.get(filename)
: Promise.resolve()
writers.set(filename, p.then(fn))
return p
}
async function owner() {
let HOME = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE
let search = await Promise.all([
path.join(DATA, "status", "owner.json"),
path.join(HOME, ".wiki", "owner.json"),
path.join(HOME, ".wiki", "localhost.owner.json")
].map(
async candidate => fs.access(candidate, fsConstants.R_OK)
.then(() => [candidate, true])
.catch(error => {
if (error.code != "ENOENT") {
throw error
}
return [candidate, false]
})
))
let ownerfile = search.find(([f, readable]) => readable)[0]
try {
let json = JSON.parse(await fs.readFile(ownerfile, "utf8"))
return json.name
} catch (error) {
console.warn('No owner found. Will use "Anonymous"')
return "Anonymous"
}
}
async function copyWikiClientCode() {
for (let filename of await findfiles(path.join(CLIENT, "client", "**"))) {
let destination = path.join(
BASE,
filename.slice(path.join(CLIENT, "client").length)
)
copyp(filename, destination)
}
}
async function createFactories() {
let factoriesfile = path.join(BASE, "system", "factories.json")
await mkdirp(path.dirname(factoriesfile))
Promise.all(
(await findfiles(path.join(DEPS, "wiki-plugin-*/factory.json")))
.map(async factory => require(factory))
).then(factories => {
guard(factoriesfile, () =>
fs.writeFile(factoriesfile, JSON.stringify(factories, null, 2)))
})
}
async function copyPlugins() {
for (let name of await findfolders(path.join(DEPS, "wiki-plugin-*", "client"))) {
let plugin = name.slice(0, -7)
let type = plugin.slice(path.join(DEPS, "wiki-plugin-").length)
let pluginsdir = path.join(BASE, "plugins", type)
copyCode(name, pluginsdir)
copyPages(
await findfiles(path.join(plugin, "pages", "**")),
json => {json.plugin = type})
}
}
async function copyCode(source, destination) {
let sources = await findfiles(path.join(source, "**"))
sources.map(async file => {
copyp(
file,
path.join(destination, file.slice(source.length))
)
})
}
async function copyPages(pages, jsonfn=x=>x) {
return await Promise.all(
pages.map(async name => {
let slug = path.basename(name)
let jsonFile = path.join(BASE, `${slug}.json`)
fs.readFile(name, "utf8").then(JSON.parse).then(async json => {
jsonfn(json)
let string = JSON.stringify(json, null, 2)
guard(jsonFile, () =>
fs.writeFile(jsonFile, string).catch(error => console.error({error})))
})
let htmlFile = path.join(BASE, `${slug}.html`)
await mkdirp(path.dirname(htmlFile))
guard(htmlFile, () =>
fs.writeFile(htmlFile, htmlTemplate({ownedBy,pages: [{page: slug}]}))
.catch(err => console.error({err})))
})
)
}
async function copySecurityFriends() {
return await copyCode(
path.join(DEPS, "wiki-security-friends", "client"),
path.join(BASE, "security")
)
}
async function copyDefaultData() {
copyPages(await findfiles(path.join(SERVER, "default-data", "pages", "**")))
.then(create404)
copyp(
path.join(SERVER, "default-data", "status", "favicon.png"),
path.join(BASE, "favicon.png"))
}
async function create404() {
let welcomefile = path.join(BASE, "welcome-visitors.html")
let welcome = await fs.readFile(welcomefile, "utf8")
let script = ` <script type="text/javascript" src="/404.js"></script>`
let html = welcome.split(/\n/).reduce((html, line) => {
if (/(script src=.\/client\.js.|wiki\.security.user.)/.test(line)) {
return html
}
if (/<\/body>/.test(line)) {
return `${html}${script}\n${line}\n`
} else {
return `${html}${line}\n`
}
}, "")
let the404file = path.join(BASE, "404.html")
guard(the404file, () => fs.writeFile(the404file, html))
.catch(err => console.error({err}))
let the404jsfile = path.join(BASE, "404.js")
copyp(new URL("./404.js", import.meta.url), the404jsfile)
}
async function copyWikiData() {
copyPages(await findfiles(path.join(DATA, "pages", "**")))
let STATUS = path.join(DATA, "status")
for (let filename of await findfiles(path.join(STATUS, "site*json"))) {
let destination = path.join(BASE,"system", filename.slice(STATUS.length))
copyp(filename, destination)
}
copyp(
path.join(STATUS, "sitemap.xml"),
path.join(BASE, "sitemap.xml"))
for (let filename of await findfiles(path.join(DATA, "assets", "**"))) {
let destination = path.join(
BASE, filename.slice(path.join(DATA).length))
copyp(filename, destination)
}
copyp(
path.join(DATA, "status", "favicon.png"),
path.join(BASE, "favicon.png")
).catch (error => {
if (error.code != 'ENOENT') {
throw error
}
console.log("No favicon.png found in data/status. We'll just keep the default.")
})
copyp(
path.join(BASE, "welcome-visitors.html"),
path.join(BASE, "index.html")
)
}
function createTemplate(filename) {
return fs.readFile(require.resolve(filename), "utf8")
.then(hb.compile)
}
async function copyp(source, destination) {
// ensure path to destination exists before copying
await mkdirp(path.dirname(destination))
guard(destination, () =>
fs.copyFile(source, destination)
.catch(error => {
if (error.code != 'ENOENT') {
throw error
}
console.warn(`couldn't copy "${error.path}". Skipping.`)
}))
}
const findfiles = pattern => glob(pattern, {nodir: true})
const findfolders = pattern => glob(path.join(pattern, "/"))