@zwa73/dev-utils 1.0.3 → 1.0.5
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/bin/cli +5 -0
- package/data/index.d.ts +1 -0
- package/data/index.js +1 -0
- package/data/scripts/compile.ps1 +5 -0
- package/data/scripts/watch.ps1 +10 -0
- package/data/tsconfig.compile.json +4 -0
- package/data/tsconfig.json +22 -0
- package/dist/Command/Init.d.ts +3 -0
- package/dist/Command/Init.js +69 -0
- package/dist/Command/Node.d.ts +3 -0
- package/dist/Command/Node.js +21 -0
- package/dist/Command/Release.d.ts +3 -0
- package/dist/Command/Release.js +71 -0
- package/dist/Command/Route.d.ts +1 -0
- package/dist/Command/Route.js +14 -0
- package/dist/Command/RouteInterface.d.ts +20 -0
- package/dist/Command/RouteInterface.js +90 -0
- package/dist/Command/index.d.ts +1 -0
- package/dist/Command/index.js +17 -0
- package/dist/UtilSchemaTool.js +31 -5
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +33 -27
- package/scripts/compile.ps1 +5 -0
- package/scripts/watch.ps1 +10 -0
- package/src/Command/Init.ts +49 -0
- package/src/Command/Node.ts +18 -0
- package/src/Command/Release.ts +65 -0
- package/src/Command/Route.ts +12 -0
- package/src/Command/RouteInterface.ts +68 -0
- package/src/Command/index.ts +1 -0
- package/src/UtilSchemaTool.ts +5 -4
- package/src/index.ts +2 -1
- package/tsconfig.json +2 -0
- package/dist/Schema.js +0 -1
- package/schema/TestType1.schema.json +0 -4
- package/schema/TestType2.schema.json +0 -4
- package/schema/schemas.json +0 -35
- /package/{dist/Schema.d.ts → data/src/index.ts} +0 -0
package/bin/cli
ADDED
package/data/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './dist';
|
package/data/index.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
module.exports = require("./dist");
|
@@ -0,0 +1,22 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"allowJs": true,
|
4
|
+
"strict": true,
|
5
|
+
"target": "ES2022",
|
6
|
+
"module": "CommonJS",
|
7
|
+
"moduleResolution": "node",
|
8
|
+
"esModuleInterop": true,
|
9
|
+
"outDir": "./dist",
|
10
|
+
"declaration": true,
|
11
|
+
"baseUrl": ".",
|
12
|
+
"emitDecoratorMetadata": true,
|
13
|
+
"experimentalDecorators": true,
|
14
|
+
"paths": {
|
15
|
+
"@src/*": ["./src/*"],
|
16
|
+
"@/*": ["./*"],
|
17
|
+
"@": ["./src/index"]
|
18
|
+
}
|
19
|
+
},
|
20
|
+
"include": ["./src/**/*.ts", "./src/**/*.js", "./jest/**/*.ts"],
|
21
|
+
"exclude": ["./node_modules/**/*"]
|
22
|
+
}
|
@@ -0,0 +1,69 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
+
if (mod && mod.__esModule) return mod;
|
20
|
+
var result = {};
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
+
__setModuleDefault(result, mod);
|
23
|
+
return result;
|
24
|
+
};
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
26
|
+
exports.CmdInit = void 0;
|
27
|
+
const path = __importStar(require("path"));
|
28
|
+
const utils_1 = require("@zwa73/utils");
|
29
|
+
const fs = __importStar(require("fs"));
|
30
|
+
const RouteInterface_1 = require("./RouteInterface");
|
31
|
+
/**复制基础文件 */
|
32
|
+
async function copyData() {
|
33
|
+
const filelist = await fs.promises.readdir(RouteInterface_1.DATA_PATH);
|
34
|
+
const plist = filelist.map(async (fileName) => {
|
35
|
+
utils_1.SLogger.info(`正在复制 ${fileName}`);
|
36
|
+
const filePath = path.join(RouteInterface_1.DATA_PATH, fileName);
|
37
|
+
const targetPath = path.join(RouteInterface_1.PROJECT_PATH, fileName);
|
38
|
+
if (!await utils_1.UtilFT.pathExists(targetPath))
|
39
|
+
await fs.promises.cp(filePath, targetPath, { recursive: true });
|
40
|
+
else
|
41
|
+
utils_1.SLogger.info(`${fileName} 已存在 跳过`);
|
42
|
+
return null;
|
43
|
+
});
|
44
|
+
await Promise.all(plist);
|
45
|
+
}
|
46
|
+
/**安装npm包 */
|
47
|
+
async function installPackage() {
|
48
|
+
const install = async (name) => await utils_1.UtilFunc.exec(`npm i ${name}`);
|
49
|
+
const packageList = ['@zwa73/utils'];
|
50
|
+
for (const name of packageList) {
|
51
|
+
utils_1.SLogger.info(`正在安装 ${name}`);
|
52
|
+
await install(name);
|
53
|
+
}
|
54
|
+
}
|
55
|
+
/**对项目进行初始化 */
|
56
|
+
const CmdInit = (program) => program
|
57
|
+
.command('init')
|
58
|
+
.description('对当前目录进行项目初始化')
|
59
|
+
.action(async (opt) => {
|
60
|
+
(0, RouteInterface_1.checkProject)();
|
61
|
+
utils_1.SLogger.info(`开始初始化设置当前目录 ${RouteInterface_1.PROJECT_PATH}`);
|
62
|
+
await (0, RouteInterface_1.withMirrorSource)(async () => {
|
63
|
+
await Promise.all([
|
64
|
+
copyData(),
|
65
|
+
installPackage(),
|
66
|
+
]);
|
67
|
+
});
|
68
|
+
});
|
69
|
+
exports.CmdInit = CmdInit;
|
@@ -0,0 +1,21 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.CmdNode = void 0;
|
4
|
+
const utils_1 = require("@zwa73/utils");
|
5
|
+
/**编译并运行某个文件 */
|
6
|
+
const CmdNode = (program) => program
|
7
|
+
.command('node <path>')
|
8
|
+
.option('-p, --project [path]', 'tsconfig的目录')
|
9
|
+
.description('编译并运行某个文件')
|
10
|
+
.action(async (arg, opt) => {
|
11
|
+
utils_1.SLogger.info(`编译并运行: ${arg}`);
|
12
|
+
let cmd = `ts-node -r tsconfig-paths/register ${arg}`;
|
13
|
+
if (opt.project)
|
14
|
+
cmd += `-p ${opt.project}`;
|
15
|
+
const { stderr, stdout } = await utils_1.UtilFunc.exec(cmd);
|
16
|
+
if (stderr)
|
17
|
+
utils_1.SLogger.error(stderr);
|
18
|
+
else
|
19
|
+
utils_1.SLogger.info(stdout);
|
20
|
+
});
|
21
|
+
exports.CmdNode = CmdNode;
|
@@ -0,0 +1,71 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.CmdRelease = void 0;
|
7
|
+
const path_1 = __importDefault(require("path"));
|
8
|
+
const utils_1 = require("@zwa73/utils");
|
9
|
+
const RouteInterface_1 = require("./RouteInterface");
|
10
|
+
/**解析版本号为number数组 */
|
11
|
+
function parseVersion(version) {
|
12
|
+
const arr = version.split(".").map(Number);
|
13
|
+
if (arr.length !== 3)
|
14
|
+
(0, utils_1.throwError)(`${version} 不是有效的版本号`, 'error');
|
15
|
+
for (let i = 0; i < 3; i++) {
|
16
|
+
if (!isFinite(arr[i]))
|
17
|
+
(0, utils_1.throwError)(`${version} 不是有效的版本号`, 'error');
|
18
|
+
}
|
19
|
+
return arr;
|
20
|
+
}
|
21
|
+
/**判断版本号是否有效 */
|
22
|
+
function checkVersion(oldVersion, newVersion) {
|
23
|
+
const oldParts = parseVersion(oldVersion);
|
24
|
+
const newParts = parseVersion(newVersion);
|
25
|
+
for (let i = 0; i < 3; i++) {
|
26
|
+
if (newParts[i] < oldParts[i])
|
27
|
+
(0, utils_1.throwError)(`${newVersion} 低于当前版本号 ${oldVersion}`, 'error');
|
28
|
+
if (newParts[i] > oldParts[i])
|
29
|
+
break;
|
30
|
+
}
|
31
|
+
}
|
32
|
+
/**更新版本号 */
|
33
|
+
async function updateVersion(newVersion) {
|
34
|
+
const packagePath = path_1.default.join(RouteInterface_1.PROJECT_PATH, "package.json");
|
35
|
+
const packageData = await utils_1.UtilFT.loadJSONFile(packagePath);
|
36
|
+
if (newVersion) {
|
37
|
+
checkVersion(packageData.version, newVersion);
|
38
|
+
packageData.version = newVersion;
|
39
|
+
}
|
40
|
+
else {
|
41
|
+
const version = packageData.version.split(".");
|
42
|
+
version[2] = (parseInt(version[2]) + 1).toString();
|
43
|
+
packageData.version = version.join(".");
|
44
|
+
}
|
45
|
+
await utils_1.UtilFT.writeJSONFile(packagePath, packageData);
|
46
|
+
return packageData.version;
|
47
|
+
}
|
48
|
+
/**更新版本号并发布npm包 */
|
49
|
+
const CmdRelease = (program) => program
|
50
|
+
.command("release")
|
51
|
+
.description("更新版本号并发布包")
|
52
|
+
.option("-v, --version [version]", "指定发布的版本号 `${number}.${number}.${number}`")
|
53
|
+
.option("-a, --access [access]", "npm publish 的 access 参数", "public")
|
54
|
+
.action(async (opt) => {
|
55
|
+
(0, RouteInterface_1.checkProject)();
|
56
|
+
utils_1.SLogger.info(`开始发布项目`);
|
57
|
+
(0, RouteInterface_1.withOfficialSource)(async () => {
|
58
|
+
try {
|
59
|
+
const newVersion = await updateVersion(opt.version);
|
60
|
+
utils_1.SLogger.info(`新版本号: ${newVersion}`);
|
61
|
+
utils_1.SLogger.info("正在发布...");
|
62
|
+
const cmd = `npm publish --access ${opt.access}`;
|
63
|
+
await utils_1.UtilFunc.exec(cmd);
|
64
|
+
utils_1.SLogger.info("发布完成");
|
65
|
+
}
|
66
|
+
catch (error) {
|
67
|
+
utils_1.SLogger.error(error);
|
68
|
+
}
|
69
|
+
});
|
70
|
+
});
|
71
|
+
exports.CmdRelease = CmdRelease;
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare function cliRoute(): Promise<void>;
|
@@ -0,0 +1,14 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.cliRoute = void 0;
|
4
|
+
const commander_1 = require("commander");
|
5
|
+
const Node_1 = require("./Node");
|
6
|
+
const Init_1 = require("./Init");
|
7
|
+
const Release_1 = require("./Release");
|
8
|
+
async function cliRoute() {
|
9
|
+
(0, Init_1.CmdInit)(commander_1.program);
|
10
|
+
(0, Node_1.CmdNode)(commander_1.program);
|
11
|
+
(0, Release_1.CmdRelease)(commander_1.program);
|
12
|
+
commander_1.program.parse(process.argv);
|
13
|
+
}
|
14
|
+
exports.cliRoute = cliRoute;
|
@@ -0,0 +1,20 @@
|
|
1
|
+
/**npm镜像源 */
|
2
|
+
export declare const MIRROR_SOURCE = "https://registry.npmmirror.com/";
|
3
|
+
/**npm官方源 */
|
4
|
+
export declare const OFFICIAL_SOURCE = "https://registry.npmjs.org/";
|
5
|
+
/**data文件夹路径 */
|
6
|
+
export declare const DATA_PATH: string;
|
7
|
+
/**项目路径 */
|
8
|
+
export declare const PROJECT_PATH: string;
|
9
|
+
/** 检测非浮点的整数类型 */
|
10
|
+
export declare function parseInteger(value: string): number;
|
11
|
+
/** 检测任意数字类型 */
|
12
|
+
export declare function parseNumber(value: string): number;
|
13
|
+
/** 检测布尔类型 */
|
14
|
+
export declare function parseBoolean(value: string): boolean;
|
15
|
+
/**在镜像源内镜像操作 */
|
16
|
+
export declare function withMirrorSource(func: (() => any)): Promise<void>;
|
17
|
+
/**在官方源内镜像操作 */
|
18
|
+
export declare function withOfficialSource(func: (() => any)): Promise<void>;
|
19
|
+
/**检查目录是否为项目 */
|
20
|
+
export declare function checkProject(): Promise<void>;
|
@@ -0,0 +1,90 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
+
if (mod && mod.__esModule) return mod;
|
20
|
+
var result = {};
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
+
__setModuleDefault(result, mod);
|
23
|
+
return result;
|
24
|
+
};
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
26
|
+
exports.checkProject = exports.withOfficialSource = exports.withMirrorSource = exports.parseBoolean = exports.parseNumber = exports.parseInteger = exports.PROJECT_PATH = exports.DATA_PATH = exports.OFFICIAL_SOURCE = exports.MIRROR_SOURCE = void 0;
|
27
|
+
const utils_1 = require("@zwa73/utils");
|
28
|
+
const path = __importStar(require("path"));
|
29
|
+
/**npm镜像源 */
|
30
|
+
exports.MIRROR_SOURCE = "https://registry.npmmirror.com/";
|
31
|
+
/**npm官方源 */
|
32
|
+
exports.OFFICIAL_SOURCE = "https://registry.npmjs.org/";
|
33
|
+
/**data文件夹路径 */
|
34
|
+
exports.DATA_PATH = path.join(__dirname, '..', 'data');
|
35
|
+
/**项目路径 */
|
36
|
+
exports.PROJECT_PATH = process.cwd();
|
37
|
+
/** 检测非浮点的整数类型 */
|
38
|
+
function parseInteger(value) {
|
39
|
+
const num = Number(value);
|
40
|
+
if (!(typeof num === 'number' && isFinite(num) && Math.floor(num) === num))
|
41
|
+
(0, utils_1.throwError)(`${value} 不是有效的整数数字`);
|
42
|
+
return num;
|
43
|
+
}
|
44
|
+
exports.parseInteger = parseInteger;
|
45
|
+
/** 检测任意数字类型 */
|
46
|
+
function parseNumber(value) {
|
47
|
+
const num = Number(value);
|
48
|
+
if (!(typeof value === 'number' && isFinite(value)))
|
49
|
+
(0, utils_1.throwError)(`${value} 不是有效的数字`);
|
50
|
+
return num;
|
51
|
+
}
|
52
|
+
exports.parseNumber = parseNumber;
|
53
|
+
/** 检测布尔类型 */
|
54
|
+
function parseBoolean(value) {
|
55
|
+
return value ? true : false;
|
56
|
+
}
|
57
|
+
exports.parseBoolean = parseBoolean;
|
58
|
+
/**在镜像源内镜像操作 */
|
59
|
+
async function withMirrorSource(func) {
|
60
|
+
const out = await utils_1.UtilFunc.exec('npm config get registry');
|
61
|
+
const OriginSource = out.stdout.trim(); //?
|
62
|
+
utils_1.SLogger.info(`正在修改npm源为 镜像源: ${exports.MIRROR_SOURCE}`);
|
63
|
+
await utils_1.UtilFunc.exec(`npm config set registry ${exports.MIRROR_SOURCE}`);
|
64
|
+
utils_1.SLogger.info(`成功修改npm源为 镜像源: ${exports.MIRROR_SOURCE}`);
|
65
|
+
await func();
|
66
|
+
utils_1.SLogger.info(`正在修改npm源为 原始源: ${OriginSource}`);
|
67
|
+
await utils_1.UtilFunc.exec(`npm config set registry ${OriginSource}`);
|
68
|
+
utils_1.SLogger.info(`成功修改npm源为 原始源: ${OriginSource}`);
|
69
|
+
}
|
70
|
+
exports.withMirrorSource = withMirrorSource;
|
71
|
+
/**在官方源内镜像操作 */
|
72
|
+
async function withOfficialSource(func) {
|
73
|
+
const out = await utils_1.UtilFunc.exec('npm config get registry');
|
74
|
+
const OriginSource = out.stdout.trim(); //?
|
75
|
+
utils_1.SLogger.info(`正在修改npm源为 官方源: ${exports.OFFICIAL_SOURCE}`);
|
76
|
+
await utils_1.UtilFunc.exec(`npm config set registry ${exports.OFFICIAL_SOURCE}`);
|
77
|
+
utils_1.SLogger.info(`成功修改npm源为 官方源: ${exports.OFFICIAL_SOURCE}`);
|
78
|
+
await func();
|
79
|
+
utils_1.SLogger.info(`正在修改npm源为 原始源: ${OriginSource}`);
|
80
|
+
await utils_1.UtilFunc.exec(`npm config set registry ${OriginSource}`);
|
81
|
+
utils_1.SLogger.info(`成功修改npm源为 原始源: ${OriginSource}`);
|
82
|
+
}
|
83
|
+
exports.withOfficialSource = withOfficialSource;
|
84
|
+
/**检查目录是否为项目 */
|
85
|
+
async function checkProject() {
|
86
|
+
const filePath = path.join(exports.PROJECT_PATH, "package.json");
|
87
|
+
if (!(await utils_1.UtilFT.pathExists(filePath)))
|
88
|
+
(0, utils_1.throwError)(`当前目录: ${exports.PROJECT_PATH} 不是npm项目目录, 请先使用 npm init`);
|
89
|
+
}
|
90
|
+
exports.checkProject = checkProject;
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './Route';
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./Route"), exports);
|
package/dist/UtilSchemaTool.js
CHANGED
@@ -1,8 +1,31 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
+
if (mod && mod.__esModule) return mod;
|
20
|
+
var result = {};
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
+
__setModuleDefault(result, mod);
|
23
|
+
return result;
|
24
|
+
};
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
26
|
exports.UtilST = void 0;
|
4
|
-
const TJS = require("typescript-json-schema");
|
5
|
-
const path = require("path");
|
27
|
+
const TJS = __importStar(require("typescript-json-schema"));
|
28
|
+
const path = __importStar(require("path"));
|
6
29
|
const utils_1 = require("@zwa73/utils");
|
7
30
|
var UtilST;
|
8
31
|
(function (UtilST) {
|
@@ -14,9 +37,12 @@ var UtilST;
|
|
14
37
|
*/
|
15
38
|
async function builAllSchema(configPath, outDir, coverDefine = {}) {
|
16
39
|
outDir = path.join(outDir, "schemas.json");
|
17
|
-
const
|
18
|
-
|
19
|
-
|
40
|
+
const { stderr, stdout } = await utils_1.UtilFunc.exec(`typescript-json-schema ${configPath} * --out ${outDir} --required --strictNullChecks --aliasRefs`);
|
41
|
+
if (stderr)
|
42
|
+
utils_1.SLogger.error(stderr);
|
43
|
+
else
|
44
|
+
utils_1.SLogger.info(stdout);
|
45
|
+
const schema = (await utils_1.UtilFT.loadJSONFile(outDir));
|
20
46
|
//进行预处理并展开
|
21
47
|
await expandSchema(outDir, schema, coverDefine);
|
22
48
|
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
@@ -1,29 +1,35 @@
|
|
1
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
|
-
|
2
|
+
"name": "@zwa73/dev-utils",
|
3
|
+
"version": "1.0.5",
|
4
|
+
"description": "编译与调试工具",
|
5
|
+
"main": "index.js",
|
6
|
+
"scripts": {
|
7
|
+
"test": "call npm run compile && jest",
|
8
|
+
"release": "call npm run compile && call node release.js && call npm publish --access public",
|
9
|
+
"compile": "powershell scripts/compile",
|
10
|
+
"watch": "powershell scripts/watch"
|
11
|
+
},
|
12
|
+
"keywords": [
|
13
|
+
"schema"
|
14
|
+
],
|
15
|
+
"author": "zwa73",
|
16
|
+
"license": "ISC",
|
17
|
+
"dependencies": {
|
18
|
+
"@zwa73/utils": "^1.0.100",
|
19
|
+
"commander": "^11.1.0",
|
20
|
+
"ts-node": "^10.9.2",
|
21
|
+
"tsconfig-paths": "^4.2.0",
|
22
|
+
"typescript-json-schema": "^0.63.0"
|
23
|
+
},
|
24
|
+
"devDependencies": {
|
25
|
+
"@types/jest": "^29.5.12",
|
26
|
+
"@types/node": "^20.11.19",
|
27
|
+
"jest": "^29.7.0",
|
28
|
+
"ts-jest": "^29.1.2",
|
29
|
+
"tsc-alias": "^1.8.8",
|
30
|
+
"typescript": "^5.3.3"
|
31
|
+
},
|
32
|
+
"bin": {
|
33
|
+
"zcli": "bin/cli"
|
34
|
+
}
|
29
35
|
}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# 定义一个函数来运行脚本
|
2
|
+
function New-Script {
|
3
|
+
param($command)
|
4
|
+
# 在后台运行命令
|
5
|
+
Start-Process -NoNewWindow -FilePath "powershell" -ArgumentList "-Command", $command
|
6
|
+
}
|
7
|
+
|
8
|
+
# 开始运行脚本
|
9
|
+
New-Script "tsc -w -p tsconfig.compile.json"
|
10
|
+
New-Script "tsc-alias -w -p tsconfig.compile.json"
|
@@ -0,0 +1,49 @@
|
|
1
|
+
import * as path from 'path';
|
2
|
+
import { AnyFunc, SLogger, UtilFP, UtilFT, UtilFunc } from '@zwa73/utils';
|
3
|
+
import * as fs from 'fs';
|
4
|
+
import { DATA_PATH, MIRROR_SOURCE, PROJECT_PATH, checkProject, withMirrorSource } from './RouteInterface';
|
5
|
+
import { Command } from 'commander';
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
/**复制基础文件 */
|
10
|
+
async function copyData() {
|
11
|
+
const filelist = await fs.promises.readdir(DATA_PATH);
|
12
|
+
const plist = filelist.map(async (fileName)=>{
|
13
|
+
SLogger.info(`正在复制 ${fileName}`);
|
14
|
+
const filePath = path.join(DATA_PATH,fileName);
|
15
|
+
const targetPath = path.join(PROJECT_PATH,fileName);
|
16
|
+
if(!await UtilFT.pathExists(targetPath))
|
17
|
+
await fs.promises.cp(filePath,targetPath,{recursive:true});
|
18
|
+
else
|
19
|
+
SLogger.info(`${fileName} 已存在 跳过`);
|
20
|
+
return null;
|
21
|
+
})
|
22
|
+
await Promise.all(plist);
|
23
|
+
}
|
24
|
+
|
25
|
+
/**安装npm包 */
|
26
|
+
async function installPackage() {
|
27
|
+
const install = async (name:string)=>
|
28
|
+
await UtilFunc.exec(`npm i ${name}`);
|
29
|
+
const packageList = ['@zwa73/utils'];
|
30
|
+
for(const name of packageList){
|
31
|
+
SLogger.info(`正在安装 ${name}`);
|
32
|
+
await install(name);
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
/**对项目进行初始化 */
|
37
|
+
export const CmdInit = (program:Command)=>program
|
38
|
+
.command('init')
|
39
|
+
.description('对当前目录进行项目初始化')
|
40
|
+
.action(async (opt) => {
|
41
|
+
checkProject();
|
42
|
+
SLogger.info(`开始初始化设置当前目录 ${PROJECT_PATH}`);
|
43
|
+
await withMirrorSource(async ()=>{
|
44
|
+
await Promise.all([
|
45
|
+
copyData(),
|
46
|
+
installPackage(),
|
47
|
+
]);
|
48
|
+
});
|
49
|
+
});
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import { SLogger, UtilFunc } from "@zwa73/utils";
|
2
|
+
import { Command } from "commander";
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
/**编译并运行某个文件 */
|
7
|
+
export const CmdNode = (program:Command)=>program
|
8
|
+
.command('node <path>')
|
9
|
+
.option('-p, --project [path]','tsconfig的目录')
|
10
|
+
.description('编译并运行某个文件')
|
11
|
+
.action(async (arg, opt) => {
|
12
|
+
SLogger.info(`编译并运行: ${arg}`);
|
13
|
+
let cmd = `ts-node -r tsconfig-paths/register ${arg}`;
|
14
|
+
if(opt.project) cmd+=`-p ${opt.project}`;
|
15
|
+
const {stderr,stdout}=await UtilFunc.exec(cmd);
|
16
|
+
if(stderr) SLogger.error(stderr);
|
17
|
+
else SLogger.info(stdout);
|
18
|
+
});
|
@@ -0,0 +1,65 @@
|
|
1
|
+
import { Command } from "commander";
|
2
|
+
import fs from "fs";
|
3
|
+
import path from "path";
|
4
|
+
import { SLogger, UtilFT, UtilFunc, throwError } from "@zwa73/utils";
|
5
|
+
import { PROJECT_PATH, checkProject, withOfficialSource } from "./RouteInterface";
|
6
|
+
|
7
|
+
/**解析版本号为number数组 */
|
8
|
+
function parseVersion(version: string){
|
9
|
+
const arr = version.split(".").map(Number);
|
10
|
+
if (arr.length !== 3)
|
11
|
+
throwError(`${version} 不是有效的版本号`,'error');
|
12
|
+
for (let i = 0; i < 3; i++){
|
13
|
+
if (!isFinite(arr[i]))
|
14
|
+
throwError(`${version} 不是有效的版本号`,'error');
|
15
|
+
}
|
16
|
+
return arr;
|
17
|
+
}
|
18
|
+
/**判断版本号是否有效 */
|
19
|
+
function checkVersion(oldVersion: string, newVersion: string) {
|
20
|
+
const oldParts = parseVersion(oldVersion);
|
21
|
+
const newParts = parseVersion(newVersion);
|
22
|
+
for (let i = 0; i < 3; i++) {
|
23
|
+
if (newParts[i] < oldParts[i])
|
24
|
+
throwError(`${newVersion} 低于当前版本号 ${oldVersion}`,'error');
|
25
|
+
if (newParts[i] > oldParts[i])
|
26
|
+
break;
|
27
|
+
}
|
28
|
+
}
|
29
|
+
/**更新版本号 */
|
30
|
+
async function updateVersion(newVersion?: string): Promise<string> {
|
31
|
+
const packagePath = path.join(PROJECT_PATH, "package.json");
|
32
|
+
const packageData = await UtilFT.loadJSONFile(packagePath) as any;
|
33
|
+
if (newVersion) {
|
34
|
+
checkVersion(packageData.version, newVersion);
|
35
|
+
packageData.version = newVersion;
|
36
|
+
} else {
|
37
|
+
const version = packageData.version.split(".");
|
38
|
+
version[2] = (parseInt(version[2]) + 1).toString();
|
39
|
+
packageData.version = version.join(".");
|
40
|
+
}
|
41
|
+
await UtilFT.writeJSONFile(packagePath, packageData);
|
42
|
+
return packageData.version;
|
43
|
+
}
|
44
|
+
/**更新版本号并发布npm包 */
|
45
|
+
export const CmdRelease = (program: Command) =>program
|
46
|
+
.command("release")
|
47
|
+
.description("更新版本号并发布包")
|
48
|
+
.option("-v, --version [version]", "指定发布的版本号 `${number}.${number}.${number}`")
|
49
|
+
.option("-a, --access [access]", "npm publish 的 access 参数","public")
|
50
|
+
.action(async (opt) => {
|
51
|
+
checkProject();
|
52
|
+
SLogger.info(`开始发布项目`);
|
53
|
+
withOfficialSource(async ()=>{
|
54
|
+
try {
|
55
|
+
const newVersion = await updateVersion(opt.version);
|
56
|
+
SLogger.info(`新版本号: ${newVersion}`);
|
57
|
+
SLogger.info("正在发布...");
|
58
|
+
const cmd = `npm publish --access ${opt.access}`;
|
59
|
+
await UtilFunc.exec(cmd);
|
60
|
+
SLogger.info("发布完成");
|
61
|
+
} catch (error) {
|
62
|
+
SLogger.error(error);
|
63
|
+
}
|
64
|
+
})
|
65
|
+
});
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { program } from "commander";
|
2
|
+
import { CmdNode } from "./Node";
|
3
|
+
import { CmdInit } from "./Init";
|
4
|
+
import { CmdRelease } from "./Release";
|
5
|
+
|
6
|
+
|
7
|
+
export async function cliRoute(){
|
8
|
+
CmdInit(program);
|
9
|
+
CmdNode(program);
|
10
|
+
CmdRelease(program);
|
11
|
+
program.parse(process.argv);
|
12
|
+
}
|
@@ -0,0 +1,68 @@
|
|
1
|
+
import { SLogger, UtilFT, UtilFunc, throwError } from "@zwa73/utils";
|
2
|
+
import { Command, OptionValues } from "commander";
|
3
|
+
import * as path from 'path';
|
4
|
+
|
5
|
+
|
6
|
+
/**npm镜像源 */
|
7
|
+
export const MIRROR_SOURCE = "https://registry.npmmirror.com/";
|
8
|
+
/**npm官方源 */
|
9
|
+
export const OFFICIAL_SOURCE = "https://registry.npmjs.org/";
|
10
|
+
/**data文件夹路径 */
|
11
|
+
export const DATA_PATH = path.join(__dirname,'..','data');
|
12
|
+
/**项目路径 */
|
13
|
+
export const PROJECT_PATH = process.cwd();
|
14
|
+
|
15
|
+
/** 检测非浮点的整数类型 */
|
16
|
+
export function parseInteger(value: string): number {
|
17
|
+
const num = Number(value);
|
18
|
+
if(!(typeof num === 'number' && isFinite(num) && Math.floor(num) === num))
|
19
|
+
throwError(`${value} 不是有效的整数数字`);
|
20
|
+
return num;
|
21
|
+
}
|
22
|
+
|
23
|
+
/** 检测任意数字类型 */
|
24
|
+
export function parseNumber(value: string):number {
|
25
|
+
const num = Number(value);
|
26
|
+
if(!(typeof value === 'number' && isFinite(value)))
|
27
|
+
throwError(`${value} 不是有效的数字`);
|
28
|
+
return num;
|
29
|
+
}
|
30
|
+
|
31
|
+
/** 检测布尔类型 */
|
32
|
+
export function parseBoolean(value: string): boolean {
|
33
|
+
return value? true:false;
|
34
|
+
}
|
35
|
+
|
36
|
+
|
37
|
+
/**在镜像源内镜像操作 */
|
38
|
+
export async function withMirrorSource(func:(()=>any)){
|
39
|
+
const out = await UtilFunc.exec('npm config get registry');
|
40
|
+
const OriginSource = out.stdout.trim()//?
|
41
|
+
SLogger.info(`正在修改npm源为 镜像源: ${MIRROR_SOURCE}`);
|
42
|
+
await UtilFunc.exec(`npm config set registry ${MIRROR_SOURCE}`);
|
43
|
+
SLogger.info(`成功修改npm源为 镜像源: ${MIRROR_SOURCE}`);
|
44
|
+
await func();
|
45
|
+
SLogger.info(`正在修改npm源为 原始源: ${OriginSource}`);
|
46
|
+
await UtilFunc.exec(`npm config set registry ${OriginSource}`);
|
47
|
+
SLogger.info(`成功修改npm源为 原始源: ${OriginSource}`);
|
48
|
+
}
|
49
|
+
|
50
|
+
/**在官方源内镜像操作 */
|
51
|
+
export async function withOfficialSource(func:(()=>any)){
|
52
|
+
const out = await UtilFunc.exec('npm config get registry');
|
53
|
+
const OriginSource = out.stdout.trim()//?
|
54
|
+
SLogger.info(`正在修改npm源为 官方源: ${OFFICIAL_SOURCE}`);
|
55
|
+
await UtilFunc.exec(`npm config set registry ${OFFICIAL_SOURCE}`);
|
56
|
+
SLogger.info(`成功修改npm源为 官方源: ${OFFICIAL_SOURCE}`);
|
57
|
+
await func();
|
58
|
+
SLogger.info(`正在修改npm源为 原始源: ${OriginSource}`);
|
59
|
+
await UtilFunc.exec(`npm config set registry ${OriginSource}`);
|
60
|
+
SLogger.info(`成功修改npm源为 原始源: ${OriginSource}`);
|
61
|
+
}
|
62
|
+
|
63
|
+
/**检查目录是否为项目 */
|
64
|
+
export async function checkProject() {
|
65
|
+
const filePath = path.join(PROJECT_PATH,"package.json");
|
66
|
+
if(!(await UtilFT.pathExists(filePath)))
|
67
|
+
throwError(`当前目录: ${PROJECT_PATH} 不是npm项目目录, 请先使用 npm init`);
|
68
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './Route';
|
package/src/UtilSchemaTool.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import * as TJS from 'typescript-json-schema';
|
2
2
|
import * as path from 'path';
|
3
|
-
import { JObject, UtilFT, UtilFunc } from '@zwa73/utils';
|
3
|
+
import { JObject, SLogger, UtilFT, UtilFunc } from '@zwa73/utils';
|
4
4
|
|
5
5
|
export namespace UtilST{
|
6
6
|
/**构造Schema
|
@@ -11,9 +11,10 @@ export namespace UtilST{
|
|
11
11
|
*/
|
12
12
|
export async function builAllSchema(configPath:string,outDir:string,coverDefine:JObject={}){
|
13
13
|
outDir = path.join(outDir,"schemas.json");
|
14
|
-
const
|
15
|
-
|
16
|
-
|
14
|
+
const {stderr,stdout} = await UtilFunc.exec(`typescript-json-schema ${configPath} * --out ${outDir} --required --strictNullChecks --aliasRefs`);
|
15
|
+
if(stderr) SLogger.error(stderr);
|
16
|
+
else SLogger.info(stdout);
|
17
|
+
const schema = (await UtilFT.loadJSONFile(outDir)) as any;
|
17
18
|
//进行预处理并展开
|
18
19
|
await expandSchema(outDir,schema,coverDefine);
|
19
20
|
}
|
package/src/index.ts
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
export * from './UtilSchemaTool';
|
1
|
+
export * from './UtilSchemaTool';
|
2
|
+
export * from './Command';
|
package/tsconfig.json
CHANGED
package/dist/Schema.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
"use strict";
|
package/schema/schemas.json
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"$schema": "http://json-schema.org/draft-07/schema#",
|
3
|
-
"definitions": {
|
4
|
-
"TestType2": {
|
5
|
-
"type": "object",
|
6
|
-
"properties": {
|
7
|
-
"a": {
|
8
|
-
"type": "string"
|
9
|
-
},
|
10
|
-
"b": {
|
11
|
-
"type": "number"
|
12
|
-
}
|
13
|
-
},
|
14
|
-
"required": [
|
15
|
-
"a",
|
16
|
-
"b"
|
17
|
-
]
|
18
|
-
},
|
19
|
-
"TestType1": {
|
20
|
-
"type": "object",
|
21
|
-
"properties": {
|
22
|
-
"a": {
|
23
|
-
"type": "string"
|
24
|
-
},
|
25
|
-
"b": {
|
26
|
-
"type": "number"
|
27
|
-
}
|
28
|
-
},
|
29
|
-
"required": [
|
30
|
-
"a",
|
31
|
-
"b"
|
32
|
-
]
|
33
|
-
}
|
34
|
-
}
|
35
|
-
}
|
File without changes
|