@zwa73/dev-utils 1.0.14 → 1.0.16
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/data/scripts/compile.ps1 +5 -5
- package/data/scripts/watch.ps1 +10 -10
- package/dist/Command/Init.js +1 -1
- package/dist/Command/MapPath.d.ts +3 -0
- package/dist/Command/MapPath.js +60 -0
- package/dist/Command/Node.d.ts +1 -1
- package/dist/Command/Node.js +1 -1
- package/dist/Command/Release.d.ts +1 -1
- package/dist/Command/Release.js +2 -2
- package/dist/Command/Route.js +3 -1
- package/dist/Command/RouteInterface.js +1 -1
- package/dist/UtilSchemaTool.d.ts +14 -4
- package/dist/UtilSchemaTool.js +8 -6
- package/package.json +3 -3
- package/src/Command/Init.ts +4 -4
- package/src/Command/MapPath.ts +56 -0
- package/src/Command/Node.ts +3 -4
- package/src/Command/Release.ts +8 -7
- package/src/Command/Route.ts +6 -4
- package/src/Command/RouteInterface.ts +1 -2
- package/src/UtilSchemaTool.ts +17 -6
- package/test.js +29 -0
package/data/scripts/compile.ps1
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
Write-Output 开始删除原dist
|
2
|
-
Remove-Item -Recurse -Force dist
|
3
|
-
Write-Output 开始编译
|
4
|
-
tsc
|
5
|
-
tsc-alias
|
1
|
+
Write-Output 开始删除原dist
|
2
|
+
Remove-Item -Recurse -Force dist
|
3
|
+
Write-Output 开始编译
|
4
|
+
tsc -p tsconfig.compile.json
|
5
|
+
tsc-alias -p tsconfig.compile.json
|
package/data/scripts/watch.ps1
CHANGED
@@ -1,10 +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"
|
10
|
-
New-Script "tsc-alias -w"
|
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"
|
package/dist/Command/Init.js
CHANGED
@@ -24,9 +24,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
24
24
|
};
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
26
26
|
exports.CmdInit = void 0;
|
27
|
+
const fs = __importStar(require("fs"));
|
27
28
|
const path = __importStar(require("path"));
|
28
29
|
const utils_1 = require("@zwa73/utils");
|
29
|
-
const fs = __importStar(require("fs"));
|
30
30
|
const RouteInterface_1 = require("./RouteInterface");
|
31
31
|
/**复制基础文件 */
|
32
32
|
async function copyData() {
|
@@ -0,0 +1,60 @@
|
|
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.CmdMapPath = void 0;
|
7
|
+
const utils_1 = require("@zwa73/utils");
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
9
|
+
const path_1 = __importDefault(require("path"));
|
10
|
+
const DupMethodList = ["no-rename", "overwrite", "move"];
|
11
|
+
/**重命名文件或路径 */
|
12
|
+
const CmdMapPath = (program) => program
|
13
|
+
.command("mappath")
|
14
|
+
.description("根据正则表达式对文件名进行映射")
|
15
|
+
.argument("<regex>", "要匹配的正则表达式")
|
16
|
+
.argument("<replacement>", "替换字符串")
|
17
|
+
.option("-e, --exclude <regex>", "排除文件的正则表达式")
|
18
|
+
.option(`-d, --duplicate-handling <${DupMethodList.join('|')}>`, "处理重名文件的方式", "no-rename")
|
19
|
+
.option("-do, --duplicate-output <dir>", "移动重命名的重名文件的目录", "Duplicates")
|
20
|
+
.option("-r, --recursive", "是否处理子目录", false)
|
21
|
+
.option("-c, --copy", "复制而不是重命名文件", false)
|
22
|
+
.action(async (regexStr, replacement, options) => {
|
23
|
+
const regex = new RegExp(regexStr);
|
24
|
+
const excludeRegex = options.exclude ? new RegExp(options.exclude) : null;
|
25
|
+
if (!DupMethodList.includes(options.duplicateHandling))
|
26
|
+
(0, utils_1.throwError)(`${options.duplicateHandling} 不是有效的 duplicate-handling`);
|
27
|
+
const duplicateHandling = options.duplicateHandling;
|
28
|
+
const basePath = process.cwd();
|
29
|
+
// 遍历当前目录下的所有文件
|
30
|
+
const filePaths = utils_1.UtilFT.fileSearchRegex(basePath, regex.source, { relative: options.recursive, normalize: "posix" })
|
31
|
+
.map((filePath) => path_1.default.relative(basePath, filePath))
|
32
|
+
.filter((filePath) => excludeRegex ? (!excludeRegex.test(filePath)) : true);
|
33
|
+
//对单个路径映射
|
34
|
+
const mapPath = async (source, target) => {
|
35
|
+
const dir = path_1.default.parse(target).dir;
|
36
|
+
await utils_1.UtilFT.ensurePathExists(dir, { dir: true });
|
37
|
+
if (options.copy)
|
38
|
+
await fs_1.default.promises.copyFile(source, target);
|
39
|
+
else
|
40
|
+
await fs_1.default.promises.rename(source, target);
|
41
|
+
};
|
42
|
+
for (const filePath of filePaths) {
|
43
|
+
// 重命名文件
|
44
|
+
const newFilePath = filePath.replace(regex, replacement);
|
45
|
+
// 如果文件名发生了变化
|
46
|
+
if (newFilePath === filePath)
|
47
|
+
continue;
|
48
|
+
//如果文件已存在
|
49
|
+
if (await utils_1.UtilFT.pathExists(newFilePath)) {
|
50
|
+
await (0, utils_1.matchProc)(duplicateHandling, {
|
51
|
+
'move': () => mapPath(filePath, path_1.default.join(options.duplicateOutput, newFilePath)),
|
52
|
+
'no-rename': () => utils_1.SLogger.info(`重名文件存在,跳过:${newFilePath}`),
|
53
|
+
'overwrite': () => mapPath(filePath, newFilePath),
|
54
|
+
});
|
55
|
+
}
|
56
|
+
else
|
57
|
+
await mapPath(filePath, newFilePath);
|
58
|
+
}
|
59
|
+
});
|
60
|
+
exports.CmdMapPath = CmdMapPath;
|
package/dist/Command/Node.d.ts
CHANGED
package/dist/Command/Node.js
CHANGED
@@ -5,7 +5,7 @@ const utils_1 = require("@zwa73/utils");
|
|
5
5
|
/**编译并运行某个文件 */
|
6
6
|
const CmdNode = (program) => program
|
7
7
|
.command('node <path>')
|
8
|
-
.option('-p, --project
|
8
|
+
.option('-p, --project <path>', 'tsconfig的目录')
|
9
9
|
.description('编译并运行某个文件')
|
10
10
|
.action(async (arg, opt) => {
|
11
11
|
utils_1.SLogger.info(`编译并运行: ${arg}`);
|
package/dist/Command/Release.js
CHANGED
@@ -49,8 +49,8 @@ async function updateVersion(newVersion) {
|
|
49
49
|
const CmdRelease = (program) => program
|
50
50
|
.command("release")
|
51
51
|
.description("更新版本号并发布包")
|
52
|
-
.option("-v, --version
|
53
|
-
.option("-a, --access
|
52
|
+
.option("-v, --version <version>", "指定发布的版本号 `${number}.${number}.${number}`")
|
53
|
+
.option("-a, --access <access>", "npm publish 的 access 参数", "public")
|
54
54
|
.action(async (opt) => {
|
55
55
|
(0, RouteInterface_1.checkProject)();
|
56
56
|
utils_1.SLogger.info(`开始发布项目`);
|
package/dist/Command/Route.js
CHANGED
@@ -2,13 +2,15 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.cliRoute = void 0;
|
4
4
|
const commander_1 = require("commander");
|
5
|
-
const Node_1 = require("./Node");
|
6
5
|
const Init_1 = require("./Init");
|
6
|
+
const Node_1 = require("./Node");
|
7
7
|
const Release_1 = require("./Release");
|
8
|
+
const MapPath_1 = require("./MapPath");
|
8
9
|
async function cliRoute() {
|
9
10
|
(0, Init_1.CmdInit)(commander_1.program);
|
10
11
|
(0, Node_1.CmdNode)(commander_1.program);
|
11
12
|
(0, Release_1.CmdRelease)(commander_1.program);
|
13
|
+
(0, MapPath_1.CmdMapPath)(commander_1.program);
|
12
14
|
commander_1.program.parse(process.argv);
|
13
15
|
}
|
14
16
|
exports.cliRoute = cliRoute;
|
@@ -24,8 +24,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
24
24
|
};
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
26
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
27
|
const path = __importStar(require("path"));
|
28
|
+
const utils_1 = require("@zwa73/utils");
|
29
29
|
/**npm镜像源 */
|
30
30
|
exports.MIRROR_SOURCE = "https://registry.npmmirror.com/";
|
31
31
|
/**npm官方源 */
|
package/dist/UtilSchemaTool.d.ts
CHANGED
@@ -6,13 +6,23 @@ export declare namespace UtilST {
|
|
6
6
|
* @param outDir - schema文件夹路径 如 ./schema/
|
7
7
|
* @param coverDefine - 将会覆盖 definitions 对应内容的表
|
8
8
|
*/
|
9
|
-
function builAllSchema(configPath: string, outDir: string, coverDefine?: JObject): Promise<void>;
|
9
|
+
export function builAllSchema(configPath: string, outDir: string, coverDefine?: JObject): Promise<void>;
|
10
|
+
/**buildMatchSchema可选项 */
|
11
|
+
type BuildMatchSchemaOpt = Partial<{
|
12
|
+
/**忽略某些glob */
|
13
|
+
ignore?: string | string[];
|
14
|
+
/** */
|
15
|
+
coverDefine: JObject;
|
16
|
+
}>;
|
10
17
|
/**生成匹配的文件的所有type的schema
|
11
18
|
* @async
|
19
|
+
* @param dir - 目录
|
12
20
|
* @param globPattern - glob匹配符
|
13
21
|
* @param outDir - schema输出路径目录 如 ./schema/
|
14
|
-
* @param
|
15
|
-
* @param
|
22
|
+
* @param opt - 可选参数
|
23
|
+
* @param opt.ignore - glob忽略匹配符
|
24
|
+
* @param opt.coverDefine - 将会覆盖 definitions 对应内容的表
|
16
25
|
*/
|
17
|
-
function buildMatchSchema(globPattern: string | string[], outDir: string,
|
26
|
+
export function buildMatchSchema(dir: string, globPattern: string | string[], outDir: string, opt?: BuildMatchSchemaOpt): Promise<void>;
|
27
|
+
export {};
|
18
28
|
}
|
package/dist/UtilSchemaTool.js
CHANGED
@@ -24,8 +24,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
24
24
|
};
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
26
26
|
exports.UtilST = void 0;
|
27
|
-
const TJS = __importStar(require("typescript-json-schema"));
|
28
27
|
const path = __importStar(require("path"));
|
28
|
+
const TJS = __importStar(require("typescript-json-schema"));
|
29
29
|
const utils_1 = require("@zwa73/utils");
|
30
30
|
var UtilST;
|
31
31
|
(function (UtilST) {
|
@@ -45,12 +45,14 @@ var UtilST;
|
|
45
45
|
UtilST.builAllSchema = builAllSchema;
|
46
46
|
/**生成匹配的文件的所有type的schema
|
47
47
|
* @async
|
48
|
+
* @param dir - 目录
|
48
49
|
* @param globPattern - glob匹配符
|
49
50
|
* @param outDir - schema输出路径目录 如 ./schema/
|
50
|
-
* @param
|
51
|
-
* @param
|
51
|
+
* @param opt - 可选参数
|
52
|
+
* @param opt.ignore - glob忽略匹配符
|
53
|
+
* @param opt.coverDefine - 将会覆盖 definitions 对应内容的表
|
52
54
|
*/
|
53
|
-
async function buildMatchSchema(globPattern, outDir,
|
55
|
+
async function buildMatchSchema(dir, globPattern, outDir, opt) {
|
54
56
|
//生成
|
55
57
|
const settings = {
|
56
58
|
required: true,
|
@@ -59,13 +61,13 @@ var UtilST;
|
|
59
61
|
const compilerOptions = {
|
60
62
|
strictNullChecks: true,
|
61
63
|
};
|
62
|
-
const files = utils_1.UtilFT.fileSearchGlob(globPattern, ignore);
|
64
|
+
const files = utils_1.UtilFT.fileSearchGlob(dir, globPattern, { ingore: opt?.ignore });
|
63
65
|
const program = TJS.getProgramFromFiles(files, compilerOptions);
|
64
66
|
const schema = TJS.generateSchema(program, "*", settings);
|
65
67
|
outDir = outDir ?? path.join(process.cwd(), 'schema');
|
66
68
|
const schemasPath = path.join(outDir, 'schemas.json');
|
67
69
|
//进行预处理并展开
|
68
|
-
await expandSchema(schemasPath, schema, coverDefine);
|
70
|
+
await expandSchema(schemasPath, schema, opt?.coverDefine ?? {});
|
69
71
|
}
|
70
72
|
UtilST.buildMatchSchema = buildMatchSchema;
|
71
73
|
/**展开schema以供使用 */
|
package/package.json
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
{
|
2
2
|
"name": "@zwa73/dev-utils",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.16",
|
4
4
|
"description": "编译与调试工具",
|
5
5
|
"main": "index.js",
|
6
6
|
"scripts": {
|
7
|
-
"test": "
|
7
|
+
"test": "jest",
|
8
8
|
"release": "powershell scripts/release",
|
9
9
|
"compile": "powershell scripts/compile",
|
10
10
|
"watch": "powershell scripts/watch"
|
@@ -15,7 +15,7 @@
|
|
15
15
|
"author": "zwa73",
|
16
16
|
"license": "ISC",
|
17
17
|
"dependencies": {
|
18
|
-
"@zwa73/utils": "^1.0.
|
18
|
+
"@zwa73/utils": "^1.0.109",
|
19
19
|
"commander": "^11.1.0",
|
20
20
|
"ts-node": "^10.9.2",
|
21
21
|
"tsconfig-paths": "^4.2.0",
|
package/src/Command/Init.ts
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
import * as path from 'path';
|
2
|
-
import { SLogger, UtilFT, UtilFunc } from '@zwa73/utils';
|
3
|
-
import * as fs from 'fs';
|
4
|
-
import { DATA_PATH, MIRROR_SOURCE, PROJECT_PATH, checkProject } from './RouteInterface';
|
5
1
|
import { Command } from 'commander';
|
2
|
+
import * as fs from 'fs';
|
3
|
+
import * as path from 'path';
|
6
4
|
|
5
|
+
import { SLogger, UtilFT, UtilFunc } from '@zwa73/utils';
|
7
6
|
|
7
|
+
import { checkProject, DATA_PATH, MIRROR_SOURCE, PROJECT_PATH } from './RouteInterface';
|
8
8
|
|
9
9
|
/**复制基础文件 */
|
10
10
|
async function copyData() {
|
@@ -0,0 +1,56 @@
|
|
1
|
+
import { SLogger, UtilFT, matchProc, throwError } from "@zwa73/utils";
|
2
|
+
import { Command } from "commander";
|
3
|
+
import fs from "fs";
|
4
|
+
import path from "path";
|
5
|
+
|
6
|
+
const DupMethodList = ["no-rename","overwrite","move"] as const;
|
7
|
+
type DupMethod = typeof DupMethodList[number];
|
8
|
+
|
9
|
+
/**重命名文件或路径 */
|
10
|
+
export const CmdMapPath = (program: Command) => program
|
11
|
+
.command("mappath")
|
12
|
+
.description("根据正则表达式对文件名进行映射")
|
13
|
+
.argument("<regex>", "要匹配的正则表达式")
|
14
|
+
.argument("<replacement>", "替换字符串")
|
15
|
+
.option("-e, --exclude <regex>", "排除文件的正则表达式")
|
16
|
+
.option(`-d, --duplicate-handling <${DupMethodList.join('|')}>`, "处理重名文件的方式", "no-rename")
|
17
|
+
.option("-do, --duplicate-output <dir>", "移动重命名的重名文件的目录", "Duplicates")
|
18
|
+
.option("-r, --recursive", "是否处理子目录", false)
|
19
|
+
.option("-c, --copy", "复制而不是重命名文件", false)
|
20
|
+
.action(async (regexStr, replacement, options) => {
|
21
|
+
const regex = new RegExp(regexStr);
|
22
|
+
const excludeRegex = options.exclude ? new RegExp(options.exclude) : null;
|
23
|
+
if(!DupMethodList.includes(options.duplicateHandling))
|
24
|
+
throwError(`${options.duplicateHandling} 不是有效的 duplicate-handling`);
|
25
|
+
const duplicateHandling = options.duplicateHandling as DupMethod;
|
26
|
+
const basePath = process.cwd();
|
27
|
+
// 遍历当前目录下的所有文件
|
28
|
+
const filePaths = UtilFT.fileSearchRegex(basePath, regex.source,{relative:options.recursive,normalize:"posix"})
|
29
|
+
.map((filePath)=>path.relative(basePath, filePath))
|
30
|
+
.filter((filePath)=>excludeRegex ? (!excludeRegex.test(filePath)) : true);
|
31
|
+
|
32
|
+
//对单个路径映射
|
33
|
+
const mapPath = async (source:string,target:string)=>{
|
34
|
+
const dir = path.parse(target).dir;
|
35
|
+
await UtilFT.ensurePathExists(dir,{dir:true});
|
36
|
+
if (options.copy)
|
37
|
+
await fs.promises.copyFile(source, target);
|
38
|
+
else
|
39
|
+
await fs.promises.rename(source, target);
|
40
|
+
}
|
41
|
+
|
42
|
+
for(const filePath of filePaths){
|
43
|
+
// 重命名文件
|
44
|
+
const newFilePath = filePath.replace(regex, replacement);
|
45
|
+
// 如果文件名发生了变化
|
46
|
+
if (newFilePath === filePath) continue;
|
47
|
+
//如果文件已存在
|
48
|
+
if(await UtilFT.pathExists(newFilePath)){
|
49
|
+
await matchProc(duplicateHandling,{
|
50
|
+
'move': ()=> mapPath(filePath, path.join(options.duplicateOutput, newFilePath)),
|
51
|
+
'no-rename': ()=> SLogger.info(`重名文件存在,跳过:${newFilePath}`),
|
52
|
+
'overwrite': ()=> mapPath(filePath,newFilePath),
|
53
|
+
})
|
54
|
+
} else await mapPath(filePath,newFilePath);
|
55
|
+
}
|
56
|
+
});
|
package/src/Command/Node.ts
CHANGED
@@ -1,12 +1,11 @@
|
|
1
|
-
import {
|
2
|
-
import { Command } from "commander";
|
3
|
-
|
1
|
+
import { Command } from 'commander';
|
4
2
|
|
3
|
+
import { SLogger, UtilFunc } from '@zwa73/utils';
|
5
4
|
|
6
5
|
/**编译并运行某个文件 */
|
7
6
|
export const CmdNode = (program:Command)=>program
|
8
7
|
.command('node <path>')
|
9
|
-
.option('-p, --project
|
8
|
+
.option('-p, --project <path>','tsconfig的目录')
|
10
9
|
.description('编译并运行某个文件')
|
11
10
|
.action(async (arg, opt) => {
|
12
11
|
SLogger.info(`编译并运行: ${arg}`);
|
package/src/Command/Release.ts
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
-
import { Command } from
|
2
|
-
import
|
3
|
-
|
4
|
-
import { SLogger, UtilFT, UtilFunc
|
5
|
-
|
1
|
+
import { Command } from 'commander';
|
2
|
+
import path from 'path';
|
3
|
+
|
4
|
+
import { SLogger, throwError, UtilFT, UtilFunc } from '@zwa73/utils';
|
5
|
+
|
6
|
+
import { checkProject, OFFICIAL_SOURCE, PROJECT_PATH } from './RouteInterface';
|
6
7
|
|
7
8
|
/**解析版本号为number数组 */
|
8
9
|
function parseVersion(version: string){
|
@@ -45,8 +46,8 @@ async function updateVersion(newVersion?: string): Promise<string> {
|
|
45
46
|
export const CmdRelease = (program: Command) =>program
|
46
47
|
.command("release")
|
47
48
|
.description("更新版本号并发布包")
|
48
|
-
.option("-v, --version
|
49
|
-
.option("-a, --access
|
49
|
+
.option("-v, --version <version>", "指定发布的版本号 `${number}.${number}.${number}`")
|
50
|
+
.option("-a, --access <access>", "npm publish 的 access 参数","public")
|
50
51
|
.action(async (opt) => {
|
51
52
|
checkProject();
|
52
53
|
SLogger.info(`开始发布项目`);
|
package/src/Command/Route.ts
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
import { program } from
|
2
|
-
import { CmdNode } from "./Node";
|
3
|
-
import { CmdInit } from "./Init";
|
4
|
-
import { CmdRelease } from "./Release";
|
1
|
+
import { program } from 'commander';
|
5
2
|
|
3
|
+
import { CmdInit } from './Init';
|
4
|
+
import { CmdNode } from './Node';
|
5
|
+
import { CmdRelease } from './Release';
|
6
|
+
import { CmdMapPath } from './MapPath';
|
6
7
|
|
7
8
|
export async function cliRoute(){
|
8
9
|
CmdInit(program);
|
9
10
|
CmdNode(program);
|
10
11
|
CmdRelease(program);
|
12
|
+
CmdMapPath(program);
|
11
13
|
program.parse(process.argv);
|
12
14
|
}
|
@@ -1,7 +1,6 @@
|
|
1
|
-
import { SLogger, UtilFT, UtilFunc, sleep, throwError } from "@zwa73/utils";
|
2
|
-
import { Command, OptionValues } from "commander";
|
3
1
|
import * as path from 'path';
|
4
2
|
|
3
|
+
import { SLogger, throwError, UtilFT, UtilFunc } from '@zwa73/utils';
|
5
4
|
|
6
5
|
/**npm镜像源 */
|
7
6
|
export const MIRROR_SOURCE = "https://registry.npmmirror.com/";
|
package/src/UtilSchemaTool.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
import * as TJS from 'typescript-json-schema';
|
2
1
|
import * as path from 'path';
|
2
|
+
import * as TJS from 'typescript-json-schema';
|
3
|
+
|
3
4
|
import { JObject, SLogger, UtilFT, UtilFunc } from '@zwa73/utils';
|
4
5
|
|
5
6
|
export namespace UtilST{
|
@@ -16,14 +17,24 @@ export async function builAllSchema(configPath:string,outDir:string,coverDefine:
|
|
16
17
|
//进行预处理并展开
|
17
18
|
await expandSchema(outDir,schema,coverDefine);
|
18
19
|
}
|
20
|
+
|
21
|
+
/**buildMatchSchema可选项 */
|
22
|
+
type BuildMatchSchemaOpt = Partial<{
|
23
|
+
/**忽略某些glob */
|
24
|
+
ignore?:string|string[],
|
25
|
+
/** */
|
26
|
+
coverDefine:JObject,
|
27
|
+
}>
|
19
28
|
/**生成匹配的文件的所有type的schema
|
20
29
|
* @async
|
30
|
+
* @param dir - 目录
|
21
31
|
* @param globPattern - glob匹配符
|
22
32
|
* @param outDir - schema输出路径目录 如 ./schema/
|
23
|
-
* @param
|
24
|
-
* @param
|
33
|
+
* @param opt - 可选参数
|
34
|
+
* @param opt.ignore - glob忽略匹配符
|
35
|
+
* @param opt.coverDefine - 将会覆盖 definitions 对应内容的表
|
25
36
|
*/
|
26
|
-
export async function buildMatchSchema(globPattern:string|string[],outDir:string,
|
37
|
+
export async function buildMatchSchema(dir:string,globPattern:string|string[],outDir:string,opt?:BuildMatchSchemaOpt){
|
27
38
|
//生成
|
28
39
|
const settings: TJS.PartialArgs = {
|
29
40
|
required: true,
|
@@ -32,7 +43,7 @@ export async function buildMatchSchema(globPattern:string|string[],outDir:string
|
|
32
43
|
const compilerOptions: TJS.CompilerOptions = {
|
33
44
|
strictNullChecks: true,
|
34
45
|
};
|
35
|
-
const files = UtilFT.fileSearchGlob(globPattern,ignore);
|
46
|
+
const files = UtilFT.fileSearchGlob(dir,globPattern,{ingore:opt?.ignore});
|
36
47
|
const program = TJS.getProgramFromFiles(
|
37
48
|
files,
|
38
49
|
compilerOptions,
|
@@ -41,7 +52,7 @@ export async function buildMatchSchema(globPattern:string|string[],outDir:string
|
|
41
52
|
outDir = outDir??path.join(process.cwd(),'schema');
|
42
53
|
const schemasPath = path.join(outDir,'schemas.json');
|
43
54
|
//进行预处理并展开
|
44
|
-
await expandSchema(schemasPath,schema,coverDefine);
|
55
|
+
await expandSchema(schemasPath,schema,opt?.coverDefine??{});
|
45
56
|
}
|
46
57
|
/**展开schema以供使用 */
|
47
58
|
async function expandSchema(schemasPath:string,schema:any,coverDefine:JObject){
|
package/test.js
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
const {UtilFT} = require('@zwa73/utils')
|
10
|
+
const fs = require('fs');
|
11
|
+
const path = require('path');
|
12
|
+
async function main(){
|
13
|
+
const root = __filename;
|
14
|
+
const dirpath = path.join(__dirname,'src');
|
15
|
+
|
16
|
+
const ts = await fs.promises.readdir(dirpath,{withFileTypes:true})
|
17
|
+
ts//?
|
18
|
+
const list = await UtilFT.fileSearchRegex(dirpath, '')
|
19
|
+
.map((filePath)=>path.relative(dirpath, filePath));
|
20
|
+
|
21
|
+
console.log(
|
22
|
+
path.parse(root),
|
23
|
+
path.basename(root)
|
24
|
+
);
|
25
|
+
list.forEach((file)=>{
|
26
|
+
console.log(file)
|
27
|
+
})
|
28
|
+
}
|
29
|
+
main();
|