Skip to content

Commit 2194be7

Browse files
committed
feat: add static command
1 parent 78761d6 commit 2194be7

File tree

11 files changed

+1134
-213
lines changed

11 files changed

+1134
-213
lines changed

bin/docsify

+33-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const yargs = require("yargs")
8686
handler: argv => run.serve(argv.path, argv.open, argv.port, argv.P, argv.i)
8787
})
8888
.command({
89-
command: "start <path>",
89+
command: "start [path]",
9090
desc: chalk.gray(y18n.__("start")),
9191
builder: yargs =>
9292
yargs.options({
@@ -109,6 +109,38 @@ const yargs = require("yargs")
109109
}),
110110
handler: argv => run.start(argv.path, argv.config, argv.port)
111111
})
112+
.command({
113+
command: "static [path]",
114+
desc: chalk.gray(y18n.__("static")),
115+
builder: yargs =>
116+
yargs.options({
117+
dest: {
118+
alias: "d",
119+
default: "docs-static",
120+
desc: chalk.gray(y18n.__("static.dest")),
121+
nargs: 0,
122+
requiresArg: false,
123+
type: "string"
124+
},
125+
config: {
126+
alias: "c",
127+
default: false,
128+
desc: chalk.gray(y18n.__("static.config")),
129+
nargs: 0,
130+
requiresArg: false,
131+
type: "string"
132+
},
133+
"index-name": {
134+
alias: "i",
135+
desc: chalk.gray(y18n.__("serve.indexname")),
136+
nargs: 1,
137+
requiresArg: true,
138+
type: "string"
139+
}
140+
}),
141+
handler: argv =>
142+
run.static(argv.path, argv.config, argv.indexName, argv.dest)
143+
})
112144
.help()
113145
.option("help", {
114146
alias: "h",

lib/commands/start.js

+4-50
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,21 @@
33
const connect = require("connect");
44
const serveStatic = require("serve-static");
55
const Renderer = require("docsify-server-renderer");
6-
const fs = require("fs");
76
const util = require("../util/index");
87
const chalk = require("chalk");
98
const LRU = require("lru-cache");
109

11-
const defaultConfig = {
12-
template: `<!DOCTYPE html>
13-
<html lang="en">
14-
<head>
15-
<meta charset="UTF-8">
16-
<title>My Doc</title>
17-
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
18-
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
19-
<link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css" title="vue">
20-
</head>
21-
<body>
22-
<!--inject-app-->
23-
<!--inject-config-->
24-
<script src="//unpkg.com/docsify/lib/docsify.min.js"></script>
25-
</body>
26-
</html>`
27-
};
28-
29-
function loadConfig(config) {
30-
try {
31-
return require(util.cwd(config));
32-
} catch (e) {
33-
console.log(chalk.red(`${e.message} in ${config}`));
34-
process.exit(1);
35-
}
36-
}
37-
3810
module.exports = function(path, configFile, port) {
39-
let config = defaultConfig;
40-
const pkg = util.pkg();
41-
const ctx = util.cwd(".");
42-
43-
path = path || "./";
44-
45-
if (configFile) {
46-
config = loadConfig(configFile);
47-
config.template = /\.html$/.test(config.template)
48-
? util.read(util.resolve(ctx, config.template))
49-
: defaultConfig.template;
50-
} else if (pkg.docsify) {
51-
const tpl = pkg.docsify.template;
11+
const config = util.getConfig(configFile);
5212

53-
config = pkg.docsify;
54-
config.template =
55-
tpl && util.exists(util.resolve(ctx, tpl))
56-
? util.read(tpl)
57-
: defaultConfig.template;
58-
}
13+
path = path || ".";
5914

60-
const renderer = new Renderer(Object.assign(defaultConfig, config));
15+
const renderer = new Renderer(config);
6116
const server = connect();
6217
const cached = new LRU(config.maxAge || 0);
6318

64-
server.use(serveStatic(path));
6519
server.use(function(req, res) {
66-
serveStatic(path)(req, res, function() {
20+
serveStatic(path, { index: false })(req, res, function() {
6721
if (
6822
/\.(jpg|jpeg|gif|png|svg|ico|mp4|webm|ogg|ogv|js|css|md)(?:\?v=[0-9.]+)?$/.test(
6923
req.url

lib/commands/static.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"use strict";
2+
3+
const serveStatic = require("serve-static");
4+
const connect = require("connect");
5+
const getPort = require("get-port");
6+
const Renderer = require("docsify-server-renderer");
7+
const util = require("../util/index");
8+
const fg = require("fast-glob");
9+
const write = require("write");
10+
const ora = require("ora");
11+
const chalk = require("chalk");
12+
const resolve = util.resolve;
13+
14+
module.exports = function(
15+
path = "",
16+
configFile,
17+
indexName,
18+
dest = "docs-static"
19+
) {
20+
path = resolve(path || ".");
21+
const config = util.getConfig(configFile);
22+
const server = connect();
23+
const indexFile = resolve(path, indexName || "index.html");
24+
const render = new Renderer(config);
25+
const files = fg.sync("**/*.md", {
26+
cwd: path
27+
});
28+
let app;
29+
const spinner = ora();
30+
31+
spinner.start("Rendering");
32+
33+
getPort()
34+
.then(port => {
35+
server.use(serveStatic(path, { index: indexFile }));
36+
app = server.listen(port);
37+
38+
return Promise.all(files.map(file => render.render(file)));
39+
})
40+
.then(data => {
41+
// 保存文件
42+
return Promise.all(
43+
data.map(({ url, content }) => {
44+
const filePath = url
45+
.replace(/README\.md$/, "index.html")
46+
.replace(/\.md$/, ".html");
47+
spinner.text = `Rendering ${url}`;
48+
write(resolve(util.cwd(), dest, filePath), content);
49+
})
50+
);
51+
})
52+
.then(() => {
53+
spinner.succeed(`Success! Generate static files at ${chalk.green(dest)}`);
54+
app && app.close();
55+
})
56+
.catch(e => {
57+
spinner.fail(e.message);
58+
app && app.close();
59+
});
60+
};

lib/config.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
exports.SSR_DEFAULT = {
2+
template: `<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>My Doc</title>
7+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
8+
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
9+
<link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css" title="vue">
10+
</head>
11+
<body>
12+
<!--inject-app-->
13+
<!--inject-config-->
14+
<script src="//unpkg.com/docsify/lib/docsify.min.js"></script>
15+
</body>
16+
</html>`
17+
};

lib/index.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
2-
init: require('./commands/init'),
3-
serve: require('./commands/serve'),
4-
start: require('./commands/start')
5-
}
2+
init: require("./commands/init"),
3+
serve: require("./commands/serve"),
4+
start: require("./commands/start"),
5+
static: require("./commands/static")
6+
};

lib/util/index.js

+34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22

33
const fs = require("fs");
4+
const SSR_DEFAULT = require("../config").SSR_DEFAULT;
5+
const chalk = require("chalk");
46

57
const resolve = (exports.resolve = require("path").resolve);
68

@@ -28,3 +30,35 @@ exports.pkg = function() {
2830
exports.read = function(path) {
2931
return fs.readFileSync(path, "utf-8").toString();
3032
};
33+
34+
function loadConfig(config) {
35+
try {
36+
return require(exports.cwd(config));
37+
} catch (e) {
38+
console.log(chalk.red(`${e.message} in ${config}`));
39+
process.exit(1);
40+
}
41+
}
42+
43+
exports.getConfig = function(configFile) {
44+
const pkg = exports.pkg();
45+
const ctx = exports.cwd(".");
46+
let config = SSR_DEFAULT;
47+
48+
if (configFile) {
49+
config = loadConfig(configFile);
50+
config.template = /\.html$/.test(config.template)
51+
? read(resolve(ctx, config.template))
52+
: SSR_DEFAULT.template;
53+
} else if (pkg.docsify) {
54+
const tpl = pkg.docsify.template;
55+
56+
config = pkg.docsify;
57+
config.template =
58+
tpl && exports.exists(resolve(ctx, tpl))
59+
? exports.read(tpl)
60+
: SSR_DEFAULT.template;
61+
}
62+
63+
return Object.assign(SSR_DEFAULT, config);
64+
};

0 commit comments

Comments
 (0)