jjb-cmd 1.0.13 → 1.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,227 +1,342 @@
1
1
  const fs = require('fs');
2
- const os = require('os');
2
+ const path = require('path');
3
+ const request = require('request');
4
+ const compressing = require('compressing');
3
5
  const {
4
- GIT_HOST,
5
- GIT_TEMP_DIR,
6
- CLOUD_PROJECT
6
+ f_rm_rf,
7
+ f_file_copy,
8
+ f_content_replace,
9
+ f_create_package_json,
10
+ f_pull_git_repository,
11
+ f_scan_jjb_config_json,
12
+ f_scan_jjb_config_json_rules,
13
+ f_update_project_package_json
14
+ } = require('./tools');
15
+ const {
16
+ TEMPLATE_FOLDER,
17
+ COMMON_CONTENT_SPA_REPLACE,
18
+ CLI_DVA_ROUTER_PATH,
19
+ CLI_DVA_REGISTER_PATH,
20
+ CLI_DVA_ROUTER_SPA,
21
+ CLI_DVA_REGISTER_SPA,
22
+ CLI_DVA_ROUTER_SAAS,
23
+ CLI_DVA_REGISTER_SAAS,
24
+ COMMON_CONTENT_MICRO_REPLACE,
25
+ COMMON_CONTENT_NOR_REPLACE,
26
+ COMPONENTS_CONTENT_REPLACE
7
27
  } = require('./config');
28
+ const start_time = Date.now();
8
29
 
9
- /**
10
- * @description 删除全部
11
- * @param path {string} 路径
12
- */
13
- exports.f_rm_rf = function (path) {
14
- if (fs.existsSync(path)) {
15
- const list = fs.readdirSync(path);
16
- for (let i = 0; i < list.length; i++) {
17
- const item = list[ i ];
18
- const vPath = `${path}/${item}`;
19
- if (fs.statSync(vPath).isDirectory()) {
20
- exports.f_rm_rf(vPath);
21
- fs.rmdirSync(vPath);
30
+ module.exports = () => {
31
+ /**
32
+ * 下发数据根路径
33
+ * @type {string}
34
+ */
35
+ const root_path = path.resolve('./');
36
+ /**
37
+ * 下发数据src路径
38
+ * @type {string}
39
+ */
40
+ const target_src = [
41
+ root_path,
42
+ 'src\\'
43
+ ].join('\\');
44
+ /**
45
+ * 下发数据node_modules路径
46
+ * @type {string}
47
+ */
48
+ const target_node_modules = [
49
+ root_path,
50
+ 'node_modules'
51
+ ].join('\\');
52
+ /**
53
+ * 下发数据package.json路径
54
+ * @type {string}
55
+ */
56
+ const target_package_json = [
57
+ root_path,
58
+ 'package.json'
59
+ ].join('\\');
60
+ if (f_scan_jjb_config_json(root_path)) {
61
+ /**
62
+ * 检查配置规则
63
+ */
64
+ const jjb_config = f_scan_jjb_config_json_rules(root_path);
65
+ /**
66
+ * 检查项目是否存在node_modules
67
+ */
68
+ if (jjb_config.installTarget === 'node_modules') {
69
+ if (!fs.existsSync(target_node_modules)) {
70
+ console.log('【Error】:安装失败,node_modules目录不存在。');
71
+ process.exit(0);
22
72
  } else {
23
- fs.unlinkSync(vPath);
73
+ console.log('【Warning】:若安装在node_modules中,请确保projectType = micro-spa,并关闭webpack.config.js rules限制。');
74
+ }
75
+ } else {
76
+ if (!fs.existsSync(target_src)) {
77
+ console.log('【Error】:安装失败,src目录不存在。');
78
+ process.exit(0);
24
79
  }
25
80
  }
26
- }
27
- };
28
-
29
- /**
30
- * @typedef {object} GitResource
31
- * @property {string} path
32
- * @property {string} compress
33
- * @property {string} repository
34
- */
35
-
36
- /**
37
- * @description 拉取git资源
38
- * @param installResources {Resource[]} 资源名称
39
- * @return {GitResource[]}
40
- */
41
- exports.f_pull_git_repository = function (installResources = []) {
42
- return installResources.map(item => {
43
- const resource = CLOUD_PROJECT[ item.name ] || undefined;
44
- const template = os.tmpdir();
45
- return {
46
- path: `${template}\\${GIT_TEMP_DIR}\\${item.name}.zip`,
47
- compress: `${template}\\${GIT_TEMP_DIR}\\${item.name}_zip`,
48
- repository: `${GIT_HOST}/api/v4/projects/${resource.projectId}/repository/archive.zip?private_token=${resource.token}&ref=master`
49
- };
50
- });
51
- };
52
-
53
- /**
54
- * @description 扫描是否存在jjb.config.json
55
- * @param root 路径
56
- * @returns {boolean}
57
- */
58
- exports.f_scan_jjb_config_json = function (root) {
59
- return fs.existsSync(`${root}\\jjb.config.json`);
60
- };
61
-
62
- /**
63
- * @typedef {object} JJB_CONFIG_JSON
64
- * @property {string} projectType
65
- * @property {string} installTarget
66
- * @property {Resource[]} installResources
67
- */
68
-
69
- /**
70
- * @description 验证规则
71
- * @param root {string} 路径
72
- * @return {JJB_CONFIG_JSON}
73
- */
74
- exports.f_scan_jjb_config_json_rules = function (root) {
75
- let jjb_config_json = {};
76
- try {
77
- jjb_config_json = JSON.parse(fs.readFileSync(`${root}\\jjb.config.json`).toString());
78
- } catch (e) {
79
- console.log('【Error】:[jjb.config.json]文件解析失败,请确认是否配置正确。');
80
- process.exit(0);
81
- }
82
- if (!('projectType' in jjb_config_json)) {
83
- console.log('【Error】:[jjb.config.json]文件配置无效,需要projectType属性。');
84
- process.exit(0);
85
- }
86
- if (!('installTarget' in jjb_config_json)) {
87
- console.log('【Error】:[jjb.config.json]文件配置无效,需要installTarget属性。');
88
- process.exit(0);
89
- }
90
- if (!('installResources' in jjb_config_json)) {
91
- console.log('【Error】:[jjb.config.json]文件配置无效,需要installResources属性。');
92
- process.exit(0);
93
- }
94
- if (typeof jjb_config_json.projectType !== 'string') {
95
- console.log('【Error】:[jjb.config.json.projectType]类型是一个string。');
96
- process.exit(0);
97
- }
98
- if (![
99
- 'multi',
100
- 'spa',
101
- 'uniapp',
102
- 'micro-spa'
103
- ].includes(jjb_config_json.projectType)) {
104
- console.log('【Error】:[jjb.config.json.projectType]配置无效,有效值<multi | spa | micro-spa | uniapp>。');
105
- process.exit(0);
106
- }
107
- if (typeof jjb_config_json.installTarget !== 'string') {
108
- console.log('【Error】:[jjb.config.json.installTarget]类型是一个string。');
109
- process.exit(0);
110
- }
111
- if (![
112
- 'node_modules',
113
- 'src'
114
- ].includes(jjb_config_json.installTarget)) {
115
- console.log('【Error】:[jjb.config.json.node_modules]配置无效,有效值<node_modules | src>。');
116
- process.exit(0);
117
- }
118
- if (!Array.isArray(jjb_config_json.installResources)) {
119
- console.log('【Error】:[jjb.config.json.installResources]类型是一个Array<string>。');
120
- process.exit(0);
121
- }
122
- if (jjb_config_json.installResources.length === 0) {
123
- console.log('【Error】:[jjb.config.json.installResources]无资源。');
124
- process.exit(0);
125
- }
126
- const resources = exports.f_resolve_install_resources(jjb_config_json.installResources);
127
- if (resources.map(item => item.name).filter(v => ![
128
- 'common',
129
- 'react-admin-component',
130
- 'vue-unisass-component'
131
- ].includes(v)).length !== 0) {
132
- console.log('【Error】:[jjb.config.json.installResources]配置无效,有效值<common | react-admin-component | vue-unisass-component>。');
133
- process.exit(0);
134
- }
135
- jjb_config_json.installResources = resources;
136
- return jjb_config_json;
137
- };
138
-
139
- /**
140
- * @description 创建package.json
141
- * @param path {string} 路径
142
- * @param name {string} 包名
143
- * @param version {string} 版本
144
- */
145
- exports.f_create_package_json = function (path, name, version) {
146
- fs.writeFileSync(`${path}\\package.json`, `{"name":"${name}","version":"${version}","main": "index.js"}`);
147
- };
148
-
149
- /**
150
- * @typedef {object} Resource
151
- * @property {string} name
152
- * @property {string[]} importList
153
- */
154
-
155
- /**
156
- * @description 分析resources
157
- * @param installResources
158
- * @return {Resource[]}
159
- */
160
- exports.f_resolve_install_resources = function (installResources = []) {
161
- const resources = [];
162
- if (Array.isArray(installResources)) {
163
- installResources.forEach(resource => {
164
- if (Array.isArray(resource)) {
165
- const [ name, importList = [] ] = resource;
166
- resources.push({
167
- name,
168
- importList
81
+ /**
82
+ * 文件操作:定义一个临时文件夹
83
+ */
84
+ fs.mkdirSync(TEMPLATE_FOLDER);
85
+ /**
86
+ * 拉取数据
87
+ */
88
+ f_pull_git_repository(jjb_config.installResources).map(item => {
89
+ const stream = fs.createWriteStream(item.path);
90
+ /**
91
+ * 流操作:下载git仓库
92
+ */
93
+ request(item.repository).pipe(stream).on('close', () => {
94
+ fs.mkdirSync(item.compress);
95
+ /**
96
+ * 流操作:解压
97
+ */
98
+ compressing.zip.uncompress(item.path, item.compress);
99
+ });
100
+ });
101
+ const timer = setInterval(() => {
102
+ /**
103
+ * 文件操作:扫描临时文件夹
104
+ */
105
+ const folder_list = fs.readdirSync(TEMPLATE_FOLDER);
106
+ /**
107
+ * 文件操作:抓取临时文件夹中的内容
108
+ */
109
+ const match_folder = folder_list.filter(item => jjb_config.installResources.map(item => `${item.name}_zip`).includes(item));
110
+ /**
111
+ * 文件操作:判断是否已解压完成
112
+ */
113
+ if (match_folder.length === jjb_config.installResources.length) {
114
+ /**
115
+ * 解压完成关闭定时器
116
+ */
117
+ clearInterval(timer);
118
+ const resource_folder = [];
119
+ match_folder.forEach(name => {
120
+ /**
121
+ * 文件操作:定义解压包路径
122
+ */
123
+ const unzip = [
124
+ TEMPLATE_FOLDER,
125
+ name
126
+ ].join('\\');
127
+ /**
128
+ * 文件操作:抓取解压包中的文件
129
+ */
130
+ fs.readdirSync(unzip).forEach(dir => resource_folder.push({
131
+ name: name,
132
+ path: [
133
+ unzip,
134
+ dir
135
+ ].join('\\'),
136
+ pure_name: name.replace(/_zip$/, ''),
137
+ import_list: jjb_config.installResources.find(item => `${item.name}_zip` === name).importList
138
+ }));
169
139
  });
170
- } else {
171
- resources.push({
172
- name: resource,
173
- importList: []
140
+ resource_folder.forEach(item => {
141
+ /**
142
+ * 文件操作:依赖名称
143
+ */
144
+ const folder_name = item.pure_name;
145
+ /**
146
+ * 文件操作:按需加载
147
+ */
148
+ const folder_import_list = item.import_list;
149
+ /**
150
+ * 文件操作:组装路径
151
+ */
152
+ const folder_idea_path = [
153
+ item.path,
154
+ '.idea'
155
+ ].join('//');
156
+ /**
157
+ * 文件操作:组装路径
158
+ */
159
+ const folder_git_ignore_path = [
160
+ item.path,
161
+ '.gitignore'
162
+ ].join('//');
163
+ /**
164
+ * 特殊处理:删除.idea文件夹
165
+ */
166
+ if (fs.existsSync(folder_idea_path)) {
167
+ f_rm_rf(folder_idea_path);
168
+ fs.rmdirSync(folder_idea_path);
169
+ }
170
+ /**
171
+ * 特殊处理:删除.gitignore文件
172
+ */
173
+ if (fs.existsSync(folder_git_ignore_path)) {
174
+ fs.unlinkSync(folder_git_ignore_path);
175
+ }
176
+ /**
177
+ * 文件操作:按需加载
178
+ */
179
+ if (folder_import_list.length) {
180
+ /**
181
+ * 文件操作:根据jjb.config.json中的importList获取需要的文件
182
+ */
183
+ fs.readdirSync(item.path).filter(item => !folder_import_list.includes(item) && item !== 'index.js' && item !== 'setting.json').forEach(dir => {
184
+ /**
185
+ * 文件操作:组装路径
186
+ */
187
+ const dirPath = `${item.path}\\${dir}`;
188
+ /**
189
+ * 文件操作:删除不需要的文件夹内容
190
+ */
191
+ f_rm_rf(dirPath);
192
+ /**
193
+ * 文件操作:删除不需要的文件夹
194
+ */
195
+ fs.rmdirSync(dirPath);
196
+ });
197
+ if (folder_name === 'react-admin-component') {
198
+ const codeList = [];
199
+ fs.readdirSync(item.path).filter(dir => dir !== 'index.js').forEach(dir => {
200
+ if (dir === 'RouterContainer') {
201
+ codeList.push(`export { default as RouterContainer, PATH as RouterContainerPATH } from './RouterContainer';`);
202
+ } else {
203
+ codeList.push(`export { default as ${dir} } from './${dir}';`);
204
+ }
205
+ });
206
+ fs.writeFileSync(`${item.path}\\index.js`, codeList.join('\n'));
207
+ }
208
+ }
209
+ if (folder_name === 'common') {
210
+ /**
211
+ * 组装dva/automatic路径
212
+ */
213
+ const automaticPath = [
214
+ item.path,
215
+ '\\dva\\automatic'
216
+ ].join('\\');
217
+ if ([
218
+ 'spa',
219
+ 'micro-spa'
220
+ ].includes(jjb_config.projectType)) {
221
+ /**
222
+ * 特殊处理:由于micro-spa模式下需要使用react-router-dom自动化技术,spa模式下使用基于dva/router的自动化技术,两者有差异,需要做替换操作
223
+ */
224
+ if (fs.existsSync(automaticPath)) {
225
+ /**
226
+ * 文件操作:常规替换
227
+ */
228
+ f_content_replace(jjb_config.projectType === 'spa'
229
+ ? COMMON_CONTENT_SPA_REPLACE
230
+ : [
231
+ ...COMMON_CONTENT_MICRO_REPLACE,
232
+ ...COMMON_CONTENT_NOR_REPLACE
233
+ ], item.path);
234
+ /**
235
+ * 文件操作:替换dva/automatic/router.js
236
+ */
237
+ fs.writeFileSync(item.path + CLI_DVA_ROUTER_PATH, fs.readFileSync(jjb_config.projectType === 'spa'
238
+ ? CLI_DVA_ROUTER_SPA
239
+ : CLI_DVA_ROUTER_SAAS).toString());
240
+ /**
241
+ * 文件操作:替换dva/automatic/register.js
242
+ */
243
+ fs.writeFileSync(item.path + CLI_DVA_REGISTER_PATH, fs.readFileSync(jjb_config.projectType === 'spa'
244
+ ? CLI_DVA_REGISTER_SPA
245
+ : CLI_DVA_REGISTER_SAAS).toString());
246
+ }
247
+ } else if (jjb_config.projectType === 'multi') {
248
+ /**
249
+ * 特殊处理:由于multi模式下不需要使用自动化技术,需要删除dva/automatic文件夹
250
+ */
251
+ if (fs.existsSync(automaticPath)) {
252
+ f_rm_rf(automaticPath);
253
+ fs.rmdirSync(automaticPath);
254
+ }
255
+ }
256
+ } else if (folder_name === 'react-admin-component') {
257
+ /**
258
+ * 特殊处理:仅限micro-spa模式下允许react-admin-component使用node_modules/jjb-common,其他模式使用src/components,目的是兼容老项目
259
+ */
260
+ if (jjb_config.projectType === 'micro-spa') {
261
+ f_content_replace(COMPONENTS_CONTENT_REPLACE, item.path);
262
+ }
263
+ } else if (folder_name === 'vue-unisass-component') {
264
+
265
+ }
266
+ if (jjb_config.installTarget === 'node_modules') {
267
+ /**
268
+ * 特殊处理:为了避免与node_modules其他依赖重名,添加一个标识符"jjb-"
269
+ */
270
+ const package_name = `jjb-${folder_name}`;
271
+ /**
272
+ * 文件操作:判断node_modules目录下是否已存在
273
+ */
274
+ if (fs.existsSync(`${target_node_modules}\\${package_name}`)) {
275
+ /**
276
+ * 文件操作:删除node_modules目录下文件内容(保留文件夹)
277
+ */
278
+ f_rm_rf(`${target_node_modules}\\${package_name}`);
279
+ } else {
280
+ /**
281
+ * 文件操作:如果不存在,定义一个新的文件夹
282
+ */
283
+ fs.mkdirSync(`${target_node_modules}\\${package_name}`);
284
+ }
285
+ /**
286
+ * 文件操作:复制文件到node_modules中
287
+ */
288
+ f_file_copy(item.path, `${target_node_modules}\\${package_name}`);
289
+ /**
290
+ * 文件操作:创建package.json
291
+ */
292
+ f_create_package_json(`${target_node_modules}\\${package_name}`, package_name, '1.0.0');
293
+ /**
294
+ * 文件操作:在src/package.json中定义一个组件依赖
295
+ */
296
+ f_update_project_package_json(target_package_json, package_name, '1.0.0');
297
+ } else if (jjb_config.installTarget === 'src') {
298
+ /**
299
+ * 特殊处理:由于老项目问题,此处要将react-admin-component转换为components(仅限src模式)
300
+ */
301
+ const t_name = folder_name === 'react-admin-component'
302
+ ? 'components'
303
+ : folder_name;
304
+ /**
305
+ * 文件操作:判断src目录下是否已存在
306
+ */
307
+ if (fs.existsSync(target_src + t_name)) {
308
+ /**
309
+ * 文件操作:删除src目录下文件内容(保留文件夹)
310
+ */
311
+ f_rm_rf(target_src + t_name);
312
+ } else {
313
+ /**
314
+ * 文件操作:如果不存在,定义一个新的文件夹
315
+ */
316
+ fs.mkdirSync(target_src + t_name);
317
+ }
318
+ /**
319
+ * 文件操作:复制文件到src中
320
+ */
321
+ f_file_copy(item.path, target_src + t_name);
322
+ }
174
323
  });
324
+ setTimeout(() => {
325
+ /**
326
+ * 文件操作:删除临时文件夹内容
327
+ */
328
+ f_rm_rf(TEMPLATE_FOLDER);
329
+ /**
330
+ * 文件操作:删除临时文件夹
331
+ */
332
+ fs.rmdirSync(TEMPLATE_FOLDER);
333
+ const end_time = Date.now();
334
+ console.log(`【jjb-cmd】:install命令执行完成,用时${(end_time - start_time) / 1000}s`);
335
+ }, 1500);
175
336
  }
176
- });
337
+ }, 1000);
338
+ } else {
339
+ console.log('【Error】:执行失败,在您的项目根目录,需要一个jjb.config.json文件。');
177
340
  }
178
- return resources;
179
- };
180
-
181
- /**
182
- * @description 更新项目package.json文件
183
- * @param path {string} 路径
184
- * @param name {string} 包名
185
- * @param version {string} 版本
186
- */
187
- exports.f_update_project_package_json = function (path, name, version) {
188
- const packageJSONFile = JSON.parse(fs.readFileSync(path).toString());
189
- packageJSONFile.dependencies[ name ] = version;
190
- fs.writeFileSync(path, JSON.stringify(packageJSONFile, null, 2));
191
- };
192
-
193
- /**
194
- * @description 复制文件
195
- * @param originSrc
196
- * @param targetSrc
197
- */
198
- exports.f_file_copy = function (originSrc, targetSrc) {
199
- fs.readdirSync(originSrc).forEach(dir => {
200
- const oPath = `${originSrc}\\${dir}`;
201
- const tPath = `${targetSrc}\\${dir}`;
202
- if (fs.statSync(oPath).isDirectory()) {
203
- fs.mkdirSync(tPath);
204
- exports.f_file_copy(oPath, tPath);
205
- } else {
206
- fs.writeFileSync(tPath, fs.readFileSync(oPath).toString());
207
- }
208
- });
209
341
  };
210
342
 
211
- /**
212
- * @description 替换文件操作
213
- * @param source {[]} 替换源
214
- * @param root {string} 路径
215
- */
216
- exports.f_content_replace = function (source = [], root) {
217
- source.forEach(item => {
218
- const path = root + item.path;
219
- if (fs.existsSync(path)) {
220
- let content = fs.readFileSync(path).toString();
221
- item.replace.forEach(rep => {
222
- content = content.replace(rep[ 0 ], rep[ 1 ]);
223
- });
224
- fs.writeFileSync(path, content);
225
- }
226
- });
227
- };
@@ -98,9 +98,10 @@ exports.f_scan_jjb_config_json_rules = function (root) {
98
98
  if (![
99
99
  'multi',
100
100
  'spa',
101
+ 'uniapp',
101
102
  'micro-spa'
102
103
  ].includes(jjb_config_json.projectType)) {
103
- console.log('【Error】:[jjb.config.json.projectType]配置无效,有效值<multi | spa | micro-spa>。');
104
+ console.log('【Error】:[jjb.config.json.projectType]配置无效,有效值<multi | spa | micro-spa | uniapp>。');
104
105
  process.exit(0);
105
106
  }
106
107
  if (typeof jjb_config_json.installTarget !== 'string') {
@@ -125,9 +126,10 @@ exports.f_scan_jjb_config_json_rules = function (root) {
125
126
  const resources = exports.f_resolve_install_resources(jjb_config_json.installResources);
126
127
  if (resources.map(item => item.name).filter(v => ![
127
128
  'common',
128
- 'react-admin-component'
129
+ 'react-admin-component',
130
+ 'vue-unisass-component'
129
131
  ].includes(v)).length !== 0) {
130
- console.log('【Error】:[jjb.config.json.installResources]配置无效,有效值<common | react-admin-component>。');
132
+ console.log('【Error】:[jjb.config.json.installResources]配置无效,有效值<common | react-admin-component | vue-unisass-component>。');
131
133
  process.exit(0);
132
134
  }
133
135
  jjb_config_json.installResources = resources;
@@ -0,0 +1,88 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const readline = require('readline');
4
+ const ProgressBar = require('./progress-bar');
5
+ const io = readline.createInterface({
6
+ input: process.stdin,
7
+ output: process.stdout
8
+ });
9
+ const progress = new ProgressBar('删除进度', 50);
10
+
11
+ let f_total = 0;
12
+ let f_number = 0;
13
+
14
+ module.exports = function () {
15
+ const root_path = path.resolve('./');
16
+
17
+ io.question('是否确认删除?删除后不可恢复![y/n]:', function (answer) {
18
+ if (answer.trim() === 'y') {
19
+ console.log('正在计算项目数,请稍等...');
20
+ setTimeout(() => {
21
+ scanner(root_path);
22
+ console.log(`计算完成共计[${f_total}]项目。`);
23
+ setTimeout(() => {
24
+ console.clear();
25
+ exec(root_path);
26
+ setTimeout(() => {
27
+ console.log('删除完成。');
28
+ console.clear();
29
+ process.exit(0);
30
+ }, 500);
31
+ }, 500);
32
+ }, 500);
33
+ } else if (answer.trim() === 'n') {
34
+ console.log('取消删除。');
35
+ process.exit(0);
36
+ } else {
37
+ console.log('无效操作。');
38
+ process.exit(0);
39
+ }
40
+ });
41
+ };
42
+
43
+ function exec (path) {
44
+ if (fs.existsSync(path)) {
45
+ const list = fs.readdirSync(path);
46
+ for (let i = 0; i < list.length; i++) {
47
+ const item = list[ i ];
48
+ const vPath = `${path}/${item}`;
49
+ try {
50
+ if (fs.statSync(vPath).isDirectory()) {
51
+ exec(vPath);
52
+ fs.rmdirSync(vPath, { recursive: true });
53
+ f_number = f_number + 1;
54
+ // console.log('删除文件夹:' + vPath);
55
+ } else {
56
+ fs.unlinkSync(vPath);
57
+ f_number = f_number + 1;
58
+ // console.log('删除文件:' + vPath);
59
+ }
60
+ progress.render({
61
+ completed: f_number,
62
+ total: f_total
63
+ });
64
+ } catch (e) {
65
+ // console.log('删除异常:' + vPath);
66
+ }
67
+ }
68
+ }
69
+ }
70
+
71
+ function scanner (path) {
72
+ if (fs.existsSync(path)) {
73
+ const list = fs.readdirSync(path);
74
+ for (let i = 0; i < list.length; i++) {
75
+ const item = list[ i ];
76
+ const vPath = `${path}/${item}`;
77
+ try {
78
+ if (fs.statSync(vPath).isDirectory()) {
79
+ scanner(vPath);
80
+ f_total = f_total + 1;
81
+ } else {
82
+ f_total = f_total + 1;
83
+ }
84
+ } catch (e) {
85
+ }
86
+ }
87
+ }
88
+ }
@@ -0,0 +1,23 @@
1
+ const slog = require('single-line-log').stdout;
2
+
3
+ function ProgressBar (description, bar_length) {
4
+ this.description = description || 'Progress';
5
+ this.length = bar_length || 25;
6
+ this.render = function (opts) {
7
+ let i;
8
+ const percent = (opts.completed / opts.total).toFixed(4);
9
+ const cell_num = Math.floor(percent * this.length);
10
+ let cell = '';
11
+ for (i = 0; i < cell_num; i++) {
12
+ cell += '█';
13
+ }
14
+ let empty = '';
15
+ for (i = 0; i < this.length - cell_num; i++) {
16
+ empty += '░';
17
+ }
18
+ const cmdText = this.description + ': ' + (100 * percent).toFixed(2) + '% ' + cell + empty + ' ' + opts.completed + '/' + opts.total;
19
+ slog(cmdText);
20
+ };
21
+ }
22
+
23
+ module.exports = ProgressBar;