-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathstatic.js
74 lines (67 loc) · 1.8 KB
/
static.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
"use strict";
const serveStatic = require("serve-static");
const connect = require("connect");
const getPort = require("get-port");
const Renderer = require("docsify-server-renderer");
const util = require("../util/index");
const fg = require("fast-glob");
const write = require("write");
const ora = require("ora");
const chalk = require("chalk");
const fse = require("fs-extra");
const resolve = util.resolve;
module.exports = function(
path = "",
configFile,
indexName,
dest = "docs-static"
) {
path = resolve(path || ".");
const config = util.getConfig(configFile);
// docs absolute path
config.path = path;
const server = connect();
const indexFile = resolve(path, indexName || "index.html");
const render = new Renderer(config);
const files = fg.sync("**/*.md", {
cwd: path,
ignore: ["_*"]
});
let app;
const spinner = ora();
spinner.start("Rendering");
getPort()
.then(async port => {
server.use(serveStatic(path, { index: indexFile }));
app = server.listen(port);
const queue = [];
for (const file of files) {
const res = await render.render(file);
queue.push(res);
console.log("Rendering " + file);
}
return queue;
})
.then(data => {
// save file
return Promise.all(
data.map(({ url, content, path }) => {
const filePath = url
.replace(/README\.md$/, "index.html")
.replace(/\.md$/, ".html");
write(resolve(util.cwd(), dest, filePath), content);
})
);
})
.then(() => {
return fse.copy(path, dest);
})
.then(() => {
spinner.succeed(`Success! Generate static files at ${chalk.green(dest)}`);
app && app.close();
})
.catch(e => {
spinner.fail(e.message);
app && app.close();
});
};