@tmsfe/tmskit 0.0.5-beta.5 → 0.0.7
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.
- package/README.md +27 -25
- package/dist/index.cjs.js +779 -664
- package/main.js +3 -3
- package/package.json +75 -69
- package/src/config/constant.js +71 -70
- package/src/config/defaultTmsConfig.js +16 -16
- package/src/entry.js +60 -60
- package/src/gulp/build.js +5 -8
- package/src/gulp/compile.js +81 -87
- package/src/gulp/dev.js +102 -108
- package/src/gulp/plugins/less.js +116 -116
- package/src/gulp/plugins/mpCommonDep.js +131 -131
- package/src/gulp/plugins/mpJsonDep.js +108 -108
- package/src/gulp/plugins/mpWxmlDep.js +194 -194
- package/src/gulp/plugins/postcss-font-base64.js +72 -72
- package/src/gulp/{replaceEnv.js → plugins/replaceEnv.js} +29 -29
- package/src/gulp/plugins/utils/pluginError.js +25 -25
- package/src/index.js +62 -62
- package/src/init.js +33 -33
- package/src/scripts/create/ask.js +63 -63
- package/src/scripts/create/generator.js +25 -25
- package/src/scripts/create/ignoreFiles.js +7 -7
- package/src/scripts/create/index.js +72 -72
- package/src/scripts/create/render.js +19 -19
- package/src/scripts/run/build/index.js +17 -16
- package/src/scripts/run/dev/index.js +84 -67
- package/src/scripts/run/index.js +68 -63
- package/src/scripts/run/init/index.js +87 -80
- package/src/scripts/run/install/index.js +29 -31
- package/src/utils/buildAppJson.js +221 -200
- package/src/utils/checkDependencies.js +77 -77
- package/src/utils/cliUtils.js +35 -35
- package/src/utils/cloneModules.js +116 -91
- package/src/utils/findCssImport.js +30 -30
- package/src/utils/global.js +36 -36
- package/src/utils/handleError.js +16 -0
- package/src/utils/io.js +106 -106
- package/src/utils/log.js +44 -44
- package/src/utils/mpCiUtils.js +73 -74
- package/src/utils/npmUtils.js +166 -148
- package/src/utils/tkitUtils.js +158 -84
- package/src/utils/widgets.js +167 -173
|
@@ -1,91 +1,116 @@
|
|
|
1
|
-
const MetalSmith = require('metalsmith');
|
|
2
|
-
const { getGlobalInstance } = require('./global.js');
|
|
3
|
-
const { downloadRepoForGit, resolve } = require('./widgets');
|
|
4
|
-
const {
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*
|
|
12
|
-
* @param {
|
|
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
|
-
const
|
|
62
|
-
|
|
63
|
-
//
|
|
64
|
-
const
|
|
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
|
-
|
|
1
|
+
const MetalSmith = require('metalsmith');
|
|
2
|
+
const { getGlobalInstance } = require('./global.js');
|
|
3
|
+
const { downloadRepoForGit, resolve } = require('./widgets');
|
|
4
|
+
const { readTmsPrivateCf } = require('../utils/tkitUtils');
|
|
5
|
+
const { fail } = require('./log');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const shelljs = require('shelljs');
|
|
8
|
+
const { handleError } = require('./handleError.js');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 处理用户没有clone git仓库权限问题——拼接账号信息
|
|
12
|
+
* @param {*} httpRepoUrl
|
|
13
|
+
* @param {*} moduleName
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
function replaceGitUrlAccount(httpRepoUrl, moduleName, privateCf) {
|
|
17
|
+
let gitUrl = httpRepoUrl;
|
|
18
|
+
const { username = '', pass = '' } = privateCf?.gitAccout?.[moduleName] || {};
|
|
19
|
+
|
|
20
|
+
const urlPrefixReg = /http(s)?:\/\//;
|
|
21
|
+
if (username && pass && urlPrefixReg.test(gitUrl)) {
|
|
22
|
+
gitUrl = gitUrl.replace(urlPrefixReg, val => `${val}${encodeURIComponent(username)}:${pass}@`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return gitUrl;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 对克隆下来的模块进行相应的文件处理操作,比如收集处理模块信息,进行信息缓存等操作
|
|
30
|
+
* @param { string } sourceDir 缓存文件夹
|
|
31
|
+
* @param { string } targetDir 目标文件夹
|
|
32
|
+
* @param { arrary } ignore
|
|
33
|
+
* @returns { undefined } no return
|
|
34
|
+
*/
|
|
35
|
+
function moveFile(sourceDir, targetDir, ignore = []) {
|
|
36
|
+
// 删除不是文件夹的文件
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
MetalSmith(__dirname)
|
|
39
|
+
.ignore(ignore)
|
|
40
|
+
.source(sourceDir)
|
|
41
|
+
.destination(targetDir)
|
|
42
|
+
.build((e) => {
|
|
43
|
+
if (e) {
|
|
44
|
+
fail(`${sourceDir} moveFile ${targetDir}出现错误: ${e}`);
|
|
45
|
+
reject(e);
|
|
46
|
+
}
|
|
47
|
+
resolve();
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 下载目标模块
|
|
54
|
+
* @param { string } sourceDir 缓存文件夹
|
|
55
|
+
* @param { string } targetDir 目标文件夹
|
|
56
|
+
* @param { boolean } isDev 是否是dev
|
|
57
|
+
* @returns { array } modules 描述模块的列表
|
|
58
|
+
*/
|
|
59
|
+
async function cloneModules(sourceDir, targetDir, modules, isDev) {
|
|
60
|
+
// 用户本地的私有项目配置(用来配置环境\模块信息\账号信息)
|
|
61
|
+
const privateCf = readTmsPrivateCf();
|
|
62
|
+
|
|
63
|
+
// 根据小程序的配置文件下载模块, 并且处理信息
|
|
64
|
+
for (const moduleInfo of modules) {
|
|
65
|
+
if (moduleInfo.repoInfo) {
|
|
66
|
+
await downLoadAndMoveModule(sourceDir, targetDir, moduleInfo, privateCf, isDev);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 下载模块信息并且将它移动到对应的位置
|
|
73
|
+
* @param { string } sourceDir 代码缓存文件夹
|
|
74
|
+
* @param { string } targetDir 代码要放到的目标文件夹
|
|
75
|
+
* @param { boolean } isDev 是否是dev
|
|
76
|
+
* @returns { array } moduleInfo 描述模块的信息
|
|
77
|
+
*/
|
|
78
|
+
async function downLoadAndMoveModule(sourceDir, targetDir, moduleInfo, privateCf, isDev) {
|
|
79
|
+
const { repoInfo: { buildGitTag, httpRepoUrl }, path, name } = moduleInfo;
|
|
80
|
+
|
|
81
|
+
// 源码临时存在的源目录
|
|
82
|
+
let sourcePath = resolve(sourceDir, name);
|
|
83
|
+
// 源码要放到目标目录
|
|
84
|
+
const targetPath = resolve(targetDir, path);
|
|
85
|
+
// 设置模块的构建分支
|
|
86
|
+
const cloneBranch = buildGitTag && typeof buildGitTag === 'string' ? buildGitTag : 'master';
|
|
87
|
+
|
|
88
|
+
// 检查缓存中有没有
|
|
89
|
+
const globalInstance = getGlobalInstance();
|
|
90
|
+
const moduleInCache = globalInstance.getModuleCache(httpRepoUrl, cloneBranch);
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
if (!moduleInCache) {
|
|
94
|
+
// 处理仓库权限问题
|
|
95
|
+
const gitUrl = replaceGitUrlAccount(httpRepoUrl, name, privateCf);
|
|
96
|
+
await downloadRepoForGit(gitUrl, sourcePath, cloneBranch);
|
|
97
|
+
globalInstance.setModuleCache(httpRepoUrl, cloneBranch, sourcePath);
|
|
98
|
+
} else {
|
|
99
|
+
sourcePath = globalInstance.getModuleCache(httpRepoUrl, cloneBranch).dest;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (fs.existsSync(targetPath)) {
|
|
103
|
+
shelljs.rm('-rf', `${targetPath}/*`);
|
|
104
|
+
}
|
|
105
|
+
await moveFile(sourcePath, targetPath, [
|
|
106
|
+
'node_modules',
|
|
107
|
+
'.git',
|
|
108
|
+
]);
|
|
109
|
+
} catch (e) {
|
|
110
|
+
handleError(e, isDev);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
module.exports = {
|
|
115
|
+
cloneModules,
|
|
116
|
+
};
|
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
/* eslint-disable no-param-reassign */
|
|
2
|
-
const strip = require('strip-comments');
|
|
3
|
-
// 匹配规则
|
|
4
|
-
const MATCH_RULE = new RegExp(/@import *(?:url\(['"]?([^'")]+)['"]?\)|['"]([^'"]+)['"]);?/g);
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 获取样式文件中的@import语句
|
|
8
|
-
* @param code 代码片段
|
|
9
|
-
* @returns 结果数组
|
|
10
|
-
*/
|
|
11
|
-
const findCssImports = (code) => {
|
|
12
|
-
// 将buffer转成字符串
|
|
13
|
-
code = code.toString();
|
|
14
|
-
// 去除注释
|
|
15
|
-
code = strip.block(code);
|
|
16
|
-
// 定义最后返回的结果
|
|
17
|
-
const result = [];
|
|
18
|
-
let matchList;
|
|
19
|
-
// 循环遍历代码段,直至匹配结果为空
|
|
20
|
-
while ((matchList = MATCH_RULE.exec(code))) {
|
|
21
|
-
// 存入结果数组
|
|
22
|
-
result.push(matchList?.[1] || matchList?.[2]);
|
|
23
|
-
}
|
|
24
|
-
// 返回结果
|
|
25
|
-
return result;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
module.exports = {
|
|
29
|
-
findCssImports,
|
|
30
|
-
};
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
const strip = require('strip-comments');
|
|
3
|
+
// 匹配规则
|
|
4
|
+
const MATCH_RULE = new RegExp(/@import *(?:url\(['"]?([^'")]+)['"]?\)|['"]([^'"]+)['"]);?/g);
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 获取样式文件中的@import语句
|
|
8
|
+
* @param code 代码片段
|
|
9
|
+
* @returns 结果数组
|
|
10
|
+
*/
|
|
11
|
+
const findCssImports = (code) => {
|
|
12
|
+
// 将buffer转成字符串
|
|
13
|
+
code = code.toString();
|
|
14
|
+
// 去除注释
|
|
15
|
+
code = strip.block(code);
|
|
16
|
+
// 定义最后返回的结果
|
|
17
|
+
const result = [];
|
|
18
|
+
let matchList;
|
|
19
|
+
// 循环遍历代码段,直至匹配结果为空
|
|
20
|
+
while ((matchList = MATCH_RULE.exec(code))) {
|
|
21
|
+
// 存入结果数组
|
|
22
|
+
result.push(matchList?.[1] || matchList?.[2]);
|
|
23
|
+
}
|
|
24
|
+
// 返回结果
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
module.exports = {
|
|
29
|
+
findCssImports,
|
|
30
|
+
};
|
package/src/utils/global.js
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
class Globale {
|
|
2
|
-
constructor() {
|
|
3
|
-
this.moduleCache = {};
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
setModuleCache(url, branch, dest) {
|
|
7
|
-
const instance = getGlobalInstance();
|
|
8
|
-
|
|
9
|
-
const key = `${branch}:${url}`;
|
|
10
|
-
|
|
11
|
-
instance.moduleCache[key] = {
|
|
12
|
-
dest,
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
return instance.moduleCache[key];
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
getModuleCache(url, branch) {
|
|
19
|
-
const instance = getGlobalInstance();
|
|
20
|
-
|
|
21
|
-
return instance.moduleCache[`${branch}:${url}`];
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
let instance;
|
|
26
|
-
function getGlobalInstance() {
|
|
27
|
-
if (instance) {
|
|
28
|
-
return instance;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return instance = new Globale();
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
module.exports = {
|
|
35
|
-
getGlobalInstance,
|
|
36
|
-
};
|
|
1
|
+
class Globale {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.moduleCache = {};
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
setModuleCache(url, branch, dest) {
|
|
7
|
+
const instance = getGlobalInstance();
|
|
8
|
+
|
|
9
|
+
const key = `${branch}:${url}`;
|
|
10
|
+
|
|
11
|
+
instance.moduleCache[key] = {
|
|
12
|
+
dest,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return instance.moduleCache[key];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
getModuleCache(url, branch) {
|
|
19
|
+
const instance = getGlobalInstance();
|
|
20
|
+
|
|
21
|
+
return instance.moduleCache[`${branch}:${url}`];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let instance;
|
|
26
|
+
function getGlobalInstance() {
|
|
27
|
+
if (instance) {
|
|
28
|
+
return instance;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return instance = new Globale();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
module.exports = {
|
|
35
|
+
getGlobalInstance,
|
|
36
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const { fail } = require('./log');
|
|
2
|
+
|
|
3
|
+
function handleError(error, isDev = false) {
|
|
4
|
+
const errMsg = typeof error === 'object' ? error.message : error;
|
|
5
|
+
if (isDev) {
|
|
6
|
+
fail(errMsg);
|
|
7
|
+
} else {
|
|
8
|
+
fail(errMsg);
|
|
9
|
+
process.exit(1);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = {
|
|
14
|
+
handleError,
|
|
15
|
+
};
|
|
16
|
+
|
package/src/utils/io.js
CHANGED
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
/**
|
|
4
|
-
* 判断目录是否为空
|
|
5
|
-
* @param {string} dirname 目录名
|
|
6
|
-
* @returns
|
|
7
|
-
*/
|
|
8
|
-
const isDirEmpty = dirname => fs.promises.readdir(dirname).then(files => files.length === 0);
|
|
9
|
-
|
|
10
|
-
// 判断是否是文件
|
|
11
|
-
const isFile = (pathName) => {
|
|
12
|
-
try {
|
|
13
|
-
const stat = fs.lstatSync(pathName);
|
|
14
|
-
return stat.isFile();
|
|
15
|
-
} catch {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 确保目录存在,不存在就创建一个
|
|
22
|
-
* @param {*} dirname 目录名
|
|
23
|
-
*/
|
|
24
|
-
const ensureDirExist = (dirname) => {
|
|
25
|
-
if (!fs.existsSync(dirname)) {
|
|
26
|
-
fs.mkdirSync(dirname, { recursive: true });
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
// 复制文件
|
|
31
|
-
const copyFile = function (src, dest) {
|
|
32
|
-
if (fs.existsSync(dest)) {
|
|
33
|
-
fs.unlinkSync(dest);
|
|
34
|
-
}
|
|
35
|
-
const dir =
|
|
36
|
-
ensureDirExist(dir);
|
|
37
|
-
fs.copyFileSync(src, dest);
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
// 判断文件内容是否一致,不一致再进行拷贝
|
|
41
|
-
function diffContentCopyFile(originFile, destFile) {
|
|
42
|
-
if (fs.existsSync(destFile)) {
|
|
43
|
-
const depDestContent = fs.readFileSync(destFile, 'utf8');
|
|
44
|
-
const depOriginContent = fs.readFileSync(originFile, 'utf8');
|
|
45
|
-
if (depDestContent !== depOriginContent) {
|
|
46
|
-
console.log(`拷贝${originFile}内容到${destFile}`);
|
|
47
|
-
copyFile(originFile, destFile);
|
|
48
|
-
}
|
|
49
|
-
} else {
|
|
50
|
-
console.log(`拷贝${originFile}内容到${destFile}`);
|
|
51
|
-
copyFile(originFile, destFile);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// 添加后缀
|
|
56
|
-
function ext(filePath, extensions) {
|
|
57
|
-
let newFilePath = filePath;
|
|
58
|
-
let extPath = '';
|
|
59
|
-
// try catch需要包裹:用来处理'./lib/timer'没有后缀的情况
|
|
60
|
-
try {
|
|
61
|
-
const stat = fs.lstatSync(newFilePath);
|
|
62
|
-
if (stat.isDirectory()) {
|
|
63
|
-
extPath = newFilePath[newFilePath.length - 1] === '/' ? 'index' : '/index';
|
|
64
|
-
newFilePath += extPath;
|
|
65
|
-
}
|
|
66
|
-
} catch (e) {}
|
|
67
|
-
|
|
68
|
-
for (const ext of extensions) {
|
|
69
|
-
const file = newFilePath.endsWith(ext) ? newFilePath : newFilePath + ext;
|
|
70
|
-
if (fs.existsSync(file)) {
|
|
71
|
-
return {
|
|
72
|
-
ext,
|
|
73
|
-
extPath: extPath + ext,
|
|
74
|
-
file,
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return {
|
|
79
|
-
ext: '',
|
|
80
|
-
extPath,
|
|
81
|
-
file: filePath,
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// 判断文件是否在某个目录
|
|
86
|
-
const fileInDir = (dir, file) => {
|
|
87
|
-
if (!fs.existsSync(dir) || !fs.existsSync(file)) {
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
const relativePath = path.relative(dir, file);
|
|
91
|
-
if (relativePath.startsWith('..')) {
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
return true;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
module.exports = {
|
|
99
|
-
isDirEmpty,
|
|
100
|
-
copyFile,
|
|
101
|
-
diffContentCopyFile,
|
|
102
|
-
ensureDirExist,
|
|
103
|
-
ext,
|
|
104
|
-
fileInDir,
|
|
105
|
-
isFile,
|
|
106
|
-
};
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
/**
|
|
4
|
+
* 判断目录是否为空
|
|
5
|
+
* @param {string} dirname 目录名
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
const isDirEmpty = dirname => fs.promises.readdir(dirname).then(files => files.length === 0);
|
|
9
|
+
|
|
10
|
+
// 判断是否是文件
|
|
11
|
+
const isFile = (pathName) => {
|
|
12
|
+
try {
|
|
13
|
+
const stat = fs.lstatSync(pathName);
|
|
14
|
+
return stat.isFile();
|
|
15
|
+
} catch {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 确保目录存在,不存在就创建一个
|
|
22
|
+
* @param {*} dirname 目录名
|
|
23
|
+
*/
|
|
24
|
+
const ensureDirExist = (dirname) => {
|
|
25
|
+
if (!fs.existsSync(dirname)) {
|
|
26
|
+
fs.mkdirSync(dirname, { recursive: true });
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// 复制文件
|
|
31
|
+
const copyFile = function (src, dest) {
|
|
32
|
+
if (fs.existsSync(dest)) {
|
|
33
|
+
fs.unlinkSync(dest);
|
|
34
|
+
}
|
|
35
|
+
const dir = path.dirname(dest);
|
|
36
|
+
ensureDirExist(dir);
|
|
37
|
+
fs.copyFileSync(src, dest);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// 判断文件内容是否一致,不一致再进行拷贝
|
|
41
|
+
function diffContentCopyFile(originFile, destFile) {
|
|
42
|
+
if (fs.existsSync(destFile)) {
|
|
43
|
+
const depDestContent = fs.readFileSync(destFile, 'utf8');
|
|
44
|
+
const depOriginContent = fs.readFileSync(originFile, 'utf8');
|
|
45
|
+
if (depDestContent !== depOriginContent) {
|
|
46
|
+
console.log(`拷贝${originFile}内容到${destFile}`);
|
|
47
|
+
copyFile(originFile, destFile);
|
|
48
|
+
}
|
|
49
|
+
} else {
|
|
50
|
+
console.log(`拷贝${originFile}内容到${destFile}`);
|
|
51
|
+
copyFile(originFile, destFile);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 添加后缀
|
|
56
|
+
function ext(filePath, extensions) {
|
|
57
|
+
let newFilePath = filePath;
|
|
58
|
+
let extPath = '';
|
|
59
|
+
// try catch需要包裹:用来处理'./lib/timer'没有后缀的情况
|
|
60
|
+
try {
|
|
61
|
+
const stat = fs.lstatSync(newFilePath);
|
|
62
|
+
if (stat.isDirectory()) {
|
|
63
|
+
extPath = newFilePath[newFilePath.length - 1] === '/' ? 'index' : '/index';
|
|
64
|
+
newFilePath += extPath;
|
|
65
|
+
}
|
|
66
|
+
} catch (e) {}
|
|
67
|
+
|
|
68
|
+
for (const ext of extensions) {
|
|
69
|
+
const file = newFilePath.endsWith(ext) ? newFilePath : newFilePath + ext;
|
|
70
|
+
if (fs.existsSync(file)) {
|
|
71
|
+
return {
|
|
72
|
+
ext,
|
|
73
|
+
extPath: extPath + ext,
|
|
74
|
+
file,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
ext: '',
|
|
80
|
+
extPath,
|
|
81
|
+
file: filePath,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 判断文件是否在某个目录
|
|
86
|
+
const fileInDir = (dir, file) => {
|
|
87
|
+
if (!fs.existsSync(dir) || !fs.existsSync(file)) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
const relativePath = path.relative(dir, file);
|
|
91
|
+
if (relativePath.startsWith('..')) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
return true;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
module.exports = {
|
|
99
|
+
isDirEmpty,
|
|
100
|
+
copyFile,
|
|
101
|
+
diffContentCopyFile,
|
|
102
|
+
ensureDirExist,
|
|
103
|
+
ext,
|
|
104
|
+
fileInDir,
|
|
105
|
+
isFile,
|
|
106
|
+
};
|
package/src/utils/log.js
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
const chalk = require('chalk');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 本文件提供无依赖的在终端打印彩色文字的方法。
|
|
5
|
-
*/
|
|
6
|
-
const resetCfg = decodeURIComponent('%1B%5B0m'); // \033[0m转义后的字符按,用来还原属性
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 打印红底黑字格式的文字
|
|
10
|
-
* @param {String} message 需要打印的文字信息
|
|
11
|
-
* @returns {undefined} 无
|
|
12
|
-
*/
|
|
13
|
-
const fail = (message = '') => {
|
|
14
|
-
const redStyleConfig = decodeURIComponent('%1B%5B41%3B30m'); // \033[41;30m转义后的字符按,console时输出红色文字
|
|
15
|
-
const greenFontStyleConfig = decodeURIComponent('%1B%5B41%3B37m'); // \033[41;30m转义后的字符按,console时输出红底白色文字
|
|
16
|
-
console.log(`${redStyleConfig} ERROR ${greenFontStyleConfig} ${message}${resetCfg}`); // eslint-disable-line no-console
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* 打印绿底黑字格式的文字
|
|
21
|
-
* @param {String} message 需要打印的文字信息
|
|
22
|
-
* @returns {undefined} 无
|
|
23
|
-
*/
|
|
24
|
-
const succeed = (message = '') => {
|
|
25
|
-
const greenStyleConfig = decodeURIComponent('%1B%5B42%3B30m'); // \033[42;30m转义后的字符按,console时输出绿色文字
|
|
26
|
-
const greenFontStyleConfig = decodeURIComponent('%1B%5B40%3B32m'); // \033[40;32m转义后的字符按,console时输出绿色文字
|
|
27
|
-
console.log(`${greenStyleConfig} Success ${greenFontStyleConfig} ${message}${resetCfg}`); // eslint-disable-line no-console
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* 打印warn提示
|
|
33
|
-
* @param {String} message 需要打印的文字信息
|
|
34
|
-
* @returns {undefined} 无
|
|
35
|
-
*/
|
|
36
|
-
const warn = (message) => {
|
|
37
|
-
console.log(chalk.yellow(message));
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
module.exports = {
|
|
41
|
-
fail,
|
|
42
|
-
succeed,
|
|
43
|
-
warn,
|
|
44
|
-
};
|
|
1
|
+
const chalk = require('chalk');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 本文件提供无依赖的在终端打印彩色文字的方法。
|
|
5
|
+
*/
|
|
6
|
+
const resetCfg = decodeURIComponent('%1B%5B0m'); // \033[0m转义后的字符按,用来还原属性
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 打印红底黑字格式的文字
|
|
10
|
+
* @param {String} message 需要打印的文字信息
|
|
11
|
+
* @returns {undefined} 无
|
|
12
|
+
*/
|
|
13
|
+
const fail = (message = '') => {
|
|
14
|
+
const redStyleConfig = decodeURIComponent('%1B%5B41%3B30m'); // \033[41;30m转义后的字符按,console时输出红色文字
|
|
15
|
+
const greenFontStyleConfig = decodeURIComponent('%1B%5B41%3B37m'); // \033[41;30m转义后的字符按,console时输出红底白色文字
|
|
16
|
+
console.log(`${redStyleConfig} ERROR ${greenFontStyleConfig} ${message}${resetCfg}`); // eslint-disable-line no-console
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 打印绿底黑字格式的文字
|
|
21
|
+
* @param {String} message 需要打印的文字信息
|
|
22
|
+
* @returns {undefined} 无
|
|
23
|
+
*/
|
|
24
|
+
const succeed = (message = '') => {
|
|
25
|
+
const greenStyleConfig = decodeURIComponent('%1B%5B42%3B30m'); // \033[42;30m转义后的字符按,console时输出绿色文字
|
|
26
|
+
const greenFontStyleConfig = decodeURIComponent('%1B%5B40%3B32m'); // \033[40;32m转义后的字符按,console时输出绿色文字
|
|
27
|
+
console.log(`${greenStyleConfig} Success ${greenFontStyleConfig} ${message}${resetCfg}`); // eslint-disable-line no-console
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 打印warn提示
|
|
33
|
+
* @param {String} message 需要打印的文字信息
|
|
34
|
+
* @returns {undefined} 无
|
|
35
|
+
*/
|
|
36
|
+
const warn = (message) => {
|
|
37
|
+
console.log(chalk.yellow(message));
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
module.exports = {
|
|
41
|
+
fail,
|
|
42
|
+
succeed,
|
|
43
|
+
warn,
|
|
44
|
+
};
|