-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (86 loc) · 3.01 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
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
'use strict';
const path = require('path'),
fs = require('fs'),
browserSync = require('browser-sync'),
translate = require('./lib/translate'),
template = require('./lib/template'),
assignDefaultOption = require('./lib/assign-default-option'),
pfs = require('./lib/util/promise-fs'),
extReplace = require('./lib/util/ext-replace'),
tmpdir = require('./lib/util/tmpdir');
class MdBrowserPreview {
constructor(option) {
// default value for undefind property in option
this.option = assignDefaultOption(option);
// input file path
this.inputFilePath = path.join(process.cwd(), this.option.input);
if (path.isAbsolute(this.option.input)) {
this.inputFilePath = this.option.input;
}
// output file name and file path if required
let outputFileName = extReplace(path.basename(this.inputFilePath))
this.outputFilePath = undefined;
if (typeof this.option.output === 'string') {
let filepath;
if (path.isAbsolute(this.option.output)) {
filepath = this.option.output;
} else {
filepath = path.join(process.cwd(), this.option.output);
}
this.outputFilePath = path.join(filepath, outputFileName);
}
// htdocs local server, and file name to serve
this.tmpFilePath = path.join(tmpdir(), outputFileName);
this.bs = browserSync.create('bs' + Date.now());
// option for local server: BrowserSync
this.bsOption = {
startPath: '/' + outputFileName,
server: {baseDir: path.dirname(this.tmpFilePath)},
port: this.option.port,
browser: this.option.browser,
ui: false
};
}
compile() {
return pfs.readFile(this.inputFilePath, {
encoding: 'utf8'
}).then((mdText) => {
return translate(mdText, this.option);
}).then((partialHtml) => {
return template(partialHtml, this.option);
}).then((html) => {
let arr = [pfs.writeFile(this.tmpFilePath, html, 'utf8')];
if (this.outputFilePath) {
arr.push(pfs.writeFile(this.outputFilePath, html, 'utf8'));
}
return Promise.all(arr);
}).catch((reason) => {
return Promise.reject(reason);
});
}
serve() {
return new Promise((resolve, reject) => {
this.bs.init(this.bsOption, () => {
resolve();
});
});
}
startWatch() {
fs.watchFile(this.inputFilePath, () => {
this.compile().then(() => this.bs.reload());
});
}
exit() {
fs.unwatchFile(this.inputFilePath);
this.bs.exit();
}
static init(option) {
let mbp = new MdBrowserPreview(option);
mbp.compile().then(() => {
return mbp.serve();
}).then(() => {
mbp.startWatch();
});
}
}
module.exports = MdBrowserPreview;