-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathindex.js
64 lines (52 loc) · 1.44 KB
/
index.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
"use strict";
const fs = require("fs");
const SSR_DEFAULT = require("../config").SSR_DEFAULT;
const chalk = require("chalk");
const resolve = (exports.resolve = require("path").resolve);
exports.cwd = function(path) {
return resolve(process.cwd(), path || ".");
};
exports.pwd = function(path) {
return resolve(require("path").dirname(__dirname), path);
};
exports.exists = function(path) {
if (fs.existsSync(path)) {
return path;
}
return undefined;
};
exports.pkg = function() {
return exports.exists(exports.cwd("package.json"))
? require(exports.cwd("package.json"))
: {};
};
exports.read = function(path) {
return fs.readFileSync(path, "utf-8").toString();
};
function loadConfig(config) {
try {
return require(exports.cwd(config));
} catch (e) {
console.log(chalk.red(`${e.message} in ${config}`));
process.exit(1);
}
}
exports.getConfig = function(configFile) {
const pkg = exports.pkg();
const ctx = exports.cwd(".");
let config = SSR_DEFAULT;
if (configFile) {
config = loadConfig(configFile);
config.template = /\.html$/.test(config.template)
? read(resolve(ctx, config.template))
: SSR_DEFAULT.template;
} else if (pkg.docsify) {
const tpl = pkg.docsify.template;
config = pkg.docsify;
config.template =
tpl && exports.exists(resolve(ctx, tpl))
? exports.read(tpl)
: SSR_DEFAULT.template;
}
return Object.assign(SSR_DEFAULT, config);
};