-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmama.js
107 lines (104 loc) · 3.12 KB
/
mama.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
#!/usr/bin/env node
'use strict';
/**
* @file 分析主路径
*/
const path = require('path');
const process = require('process');
const testPath = process.argv[2] || '';
const installName = process.argv[3] || '';
const exec = require('child_process').exec;
const api2html = require('./api2html');
if (!testPath) {
console.log('请输入npm包的名字');
return;
}
if (testPath === 'i' || testPath === 'install') {
console.log('正在安装依赖...');
const installPath = path.resolve(__dirname, './shell/install.sh');
exec(`sh ${installPath}`, {cwd: __dirname}, (err, std, stderr) => {
if (!err) {
console.log(std);
console.log('安装依赖完成');
}
else {
console.log('依赖安装失败\n', err);
}
});
return;
}
if (/^i-|(^install-)/.test(testPath)) {
console.log('正在安装依赖包...');
const installPath = path.resolve(__dirname, './shell/installApi.sh');
const npmName = testPath.split('-')[1];
exec(`sh ${installPath} ${npmName}`, {cwd: __dirname}, (err, std, stderr) => {
if (!err) {
console.log(std);
console.log('安装依赖完成');
}
else {
console.log('依赖安装失败\n', err);
}
});
return;
}
const {FileSystem} = require('tutils/fileSystem');
const fsSync = require('tutils/fileSystemSync');
const fs = new FileSystem();
const chalk = require('chalk');
const runPath = process.cwd();
const astParser = require('./parser');
// const testPath = path.resolve(runPath, './test/cheerio-1.0.0/index.js');
// const testPath = path.resolve(runPath, './test/react/src/React.js');
// const testPath = path.resolve(runPath, './test/mobx-master/src/v4/mobx.ts');
let uniqueId = new Date().getTime();
class Mama {
constructor() {
}
init(npmName) {
if (!npmName) {
return;
}
let apiData = {
name: npmName,
type: 'browser'
};
try {
const packages = require(npmName);
apiData = api2html(packages, npmName, {
invalidKey: ['CommonsChunkPlugin', 'UglifyJsPlugin', 'optimize']
})
}
catch (e) {
if (e.toString().indexOf(' Cannot find module ') > -1) {
console.log(chalk.red('请先安装依赖'));
}
return;
}
return apiData
}
}
const baba = new Mama();
const data = baba.init(testPath);
if (!data) {
return;
}
// 保存数据
const viewDataPath = path.resolve(__dirname, 'view/src/apiData.js');
fs.writeFile(viewDataPath, `var tree = ${JSON.stringify(data, null, 4)}
export default tree;
`);
console.log(
chalk.green(`分析结果文件生成成功:${viewDataPath}`)
);
console.log(chalk.green('正在启动视图服务...'));
const startPath = path.resolve(__dirname, './shell/startApi.sh');
exec(`sh ${startPath}`, {cwd: __dirname}, (err, std, stderr) => {
console.log(chalk.green('启动视图服务成功!'));
if (!err) {
console.log(std);
}
else {
console.log('启动视图失败,请重试\n', err);
}
});