@zwa73/dev-utils 1.0.3
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/dist/Schema.d.ts +0 -0
- package/dist/Schema.js +1 -0
- package/dist/UtilSchemaTool.d.ts +18 -0
- package/dist/UtilSchemaTool.js +88 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/jest.config.js +16 -0
- package/package.json +29 -0
- package/release.bat +2 -0
- package/release.js +16 -0
- package/schema/TestType1.schema.json +4 -0
- package/schema/TestType2.schema.json +4 -0
- package/schema/schemas.json +35 -0
- package/src/UtilSchemaTool.ts +85 -0
- package/src/index.ts +1 -0
- package/tsconfig.compile.json +4 -0
- package/tsconfig.json +20 -0
package/dist/Schema.d.ts
ADDED
File without changes
|
package/dist/Schema.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import { JObject } from '@zwa73/utils';
|
2
|
+
export declare namespace UtilST {
|
3
|
+
/**构造Schema
|
4
|
+
* @async
|
5
|
+
* @param configPath - tsconfig路径
|
6
|
+
* @param outDir - schema文件夹路径 如 ./schema/
|
7
|
+
* @param coverDefine - 将会覆盖 definitions 对应内容的表
|
8
|
+
*/
|
9
|
+
function builAllSchema(configPath: string, outDir: string, coverDefine?: JObject): Promise<void>;
|
10
|
+
/**生成匹配的文件的所有type的schema
|
11
|
+
* @async
|
12
|
+
* @param globPattern - glob匹配符
|
13
|
+
* @param outDir - schema输出路径目录 如 ./schema/
|
14
|
+
* @param ignore - glob忽略匹配符
|
15
|
+
* @param coverDefine - 将会覆盖 definitions 对应内容的表
|
16
|
+
*/
|
17
|
+
function buildMatchSchema(globPattern: string | string[], outDir: string, ignore?: string | string[], coverDefine?: JObject): Promise<void>;
|
18
|
+
}
|
@@ -0,0 +1,88 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.UtilST = void 0;
|
4
|
+
const TJS = require("typescript-json-schema");
|
5
|
+
const path = require("path");
|
6
|
+
const utils_1 = require("@zwa73/utils");
|
7
|
+
var UtilST;
|
8
|
+
(function (UtilST) {
|
9
|
+
/**构造Schema
|
10
|
+
* @async
|
11
|
+
* @param configPath - tsconfig路径
|
12
|
+
* @param outDir - schema文件夹路径 如 ./schema/
|
13
|
+
* @param coverDefine - 将会覆盖 definitions 对应内容的表
|
14
|
+
*/
|
15
|
+
async function builAllSchema(configPath, outDir, coverDefine = {}) {
|
16
|
+
outDir = path.join(outDir, "schemas.json");
|
17
|
+
const log = await utils_1.UtilFunc.exec(`typescript-json-schema ${configPath} * --out ${outDir} --required --strictNullChecks --aliasRefs`);
|
18
|
+
console.log(log);
|
19
|
+
const schema = (await utils_1.UtilFT.loadJSONFileSync(outDir));
|
20
|
+
//进行预处理并展开
|
21
|
+
await expandSchema(outDir, schema, coverDefine);
|
22
|
+
}
|
23
|
+
UtilST.builAllSchema = builAllSchema;
|
24
|
+
/**生成匹配的文件的所有type的schema
|
25
|
+
* @async
|
26
|
+
* @param globPattern - glob匹配符
|
27
|
+
* @param outDir - schema输出路径目录 如 ./schema/
|
28
|
+
* @param ignore - glob忽略匹配符
|
29
|
+
* @param coverDefine - 将会覆盖 definitions 对应内容的表
|
30
|
+
*/
|
31
|
+
async function buildMatchSchema(globPattern, outDir, ignore, coverDefine = {}) {
|
32
|
+
//生成
|
33
|
+
const settings = {
|
34
|
+
required: true,
|
35
|
+
aliasRef: true,
|
36
|
+
};
|
37
|
+
const compilerOptions = {
|
38
|
+
strictNullChecks: true,
|
39
|
+
};
|
40
|
+
const files = utils_1.UtilFT.fileSearchGlob(globPattern, ignore);
|
41
|
+
const program = TJS.getProgramFromFiles(files, compilerOptions);
|
42
|
+
const schema = TJS.generateSchema(program, "*", settings);
|
43
|
+
outDir = outDir ?? path.join(process.cwd(), 'schema');
|
44
|
+
const schemasPath = path.join(outDir, 'schemas.json');
|
45
|
+
//进行预处理并展开
|
46
|
+
await expandSchema(schemasPath, schema, coverDefine);
|
47
|
+
}
|
48
|
+
UtilST.buildMatchSchema = buildMatchSchema;
|
49
|
+
/**展开schema以供使用 */
|
50
|
+
async function expandSchema(schemasPath, schema, coverDefine) {
|
51
|
+
//覆盖
|
52
|
+
coverObj(schema["definitions"], coverDefine);
|
53
|
+
//替换SchemaString标识符
|
54
|
+
schema = JSON.parse(JSON.stringify(schema).replace(/\^\.\*SchemaString\$/g, '^.*$'));
|
55
|
+
const plist = [];
|
56
|
+
plist.push(utils_1.UtilFT.writeJSONFile(schemasPath, schema));
|
57
|
+
const definitions = schema["definitions"];
|
58
|
+
//展开定义
|
59
|
+
for (const typeName in definitions) {
|
60
|
+
const schema = definitions[typeName];
|
61
|
+
//展开所有object与忽略检测的类型
|
62
|
+
if (!(schema.type == "object" || schema.type == "array" || schema.type == undefined))
|
63
|
+
continue;
|
64
|
+
if ((/^.+_[0-9]/).test(typeName) || (/^{./).test(typeName))
|
65
|
+
continue;
|
66
|
+
const basename = path.basename(schemasPath);
|
67
|
+
const tpath = path.join(path.dirname(schemasPath), `${typeName}.schema.json`);
|
68
|
+
if (!utils_1.UtilFT.isValidFilePath(tpath))
|
69
|
+
continue;
|
70
|
+
plist.push(utils_1.UtilFT.writeJSONFile(tpath, {
|
71
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
72
|
+
"$ref": `${basename}#/definitions/${typeName}`
|
73
|
+
}));
|
74
|
+
}
|
75
|
+
await Promise.all(plist);
|
76
|
+
}
|
77
|
+
/**覆盖object */
|
78
|
+
function coverObj(base, cover) {
|
79
|
+
for (const k in cover) {
|
80
|
+
const v = cover[k];
|
81
|
+
if (typeof v === "object" && !Array.isArray(v) &&
|
82
|
+
typeof base[k] === "object" && !Array.isArray(base[k]))
|
83
|
+
coverObj(base[k], v);
|
84
|
+
else
|
85
|
+
base[k] = v;
|
86
|
+
}
|
87
|
+
}
|
88
|
+
})(UtilST || (exports.UtilST = UtilST = {}));
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './UtilSchemaTool';
|
package/dist/index.js
ADDED
@@ -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("./UtilSchemaTool"), exports);
|
package/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './dist';
|
package/index.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
module.exports = require("./dist");
|
package/jest.config.js
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
module.exports = {
|
6
|
+
roots: ['./jest'],
|
7
|
+
transform: {
|
8
|
+
'^.+\\.tsx?$': 'ts-jest',
|
9
|
+
},
|
10
|
+
moduleNameMapper: {
|
11
|
+
'^@src/(.*)$': '<rootDir>/dist/$1',
|
12
|
+
'^@/(.*)$': '<rootDir>/$1',
|
13
|
+
'^@$': '<rootDir>/index'
|
14
|
+
},
|
15
|
+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
|
16
|
+
};
|
package/package.json
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
{
|
2
|
+
"name": "@zwa73/dev-utils",
|
3
|
+
"version": "1.0.3",
|
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": "call tsc -p tsconfig.compile.json && call tsc-alias -p tsconfig.compile.json",
|
10
|
+
"watch": "start tsc-alias -w -p tsconfig.compile.json && start tsc -w -p tsconfig.compile.json"
|
11
|
+
},
|
12
|
+
"keywords": [
|
13
|
+
"schema"
|
14
|
+
],
|
15
|
+
"author": "zwa73",
|
16
|
+
"license": "ISC",
|
17
|
+
"dependencies": {
|
18
|
+
"@types/jest": "^29.5.12",
|
19
|
+
"@types/node": "^20.11.19",
|
20
|
+
"@zwa73/utils": "^1.0.89",
|
21
|
+
"jest": "^29.7.0",
|
22
|
+
"ts-jest": "^29.1.2",
|
23
|
+
"typescript-json-schema": "^0.63.0"
|
24
|
+
},
|
25
|
+
"devDependencies": {
|
26
|
+
"tsc-alias": "^1.8.8",
|
27
|
+
"typescript": "^5.3.3"
|
28
|
+
}
|
29
|
+
}
|
package/release.bat
ADDED
package/release.js
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
const fs = require('fs');
|
2
|
+
const path = require('path');
|
3
|
+
|
4
|
+
// 读取并更新包配置文件中的版本号
|
5
|
+
function updateVersion() {
|
6
|
+
const packagePath = path.join(__dirname, 'package.json');
|
7
|
+
const packageData = JSON.parse(fs.readFileSync(packagePath));
|
8
|
+
const version = packageData.version.split('.');
|
9
|
+
version[2] = parseInt(version[2]) + 1;
|
10
|
+
packageData.version = version.join('.');
|
11
|
+
fs.writeFileSync(packagePath, JSON.stringify(packageData, null, 2));
|
12
|
+
return packageData.version;
|
13
|
+
}
|
14
|
+
|
15
|
+
|
16
|
+
updateVersion();
|
@@ -0,0 +1,35 @@
|
|
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
|
+
}
|
@@ -0,0 +1,85 @@
|
|
1
|
+
import * as TJS from 'typescript-json-schema';
|
2
|
+
import * as path from 'path';
|
3
|
+
import { JObject, UtilFT, UtilFunc } from '@zwa73/utils';
|
4
|
+
|
5
|
+
export namespace UtilST{
|
6
|
+
/**构造Schema
|
7
|
+
* @async
|
8
|
+
* @param configPath - tsconfig路径
|
9
|
+
* @param outDir - schema文件夹路径 如 ./schema/
|
10
|
+
* @param coverDefine - 将会覆盖 definitions 对应内容的表
|
11
|
+
*/
|
12
|
+
export async function builAllSchema(configPath:string,outDir:string,coverDefine:JObject={}){
|
13
|
+
outDir = path.join(outDir,"schemas.json");
|
14
|
+
const log = await UtilFunc.exec(`typescript-json-schema ${configPath} * --out ${outDir} --required --strictNullChecks --aliasRefs`);
|
15
|
+
console.log(log);
|
16
|
+
const schema = (await UtilFT.loadJSONFileSync(outDir)) as any;
|
17
|
+
//进行预处理并展开
|
18
|
+
await expandSchema(outDir,schema,coverDefine);
|
19
|
+
}
|
20
|
+
/**生成匹配的文件的所有type的schema
|
21
|
+
* @async
|
22
|
+
* @param globPattern - glob匹配符
|
23
|
+
* @param outDir - schema输出路径目录 如 ./schema/
|
24
|
+
* @param ignore - glob忽略匹配符
|
25
|
+
* @param coverDefine - 将会覆盖 definitions 对应内容的表
|
26
|
+
*/
|
27
|
+
export async function buildMatchSchema(globPattern:string|string[],outDir:string,ignore?:string|string[],coverDefine:JObject={}){
|
28
|
+
//生成
|
29
|
+
const settings: TJS.PartialArgs = {
|
30
|
+
required: true,
|
31
|
+
aliasRef:true,
|
32
|
+
};
|
33
|
+
const compilerOptions: TJS.CompilerOptions = {
|
34
|
+
strictNullChecks: true,
|
35
|
+
};
|
36
|
+
const files = UtilFT.fileSearchGlob(globPattern,ignore);
|
37
|
+
const program = TJS.getProgramFromFiles(
|
38
|
+
files,
|
39
|
+
compilerOptions,
|
40
|
+
);
|
41
|
+
const schema = TJS.generateSchema(program, "*", settings) as any;
|
42
|
+
outDir = outDir??path.join(process.cwd(),'schema');
|
43
|
+
const schemasPath = path.join(outDir,'schemas.json');
|
44
|
+
//进行预处理并展开
|
45
|
+
await expandSchema(schemasPath,schema,coverDefine);
|
46
|
+
}
|
47
|
+
/**展开schema以供使用 */
|
48
|
+
async function expandSchema(schemasPath:string,schema:any,coverDefine:JObject){
|
49
|
+
//覆盖
|
50
|
+
coverObj(schema["definitions"],coverDefine);
|
51
|
+
//替换SchemaString标识符
|
52
|
+
schema = JSON.parse(JSON.stringify(schema).replace(/\^\.\*SchemaString\$/g,'^.*$'));
|
53
|
+
const plist:Promise<void>[] = [];
|
54
|
+
plist.push(UtilFT.writeJSONFile(schemasPath,schema));
|
55
|
+
|
56
|
+
const definitions = schema["definitions"] as Record<string,JObject>;
|
57
|
+
//展开定义
|
58
|
+
for(const typeName in definitions){
|
59
|
+
const schema = definitions[typeName];
|
60
|
+
//展开所有object与忽略检测的类型
|
61
|
+
if(!(schema.type == "object" || schema.type == "array" || schema.type == undefined)) continue;
|
62
|
+
if((/^.+_[0-9]/).test(typeName) || (/^{./).test(typeName)) continue;
|
63
|
+
|
64
|
+
const basename = path.basename(schemasPath);
|
65
|
+
const tpath = path.join(path.dirname(schemasPath),`${typeName}.schema.json`);
|
66
|
+
if(!UtilFT.isValidFilePath(tpath)) continue;
|
67
|
+
plist.push(UtilFT.writeJSONFile(tpath,{
|
68
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
69
|
+
"$ref": `${basename}#/definitions/${typeName}`
|
70
|
+
}));
|
71
|
+
}
|
72
|
+
await Promise.all(plist);
|
73
|
+
}
|
74
|
+
/**覆盖object */
|
75
|
+
function coverObj(base:JObject,cover:JObject){
|
76
|
+
for(const k in cover){
|
77
|
+
const v = cover[k];
|
78
|
+
if( typeof v === "object" && !Array.isArray(v) &&
|
79
|
+
typeof base[k] === "object" && !Array.isArray(base[k]) )
|
80
|
+
coverObj(base[k] as JObject,v as JObject);
|
81
|
+
else
|
82
|
+
base[k] = v;
|
83
|
+
}
|
84
|
+
}
|
85
|
+
}
|
package/src/index.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './UtilSchemaTool';
|
package/tsconfig.json
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"allowJs": true,
|
4
|
+
"strict": true,
|
5
|
+
"target": "ES2022",
|
6
|
+
"module": "commonjs",
|
7
|
+
"outDir": "./dist",
|
8
|
+
"declaration": true,
|
9
|
+
"baseUrl": ".",
|
10
|
+
"emitDecoratorMetadata": true,
|
11
|
+
"experimentalDecorators": true,
|
12
|
+
"paths": {
|
13
|
+
"@src/*": ["./src/*"],
|
14
|
+
"@/*" : ["./*"],
|
15
|
+
"@" : ["./src/index"]
|
16
|
+
}
|
17
|
+
},
|
18
|
+
"include": ["./src/**/*.ts", "./src/**/*.js","./jest/**/*.ts"],
|
19
|
+
"exclude": ["./node_modules/**/*"]
|
20
|
+
}
|