-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
179 lines (175 loc) · 4.88 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
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
#!/usr/bin/env node --harmony
"use strict";
const _ = require("lodash");
const path = require("path");
const Hook = require("./core/hook");
const Plugin = require("./core/plugin");
const glob = require("./core/glob");
const vinylFile = require("vinyl-file");
const Vinyl = require("vinyl");
const pify = require("pify");
const fs = pify(require("graceful-fs"));
const mkdirp = pify(require("mkdirp"));
const rimraf = pify(require("rimraf"));
const markedPlugin = require("kdoc-plugin-md");
const pugRenderPlugin = require("kdoc-plugin-pugrender");
class KDoc {
constructor(
src,
output,
{
hook = new Hook(this),
plugin = new Plugin(this),
globOptions = {},
debug = false
} = {}
) {
this.data = {
files: [],
src: [],
output: "./dist"
};
this.hook = hook;
this.plugin = plugin;
this.globOptions = globOptions;
this.debug = debug;
this.setSrc(src);
this.setOutput(output);
}
async each(list, handler) {
for (let index = 0; index < list.length; index++) {
try {
const item = list[index];
await handler.call(this, item, index);
} catch (error) {
throw error;
}
}
}
async fsEach(handler) {
const _files = this.data.files;
try {
await this.each(_files, handler);
} catch (error) {
throw error;
}
}
fsAdd(_files) {
if (!Array.isArray(_files)) {
_files = [_files];
}
this.data.files = [...new Set([...this.data.files, ..._files])];
}
fsRemove(_files) {
if (!Array.isArray(_files)) {
_files = [_files];
}
this.data.files = [
...new Set([...this.data.files].filter(x => !_files.includes(x)))
];
}
fsNew(obj) {
return new Vinyl(obj);
}
async fsWrite(_path, _content) {
if (!_path || !_content) {
return;
}
const dirname = path.dirname(_path);
await mkdirp(dirname);
await fs.writeFile(_path, _content, {
flag: "w+"
});
}
async scan() {
await KDoc.hook.run("scan.before", this);
await this.hook.run("scan.before", this);
const _paths = await this.paths();
const _files = [];
await this.each(_paths, async function(_path) {
const _file = await vinylFile.read(_path);
_files.push(_file);
});
this.fsAdd(_files);
await KDoc.hook.run("scan.after", this);
await this.hook.run("scan.after", this);
}
async dist() {
let _output = this.data.output;
await this.fsEach(async file => {
await KDoc.hook.run("pipe", this, file);
await this.hook.run("pipe", this, file);
});
await KDoc.hook.run("dist.before", this, this.data.files);
await this.hook.run("dist.before", this, this.data.files);
await this.fsEach(async file => {
if (!file.contents) {
return;
}
file.path = path.join(_output, file.relative);
await this.fsWrite(file.path, file.contents);
});
await KDoc.hook.run("dist.after", this, this.data.files);
await this.hook.run("dist.after", this, this.data.files);
}
async del(_paths, globOptions) {
_paths = await glob(_paths, globOptions);
for (let i = 0; i < _paths.length; i++) {
const _path = _paths[i];
await rimraf(_path);
}
}
async run() {
await KDoc.plugin.run(this);
await this.plugin.run(this);
await this.scan();
await this.dist();
}
use(plugin) {
this.plugin.install(plugin);
}
interface(name, handler) {
KDoc.prototype[name] = handler;
}
setSrc(src) {
if (!Array.isArray(src)) {
src = [src];
}
this.data.src = this.data.src || [];
this.each(src, s => {
this.data.src.push(s);
});
return this.data.src;
}
setOutput(output) {
this.data.output = path.resolve(process.cwd(), output);
}
async paths(_paths) {
_paths = _paths || this.data.src;
_paths = await glob(
_paths,
Object.assign(
{
nodir: true
},
this.globOptions
)
);
return _paths;
}
}
const use = function(plugin) {
KDoc.plugin.install(plugin);
};
KDoc.hook = new Hook();
KDoc.plugin = new Plugin();
KDoc.plugins = {
md: markedPlugin,
pugRender: pugRenderPlugin
};
KDoc.use = use;
KDoc.prototype.fs = fs;
KDoc.prototype.vf = Vinyl;
KDoc.prototype.mkdirp = mkdirp;
KDoc.prototype.rimraf = rimraf;
module.exports = KDoc;