@tmsfe/tmskit 0.0.21 → 0.0.22

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,13 +1,10 @@
1
1
 
2
- const program = require('commander');
3
- const leven = require('leven');
4
2
  const ora = require('ora');
5
3
  const path = require('path');
6
4
  const fs = require('fs');
7
5
  const shelljs = require('shelljs');
8
6
  const glob = require('glob-ignore');
9
7
  const { info } = require('./log');
10
- const chalk = require('chalk');
11
8
  const shelljsOptions = { slient: true, async: false };
12
9
 
13
10
  // 获取当前目录
@@ -16,28 +13,6 @@ function resolve(...args) {
16
13
  return path.resolve(cwd, ...args);
17
14
  };
18
15
 
19
-
20
- /**
21
- * 用户输入命令时,进行提示
22
- * @param {String} unknownCommand 非预期的命令
23
- * @returns {Undefined} 无需返回值
24
- */
25
- const suggestCommands = (unknownCommand) => {
26
- const availableCommands = program.commands.map(cmd => cmd._name);
27
-
28
- let suggestion;
29
- availableCommands.forEach((cmd) => {
30
- const isBestMatch = leven(cmd, unknownCommand) < leven(suggestion || '', unknownCommand);
31
- if (leven(cmd, unknownCommand) < 3 && isBestMatch) {
32
- suggestion = cmd;
33
- }
34
- });
35
-
36
- if (suggestion) {
37
- info(` ${chalk.red(`Did you mean ${chalk.yellow(suggestion)}?`)}`);
38
- }
39
- };
40
-
41
16
  /**
42
17
  * 判断变量是否是一个数组
43
18
  * @param { unknown } obj 变量
@@ -109,13 +84,15 @@ function pullRepoForGit(dest, branch) {
109
84
 
110
85
  /**
111
86
  * npm 下载依赖
112
- * @param {*} dir
87
+ * @param {string} dir 下载npm的目录
88
+ * @param {object} npm npm的配置 (见下获取npm源入参)
113
89
  * @returns
114
90
  */
115
- function npmInstall(dir) {
91
+ function npmInstall(dir, npmConfig) {
116
92
  return new Promise((resolve, reject) => {
93
+ const registry = getNpmRegistry(npmConfig);
117
94
  shelljs.exec(
118
- 'npm install --production --registry http://mirrors.tencent.com/npm/',
95
+ `npm install --production ${registry}`,
119
96
  { cwd: dir, silent: true },
120
97
  (code, stdout, stderr) => {
121
98
  if (code !== 0) {
@@ -127,6 +104,28 @@ function npmInstall(dir) {
127
104
  });
128
105
  }
129
106
 
107
+ // 获取npm源
108
+ // 入参:{
109
+ // registry: "https://registry.npmjs.org/",
110
+ // scope: {
111
+ // "@tencent": "http://mirrors.tencent.com/npm/"
112
+ // }
113
+ // }
114
+ // 出参: --@tencent:registry=http://mirrors.tencent.com/npm/ --registry=https://registry.npmjs.org/
115
+ function getNpmRegistry(npmConfig = {}) {
116
+ let resRegistry = '';
117
+ if (npmConfig.registry) {
118
+ resRegistry = ` --registry=${npmConfig.registry}`;
119
+ }
120
+
121
+ if (isObject(npmConfig.scope)) {
122
+ Object.keys(npmConfig.scope).forEach((key) => {
123
+ resRegistry += ` --${key}:registry=${npmConfig.scope[key]}`;
124
+ });
125
+ }
126
+ return resRegistry;
127
+ }
128
+
130
129
  /**
131
130
  * 计算各项任务耗时
132
131
  * @param {Number} start 任务开始时间
@@ -167,11 +166,7 @@ const camelize = str => str.replace(/-(\w)/g, (a, c) => (c ? c.toUpperCase() : '
167
166
 
168
167
  const mergeMap = function (obj, src) {
169
168
  for (const [k, v] of src) {
170
- if (obj.has(k)) {
171
- obj.set(k, obj.get(k) + v);
172
- } else {
173
- obj.set(k, v);
174
- }
169
+ obj.set(k, v);
175
170
  }
176
171
 
177
172
  return obj;
@@ -217,6 +212,25 @@ function getAbsolutePath(pathDir, cwd = '') {
217
212
  return newPath;
218
213
  }
219
214
 
215
+ // 版本比较 => 1(大于), 0(等于), -1(小于)
216
+ function versionCompare(v1, v2) {
217
+ // 将两个版本号拆成数组
218
+ const arr1 = v1.split('.');
219
+ const arr2 = v2.split('.');
220
+ const minLength = Math.min(arr1.length, arr2.length);
221
+ // 依次比较版本号每一位大小
222
+ for (let i = 0; i < minLength; i++) {
223
+ if (parseInt(arr1[i], 10) !== parseInt(arr2[i], 10)) {
224
+ return (parseInt(arr1[i], 10) > parseInt(arr2[i], 10)) ? 1 : -1;
225
+ }
226
+ }
227
+ // 若前几位分隔相同,则按分隔数比较。
228
+ if (arr1.length === arr2.length) {
229
+ return 0;
230
+ }
231
+ return (arr1.length > arr2.length) ? 1 : -1;
232
+ }
233
+
220
234
  module.exports = {
221
235
  resolve,
222
236
  isObject,
@@ -224,7 +238,6 @@ module.exports = {
224
238
  createTask,
225
239
  downloadRepoForGit,
226
240
  pullRepoForGit,
227
- suggestCommands,
228
241
  camelize,
229
242
  npmInstall,
230
243
  mergeMap,
@@ -232,4 +245,6 @@ module.exports = {
232
245
  filterField,
233
246
  findFiles,
234
247
  getAbsolutePath,
248
+ getNpmRegistry,
249
+ versionCompare,
235
250
  };