wormajs 0.2.4 → 0.2.6
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/bin/renderer.js +8 -2
- package/dist/constant.js +20 -20
- package/dist/core/WorkerPool.js +5 -3
- package/dist/core/loader/astLoader/generates/tuple.js +1 -1
- package/dist/core/loader/astLoader/index.js +1 -3
- package/dist/core/loader/astLoader/normalize/index.js +1 -1
- package/dist/core/loader/astLoader/normalize/normalizer.js +9 -11
- package/dist/core/loader/astLoader/normalize/rules/mergeAnyOf.js +9 -8
- package/dist/core/loader/astLoader/normalize/rules/normalizeCombiningKeywords.js +6 -6
- package/dist/core/loader/astLoader/normalize/rules/normalizeNullType.js +1 -1
- package/dist/core/loader/astLoader/parsers/const.js +4 -4
- package/dist/core/loader/astLoader/parsers/enum.js +3 -3
- package/dist/core/loader/astLoader/parsers/forward/array.js +3 -3
- package/dist/core/loader/astLoader/parsers/forward/const.js +3 -3
- package/dist/core/loader/astLoader/parsers/forward/enum.js +2 -2
- package/dist/core/loader/astLoader/parsers/forward/group.js +4 -4
- package/dist/core/loader/astLoader/parsers/forward/object.js +4 -4
- package/dist/core/loader/astLoader/parsers/forward/tuple.js +3 -3
- package/dist/core/loader/astLoader/parsers/group.js +1 -1
- package/dist/core/loader/astLoader/parsers/tuple.js +2 -2
- package/dist/core/loader/callingCodeLoader/index.js +1 -3
- package/dist/core/loader/schemaLoader/index.js +1 -3
- package/dist/core/loader/standardLoader/helper.js +1 -1
- package/dist/core/loader/standardLoader/index.js +1 -3
- package/dist/core/parser/openApiParser/index.js +1 -3
- package/dist/core/parser/templateParser/helper.js +5 -5
- package/dist/core/parser/templateParser/index.js +14 -14
- package/dist/core/workerPool/poolManager.js +3 -3
- package/dist/functions/wormaJson.js +2 -2
- package/dist/helper/PluginDriver.js +2 -0
- package/dist/helper/comment.js +18 -20
- package/dist/helper/config/ConfigHelper.js +2 -3
- package/dist/helper/config/ConfigManager.js +16 -14
- package/dist/helper/config/GeneratorHelper.js +15 -13
- package/dist/helper/config/zType.js +1 -1
- package/dist/helper/document/index.js +13 -14
- package/dist/helper/logger/index.js +6 -8
- package/dist/helper/progress.js +5 -3
- package/dist/helper/template/index.js +13 -11
- package/dist/plugins/presets/aiDoc.js +1 -1
- package/dist/plugins/presets/apifox.js +2 -2
- package/dist/plugins/presets/payloadModifier/index.js +3 -0
- package/dist/plugins/presets/rename.js +5 -0
- package/dist/readConfig.js +9 -9
- package/dist/template/index.js +14 -14
- package/dist/template/presets/ai-doc/SKILL.md.handlebars +3 -3
- package/dist/template/presets/alova/common/helper.cjs.handlebars +18 -3
- package/dist/template/presets/alova/common/services/#index.cjs.handlebars +2 -2
- package/dist/template/presets/alova/common/services/{tag}.cjs.handlebars +7 -9
- package/dist/template/presets/alova/common/services/{tag}.d.cts.handlebars +5 -7
- package/dist/template/presets/alova/module/helper.js.handlebars +17 -2
- package/dist/template/presets/alova/module/services/#index.js.handlebars +1 -1
- package/dist/template/presets/alova/module/services/{tag}.d.ts.handlebars +5 -7
- package/dist/template/presets/alova/module/services/{tag}.js.handlebars +6 -6
- package/dist/template/presets/alova/partials/comment.handlebars +11 -2
- package/dist/template/presets/alova/partials/dts-fn-declare.handlebars +1 -1
- package/dist/template/presets/alova/partials/extract-responded.handlebars +3 -3
- package/dist/template/presets/alova/partials/typed-body.handlebars +3 -1
- package/dist/template/presets/alova/typescript/helper.ts.handlebars +11 -6
- package/dist/template/presets/alova/typescript/services/#index.ts.handlebars +1 -1
- package/dist/template/presets/alova/typescript/services/{tag}.ts.handlebars +7 -9
- package/dist/template/presets/alova-globals/partials/comment.handlebars +11 -2
- package/dist/template/presets/alova-globals/partials/globals-d-ts.handlebars +1 -1
- package/dist/template/presets/axios/common/helper.cjs.handlebars +18 -3
- package/dist/template/presets/axios/common/services/#index.cjs.handlebars +2 -2
- package/dist/template/presets/axios/common/services/{tag}.cjs.handlebars +25 -28
- package/dist/template/presets/axios/common/services/{tag}.d.cts.handlebars +1 -3
- package/dist/template/presets/axios/module/helper.js.handlebars +17 -2
- package/dist/template/presets/axios/module/services/#index.js.handlebars +2 -2
- package/dist/template/presets/axios/module/services/{tag}.d.ts.handlebars +1 -3
- package/dist/template/presets/axios/module/services/{tag}.js.handlebars +20 -20
- package/dist/template/presets/axios/partials/comment.handlebars +11 -2
- package/dist/template/presets/axios/partials/dts-fn-declare.handlebars +1 -1
- package/dist/template/presets/axios/typescript/helper.ts.handlebars +11 -6
- package/dist/template/presets/axios/typescript/services/#index.ts.handlebars +1 -1
- package/dist/template/presets/axios/typescript/services/{tag}.ts.handlebars +7 -9
- package/dist/template/presets/fetch/common/helper.cjs.handlebars +18 -3
- package/dist/template/presets/fetch/common/services/#index.cjs.handlebars +2 -2
- package/dist/template/presets/fetch/common/services/{tag}.cjs.handlebars +6 -9
- package/dist/template/presets/fetch/common/services/{tag}.d.cts.handlebars +1 -3
- package/dist/template/presets/fetch/module/helper.js.handlebars +17 -2
- package/dist/template/presets/fetch/module/services/#index.js.handlebars +2 -2
- package/dist/template/presets/fetch/module/services/{tag}.d.ts.handlebars +1 -3
- package/dist/template/presets/fetch/module/services/{tag}.js.handlebars +6 -6
- package/dist/template/presets/fetch/partials/comment.handlebars +11 -2
- package/dist/template/presets/fetch/partials/dts-fn-declare.handlebars +1 -1
- package/dist/template/presets/fetch/typescript/helper.ts.handlebars +11 -6
- package/dist/template/presets/fetch/typescript/services/#index.ts.handlebars +1 -1
- package/dist/template/presets/fetch/typescript/services/{tag}.ts.handlebars +7 -9
- package/dist/template/presets/ky/module/helper.js.handlebars +17 -2
- package/dist/template/presets/ky/module/services/#index.js.handlebars +2 -2
- package/dist/template/presets/ky/module/services/{tag}.d.ts.handlebars +1 -3
- package/dist/template/presets/ky/module/services/{tag}.js.handlebars +6 -6
- package/dist/template/presets/ky/partials/comment.handlebars +11 -2
- package/dist/template/presets/ky/partials/dts-fn-declare.handlebars +1 -1
- package/dist/template/presets/ky/typescript/helper.ts.handlebars +11 -6
- package/dist/template/presets/ky/typescript/services/#index.ts.handlebars +1 -1
- package/dist/template/presets/ky/typescript/services/{tag}.ts.handlebars +7 -9
- package/dist/utils/base.js +9 -9
- package/dist/utils/openapi.js +15 -15
- package/package.json +2 -2
- package/typings/index.d.ts +1 -1
- package/typings/plugins.d.ts +30 -19
- package/dist/tsconfig.build.tsbuildinfo +0 -1
|
@@ -47,7 +47,7 @@ const helper_1 = require("../../../helper");
|
|
|
47
47
|
const document_1 = require("../../../helper/document");
|
|
48
48
|
const openapi_1 = require("../../../utils/openapi");
|
|
49
49
|
const helper_2 = require("./helper");
|
|
50
|
-
/** M2-B4:
|
|
50
|
+
/** M2-B4: lightweight concurrency limiter, no extra dependencies */
|
|
51
51
|
async function pMap(items, fn, concurrency) {
|
|
52
52
|
const results = Array.from({ length: items.length });
|
|
53
53
|
let idx = 0;
|
|
@@ -60,22 +60,22 @@ async function pMap(items, fn, concurrency) {
|
|
|
60
60
|
await Promise.all(Array.from({ length: Math.min(concurrency, items.length) }, () => worker()));
|
|
61
61
|
return results;
|
|
62
62
|
}
|
|
63
|
-
/**
|
|
63
|
+
/** Automatically compute a reasonable concurrency limit based on CPU core count */
|
|
64
64
|
function autoConcurrency() {
|
|
65
65
|
const cpuCount = Math.max(1, (0, node_os_1.cpus)().length);
|
|
66
66
|
return Math.min(64, Math.max(8, cpuCount * 4));
|
|
67
67
|
}
|
|
68
68
|
class TemplateParser {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
69
|
+
name = 'templateParser';
|
|
70
|
+
schemasMap = new Map();
|
|
71
|
+
operationIdSet = new Set();
|
|
72
|
+
pathMap = [];
|
|
73
|
+
refNameMap = new Map();
|
|
74
|
+
/** M1-A4: replaced tagedApis.find() linear lookup with a Map index, O(n·t) → O(n+t) */
|
|
75
|
+
tagMap = new Map();
|
|
76
|
+
document;
|
|
77
|
+
options;
|
|
78
|
+
openApiHelper = new document_1.OpenApiHelper();
|
|
79
79
|
async parse(document, options) {
|
|
80
80
|
this.document = document;
|
|
81
81
|
this.options = options;
|
|
@@ -173,7 +173,7 @@ class TemplateParser {
|
|
|
173
173
|
});
|
|
174
174
|
templateData.components = [...new Set(this.schemasMap.values())];
|
|
175
175
|
templateData.componentNames = [...this.schemasMap.keys()];
|
|
176
|
-
//
|
|
176
|
+
// sort by name lexicographically to ensure deterministic output order (avoid Map insertion-order drift under concurrency)
|
|
177
177
|
const sorted = [...this.schemasMap.entries()].sort(([a], [b]) => a.localeCompare(b));
|
|
178
178
|
templateData.componentNames = sorted.map(([k]) => k);
|
|
179
179
|
templateData.components = sorted.map(([, v]) => v);
|
|
@@ -301,7 +301,7 @@ class TemplateParser {
|
|
|
301
301
|
let tagApis = this.tagMap.get(api.tag);
|
|
302
302
|
if (!tagApis) {
|
|
303
303
|
tagApis = {
|
|
304
|
-
|
|
304
|
+
tag: api.tag,
|
|
305
305
|
apis: [],
|
|
306
306
|
};
|
|
307
307
|
this.tagMap.set(api.tag, tagApis);
|
|
@@ -8,15 +8,15 @@ exports.PoolManager = void 0;
|
|
|
8
8
|
*/
|
|
9
9
|
const WorkerPool_1 = require("../../core/WorkerPool");
|
|
10
10
|
class PoolManager {
|
|
11
|
+
static instance;
|
|
12
|
+
pools = new Map();
|
|
11
13
|
static getInstance() {
|
|
12
14
|
if (!PoolManager.instance) {
|
|
13
15
|
PoolManager.instance = new PoolManager();
|
|
14
16
|
}
|
|
15
17
|
return PoolManager.instance;
|
|
16
18
|
}
|
|
17
|
-
constructor() {
|
|
18
|
-
this.pools = new Map();
|
|
19
|
-
}
|
|
19
|
+
constructor() { }
|
|
20
20
|
get(options) {
|
|
21
21
|
const { key, workerScript, sharedContext, poolSize, idleTimeout } = options;
|
|
22
22
|
let pool = this.pools.get(key);
|
|
@@ -77,10 +77,10 @@ function computePerTagHashes(allApis, tagedApis) {
|
|
|
77
77
|
const allHashes = [];
|
|
78
78
|
if (tagedApis) {
|
|
79
79
|
// P2: Use pre-grouped tagedApis to avoid O(n) re-grouping loop
|
|
80
|
-
for (const {
|
|
80
|
+
for (const { tag, apis: groupApis } of tagedApis) {
|
|
81
81
|
const hashes = groupApis.map(computeApiHash).sort();
|
|
82
82
|
allHashes.push(...hashes);
|
|
83
|
-
tags[
|
|
83
|
+
tags[tag] = (0, node_crypto_1.createHash)('sha256').update(hashes.join('')).digest('hex').slice(0, 16);
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
else {
|
|
@@ -12,6 +12,8 @@ const CTX = Object.freeze({});
|
|
|
12
12
|
* - Provides three execution strategies: parallelEach, seqEach (chaining), pipe (content transform).
|
|
13
13
|
*/
|
|
14
14
|
class PluginDriver {
|
|
15
|
+
plugins;
|
|
16
|
+
reporter;
|
|
15
17
|
constructor(plugins = [], opts) {
|
|
16
18
|
this.plugins = plugins.filter(Boolean);
|
|
17
19
|
this.reporter = opts?.reporter ?? (() => progress_1.noopReportProgress);
|
package/dist/helper/comment.js
CHANGED
|
@@ -2,26 +2,24 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.commentHelper = exports.CommentHelper = void 0;
|
|
4
4
|
class CommentHelper {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
'[title]
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
'[cycle]
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
'[summary]
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
};
|
|
24
|
-
}
|
|
5
|
+
type = 'line';
|
|
6
|
+
comment = '';
|
|
7
|
+
transformMap = {
|
|
8
|
+
'[deprecated]': '@deprecated',
|
|
9
|
+
'[required]': '[required]',
|
|
10
|
+
'[title]': (text) => {
|
|
11
|
+
const [, nextText = ''] = /\[title\](.*)/.exec(text) ?? [];
|
|
12
|
+
return `${nextText.trim()}\n---`;
|
|
13
|
+
},
|
|
14
|
+
'[cycle]': (text) => {
|
|
15
|
+
const [, nextText = ''] = /\[cycle\](.*)/.exec(text) ?? [];
|
|
16
|
+
return `${nextText.trim()}\n---`;
|
|
17
|
+
},
|
|
18
|
+
'[summary]': (text) => {
|
|
19
|
+
const [, nextText = ''] = /\[summary\](.*)/.exec(text) ?? [];
|
|
20
|
+
return `${nextText.trim()}\n---`;
|
|
21
|
+
},
|
|
22
|
+
};
|
|
25
23
|
static load(options) {
|
|
26
24
|
return new CommentHelper().load(options);
|
|
27
25
|
}
|
|
@@ -6,9 +6,8 @@ const helper_1 = require("../../helper");
|
|
|
6
6
|
const ConfigManager_1 = require("./ConfigManager");
|
|
7
7
|
const GeneratorHelper_1 = require("./GeneratorHelper");
|
|
8
8
|
class ConfigHelper {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
9
|
+
configManager = new ConfigManager_1.ConfigManager();
|
|
10
|
+
projectPath;
|
|
12
11
|
async load(config, projectPath = process.cwd(), tracker) {
|
|
13
12
|
this.projectPath = projectPath;
|
|
14
13
|
await this.configManager.load(config, projectPath, tracker);
|
|
@@ -10,47 +10,49 @@ const GeneratorHelper_1 = require("../../helper/config/GeneratorHelper");
|
|
|
10
10
|
const logger_1 = require("../../helper/logger");
|
|
11
11
|
const zType_1 = require("./zType");
|
|
12
12
|
class ConfigManager {
|
|
13
|
+
config;
|
|
14
|
+
readConfig;
|
|
15
|
+
defaultConfig = Object.freeze({
|
|
16
|
+
generator: [],
|
|
17
|
+
});
|
|
18
|
+
defaultGeneratorConfig = GeneratorHelper_1.generatorHelper.getDefaultConfig();
|
|
13
19
|
constructor() {
|
|
14
|
-
this.defaultConfig = Object.freeze({
|
|
15
|
-
generator: [],
|
|
16
|
-
});
|
|
17
|
-
this.defaultGeneratorConfig = GeneratorHelper_1.generatorHelper.getDefaultConfig();
|
|
18
20
|
this.config = this.defaultConfig;
|
|
19
21
|
}
|
|
20
22
|
/**
|
|
21
|
-
*
|
|
23
|
+
* Load and validate the configuration
|
|
22
24
|
*/
|
|
23
25
|
async load(config, projectPath = process.cwd(), tracker) {
|
|
24
|
-
//
|
|
26
|
+
// process config
|
|
25
27
|
const userConfig = await this.handleConfig(config, projectPath, tracker);
|
|
26
|
-
//
|
|
28
|
+
// validate config
|
|
27
29
|
const validatedConfig = this.validateConfig(userConfig);
|
|
28
|
-
//
|
|
30
|
+
// update config
|
|
29
31
|
this.config = validatedConfig;
|
|
30
32
|
this.readConfig = Object.freeze(this.config);
|
|
31
33
|
logger_1.logger.debug('Configuration loaded successfully', this.config);
|
|
32
34
|
}
|
|
33
35
|
/**
|
|
34
|
-
*
|
|
36
|
+
* Get the full configuration
|
|
35
37
|
*/
|
|
36
38
|
getConfig() {
|
|
37
39
|
return this.readConfig;
|
|
38
40
|
}
|
|
39
41
|
/**
|
|
40
|
-
*
|
|
42
|
+
* Update configuration
|
|
41
43
|
*/
|
|
42
44
|
async update(partialConfig) {
|
|
43
45
|
await this.load({ ...this.config, ...partialConfig });
|
|
44
46
|
}
|
|
45
47
|
async handleConfig(config, projectPath = process.cwd(), tracker) {
|
|
46
|
-
//
|
|
48
|
+
// merge config
|
|
47
49
|
const userConfig = this.mergeConfig(this.defaultConfig, config);
|
|
48
|
-
//
|
|
50
|
+
// process plugin config configurations
|
|
49
51
|
userConfig.generator = await Promise.all(userConfig.generator.map(item => (0, prepareConfig_1.default)(item, projectPath, tracker)));
|
|
50
52
|
return userConfig;
|
|
51
53
|
}
|
|
52
54
|
/**
|
|
53
|
-
*
|
|
55
|
+
* Validate configuration
|
|
54
56
|
*/
|
|
55
57
|
validateConfig(config) {
|
|
56
58
|
let result = config;
|
|
@@ -64,7 +66,7 @@ class ConfigManager {
|
|
|
64
66
|
return result;
|
|
65
67
|
}
|
|
66
68
|
/**
|
|
67
|
-
*
|
|
69
|
+
* Merge configuration (shallow copy)
|
|
68
70
|
*/
|
|
69
71
|
mergeConfig(defaultConfig, userConfig) {
|
|
70
72
|
const result = { ...defaultConfig, ...userConfig };
|
|
@@ -15,19 +15,21 @@ const helper_2 = require("../../helper");
|
|
|
15
15
|
const progress_1 = require("../../helper/progress");
|
|
16
16
|
const zType_1 = require("./zType");
|
|
17
17
|
class GeneratorHelper {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
18
|
+
static instance;
|
|
19
|
+
config;
|
|
20
|
+
readConfig;
|
|
21
|
+
pluginDriver;
|
|
22
|
+
/**
|
|
23
|
+
* Default configuration values
|
|
24
|
+
*/
|
|
25
|
+
defaultConfig = Object.freeze({
|
|
26
|
+
input: '',
|
|
27
|
+
output: '',
|
|
28
|
+
responseMediaType: 'application/json',
|
|
29
|
+
bodyMediaType: 'application/json',
|
|
30
|
+
type: constant_1.ConfigTypeEnum.AUTO,
|
|
31
|
+
defaultRequire: false,
|
|
32
|
+
});
|
|
31
33
|
static getInstance() {
|
|
32
34
|
if (!GeneratorHelper.instance) {
|
|
33
35
|
GeneratorHelper.instance = new GeneratorHelper();
|
|
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.zConfig = exports.zGeneratorConfig = exports._zGeneratorConfig = exports.zApiPlugin = exports.zFetchOptions = exports.zHandleApi = exports.zApiDescriptor = exports.zTemplateResult = exports.zTemplateType = exports.zConfigType = void 0;
|
|
7
7
|
const node_path_1 = __importDefault(require("node:path"));
|
|
8
|
-
const v3_1 = require("zod/v3"); // v4
|
|
8
|
+
const v3_1 = require("zod/v3"); // v4 is unstable, temporarily using v3
|
|
9
9
|
/**
|
|
10
10
|
* Find the corresponding input attribute value
|
|
11
11
|
*/
|
|
@@ -4,7 +4,7 @@ exports.openApiHelper = exports.OpenApiHelper = exports.supportedApiMethods = vo
|
|
|
4
4
|
const type_1 = require("../../type");
|
|
5
5
|
const utils_1 = require("../../utils");
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* List of supported API methods
|
|
8
8
|
* @see https://github.com/alovajs/alova/blob/main/packages/alova/typings/index.d.ts#L640
|
|
9
9
|
*/
|
|
10
10
|
exports.supportedApiMethods = [
|
|
@@ -17,9 +17,8 @@ exports.supportedApiMethods = [
|
|
|
17
17
|
type_1.HttpMethod.OPTIONS,
|
|
18
18
|
];
|
|
19
19
|
class OpenApiHelper {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
20
|
+
document;
|
|
21
|
+
usedRefsCache = new Set();
|
|
23
22
|
load(document) {
|
|
24
23
|
this.document = document;
|
|
25
24
|
// reset cache when document changes
|
|
@@ -54,12 +53,12 @@ class OpenApiHelper {
|
|
|
54
53
|
return apiMethods;
|
|
55
54
|
}
|
|
56
55
|
/**
|
|
57
|
-
*
|
|
58
|
-
*
|
|
56
|
+
* Write the ApiMethods array back into the openapi document's paths.
|
|
57
|
+
* Only updates the operationObject for the passed url+method, keeping the rest untouched.
|
|
59
58
|
*/
|
|
60
59
|
saveApiMethods(apiMethods) {
|
|
61
60
|
this.document.paths = {};
|
|
62
|
-
//
|
|
61
|
+
// build a url -> { method -> operationObject } map; later writes overwrite earlier ones
|
|
63
62
|
const grouped = {};
|
|
64
63
|
for (const item of apiMethods || []) {
|
|
65
64
|
if (!item || !item.url || !item.method || !item.operationObject) {
|
|
@@ -67,7 +66,7 @@ class OpenApiHelper {
|
|
|
67
66
|
}
|
|
68
67
|
const method = String(item.method).toLowerCase();
|
|
69
68
|
if (!exports.supportedApiMethods.includes(method)) {
|
|
70
|
-
//
|
|
69
|
+
// skip unsupported http methods
|
|
71
70
|
continue;
|
|
72
71
|
}
|
|
73
72
|
if (!grouped[item.url]) {
|
|
@@ -75,7 +74,7 @@ class OpenApiHelper {
|
|
|
75
74
|
}
|
|
76
75
|
grouped[item.url][method] = item.operationObject;
|
|
77
76
|
}
|
|
78
|
-
//
|
|
77
|
+
// write the grouped methods back into document.paths
|
|
79
78
|
for (const [url, methodsMap] of Object.entries(grouped)) {
|
|
80
79
|
const pathInfo = (this.document.paths?.[url] || {});
|
|
81
80
|
for (const [method, operationObject] of Object.entries(methodsMap)) {
|
|
@@ -85,13 +84,13 @@ class OpenApiHelper {
|
|
|
85
84
|
this.document.paths[url] = pathInfo;
|
|
86
85
|
}
|
|
87
86
|
}
|
|
88
|
-
//
|
|
87
|
+
// clear the reference cache after updating, ensuring subsequent used-reference computations are up to date
|
|
89
88
|
this.usedRefsCache.clear();
|
|
90
89
|
return this;
|
|
91
90
|
}
|
|
92
91
|
/**
|
|
93
|
-
*
|
|
94
|
-
* @param ref ReferenceObject
|
|
92
|
+
* Check whether a reference is used in the document
|
|
93
|
+
* @param ref ReferenceObject or its `$ref` path string
|
|
95
94
|
*/
|
|
96
95
|
isReferenceUsed(ref) {
|
|
97
96
|
const refPath = typeof ref === 'string' ? ref : ref.$ref;
|
|
@@ -99,7 +98,7 @@ class OpenApiHelper {
|
|
|
99
98
|
return usedSet.has(refPath);
|
|
100
99
|
}
|
|
101
100
|
/**
|
|
102
|
-
*
|
|
101
|
+
* Precompute and return the set of used `$ref`s, reachable only from paths
|
|
103
102
|
*/
|
|
104
103
|
getUsedReferenceSet() {
|
|
105
104
|
if (this.usedRefsCache.size > 0) {
|
|
@@ -146,7 +145,7 @@ class OpenApiHelper {
|
|
|
146
145
|
return used;
|
|
147
146
|
}
|
|
148
147
|
/**
|
|
149
|
-
*
|
|
148
|
+
* Filter out unused references and return the list of still-used references
|
|
150
149
|
*/
|
|
151
150
|
filterUsedReferences(refs) {
|
|
152
151
|
const usedSet = this.getUsedReferenceSet();
|
|
@@ -4,13 +4,11 @@ exports.logger = void 0;
|
|
|
4
4
|
const config_1 = require("../../config");
|
|
5
5
|
const DEFAULT_CONFIG = (0, config_1.getGlobalConfig)();
|
|
6
6
|
class Logger {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
}
|
|
7
|
+
options = {
|
|
8
|
+
level: 'info',
|
|
9
|
+
timestamp: true,
|
|
10
|
+
colors: true,
|
|
11
|
+
};
|
|
14
12
|
configure(options) {
|
|
15
13
|
this.options = { ...this.options, ...options };
|
|
16
14
|
return this;
|
|
@@ -68,7 +66,7 @@ class Logger {
|
|
|
68
66
|
return new DEFAULT_CONFIG.Error(this.errorMsg(error));
|
|
69
67
|
}
|
|
70
68
|
}
|
|
71
|
-
//
|
|
69
|
+
// export the singleton instance
|
|
72
70
|
exports.logger = new Logger().configure({
|
|
73
71
|
level: 'info',
|
|
74
72
|
timestamp: true,
|
package/dist/helper/progress.js
CHANGED
|
@@ -22,10 +22,12 @@ exports.GeneratorStage = {
|
|
|
22
22
|
SKIPPED: 'skipped',
|
|
23
23
|
};
|
|
24
24
|
class ProgressTracker {
|
|
25
|
+
snapshot = {};
|
|
26
|
+
dirty = false;
|
|
27
|
+
timer = null;
|
|
28
|
+
listener;
|
|
29
|
+
interval;
|
|
25
30
|
constructor(listener, interval = 500) {
|
|
26
|
-
this.snapshot = {};
|
|
27
|
-
this.dirty = false;
|
|
28
|
-
this.timer = null;
|
|
29
31
|
this.listener = listener;
|
|
30
32
|
this.interval = interval;
|
|
31
33
|
}
|
|
@@ -11,13 +11,13 @@ const constant_1 = require("../../constant");
|
|
|
11
11
|
const wormaJson_1 = require("../../functions/wormaJson");
|
|
12
12
|
const logger_1 = require("../../helper/logger");
|
|
13
13
|
const utils_1 = require("../../utils");
|
|
14
|
-
/** M1-A2: Handlebars
|
|
14
|
+
/** M1-A2: Handlebars instance cache — reuse compiled partials and helpers for the same templatePath */
|
|
15
15
|
const hbsInstanceCache = new Map();
|
|
16
16
|
const templateCompileCache = new Map();
|
|
17
17
|
function cacheKey(projectPath, output) {
|
|
18
18
|
return `${projectPath}::${output}`;
|
|
19
19
|
}
|
|
20
|
-
/**
|
|
20
|
+
/** module-type directory name (differs from config.type: commonjs -> common) */
|
|
21
21
|
exports.MODULE_TYPES = [constant_1.ModuleTypeDir.TYPESCRIPT, constant_1.ModuleTypeDir.MODULE, constant_1.ModuleTypeDir.COMMON];
|
|
22
22
|
// ---- helpers ----
|
|
23
23
|
const TEMPLATE_EXTENSIONS = [
|
|
@@ -62,6 +62,10 @@ function joinPath(...parts) {
|
|
|
62
62
|
return p ? normalizeSlashes(p) : p;
|
|
63
63
|
}
|
|
64
64
|
class TemplateHelper {
|
|
65
|
+
static instance;
|
|
66
|
+
/** Runtime cache of parsed template data, keyed by `${projectPath}::${output}`. Flushed to disk via flushAllData. */
|
|
67
|
+
static _cacheData = new Map();
|
|
68
|
+
config;
|
|
65
69
|
// ============ hasTagTemplates / instance ============
|
|
66
70
|
static async hasTagTemplates(templatePath, type) {
|
|
67
71
|
try {
|
|
@@ -93,7 +97,7 @@ class TemplateHelper {
|
|
|
93
97
|
static getModuleType(type) {
|
|
94
98
|
return constant_1.MODULE_TYPE_TO_KIND[type] ?? constant_1.ModuleKind.ES_MODULE;
|
|
95
99
|
}
|
|
96
|
-
// ============
|
|
100
|
+
// ============ cache ============
|
|
97
101
|
static async readData(projectPath, output) {
|
|
98
102
|
const key = cacheKey(projectPath, output);
|
|
99
103
|
const cached = TemplateHelper._cacheData.get(key);
|
|
@@ -144,7 +148,7 @@ class TemplateHelper {
|
|
|
144
148
|
await promises_1.default.unlink(p);
|
|
145
149
|
}));
|
|
146
150
|
}
|
|
147
|
-
// ============
|
|
151
|
+
// ============ template resolution ============
|
|
148
152
|
async resolveTemplateFiles(templatePath) {
|
|
149
153
|
const absolute = node_path_1.default.isAbsolute(templatePath) ? templatePath : node_path_1.default.resolve(process.cwd(), templatePath);
|
|
150
154
|
if (!(await (0, utils_1.existsPromise)(absolute)))
|
|
@@ -235,7 +239,7 @@ class TemplateHelper {
|
|
|
235
239
|
insideTagDir,
|
|
236
240
|
};
|
|
237
241
|
}
|
|
238
|
-
// ============
|
|
242
|
+
// ============ render & output ============
|
|
239
243
|
async renderTemplate(templateFilePath, data) {
|
|
240
244
|
const p = node_path_1.default.isAbsolute(this.config.templatePath)
|
|
241
245
|
? node_path_1.default.join(this.config.templatePath, `${templateFilePath}${constant_1.FileExtension.HANDLEBARS}`)
|
|
@@ -254,7 +258,7 @@ class TemplateHelper {
|
|
|
254
258
|
}
|
|
255
259
|
return template(data);
|
|
256
260
|
}
|
|
257
|
-
/** M1-A2:
|
|
261
|
+
/** M1-A2: clear Handlebars compile and instance caches (used by tests and watch mode) */
|
|
258
262
|
static clearTemplateCache() {
|
|
259
263
|
hbsInstanceCache.clear();
|
|
260
264
|
templateCompileCache.clear();
|
|
@@ -352,10 +356,10 @@ class TemplateHelper {
|
|
|
352
356
|
});
|
|
353
357
|
const { changedTags, beforeFileWrite, writeConcurrency = 32, formatFile = true } = options ?? {};
|
|
354
358
|
const hbs = await this.createHbs();
|
|
355
|
-
const tags = data.tagedApis?.map(item => item.
|
|
359
|
+
const tags = data.tagedApis?.map(item => item.tag) || [];
|
|
356
360
|
const apis = data.allApis || [];
|
|
357
361
|
const tagApisMap = new Map();
|
|
358
|
-
data.tagedApis?.forEach(item => tagApisMap.set(item.
|
|
362
|
+
data.tagedApis?.forEach(item => tagApisMap.set(item.tag, item));
|
|
359
363
|
// Collect all file paths for codeGenerated notification
|
|
360
364
|
const allFilePaths = [];
|
|
361
365
|
// --- Phase 1: Per-tag streaming (render → collect → batch write) ---
|
|
@@ -457,7 +461,7 @@ class TemplateHelper {
|
|
|
457
461
|
}
|
|
458
462
|
async renderOne(hbs, tf, data, outputDir, placeholders) {
|
|
459
463
|
const renderData = data.config ? { ...data.config, ...data } : data;
|
|
460
|
-
const content = await this.renderTemplateFromPath(hbs, tf.absolutePath, renderData);
|
|
464
|
+
const content = await this.renderTemplateFromPath(hbs, tf.absolutePath, { ...renderData, noOverwrite: tf.isNoOverwrite });
|
|
461
465
|
let relPath = tf.relativePath;
|
|
462
466
|
if (placeholders?.tag)
|
|
463
467
|
relPath = relPath.replace(constant_1.TemplatePlaceholder.TAG, placeholders.tag);
|
|
@@ -470,6 +474,4 @@ class TemplateHelper {
|
|
|
470
474
|
}
|
|
471
475
|
}
|
|
472
476
|
exports.TemplateHelper = TemplateHelper;
|
|
473
|
-
/** Runtime cache of parsed template data, keyed by `${projectPath}::${output}`. Flushed to disk via flushAllData. */
|
|
474
|
-
TemplateHelper._cacheData = new Map();
|
|
475
477
|
exports.templateHelper = TemplateHelper.getInstance();
|
|
@@ -54,7 +54,7 @@ function aiDoc(config) {
|
|
|
54
54
|
...group,
|
|
55
55
|
apis: group.apis.map(api => ({
|
|
56
56
|
...api,
|
|
57
|
-
...(isGlobals ? {} : { fileLocation: `${outputRel.replace(/\\/g, '/')}/${group.
|
|
57
|
+
...(isGlobals ? {} : { fileLocation: `${outputRel.replace(/\\/g, '/')}/${group.tag}` }),
|
|
58
58
|
})),
|
|
59
59
|
})),
|
|
60
60
|
};
|
|
@@ -18,10 +18,10 @@ function apifox({ projectId, locale = 'zh-CN', apifoxVersion = '2024-03-28', sco
|
|
|
18
18
|
branchId,
|
|
19
19
|
moduleId,
|
|
20
20
|
};
|
|
21
|
-
//
|
|
21
|
+
// set the corresponding params based on the scope type
|
|
22
22
|
switch (scopeType) {
|
|
23
23
|
case 'ALL':
|
|
24
|
-
//
|
|
24
|
+
// exporting all does not require extra params
|
|
25
25
|
break;
|
|
26
26
|
case 'SELECTED_ENDPOINTS':
|
|
27
27
|
body.scope.selectedEndpointIds = selectedEndpointIds;
|
|
@@ -128,6 +128,9 @@ function applyToSchemaField(schema, config) {
|
|
|
128
128
|
function payloadModifierApiDescriptor(apiDescriptor, config) {
|
|
129
129
|
if (!apiDescriptor)
|
|
130
130
|
return null;
|
|
131
|
+
// API path filter: if path does not match, return as-is and this config does not apply
|
|
132
|
+
if (!(0, utils_1.isMatch)(apiDescriptor.url, config.path))
|
|
133
|
+
return apiDescriptor;
|
|
131
134
|
const newDescriptor = { ...apiDescriptor };
|
|
132
135
|
const { scope } = config;
|
|
133
136
|
switch (scope) {
|
|
@@ -241,6 +241,11 @@ function transformRefNameMap(refNameMap, config, apiDescriptor) {
|
|
|
241
241
|
function renameApiDescriptor(apiDescriptor, config) {
|
|
242
242
|
if (!apiDescriptor)
|
|
243
243
|
return apiDescriptor;
|
|
244
|
+
// API path filter: when `path` is set and the url does not match, this config
|
|
245
|
+
// is a no-op and the descriptor is returned unchanged (same reference).
|
|
246
|
+
if (config.path && !(0, utils_1.isMatch)(apiDescriptor.url, config.path)) {
|
|
247
|
+
return apiDescriptor;
|
|
248
|
+
}
|
|
244
249
|
const newDescriptor = { ...apiDescriptor };
|
|
245
250
|
const scope = config.scope || constant_1.RenameScope.URL;
|
|
246
251
|
switch (scope) {
|
package/dist/readConfig.js
CHANGED
|
@@ -36,20 +36,20 @@ async function readConfig(projectPath = process.cwd()) {
|
|
|
36
36
|
}
|
|
37
37
|
return config;
|
|
38
38
|
}
|
|
39
|
-
//
|
|
39
|
+
// get the dependencies installed by the user
|
|
40
40
|
const userDependencies = await (0, utils_1.getUserInstalledDependencies)(projectPath);
|
|
41
|
-
//
|
|
42
|
-
//
|
|
43
|
-
// monorepo
|
|
41
|
+
// always treat worma itself as an external dependency to prevent esbuild from bundling worma source into the temp file
|
|
42
|
+
// otherwise __dirname would point to the user project directory, causing preset template path resolution to fail
|
|
43
|
+
// monorepo sub-packages may not directly depend on wormajs, but the config file imports from 'wormajs'
|
|
44
44
|
const allExternals = [...new Set([...userDependencies, 'worma', 'wormajs/plugin'])];
|
|
45
45
|
const configTmpFileName = `worma_tmp_${Date.now()}.cjs`;
|
|
46
|
-
//
|
|
47
|
-
//
|
|
48
|
-
//
|
|
46
|
+
// use an absolute path: esbuild resolves relative outfile against cwd,
|
|
47
|
+
// while require() resolves relative paths against the calling module's directory, and the mismatch would cause require to fail.
|
|
48
|
+
// an absolute path ensures writing and require point to the same file.
|
|
49
49
|
const outfile = node_path_1.default.resolve(projectPath, configTmpFileName);
|
|
50
50
|
await esbuild_1.default.build({
|
|
51
51
|
entryPoints: [configFile],
|
|
52
|
-
//
|
|
52
|
+
// exclude dependencies installed by the user to avoid bundling them into the final file
|
|
53
53
|
external: allExternals,
|
|
54
54
|
bundle: true,
|
|
55
55
|
format: 'cjs',
|
|
@@ -57,7 +57,7 @@ async function readConfig(projectPath = process.cwd()) {
|
|
|
57
57
|
outfile,
|
|
58
58
|
logLevel: 'silent',
|
|
59
59
|
});
|
|
60
|
-
// try/finally
|
|
60
|
+
// try/finally ensures the temp file is cleaned up in all cases (including require errors)
|
|
61
61
|
let module;
|
|
62
62
|
try {
|
|
63
63
|
// eslint-disable-next-line ts/no-require-imports
|