-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild-docs.js
53 lines (51 loc) · 1.65 KB
/
build-docs.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
var fs = require('fs');
var origin = './front_end/2019';
var target = './docs/front-end';
var configfile = './docs/.vuepress/config.js';
var config = require('./docs/.vuepress/config');
var copy = function(src, dst) {
let paths = fs.readdirSync(src); //同步读取当前目录
paths.forEach(function(path) {
if (path === 'README.md') return;
var _src = src + '/' + path;
var _dst = dst + '/' + path;
fs.stat(_src, function(err, stats) {
//stats 该对象 包含文件属性
if (err) throw err;
if (stats.isFile()) {
//如果是个文件则拷贝
let contentText = fs.readFileSync(_src);
fs.writeFileSync(_dst, '# ' + path.slice(0, -3) + '\n\n' + contentText);
// let readable = fs.createReadStream(_src); //创建读取流
// let writable = fs.createWriteStream(_dst); //创建写入流
// readable.pipe(writable);
} else if (stats.isDirectory()) {
//是目录则 递归
checkDirectory(_src, _dst, copy);
}
});
});
};
var checkDirectory = function(src, dst, callback) {
fs.access(dst, fs.constants.F_OK, err => {
if (err) {
fs.mkdirSync(dst);
callback(src, dst);
} else {
callback(src, dst);
}
});
};
var changeConfig = function() {
let paths = fs.readdirSync(origin); //同步读取当前目录
let arr = [];
paths.forEach(function(path) {
if (path === 'README.md') return;
arr.push('/front-end/' + path);
});
config.themeConfig.sidebar['/front-end/'] = arr;
fs.writeFileSync(configfile, 'module.exports = ' + JSON.stringify(config));
};
// 先拷贝文件夹
checkDirectory(origin, target, copy);
changeConfig();