@genapi/pipeline 3.4.0 → 3.5.0

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.
@@ -1,5 +1,6 @@
1
- import { ApiPipeline } from '@genapi/shared';
1
+ import { ApiPipeline } from "@genapi/shared";
2
2
 
3
+ //#region src/pipeline/index.d.ts
3
4
  /**
4
5
  * Pipeline read(input)function
5
6
  */
@@ -31,14 +32,25 @@ declare namespace pipeline {
31
32
  var dest: typeof dest;
32
33
  }
33
34
 
35
+ //#endregion
36
+ //#region src/compiler/index.d.ts
34
37
  declare function compiler(configRead: ApiPipeline.ConfigRead): ApiPipeline.ConfigRead<ApiPipeline.Config>;
35
38
 
39
+ //#endregion
40
+ //#region src/config/index.d.ts
36
41
  declare function config(userConfig: ApiPipeline.Config): ApiPipeline.ConfigRead<Required<ApiPipeline.Config>>;
37
42
 
43
+ //#endregion
44
+ //#region src/dest/index.d.ts
38
45
  declare function dest(configRead: ApiPipeline.ConfigRead): void;
39
46
 
47
+ //#endregion
48
+ //#region src/generate/index.d.ts
40
49
  declare function generate(configRead: ApiPipeline.ConfigRead): ApiPipeline.ConfigRead<ApiPipeline.Config>;
41
50
 
51
+ //#endregion
52
+ //#region src/original/index.d.ts
42
53
  declare function original(configRead: ApiPipeline.ConfigRead): Promise<ApiPipeline.ConfigRead<ApiPipeline.Config>>;
43
54
 
44
- export { type PipelineDest, type PipelineFlow, type PipelineRead, compiler, config, pipeline as default, dest, generate, original };
55
+ //#endregion
56
+ export { PipelineDest, PipelineFlow, PipelineRead, compiler, config, pipeline as default, dest, generate, original };
package/dist/index.mjs CHANGED
@@ -1,245 +1,221 @@
1
- // src/pipeline/index.ts
2
1
  import pPipe from "p-pipe";
2
+ import { astNodeToCode, codeToAstNode, createComment, createFunction, createImport, createInterface, createTypeAlias, createVariable } from "ts-factory-extra";
3
+ import { NodeFlags, factory } from "typescript";
4
+ import path from "node:path";
5
+ import process from "node:process";
6
+ import fs from "fs-extra";
7
+ import { format } from "prettier";
8
+ import got from "got";
3
9
 
4
- // src/compiler/request.ts
5
- import { codeToAstNode, createComment as createComment2, createFunction, createImport, createVariable } from "ts-factory-extra";
6
- import { factory as factory2, NodeFlags } from "typescript";
7
-
8
- // src/compiler/typings.ts
9
- import { createComment, createInterface, createTypeAlias } from "ts-factory-extra";
10
- import { factory } from "typescript";
10
+ //#region src/compiler/typings.ts
11
11
  function compilerTsTypingsDeclaration(configRead, comment = true) {
12
- configRead.graphs.comments = configRead.graphs.comments || [];
13
- configRead.graphs.typings = configRead.graphs.typings || [];
14
- configRead.graphs.interfaces = configRead.graphs.interfaces || [];
15
- const typings = configRead.graphs.typings.map((item) => {
16
- return createTypeAlias(item.export, item.name, item.value);
17
- });
18
- const interfaces = configRead.graphs.interfaces.map((item) => {
19
- return createInterface({
20
- export: item.export,
21
- name: item.name,
22
- properties: item.properties || []
23
- });
24
- });
25
- const nodes = [
26
- factory.createIdentifier(""),
27
- ...typings,
28
- factory.createIdentifier(""),
29
- ...interfaces
30
- ];
31
- if (comment)
32
- nodes.unshift(createComment("multi", configRead.graphs.comments));
33
- return nodes;
12
+ configRead.graphs.comments = configRead.graphs.comments || [];
13
+ configRead.graphs.typings = configRead.graphs.typings || [];
14
+ configRead.graphs.interfaces = configRead.graphs.interfaces || [];
15
+ const typings = configRead.graphs.typings.map((item) => {
16
+ return createTypeAlias(item.export, item.name, item.value);
17
+ });
18
+ const interfaces = configRead.graphs.interfaces.map((item) => {
19
+ return createInterface({
20
+ export: item.export,
21
+ name: item.name,
22
+ properties: item.properties || []
23
+ });
24
+ });
25
+ const nodes = [
26
+ factory.createIdentifier(""),
27
+ ...typings,
28
+ factory.createIdentifier(""),
29
+ ...interfaces
30
+ ];
31
+ if (comment) nodes.unshift(createComment("multi", configRead.graphs.comments));
32
+ return nodes;
34
33
  }
35
34
 
36
- // src/compiler/request.ts
37
- var varFlags = {
38
- let: NodeFlags.Let,
39
- const: NodeFlags.Const,
40
- var: NodeFlags.None
35
+ //#endregion
36
+ //#region src/compiler/request.ts
37
+ const varFlags = {
38
+ let: NodeFlags.Let,
39
+ const: NodeFlags.Const,
40
+ var: NodeFlags.None
41
41
  };
42
42
  function compilerTsRequestDeclaration(configRead) {
43
- configRead.graphs.imports = configRead.graphs.imports || [];
44
- configRead.graphs.comments = configRead.graphs.comments || [];
45
- configRead.graphs.variables = configRead.graphs.variables || [];
46
- configRead.graphs.functions = configRead.graphs.functions || [];
47
- const isGenerateType = configRead.outputs.some((v) => v.type === "typings");
48
- const isTypescript = configRead.outputs.some((v) => v.type === "request" && v.path.endsWith(".ts"));
49
- const comments = [
50
- createComment2("multi", configRead.graphs.comments)
51
- ];
52
- const imports = configRead.graphs.imports?.map((item) => {
53
- return createImport(item.name, item.names, item.value, item.namespace);
54
- });
55
- const variables = configRead.graphs.variables.map((item) => {
56
- return createVariable(item.export, varFlags[item.flag], item.name, item.value);
57
- });
58
- const functions = configRead.graphs.functions.flatMap((item) => {
59
- return createFunction({
60
- export: true,
61
- comment: item.description,
62
- name: item.name,
63
- parameters: item.parameters,
64
- body: item.body?.map(codeToAstNode),
65
- async: item.async,
66
- returnType: item.returnType,
67
- generics: item.generics,
68
- generator: item.generator
69
- });
70
- });
71
- const nodes = [
72
- ...comments,
73
- factory2.createIdentifier(""),
74
- ...imports,
75
- factory2.createIdentifier(""),
76
- ...variables,
77
- factory2.createIdentifier(""),
78
- ...functions
79
- ];
80
- if (!isGenerateType && isTypescript) {
81
- nodes.push(factory2.createIdentifier(""));
82
- nodes.push(factory2.createIdentifier(""));
83
- nodes.push(...compilerTsTypingsDeclaration(configRead, false));
84
- }
85
- return nodes;
43
+ configRead.graphs.imports = configRead.graphs.imports || [];
44
+ configRead.graphs.comments = configRead.graphs.comments || [];
45
+ configRead.graphs.variables = configRead.graphs.variables || [];
46
+ configRead.graphs.functions = configRead.graphs.functions || [];
47
+ const isGenerateType = configRead.outputs.some((v) => v.type === "typings");
48
+ const isTypescript = configRead.outputs.some((v) => v.type === "request" && v.path.endsWith(".ts"));
49
+ const comments = [createComment("multi", configRead.graphs.comments)];
50
+ const imports = configRead.graphs.imports?.map((item) => {
51
+ return createImport(item.name, item.names, item.value, item.namespace);
52
+ });
53
+ const variables = configRead.graphs.variables.map((item) => {
54
+ return createVariable(item.export, varFlags[item.flag], item.name, item.value);
55
+ });
56
+ const functions = configRead.graphs.functions.flatMap((item) => {
57
+ return createFunction({
58
+ export: true,
59
+ comment: item.description,
60
+ name: item.name,
61
+ parameters: item.parameters,
62
+ body: item.body?.map(codeToAstNode),
63
+ async: item.async,
64
+ returnType: item.returnType,
65
+ generics: item.generics,
66
+ generator: item.generator
67
+ });
68
+ });
69
+ const nodes = [
70
+ ...comments,
71
+ factory.createIdentifier(""),
72
+ ...imports,
73
+ factory.createIdentifier(""),
74
+ ...variables,
75
+ factory.createIdentifier(""),
76
+ ...functions
77
+ ];
78
+ if (!isGenerateType && isTypescript) {
79
+ nodes.push(factory.createIdentifier(""));
80
+ nodes.push(factory.createIdentifier(""));
81
+ nodes.push(...compilerTsTypingsDeclaration(configRead, false));
82
+ }
83
+ return nodes;
86
84
  }
87
85
 
88
- // src/compiler/index.ts
86
+ //#endregion
87
+ //#region src/compiler/index.ts
89
88
  function compiler(configRead) {
90
- for (const output of configRead.outputs) {
91
- if (output.type === "request")
92
- output.ast = compilerTsRequestDeclaration(configRead);
93
- if (output.type === "typings")
94
- output.ast = compilerTsTypingsDeclaration(configRead);
95
- }
96
- return configRead;
89
+ for (const output of configRead.outputs) {
90
+ if (output.type === "request") output.ast = compilerTsRequestDeclaration(configRead);
91
+ if (output.type === "typings") output.ast = compilerTsTypingsDeclaration(configRead);
92
+ }
93
+ return configRead;
97
94
  }
98
95
 
99
- // src/config/index.ts
100
- import path from "node:path";
101
- import process from "node:process";
96
+ //#endregion
97
+ //#region src/config/index.ts
102
98
  function config(userConfig) {
103
- userConfig.import = userConfig.import || {};
104
- userConfig.responseType = userConfig.responseType || {};
105
- if (typeof userConfig.output === "string")
106
- userConfig.output = { main: userConfig.output };
107
- userConfig.output = userConfig.output || {};
108
- userConfig.output.main = userConfig.output.main || "src/api/index.ts";
109
- if (typeof userConfig.baseURL === "string") {
110
- userConfig.baseURL = userConfig.baseURL.endsWith('/"') ? userConfig.baseURL = `${userConfig.baseURL.slice(0, userConfig.baseURL.length - 2)}"` : userConfig.baseURL;
111
- }
112
- if (userConfig.output?.type !== false)
113
- userConfig.output.type = userConfig.output.type || userConfig.output.main.replace(/\.ts|\.js/g, ".type.ts");
114
- if (typeof userConfig.responseType === "string")
115
- userConfig.responseType = { infer: userConfig.responseType };
116
- const userRoot = process.cwd();
117
- const isTypescript = userConfig.output.main.endsWith(".ts");
118
- const isGenerateType = userConfig.output?.type !== false;
119
- const importTypePath = userConfig.import.type || getImportTypePath(userConfig.output.main, userConfig.output.type || "");
120
- const imports = [
121
- isTypescript && isGenerateType && {
122
- name: "Types",
123
- value: importTypePath,
124
- type: true,
125
- namespace: true
126
- }
127
- ];
128
- const outputs = [
129
- {
130
- type: "request",
131
- root: path.join(userRoot, path.dirname(userConfig.output.main)),
132
- path: path.join(userRoot, userConfig.output.main)
133
- }
134
- ];
135
- const typings = [
136
- !!userConfig.responseType.infer && { export: true, name: "Infer<T>", value: userConfig.responseType.infer }
137
- ];
138
- if (userConfig.output.type !== false) {
139
- outputs.push({
140
- type: "typings",
141
- root: path.join(userRoot, path.dirname(userConfig.output.type)),
142
- import: importTypePath,
143
- path: path.join(userRoot, userConfig.output.type)
144
- });
145
- }
146
- const inputs = {};
147
- if (typeof userConfig.input === "string")
148
- inputs.uri = userConfig.input;
149
- if (typeof userConfig.input === "object")
150
- Object.assign(inputs, userConfig.input);
151
- const configRead = {
152
- config: userConfig,
153
- inputs,
154
- outputs,
155
- graphs: {
156
- imports: imports.filter(Boolean),
157
- variables: [],
158
- comments: [],
159
- functions: [],
160
- interfaces: [],
161
- typings: typings.filter(Boolean),
162
- response: userConfig.responseType
163
- }
164
- };
165
- return configRead;
99
+ userConfig.import = userConfig.import || {};
100
+ userConfig.responseType = userConfig.responseType || {};
101
+ if (typeof userConfig.output === "string") userConfig.output = { main: userConfig.output };
102
+ userConfig.output = userConfig.output || {};
103
+ userConfig.output.main = userConfig.output.main || "src/api/index.ts";
104
+ if (typeof userConfig.baseURL === "string") userConfig.baseURL = userConfig.baseURL.endsWith("/\"") ? userConfig.baseURL = `${userConfig.baseURL.slice(0, userConfig.baseURL.length - 2)}"` : userConfig.baseURL;
105
+ if (userConfig.output?.type !== false) userConfig.output.type = userConfig.output.type || userConfig.output.main.replace(/\.ts|\.js/g, ".type.ts");
106
+ if (typeof userConfig.responseType === "string") userConfig.responseType = { infer: userConfig.responseType };
107
+ const userRoot = process.cwd();
108
+ const isTypescript = userConfig.output.main.endsWith(".ts");
109
+ const isGenerateType = userConfig.output?.type !== false;
110
+ const importTypePath = userConfig.import.type || getImportTypePath(userConfig.output.main, userConfig.output.type || "");
111
+ const imports = [isTypescript && isGenerateType && {
112
+ name: "Types",
113
+ value: importTypePath,
114
+ type: true,
115
+ namespace: true
116
+ }];
117
+ const outputs = [{
118
+ type: "request",
119
+ root: path.join(userRoot, path.dirname(userConfig.output.main)),
120
+ path: path.join(userRoot, userConfig.output.main)
121
+ }];
122
+ const typings = [!!userConfig.responseType.infer && {
123
+ export: true,
124
+ name: "Infer<T>",
125
+ value: userConfig.responseType.infer
126
+ }];
127
+ if (userConfig.output.type !== false) outputs.push({
128
+ type: "typings",
129
+ root: path.join(userRoot, path.dirname(userConfig.output.type)),
130
+ import: importTypePath,
131
+ path: path.join(userRoot, userConfig.output.type)
132
+ });
133
+ const inputs = {};
134
+ if (typeof userConfig.input === "string") inputs.uri = userConfig.input;
135
+ if (typeof userConfig.input === "object") Object.assign(inputs, userConfig.input);
136
+ const configRead = {
137
+ config: userConfig,
138
+ inputs,
139
+ outputs,
140
+ graphs: {
141
+ imports: imports.filter(Boolean),
142
+ variables: [],
143
+ comments: [],
144
+ functions: [],
145
+ interfaces: [],
146
+ typings: typings.filter(Boolean),
147
+ response: userConfig.responseType
148
+ }
149
+ };
150
+ return configRead;
166
151
  }
167
- function prefix(path2) {
168
- return path2.startsWith(".") ? path2 : `./${path2}`;
152
+ function prefix(path$1) {
153
+ return path$1.startsWith(".") ? path$1 : `./${path$1}`;
169
154
  }
170
155
  function getImportTypePath(main, type) {
171
- let importTypePath = path.dirname(main);
172
- importTypePath = path.relative(importTypePath, type || "");
173
- importTypePath = prefix(importTypePath).replace(".ts", "");
174
- return importTypePath;
156
+ let importTypePath = path.dirname(main);
157
+ importTypePath = path.relative(importTypePath, type || "");
158
+ importTypePath = prefix(importTypePath).replace(".ts", "");
159
+ return importTypePath;
175
160
  }
176
161
 
177
- // src/dest/index.ts
178
- import fs from "fs-extra";
162
+ //#endregion
163
+ //#region src/dest/index.ts
179
164
  function dest(configRead) {
180
- configRead.outputs.map(async (output) => {
181
- await fs.ensureDir(output.root);
182
- await fs.writeFile(output.path, output.code || "", { flag: "w" });
183
- });
165
+ configRead.outputs.map(async (output) => {
166
+ await fs.ensureDir(output.root);
167
+ await fs.writeFile(output.path, output.code || "", { flag: "w" });
168
+ });
184
169
  }
185
170
 
186
- // src/generate/index.ts
187
- import { format } from "prettier";
188
- import { astNodeToCode } from "ts-factory-extra";
171
+ //#endregion
172
+ //#region src/generate/index.ts
189
173
  function generate(configRead) {
190
- for (const output of configRead.outputs || []) {
191
- if (output.ast)
192
- output.code = astNodeToCode(output.ast);
193
- if (output.code)
194
- output.code = formatTypescript(output.code);
195
- }
196
- return configRead;
174
+ for (const output of configRead.outputs || []) {
175
+ if (output.ast) output.code = astNodeToCode(output.ast);
176
+ if (output.code) output.code = formatTypescript(output.code);
177
+ }
178
+ return configRead;
197
179
  }
198
180
  function formatTypescript(code) {
199
- return format(code, { printWidth: 800, parser: "typescript" });
181
+ return format(code, {
182
+ printWidth: 800,
183
+ parser: "typescript"
184
+ });
200
185
  }
201
186
 
202
- // src/original/index.ts
203
- import got from "got";
187
+ //#endregion
188
+ //#region src/original/index.ts
204
189
  async function original(configRead) {
205
- if (configRead.inputs.uri)
206
- configRead.source = await got({ url: configRead.inputs.uri, responseType: "json" }).json();
207
- if (configRead.inputs.http)
208
- configRead.source = await got({ ...configRead.inputs.http, responseType: "json" }).json();
209
- if (configRead.inputs.json)
210
- configRead.source = await readJsonSource(configRead.inputs.json);
211
- if (!configRead.source)
212
- throw new Error("No source found, please check your input config.");
213
- if (!configRead.source.schemes?.length) {
214
- const schemes = [];
215
- if (configRead.inputs.uri?.startsWith("https://"))
216
- schemes.push("https", "http");
217
- if (configRead.inputs.uri?.startsWith("http://"))
218
- schemes.push("http");
219
- configRead.source.schemes = schemes;
220
- }
221
- return configRead;
190
+ if (configRead.inputs.uri) configRead.source = await got({
191
+ url: configRead.inputs.uri,
192
+ responseType: "json"
193
+ }).json();
194
+ if (configRead.inputs.http) configRead.source = await got({
195
+ ...configRead.inputs.http,
196
+ responseType: "json"
197
+ }).json();
198
+ if (configRead.inputs.json) configRead.source = await readJsonSource(configRead.inputs.json);
199
+ if (!configRead.source) throw new Error("No source found, please check your input config.");
200
+ if (!configRead.source.schemes?.length) {
201
+ const schemes = [];
202
+ if (configRead.inputs.uri?.startsWith("https://")) schemes.push("https", "http");
203
+ if (configRead.inputs.uri?.startsWith("http://")) schemes.push("http");
204
+ configRead.source.schemes = schemes;
205
+ }
206
+ return configRead;
222
207
  }
223
208
  function readJsonSource(json) {
224
- if (!json)
225
- return;
226
- if (typeof json === "object")
227
- return json;
228
- else
229
- return import(json).then((mod) => mod.default);
209
+ if (!json) return;
210
+ if (typeof json === "object") return json;
211
+ else return import(json).then((mod) => mod.default);
230
212
  }
231
213
 
232
- // src/pipeline/index.ts
233
- function pipeline(config2, original2, parser, compiler2, generate2, dest2) {
234
- const pipe = pPipe(
235
- config2,
236
- original2,
237
- parser,
238
- compiler2,
239
- generate2,
240
- dest2
241
- );
242
- return pipe;
214
+ //#endregion
215
+ //#region src/pipeline/index.ts
216
+ function pipeline(config$1, original$1, parser, compiler$1, generate$1, dest$1) {
217
+ const pipe = pPipe(config$1, original$1, parser, compiler$1, generate$1, dest$1);
218
+ return pipe;
243
219
  }
244
220
  pipeline.config = config;
245
221
  pipeline.original = original;
@@ -248,13 +224,9 @@ pipeline.compiler = compiler;
248
224
  pipeline.generate = generate;
249
225
  pipeline.dest = dest;
250
226
 
251
- // src/index.ts
252
- var index_default = pipeline;
253
- export {
254
- compiler,
255
- config,
256
- index_default as default,
257
- dest,
258
- generate,
259
- original
260
- };
227
+ //#endregion
228
+ //#region src/index.ts
229
+ var src_default = pipeline;
230
+
231
+ //#endregion
232
+ export { compiler, config, src_default as default, dest, generate, original };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@genapi/pipeline",
3
3
  "type": "module",
4
- "version": "3.4.0",
4
+ "version": "3.5.0",
5
5
  "author": "Hairyf <wwu710632@gmail.com>",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/hairyf/genapi#readme",
@@ -26,18 +26,18 @@
26
26
  "prettier": "^2.8.4",
27
27
  "ts-factory-extra": "^0.0.5",
28
28
  "typescript": "^5.0.0",
29
- "@genapi/shared": "3.4.0"
29
+ "@genapi/shared": "3.5.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/fs-extra": "^11.0.1"
33
33
  },
34
34
  "scripts": {
35
- "build": "tsup",
35
+ "build": "tsdown",
36
36
  "start": "tsx src/index.ts"
37
37
  },
38
38
  "exports": {
39
39
  ".": "./dist/index.mjs"
40
40
  },
41
41
  "module": "./dist/index.mjs",
42
- "types": "./dist/index.d.ts"
42
+ "types": "./dist/index.d.mts"
43
43
  }