-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·167 lines (140 loc) · 5.12 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
#!/usr/bin/env node
const Path = require('path');
const fs = require('fs');
const validator = require('validator');
const program = require('commander');
var host = "";
var dataHead = "###项目原型\r\r";
var designDataHead = "设计要素\n\n";
var dataBody = "";
var designDataBody = "";
var itmDesign = "/design/";
//定义参数,以及参数内容的描述
program
.version('0.0.1')
.option('-h, --host <url>', 'a string of type url argument','http://whalexplorer.coding.me/prototype/')
.option('-m, --mode <notanumber>', 'a string of type url argument',1);
//添加额外的文档描述
program.on('help', function() {
console.log(' Examples:');
console.log('');
console.log(' # a string of type url argument, eg: https://www.jingwhale.cc/');
});
//解析commandline arguments
program.parse(process.argv);
if(program.host){
if(validator.isURL(program.host)){
host = program.host;
}else{
console.log("The --host url format is incorrect,please re-enter the host.");
return;
}
}else{
host = "http://whalexplorer.coding.me/prototype/";
}
//输出结果
console.info('--host:'+program.host);
console.info('--mode:'+program.mode);
const pathName = Path.resolve('./');//当前命令运行的目录;__dirname:返回运行文件所在的目录
console.log(pathName);
const srcFile = pathName + '/README.md';
console.log(srcFile);
makelink(srcFile,pathName,host);
function writeFile(src,data,pathName) {
fs.exists(src, function (exists){
if(exists){
fs.unlink(src,function(err){//删除README
if (err) {
throw err;
}else{
fs.writeFile(src, data, { flag: 'a' }, function(err) {//创建README
if (err) {
throw err;
}else{
console.log('Done!');
}
});
}
});
}else{
fs.writeFile(src, data, { flag: 'a' }, function(err) {//创建README
if (err) {
throw err;
}else{
console.log('Done!');
}
});
}
});
};
function readDesignList(preUrl,designFiles,newPath,project) {
designFiles.forEach(function (itm, index) {
if(itm.indexOf(".html")!=-1){
var item ='['+itm+']('+ preUrl+itm+')\r\r';
designDataBody = designDataBody + item;
console.log(item);
}
});
if(designDataBody){
var designData = '###《'+project+'》'+designDataHead+designDataBody;
var newPathReadme = newPath+'/README.md';
writeFile(newPathReadme,designData);
designDataBody = "";
}
}
function writeSingleReadme(pathName,fileName,host,itmDesignFalg) {
var singleDataHead = '###《'+fileName+'》\n\n';
var singleDataBody1 = '['+fileName+']('+host+fileName+'/'+fileName+')\n\n';
var singleDataBody2 = '';
var singleDataBody3 = '';
if(!!itmDesignFalg){
singleDataBody2 = '[设计要素]('+'https://dev.tencent.com/u/whalexplorer/p/'+fileName+'/git/blob/master/design/README.md'+')\n\n';
}
var itemPath = pathName+'/'+fileName;
var files = fs.readdirSync(itemPath);
files.forEach(function (itm, index) {
var itmPath = itemPath+'/'+itm;
if(Path.extname(itmPath)=='.pdf'){
singleDataBody3 = '['+itm+']('+host+fileName+'/'+itm+')\n\n';
}
});
var singleData = singleDataHead + singleDataBody1 + singleDataBody2 + singleDataBody3;
var newSinglePathReadme = pathName+'/'+fileName+'/README.md';
writeFile(newSinglePathReadme,singleData);
};
function readFileList(path,host) {
var files = fs.readdirSync(path);
files.forEach(function (itm, index) {
if((itm.substr(0, 1) != '.') && (itm!="node_modules")){
var stat = fs.statSync(path + '/'+itm);
if(stat.isDirectory()){
var item = '['+itm+']('+host+itm+'/'+itm+')\r\r';
dataBody = dataBody + item;
// console.log(item);
var newPath = path+'/'+itm+'/design';
var itmDesignFalg = false;
fs.exists(newPath, function (exists) {
if(exists){
console.log(newPath);
var designFiles = fs.readdirSync(newPath);
if(designFiles && designFiles.length>0){
var preUrl = host+itm+itmDesign;
readDesignList(preUrl,designFiles,newPath,itm)
console.log(designFiles.length);
itmDesignFalg = true;
writeSingleReadme(path,itm,host,itmDesignFalg);
}
}
})
}
}
});
return (dataHead+dataBody);
}
function makelink(src,pathName,host){
var data = readFileList(pathName,host);
if(program.mode==1){
// 写入文件内容(如果文件不存在会创建一个文件)
writeFile(src,data,pathName);
}
}