-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·78 lines (67 loc) · 1.98 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
#!/usr/bin/env node
/**
* Copyright (c) 2016-present, rainie, Inc.
* All rights reserved.
*
* @flow
*/
const path = require('path');
const colors = require('colors');
const childProcess = require('child_process');
const co = require('co');
const thunkify = require('thunkify');
const program = require('commander');
const generateDocsJson = require('./generateDocsJson.js');
const file = require('./lib/file.js');
const packageJson = require('./package.json');
const root = process.cwd();
// 生成文档目录
const docsPath = path.join(root, 'docs');
// 用户配置文件路径
let docJsonPath = path.join(root, 'rainie-doc.json');
program
.version(packageJson.version)
.description('前端自动文档页面生成工具')
.option('-c , --config <file>', '配置文件路径')
.parse(process.argv);
if (program.config) {
docJsonPath = path.join(root, program.config);
}
main();
function main() {
co(function * () {
// 获得用户配置
const docConfig = yield file.read(docJsonPath);
// 创建导出目录docs
yield file.mkDir(docsPath);
// 生成docjson数据
yield generateDocsJson(docConfig);
// 复制docs resource到项目目录
yield * copyDocsResource(docsPath);
console.log('😁 Good Job!');
}).catch(err => {
console.log('😟 ' + err.message.red);
process.exit(1);
});
}
/**
* 复制docs resource到项目目录
*/
function *copyDocsResource(dirpath) {
const resourcePath = yield thunkify(getResourcePath)();
yield thunkify(copyDir)(resourcePath, dirpath);
console.log('✅ copy docs resource to: ./docs'.green);
}
function getResourcePath(callback) {
const command = 'npm root -g';
childProcess.exec(command, (err, stdout) => {
const resource_path = path.join(stdout, 'rainie-doc/dist/*').replace(/\s/, '');
callback(err, resource_path);
});
}
function copyDir(src, dist, callback) {
const command = `cp -a ${src} ${dist}`;
childProcess.exec(command, (err, stdout) => {
callback(err, stdout);
});
}