@cgzair/cgz-core 1.0.0 → 1.1.1
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/lib/index.js +17 -1
- package/lib/log.js +53 -1
- package/lib/module-to-cjs.js +49 -1
- package/lib/package-json.js +54 -1
- package/lib/terminal.js +39 -1
- package/lib/utils.js +116 -1
- package/package.json +2 -3
- package/tsconfig.json +1 -1
- package/assets/background.png +0 -0
- package/assets/font.ttf +0 -0
package/lib/index.js
CHANGED
|
@@ -1 +1,17 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
exports.__esModule = true;
|
|
13
|
+
__exportStar(require("./terminal"), exports);
|
|
14
|
+
__exportStar(require("./utils"), exports);
|
|
15
|
+
__exportStar(require("./package-json"), exports);
|
|
16
|
+
__exportStar(require("./log"), exports);
|
|
17
|
+
__exportStar(require("./module-to-cjs"), exports);
|
package/lib/log.js
CHANGED
|
@@ -1 +1,53 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
exports.__esModule = true;
|
|
6
|
+
exports.log = void 0;
|
|
7
|
+
var path_1 = __importDefault(require("path"));
|
|
8
|
+
var chalk_1 = __importDefault(require("chalk"));
|
|
9
|
+
var utils_1 = require("./utils");
|
|
10
|
+
var green = chalk_1["default"].green, blue = chalk_1["default"].blue, yellow = chalk_1["default"].yellow, red = chalk_1["default"].red;
|
|
11
|
+
var timer;
|
|
12
|
+
var log = {
|
|
13
|
+
success: function (text) {
|
|
14
|
+
clearInterval(timer);
|
|
15
|
+
console.log(green(text));
|
|
16
|
+
},
|
|
17
|
+
info: function (text) {
|
|
18
|
+
clearInterval(timer);
|
|
19
|
+
console.info(blue(text));
|
|
20
|
+
},
|
|
21
|
+
loading: function (text) {
|
|
22
|
+
clearInterval(timer);
|
|
23
|
+
var dot = [".", "..", "..."];
|
|
24
|
+
var dotIndex = 0;
|
|
25
|
+
timer = setInterval(function () {
|
|
26
|
+
console.clear();
|
|
27
|
+
console.info(blue("" + text + dot[dotIndex]));
|
|
28
|
+
dotIndex++;
|
|
29
|
+
if (dotIndex === 3) {
|
|
30
|
+
dotIndex = 0;
|
|
31
|
+
}
|
|
32
|
+
}, 1000);
|
|
33
|
+
return timer;
|
|
34
|
+
},
|
|
35
|
+
warn: function (text) {
|
|
36
|
+
clearInterval(timer);
|
|
37
|
+
console.info(yellow(text));
|
|
38
|
+
},
|
|
39
|
+
error: function (text) {
|
|
40
|
+
clearInterval(timer);
|
|
41
|
+
console.info(red(text));
|
|
42
|
+
},
|
|
43
|
+
result: function (text, root, pass) {
|
|
44
|
+
clearInterval(timer);
|
|
45
|
+
console.info([
|
|
46
|
+
blue((0, utils_1.getCurrentPkg)(path_1["default"].resolve(root)).name + " " + text),
|
|
47
|
+
" ",
|
|
48
|
+
pass ? green("\uD83C\uDF89 \uD83C\uDF89 \uD83C\uDF89 \uD83C\uDF89 \uD83C\uDF89" /* success */) : red("\u2716" /* failure */),
|
|
49
|
+
" ",
|
|
50
|
+
].join("\r\n"));
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
exports.log = log;
|
package/lib/module-to-cjs.js
CHANGED
|
@@ -1 +1,49 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
exports.__esModule = true;
|
|
6
|
+
exports.moduleToCjs = void 0;
|
|
7
|
+
var path_1 = require("path");
|
|
8
|
+
var assert_1 = __importDefault(require("assert"));
|
|
9
|
+
var fs_1 = __importDefault(require("fs"));
|
|
10
|
+
var typescript_1 = __importDefault(require("typescript"));
|
|
11
|
+
var fs_extra_1 = require("fs-extra");
|
|
12
|
+
var utils_1 = require("./utils");
|
|
13
|
+
var log_1 = require("./log");
|
|
14
|
+
var moduleToCjs = function (filePath) {
|
|
15
|
+
(0, assert_1["default"])((0, fs_extra_1.existsSync)(filePath), "\u65E0\u6CD5\u8BBF\u95EE\u5F53\u524D\u6587\u4EF6\u8DEF\u5F84\uFF1A" + filePath);
|
|
16
|
+
var dir = (0, path_1.join)(__dirname, ".cjs/");
|
|
17
|
+
if ((0, fs_extra_1.existsSync)(dir)) {
|
|
18
|
+
(0, fs_extra_1.rmSync)(dir, {
|
|
19
|
+
recursive: true
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
(0, fs_extra_1.mkdirSync)(dir);
|
|
23
|
+
var result = "";
|
|
24
|
+
try {
|
|
25
|
+
var content = typescript_1["default"].transpile(fs_1["default"].readFileSync(filePath, "utf8"), {
|
|
26
|
+
target: typescript_1["default"].ScriptTarget.ES5,
|
|
27
|
+
module: typescript_1["default"].ModuleKind.CommonJS,
|
|
28
|
+
esModuleInterop: true,
|
|
29
|
+
allowJs: true,
|
|
30
|
+
types: ["node"],
|
|
31
|
+
lib: ["es5", "dom"]
|
|
32
|
+
});
|
|
33
|
+
fs_1["default"].writeFileSync(dir + "/" + (0, path_1.basename)(filePath).replace(/\.[^.]+$/, ".cjs"), content);
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
log_1.log.error(error);
|
|
37
|
+
}
|
|
38
|
+
(0, utils_1.getChildFile)(dir).some(function (item) {
|
|
39
|
+
var startIdx = item.indexOf(".cjs");
|
|
40
|
+
var fileName = item.substring(startIdx + 5, item.length - 3);
|
|
41
|
+
if (filePath.indexOf(fileName) !== -1) {
|
|
42
|
+
result = item;
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
});
|
|
47
|
+
return require(result);
|
|
48
|
+
};
|
|
49
|
+
exports.moduleToCjs = moduleToCjs;
|
package/lib/package-json.js
CHANGED
|
@@ -1 +1,54 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
exports.__esModule = true;
|
|
6
|
+
exports.setConfig = exports.setScripts = void 0;
|
|
7
|
+
var fs_1 = __importDefault(require("fs"));
|
|
8
|
+
var path_1 = __importDefault(require("path"));
|
|
9
|
+
/**
|
|
10
|
+
* 以 2 个空格缩进将内容写入 package.json
|
|
11
|
+
* @param {string} cont
|
|
12
|
+
* @param {string} root
|
|
13
|
+
*/
|
|
14
|
+
var setFileData = function (cont, root) {
|
|
15
|
+
if (root === void 0) { root = path_1["default"].resolve(__dirname, "../../../"); }
|
|
16
|
+
var rootPackageJson = path_1["default"].resolve(root, "package.json");
|
|
17
|
+
fs_1["default"].writeFileSync(rootPackageJson, JSON.stringify(cont, null, 2));
|
|
18
|
+
fs_1["default"].writeFileSync(rootPackageJson, "\n", {
|
|
19
|
+
flag: "a"
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* 写入 scripts 命令到 package.json
|
|
24
|
+
* @param {string} key
|
|
25
|
+
* @param {string} value
|
|
26
|
+
* @param {string} root
|
|
27
|
+
*/
|
|
28
|
+
var setScripts = function (key, value, root) {
|
|
29
|
+
var _a;
|
|
30
|
+
if (root === void 0) { root = path_1["default"].resolve(__dirname, "../../../"); }
|
|
31
|
+
var rootPackageJson = path_1["default"].resolve(root, "package.json");
|
|
32
|
+
var content = JSON.parse(fs_1["default"].readFileSync(rootPackageJson, "utf-8"));
|
|
33
|
+
content.scripts
|
|
34
|
+
? (content.scripts[key] = value)
|
|
35
|
+
: (content.scripts = (_a = {},
|
|
36
|
+
_a[key] = value,
|
|
37
|
+
_a));
|
|
38
|
+
setFileData(content, root);
|
|
39
|
+
};
|
|
40
|
+
exports.setScripts = setScripts;
|
|
41
|
+
/**
|
|
42
|
+
* 写入自定义配置到 package.json
|
|
43
|
+
* @param {string} key
|
|
44
|
+
* @param {string | Record<string, any>} value
|
|
45
|
+
* @param {string} root
|
|
46
|
+
*/
|
|
47
|
+
var setConfig = function (key, value, root) {
|
|
48
|
+
if (root === void 0) { root = path_1["default"].resolve(__dirname, "../../../"); }
|
|
49
|
+
var rootPackageJson = path_1["default"].resolve(root, "package.json");
|
|
50
|
+
var content = JSON.parse(fs_1["default"].readFileSync(rootPackageJson, "utf-8"));
|
|
51
|
+
content[key] = value;
|
|
52
|
+
setFileData(content, root);
|
|
53
|
+
};
|
|
54
|
+
exports.setConfig = setConfig;
|
package/lib/terminal.js
CHANGED
|
@@ -1 +1,39 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
exports.__esModule = true;
|
|
14
|
+
exports.spawnCommand = void 0;
|
|
15
|
+
var child_process_1 = require("child_process");
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
* @param {string} command
|
|
19
|
+
* @param {string[]} args
|
|
20
|
+
* @param {SpawnOptions} options
|
|
21
|
+
* @returns {Promise<unknown>}
|
|
22
|
+
*/
|
|
23
|
+
var spawnCommand = function (command, args, options) {
|
|
24
|
+
if (args === void 0) { args = []; }
|
|
25
|
+
return new Promise(function (resolve) {
|
|
26
|
+
var childProcess = (0, child_process_1.spawn)(command, args, __assign({ stdio: "inherit" }, options));
|
|
27
|
+
var stdin = childProcess.stdin, stdout = childProcess.stdout, stderr = childProcess.stderr;
|
|
28
|
+
process.stdin.setRawMode(true);
|
|
29
|
+
if (stdin && stdout && stderr) {
|
|
30
|
+
process.stdin.pipe(stdin);
|
|
31
|
+
stdout.pipe(process.stdout);
|
|
32
|
+
stderr.pipe(process.stderr);
|
|
33
|
+
}
|
|
34
|
+
childProcess.on("close", function () {
|
|
35
|
+
resolve("ok");
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
exports.spawnCommand = spawnCommand;
|
package/lib/utils.js
CHANGED
|
@@ -1 +1,116 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
exports.__esModule = true;
|
|
6
|
+
exports.getChildFile = exports.getUserHomeDir = exports.getCurrentPkg = exports.getRootPath = exports.isAccessSync = exports.getEscapePath = exports.getExecutableSuffix = void 0;
|
|
7
|
+
var fs_1 = __importDefault(require("fs"));
|
|
8
|
+
var os_1 = __importDefault(require("os"));
|
|
9
|
+
var path_1 = __importDefault(require("path"));
|
|
10
|
+
var global_dirs_1 = __importDefault(require("global-dirs"));
|
|
11
|
+
/**
|
|
12
|
+
* 根据平台不同返回执行后缀
|
|
13
|
+
* @param {string} platform
|
|
14
|
+
* @returns {string}
|
|
15
|
+
*/
|
|
16
|
+
var getExecutableSuffix = function (platform) {
|
|
17
|
+
if (platform === "win32") {
|
|
18
|
+
return ".cmd";
|
|
19
|
+
}
|
|
20
|
+
return "";
|
|
21
|
+
};
|
|
22
|
+
exports.getExecutableSuffix = getExecutableSuffix;
|
|
23
|
+
/**
|
|
24
|
+
* 根据不同平台转义路径
|
|
25
|
+
* @param {string} platform
|
|
26
|
+
* @param {string} url
|
|
27
|
+
* @returns {string}
|
|
28
|
+
*/
|
|
29
|
+
var getEscapePath = function (platform, url) {
|
|
30
|
+
if (platform === "win32") {
|
|
31
|
+
return url.replace(/\\/g, "\\\\");
|
|
32
|
+
}
|
|
33
|
+
return url;
|
|
34
|
+
};
|
|
35
|
+
exports.getEscapePath = getEscapePath;
|
|
36
|
+
/**
|
|
37
|
+
* 文件是否存在
|
|
38
|
+
* @param {PathLike} url
|
|
39
|
+
* @param {number} mode
|
|
40
|
+
* @returns {boolean}
|
|
41
|
+
*/
|
|
42
|
+
var isAccessSync = function (url, mode) {
|
|
43
|
+
try {
|
|
44
|
+
fs_1["default"].accessSync(url, mode);
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
exports.isAccessSync = isAccessSync;
|
|
52
|
+
/**
|
|
53
|
+
* 返回当前项目安装路径
|
|
54
|
+
* @param {string} platform
|
|
55
|
+
* @param {string} url
|
|
56
|
+
* @param {string} name
|
|
57
|
+
* @returns {string}
|
|
58
|
+
*/
|
|
59
|
+
var getRootPath = function (platform, url, name) {
|
|
60
|
+
if (platform === "win32") {
|
|
61
|
+
return path_1["default"].join(url, "..", "..");
|
|
62
|
+
}
|
|
63
|
+
return path_1["default"].join(global_dirs_1["default"].npm.packages, name);
|
|
64
|
+
};
|
|
65
|
+
exports.getRootPath = getRootPath;
|
|
66
|
+
/**
|
|
67
|
+
* 返回当前项目 package.json 信息
|
|
68
|
+
* @param {string} [dir="./package.json"]
|
|
69
|
+
* @returns {PKG}
|
|
70
|
+
*/
|
|
71
|
+
var getCurrentPkg = function (dir) {
|
|
72
|
+
if (dir === void 0) { dir = process.cwd(); }
|
|
73
|
+
return JSON.parse(fs_1["default"].readFileSync(path_1["default"].join(dir, "./package.json"), "utf8"));
|
|
74
|
+
};
|
|
75
|
+
exports.getCurrentPkg = getCurrentPkg;
|
|
76
|
+
/**
|
|
77
|
+
* 获取用户目录
|
|
78
|
+
* @returns {string}
|
|
79
|
+
*/
|
|
80
|
+
var getUserHomeDir = function () {
|
|
81
|
+
var homedir = function () {
|
|
82
|
+
var env = process.env;
|
|
83
|
+
var home = env.HOME;
|
|
84
|
+
var user = env.LOGNAME || env.USER || env.LNAME || env.USERNAME;
|
|
85
|
+
if (process.platform === "win32") {
|
|
86
|
+
return env.USERPROFILE || "" + env.HOMEDRIVE + env.HOMEPATH || home || "";
|
|
87
|
+
}
|
|
88
|
+
if (process.platform === "darwin") {
|
|
89
|
+
return home || (user ? "/Users/" + user : "");
|
|
90
|
+
}
|
|
91
|
+
if (process.platform === "linux") {
|
|
92
|
+
return home || (process.getuid() === 0 ? "/root" : user ? "/home/" + user : "");
|
|
93
|
+
}
|
|
94
|
+
return home || "";
|
|
95
|
+
};
|
|
96
|
+
return typeof os_1["default"].homedir === "function" ? os_1["default"].homedir() : homedir();
|
|
97
|
+
};
|
|
98
|
+
exports.getUserHomeDir = getUserHomeDir;
|
|
99
|
+
/**
|
|
100
|
+
* 获取目录下级所有文件
|
|
101
|
+
* @param {string} root
|
|
102
|
+
* @returns {string[]}
|
|
103
|
+
*/
|
|
104
|
+
var getChildFile = function (root) {
|
|
105
|
+
var result = [];
|
|
106
|
+
fs_1["default"].readdirSync(root).forEach(function (item) {
|
|
107
|
+
if (fs_1["default"].statSync(path_1["default"].join(root, item)).isDirectory()) {
|
|
108
|
+
result = result.concat(getChildFile(path_1["default"].join(root, item)));
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
result.push(path_1["default"].join(root, item));
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
return result;
|
|
115
|
+
};
|
|
116
|
+
exports.getChildFile = getChildFile;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cgzair/cgz-core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Core tool library.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"keywords": [
|
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
"build": "tsc"
|
|
11
11
|
},
|
|
12
12
|
"files": [
|
|
13
|
-
"assets",
|
|
14
13
|
"lib",
|
|
15
14
|
"package.json",
|
|
16
15
|
"README.md",
|
|
@@ -34,5 +33,5 @@
|
|
|
34
33
|
"access": "public"
|
|
35
34
|
},
|
|
36
35
|
"license": "MIT",
|
|
37
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "ded6ffbaa1fd7da5cca106fcf1addd7c6bf8b930"
|
|
38
37
|
}
|
package/tsconfig.json
CHANGED
package/assets/background.png
DELETED
|
Binary file
|
package/assets/font.ttf
DELETED
|
Binary file
|