@vc-shell/api-client-generator 1.1.7 → 1.1.9

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/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ ## [1.1.9](https://github.com/VirtoCommerce/vc-shell/compare/v1.1.8...v1.1.9) (2025-05-15)
2
+
3
+
4
+
5
+ ## [1.1.8](https://github.com/VirtoCommerce/vc-shell/compare/v1.1.7...v1.1.8) (2025-05-15)
6
+
7
+
8
+ ### Features
9
+
10
+ * **generate-api-client:** enhance API client generation with new features and improvements ([fcf85db](https://github.com/VirtoCommerce/vc-shell/commit/fcf85db675390abc218b4cd1fa3f204be9a50da3))
11
+
12
+
13
+
1
14
  ## [1.1.7](https://github.com/VirtoCommerce/vc-shell/compare/v1.1.6...v1.1.7) (2025-05-12)
2
15
 
3
16
 
package/README.md CHANGED
@@ -1,83 +1,198 @@
1
- # Platform Manager REST Client
2
-
3
- # Generate API client
4
-
5
- This guide describes the process of generating an API client to access the VC Platform API from your custom application.
6
-
7
- !!! note
8
- Platform Manager REST Client offers generated REST API methods that make it easy to interact with the existing VirtoCommerce Platform API.
9
-
10
- ## Prerequisites
11
-
12
- * .NET Core 6.0, particularly if you are using MacOS or Linux.
13
-
14
- ## Generate TypeScript API clients
15
-
16
- To enable TypeScript API client generation in your project:
17
-
18
- 1. Add dependencies to your project:
19
-
20
- Using command
21
-
22
- ```bash
23
- yarn add @vc-shell/api-client-generator cross-env
24
- ```
25
- <br>
26
-
27
- `cross-env` runs scripts that set and use environment variables across platforms.
28
-
29
- Manually
30
-
31
- Add the dependencies to your project's **package.json**:
32
-
33
- ```json title="vc-app/package.json" linenums="1"
34
- {
35
- ...
36
- "devDependencies": {
37
- ...
38
- "@vc-shell/api-client-generator": "latest",
39
- "cross-env": "latest",
40
- ...
41
- }
42
- }
43
- ```
44
-
45
- 2. Configure client generation in your project. Inside your project's **package.json** file, add a `"generate-api-client"` command to the list of scripts:
46
-
47
- ```title="vc-app-extend/package.json" linenums="1"
48
- {
49
- "scripts": {
50
- ...
51
- "generate-api-client": cross-env api-client-generator --APP_PLATFORM_MODULES='[MarketplaceVendor,Catalog,Orders]' --APP_API_CLIENT_DIRECTORY=./src/api_client/
52
- }
53
- }
54
- ```
55
-
56
- The options are listed in the table below:
57
-
58
- | Options | Description | Example |
59
- |----------------------------- |---------------------------------------------------------------- |------------------------------------------------------------ |
60
- | `--APP_PLATFORM_MODULES` | Platform modules to generate API client.<br>{==string[]==} <br> Customize the `--APP_PLATFORM_MODULES` list<br>to match your project's requirements. | `--APP_PLATFORM_MODULES='[MarketplaceVendor,Orders,Catalog]'` |
61
- | `--APP_API_CLIENT_DIRECTORY` | Output directory for generated API clients. <br>{==string==} | `--APP_API_CLIENT_DIRECTORY=./src/api_client/` |
62
- | `--APP_PLATFORM_URL` | Platform URL to obtain client API configs. <br>{==string==} | `--APP_PLATFORM_URL=https://vcmp-dev.govirto.com/` |
63
- | `--APP_PACKAGE_NAME` | Package name for generated API clients. <br>{==string==} | `--APP_PACKAGE_NAME=vc-app-extend` |
64
- | `--APP_PACKAGE_VERSION` | Package version for generated API clients. <br>{==string==} | `--APP_PACKAGE_VERSION=1.0.0` |
65
- | `--APP_OUT_DIR` | Output directory for generated API clients. <br>{==string==} | `--APP_OUT_DIR=./src/api_client/` |
66
- | `--SKIP_BUILD` | Skip build step. <br>{==boolean==} | `--SKIP_BUILD=true` |
67
-
68
- 3. Configure Platform URL to ensure your project can access the platform's API configurations. Add the platform URL to your project's **.env** file:
69
-
70
- ```title="vc-app-extend/.env"
71
- APP_PLATFORM_URL=https://vcmp-dev.govirto.com/
72
- ```
73
-
74
- !!! note
75
- Alternatively, you can specify the Platform URL as a command option in the previous step when running the `"generate-api-client"` command.
76
-
77
- 4. Generate the API clients using the following command:
78
-
79
- ```
80
- yarn generate-api-client
81
- ```
82
-
83
- This command generates the required API clients for your custom application. Now you can effortlessly access the VC Platform API from your custom application using the generated API client.
1
+ # Platform Manager REST Client
2
+
3
+ # Generate API client
4
+
5
+ This guide describes the process of generating an API client to access the VC Platform API from your custom application.
6
+
7
+ !!! note
8
+ Platform Manager REST Client offers generated REST API methods that make it easy to interact with the existing VirtoCommerce Platform API.
9
+
10
+ ## Prerequisites
11
+
12
+ * .NET Core 6.0, particularly if you are using MacOS or Linux.
13
+
14
+ ## Generate TypeScript API clients
15
+
16
+ To enable TypeScript API client generation in your project:
17
+
18
+ 1. Add dependencies to your project:
19
+
20
+ Using command
21
+
22
+ ```bash
23
+ yarn add @vc-shell/api-client-generator cross-env
24
+ ```
25
+ <br>
26
+
27
+ `cross-env` runs scripts that set and use environment variables across platforms.
28
+
29
+ Manually
30
+
31
+ Add the dependencies to your project's **package.json**:
32
+
33
+ ```json title="vc-app/package.json" linenums="1"
34
+ {
35
+ ...
36
+ "devDependencies": {
37
+ ...
38
+ "@vc-shell/api-client-generator": "latest",
39
+ "cross-env": "latest",
40
+ ...
41
+ }
42
+ }
43
+ ```
44
+
45
+ 2. Configure client generation in your project. Inside your project's **package.json** file, add a `"generate-api-client"` command to the list of scripts:
46
+
47
+ ```title="vc-app-extend/package.json" linenums="1"
48
+ {
49
+ "scripts": {
50
+ ...
51
+ "generate-api-client": cross-env api-client-generator --APP_PLATFORM_MODULES='[Virtocommerce.MarketplaceVendor,Virtocommerce.Catalog,Virtocommerce.Orders]' --APP_API_CLIENT_DIRECTORY=./src/api_client/
52
+ }
53
+ }
54
+ ```
55
+
56
+ The options are listed in the table below:
57
+
58
+ | Options | Description | Example |
59
+ |----------------------------- |---------------------------------------------------------------- |------------------------------------------------------------ |
60
+ | `--APP_PLATFORM_MODULES` | Platform modules to generate API client.<br>{==string[]==} <br> Customize the `--APP_PLATFORM_MODULES` list<br>to match your project's requirements. | `--APP_PLATFORM_MODULES='[Virtocommerce.MarketplaceVendor,Virtocommerce.Orders,Virtocommerce.Catalog]'` |
61
+ | `--APP_API_CLIENT_DIRECTORY` | Output directory for generated API clients. <br>{==string==} | `--APP_API_CLIENT_DIRECTORY=./src/api_client/` |
62
+ | `--APP_PLATFORM_URL` | Platform URL to obtain client API configs. <br>{==string==} | `--APP_PLATFORM_URL=https://vcmp-dev.govirto.com/` |
63
+ | `--APP_PACKAGE_NAME` | Package name for generated API clients. <br>{==string==} | `--APP_PACKAGE_NAME=@api-client` |
64
+ | `--APP_PACKAGE_VERSION` | Package version for generated API clients. <br>{==string==} | `--APP_PACKAGE_VERSION=1.1.0` |
65
+ | `--APP_OUT_DIR` | Output directory for generated API clients. <br>{==string==} | `--APP_OUT_DIR=./src/api_client/` |
66
+ | `--APP_BUILD_DIR` | Directory where TypeScript files will be compiled. <br>{==string==} | `--APP_BUILD_DIR=lib` (default is "dist") |
67
+ | `--SKIP_BUILD` | Skip build step. <br>{==boolean==} | `--SKIP_BUILD=true` |
68
+ | `--VERBOSE` | Enable verbose logging. <br>{==boolean==} | `--VERBOSE=true` |
69
+
70
+ 3. Configure Platform URL to ensure your project can access the platform's API configurations. Add the platform URL to your project's **.env** file:
71
+
72
+ ```title="vc-app-extend/.env"
73
+ APP_PLATFORM_URL=https://vcmp-dev.govirto.com/
74
+ ```
75
+
76
+ !!! note
77
+ Alternatively, you can specify the Platform URL as a command option in the previous step when running the `"generate-api-client"` command.
78
+
79
+ 4. Generate the API clients using the following command:
80
+
81
+ ```
82
+ yarn generate-api-client
83
+ ```
84
+
85
+ This command generates the required API clients for your custom application. Now you can effortlessly access the VC Platform API from your custom application using the generated API client.
86
+
87
+ ## Features
88
+
89
+ ### Smart Configuration Merging
90
+
91
+ The generator now intelligently merges configuration files to preserve your custom settings. When you update an existing API client:
92
+
93
+ - Custom package.json fields like name, version, description, keywords, author and license are preserved
94
+ - Custom tsconfig.json settings are maintained
95
+ - Exports are intelligently updated to preserve existing paths
96
+
97
+ ### Metadata Tracking
98
+
99
+ API client now includes metadata to track the generation:
100
+
101
+ ```json
102
+ {
103
+ "apiClientGenerator": {
104
+ "version": "1.1.0",
105
+ "generatedAt": "2025-04-01T12:30:45.123Z"
106
+ }
107
+ }
108
+ ```
109
+
110
+ ### Multiple API Exports
111
+
112
+ The generator handles multiple API clients effectively:
113
+
114
+ - Creates standardized exports with both short names (`./{moduleName}`) and full names (`./virtocommerce.{moduleName}`)
115
+ - Prevents duplicate module exports by intelligently merging with existing ones
116
+ - Automatically removes `module` and `types` fields that conflict with multiple exports
117
+ - Works properly with incremental generation (can add new APIs without breaking existing ones)
118
+
119
+ ### Smart Root Export Handling
120
+
121
+ The generator now intelligently handles root exports based on the number of API modules:
122
+
123
+ - **Single API Module**: When only one API module is generated, it automatically becomes the root export (`.`) and `module`/`types` fields are maintained
124
+ - **Multiple API Modules**: When multiple API modules are generated, only subpath exports are used (e.g., `./moduleA`, `./moduleB`)
125
+ - **Preserving Preferences**: If a root export already exists, it will be preserved as long as it points to one of the generated modules
126
+
127
+ This enables both simple usage for single-API packages and proper subpath exporting for multi-API packages:
128
+
129
+ ```js
130
+ // Single API package (using root export)
131
+ import { MyClient } from '@myapp/api';
132
+
133
+ // Multi-API package (using subpath exports)
134
+ import { ClientA } from '@myapp/api/moduleA';
135
+ import { ClientB } from '@myapp/api/moduleB';
136
+ ```
137
+
138
+ ### Directory Creation
139
+
140
+ Target directories are automatically created if they don't exist.
141
+
142
+ ### Verbose Logging
143
+
144
+ Enable detailed logging to troubleshoot generation issues:
145
+
146
+ ```
147
+ yarn generate-api-client --VERBOSE=true
148
+ ```
149
+
150
+ ### Custom Build Directory
151
+
152
+ You can specify a custom build directory where TypeScript files will be compiled:
153
+
154
+ ```
155
+ yarn generate-api-client --APP_BUILD_DIR=lib
156
+ ```
157
+
158
+ By default, the build directory is "dist".
159
+
160
+ ### Error Handling Improvements
161
+
162
+ Better error handling and reporting make it easier to diagnose issues during client generation.
163
+
164
+ ## Recent Improvements (v1.1.8)
165
+
166
+ ### Fixed Issues
167
+
168
+ - **Configuration Preservation**: Fixed issue where package.json and tsconfig.json user settings were being overwritten
169
+ - **Export Path Management**: Improved export path handling to prevent incorrect paths with duplicated prefixes
170
+ - **Path Normalization**: Added better path normalization to handle edge cases correctly
171
+ - **Export Deduplication**: Implemented smarter export merging to prevent duplicate exports with different path formats
172
+ - **Build Directory Support**: Added proper support for custom build directories via APP_BUILD_DIR parameter
173
+ - **Declaration Files Location**: Fixed issue with TypeScript declaration files location to match the build directory
174
+ - **Error Handling**: Improved JSON parsing error handling with better fallback strategies
175
+ - **Root Export Handling**: Fixed issue with root exports being ignored or improperly handled
176
+
177
+ ### New Features
178
+
179
+ - **Intelligent Config Merging**: Complete rewrite of configuration merging logic to better preserve user settings
180
+ - **Export Path Standardization**: New system for standardizing export paths to improve consistency
181
+ - **Path Validation**: Added strict validation of export paths to prevent invalid configurations
182
+ - **Improved Logging**: Added more detailed logging for better debugging
183
+ - **Extended User Fields Preservation**: Expanded the list of user-defined fields that are preserved during updates
184
+ - **Better Directory Management**: Improved directory creation and validation process
185
+ - **Smart Single/Multi API Detection**: Automatically detects single API scenarios and creates appropriate root exports
186
+ - **Module/Types Field Handling**: Preserves module and types fields for single API packages while removing them for multi-API packages
187
+
188
+ ## Troubleshooting
189
+
190
+ If you encounter issues during API client generation:
191
+
192
+ 1. Enable verbose logging with `--VERBOSE=true`
193
+ 2. Ensure target directories have proper permissions
194
+ 3. Check your connectivity to the platform URL
195
+ 4. Verify that the specified platform modules exist
196
+ 5. If you encounter JSON parsing errors in tsconfig.json or package.json, try using the `--VERBOSE=true` flag to see detailed error messages
197
+ 6. Check for duplicate exports in your package.json which might cause conflicts
198
+ 7. If you manually modified exports in package.json, ensure they follow the correct format
@@ -1,186 +1,316 @@
1
- import g from "chalk";
2
- import { sync as O } from "cross-spawn";
3
- import { resolveConfig as I } from "vite";
4
- import { existsSync as D, readFileSync as C, writeFileSync as T } from "node:fs";
5
- import A, { dirname as b, resolve as L, join as m, relative as _ } from "node:path";
6
- import { fileURLToPath as R } from "node:url";
7
- import { cwd as S } from "node:process";
8
- const k = R(import.meta.url), E = b(k);
9
- class j {
1
+ import e from "chalk";
2
+ import { sync as D } from "cross-spawn";
3
+ import { resolveConfig as j } from "vite";
4
+ import { existsSync as w, mkdirSync as I, writeFileSync as R, readFileSync as O } from "node:fs";
5
+ import h, { dirname as B, resolve as E, join as A, relative as C } from "node:path";
6
+ import { fileURLToPath as b } from "node:url";
7
+ import { cwd as x } from "node:process";
8
+ import L from "mri";
9
+ const S = b(import.meta.url), N = B(S);
10
+ class k {
10
11
  workingDirectory;
11
12
  generatorDirectory;
12
13
  assetsDirectory;
13
14
  apiClientDirectory;
14
15
  nswagPaths;
15
- constructor(e) {
16
- this.workingDirectory = S(), this.generatorDirectory = L(E, ".."), this.assetsDirectory = m(this.generatorDirectory, "public", "assets"), this.apiClientDirectory = L(this.workingDirectory, e), this.nswagPaths = {
17
- configuration: m(_(this.workingDirectory, this.assetsDirectory), "config.nswag"),
16
+ constructor(r) {
17
+ this.workingDirectory = x(), this.generatorDirectory = E(N, ".."), this.assetsDirectory = A(this.generatorDirectory, "public", "assets"), this.apiClientDirectory = E(this.workingDirectory, r), this.nswagPaths = {
18
+ configuration: A(C(this.workingDirectory, this.assetsDirectory), "config.nswag"),
18
19
  authApiBase: "authApiBase.ts",
19
20
  templates: "templates"
20
21
  };
21
22
  }
22
- resolveApiClientPaths(e) {
23
- const i = `${e.toLowerCase()}.ts`;
23
+ resolveApiClientPaths(r) {
24
+ const p = `${r.toLowerCase()}.ts`;
24
25
  return {
25
- fileName: i,
26
- nswag: m(_(this.assetsDirectory, this.apiClientDirectory), i),
27
- console: m(_(this.workingDirectory, this.apiClientDirectory), i)
26
+ fileName: p,
27
+ nswag: A(C(this.assetsDirectory, this.apiClientDirectory), p),
28
+ console: A(C(this.workingDirectory, this.apiClientDirectory), p)
28
29
  };
29
30
  }
30
31
  }
32
+ function T(t, r, p = []) {
33
+ const n = { ...t };
34
+ for (const a of p)
35
+ a in r && (n[a] = r[a]);
36
+ for (const [a, i] of Object.entries(r)) {
37
+ const l = a;
38
+ a in n && typeof n[l] == "object" && !Array.isArray(n[l]) && typeof i == "object" && !Array.isArray(i) && i !== null ? n[l] = { ...n[l], ...i } : p.includes(a) || (n[l] = i);
39
+ }
40
+ return n;
41
+ }
42
+ function M(t, r, p, n) {
43
+ const a = {
44
+ extends: "@vc-shell/ts-config/tsconfig.json",
45
+ compilerOptions: {
46
+ baseUrl: ".",
47
+ declarationDir: h.join(p, "types"),
48
+ outDir: n,
49
+ rootDir: "./"
50
+ },
51
+ files: [],
52
+ include: ["*.ts"]
53
+ };
54
+ let i = { ...a };
55
+ if (w(t))
56
+ try {
57
+ const l = O(t, "utf-8");
58
+ try {
59
+ const o = JSON.parse(l);
60
+ i = T(a, o), o.compilerOptions && typeof o.compilerOptions == "object" && (i.compilerOptions = {
61
+ ...a.compilerOptions,
62
+ ...o.compilerOptions
63
+ }, i.compilerOptions.outDir = n, i.compilerOptions.declarationDir = h.join(n, "types"));
64
+ const s = Array.isArray(o.files) ? o.files : [];
65
+ i.files = Array.from(/* @__PURE__ */ new Set([...r, ...s])), console.log("api-client-generator %s Updated existing tsconfig.json", e.greenBright("success"));
66
+ } catch (o) {
67
+ console.error(
68
+ "api-client-generator %s Failed to parse existing tsconfig.json, creating new one",
69
+ e.yellow("warning"),
70
+ o
71
+ ), i = { ...a }, i.files = r;
72
+ }
73
+ } catch (l) {
74
+ console.error("api-client-generator %s Failed to read existing tsconfig.json", e.red("error"), l);
75
+ }
76
+ else
77
+ i.files = r, console.log("api-client-generator %s Created new tsconfig.json", e.greenBright("success"));
78
+ return i;
79
+ }
80
+ function _(t) {
81
+ return t = t.replace(/\.(js|ts)$/i, ""), t = t.replace(/\/\.\//g, "/"), t !== "." && !t.startsWith("./") && (t = "./" + t), t = t.replace(/\/+/g, "/"), t = t.replace(/\.\.\//g, ""), t;
82
+ }
31
83
  function y(t) {
32
- return t == null ? [] : Array.isArray(t) ? t : [t];
84
+ const r = _(t);
85
+ return r === "." ? "" : r.replace(/^\.\//, "").toLowerCase();
33
86
  }
34
- function $(t, e, i, n) {
35
- var c, s = t[e], u = ~n.string.indexOf(e) ? i == null || i === !0 ? "" : String(i) : typeof i == "boolean" ? i : ~n.boolean.indexOf(e) ? i === "false" ? !1 : i === "true" || (t._.push((c = +i, c * 0 === 0 ? c : i)), !!i) : (c = +i, c * 0 === 0 ? c : i);
36
- t[e] = s == null ? u : Array.isArray(s) ? s.concat(u) : [s, u];
87
+ function v(t) {
88
+ const r = y(t);
89
+ return r ? [`./${r}`] : [];
37
90
  }
38
- function B(t, e) {
39
- t = t || [], e = e || {};
40
- var i, n, c, s, u, o = { _: [] }, r = 0, a = 0, l = 0, f = t.length;
41
- const P = e.alias !== void 0, h = e.unknown !== void 0, p = e.default !== void 0;
42
- if (e.alias = e.alias || {}, e.string = y(e.string), e.boolean = y(e.boolean), P)
43
- for (i in e.alias)
44
- for (n = e.alias[i] = y(e.alias[i]), r = 0; r < n.length; r++)
45
- (e.alias[n[r]] = n.concat(i)).splice(r, 1);
46
- for (r = e.boolean.length; r-- > 0; )
47
- for (n = e.alias[e.boolean[r]] || [], a = n.length; a-- > 0; ) e.boolean.push(n[a]);
48
- for (r = e.string.length; r-- > 0; )
49
- for (n = e.alias[e.string[r]] || [], a = n.length; a-- > 0; ) e.string.push(n[a]);
50
- if (p) {
51
- for (i in e.default)
52
- if (s = typeof e.default[i], n = e.alias[i] = e.alias[i] || [], e[s] !== void 0)
53
- for (e[s].push(i), r = 0; r < n.length; r++)
54
- e[s].push(n[r]);
91
+ function F(t, r, p, n) {
92
+ const a = {}, i = /* @__PURE__ */ new Set(), l = [];
93
+ for (const o of t) {
94
+ const s = y(o);
95
+ if (!s) continue;
96
+ l.includes(s) || l.push(s);
97
+ const c = `./${r}/${s}.js`, g = `./${r}/types/${s}.d.ts`, f = {
98
+ import: c,
99
+ types: g
100
+ }, m = v(s);
101
+ for (const P of m)
102
+ a[P] = f, i.add(s), n && console.log(
103
+ "api-client-generator %s Created export: %s -> %s, %s",
104
+ e.blue("debug"),
105
+ e.whiteBright(P),
106
+ e.whiteBright(c),
107
+ e.whiteBright(g)
108
+ );
55
109
  }
56
- const d = h ? Object.keys(e.alias) : [];
57
- for (r = 0; r < f; r++) {
58
- if (c = t[r], c === "--") {
59
- o._ = o._.concat(t.slice(++r));
60
- break;
61
- }
62
- for (a = 0; a < c.length && c.charCodeAt(a) === 45; a++)
63
- ;
64
- if (a === 0)
65
- o._.push(c);
66
- else if (c.substring(a, a + 3) === "no-") {
67
- if (s = c.substring(a + 3), h && !~d.indexOf(s))
68
- return e.unknown(c);
69
- o[s] = !1;
70
- } else {
71
- for (l = a + 1; l < c.length && c.charCodeAt(l) !== 61; l++)
72
- ;
73
- for (s = c.substring(a, l), u = c.substring(++l) || r + 1 === f || ("" + t[r + 1]).charCodeAt(0) === 45 || t[++r], n = a === 2 ? [s] : s, l = 0; l < n.length; l++) {
74
- if (s = n[l], h && !~d.indexOf(s)) return e.unknown("-".repeat(a) + s);
75
- $(o, s, l + 1 < n.length || u, e);
110
+ if (p)
111
+ for (const [o, s] of Object.entries(p)) {
112
+ if (o === ".") {
113
+ if (typeof s == "object" && s !== null) {
114
+ const f = s;
115
+ f.import && f.types && n && console.log("api-client-generator %s Found existing root export", e.blue("debug"));
116
+ }
117
+ continue;
76
118
  }
119
+ const c = y(o);
120
+ if (!c || i.has(c)) continue;
121
+ const g = _(o);
122
+ if (typeof s == "object" && s !== null) {
123
+ const f = s;
124
+ f.import && f.types && (a[g] = {
125
+ import: _(f.import) + ".js",
126
+ types: _(f.types) + ".d.ts"
127
+ }, i.add(c), l.includes(c) || l.push(c), n && console.log(
128
+ "api-client-generator %s Preserved custom export: %s",
129
+ e.blue("debug"),
130
+ e.whiteBright(g)
131
+ ));
132
+ }
133
+ }
134
+ if (l.length === 1) {
135
+ const o = l[0], s = `./${r}/${o}.js`, c = `./${r}/types/${o}.d.ts`;
136
+ a["."] = {
137
+ import: s,
138
+ types: c
139
+ }, n && console.log(
140
+ "api-client-generator %s Created root export for single module: %s",
141
+ e.blue("debug"),
142
+ e.whiteBright(o)
143
+ );
144
+ } else if (p && "." in p) {
145
+ const o = p["."];
146
+ if (o && typeof o == "object" && o.import) {
147
+ const s = y(o.import);
148
+ if (l.includes(s)) {
149
+ const c = `./${r}/${s}.js`, g = `./${r}/types/${s}.d.ts`;
150
+ a["."] = {
151
+ import: c,
152
+ types: g
153
+ }, n && console.log(
154
+ "api-client-generator %s Preserved existing root export pointing to module: %s",
155
+ e.blue("debug"),
156
+ e.whiteBright(s)
157
+ );
158
+ } else n && console.log(
159
+ "api-client-generator %s Removed root export that pointed to non-existing module",
160
+ e.yellow("warning")
161
+ );
77
162
  }
78
163
  }
79
- if (p)
80
- for (i in e.default)
81
- o[i] === void 0 && (o[i] = e.default[i]);
82
- if (P)
83
- for (i in o)
84
- for (n = e.alias[i] || []; n.length > 0; )
85
- o[n.shift()] = o[i];
164
+ return a;
165
+ }
166
+ function $(t, r, p) {
167
+ const n = p.VERBOSE ?? !1, a = p.APP_BUILD_DIR ?? "dist", i = [];
168
+ a && i.push(a), i.push("package.json");
169
+ const l = {
170
+ name: p.APP_PACKAGE_NAME || "api-client",
171
+ version: p.APP_PACKAGE_VERSION || "1.0.0",
172
+ files: i,
173
+ exports: {},
174
+ // Add metadata to track generated content
175
+ apiClientGenerator: {
176
+ version: process.env.npm_package_version || "unknown",
177
+ generatedAt: (/* @__PURE__ */ new Date()).toISOString()
178
+ }
179
+ };
180
+ let o = { ...l }, s, c, g;
181
+ if (w(t))
182
+ try {
183
+ const u = O(t, "utf-8");
184
+ try {
185
+ const d = JSON.parse(u);
186
+ typeof d.module == "string" && (c = d.module), typeof d.types == "string" && (g = d.types), o = T(l, d, [
187
+ "name",
188
+ "version",
189
+ "description",
190
+ "keywords",
191
+ "author",
192
+ "license",
193
+ "private",
194
+ "scripts",
195
+ "devDependencies",
196
+ "dependencies"
197
+ ]), d.files && Array.isArray(d.files) && (o.files = Array.from(/* @__PURE__ */ new Set([...i, ...d.files]))), s = d.exports, console.log("api-client-generator %s Updated existing package.json", e.greenBright("success"));
198
+ } catch (d) {
199
+ console.error(
200
+ "api-client-generator %s Failed to parse existing package.json, creating new one",
201
+ e.yellow("warning"),
202
+ d
203
+ );
204
+ }
205
+ } catch (u) {
206
+ console.error("api-client-generator %s Failed to read existing package.json", e.red("error"), u);
207
+ }
208
+ else
209
+ console.log("api-client-generator %s Creating new package.json", e.greenBright("success"));
210
+ o.exports = F(r, a, s, n);
211
+ const f = r.map(y).filter(Boolean);
212
+ if (Array.from(new Set(f)).length === 1 && o.exports && "." in o.exports) {
213
+ const u = o.exports["."];
214
+ u && u.import && (o.module = u.import, n && console.log(
215
+ "api-client-generator %s Set module field to: %s",
216
+ e.blue("debug"),
217
+ e.whiteBright(u.import)
218
+ )), u && u.types && (o.types = u.types, n && console.log(
219
+ "api-client-generator %s Set types field to: %s",
220
+ e.blue("debug"),
221
+ e.whiteBright(u.types)
222
+ ));
223
+ } else
224
+ "module" in o && (delete o.module, n && console.log(
225
+ "api-client-generator %s Removed 'module' field from package.json as it conflicts with subpath exports",
226
+ e.blue("debug")
227
+ )), "types" in o && (delete o.types, n && console.log(
228
+ "api-client-generator %s Removed 'types' field from package.json as it conflicts with subpath exports",
229
+ e.blue("debug")
230
+ ));
86
231
  return o;
87
232
  }
88
- async function M() {
89
- await I({}, "build");
90
- const t = B(process.argv.slice(2)), e = process.env.APP_PLATFORM_URL ?? t.APP_PLATFORM_URL;
91
- if (!e)
233
+ async function U() {
234
+ await j({}, "build");
235
+ const t = L(process.argv.slice(2)), r = process.env.APP_PLATFORM_URL ?? t.APP_PLATFORM_URL, p = t.VERBOSE ?? !1;
236
+ if (!r)
92
237
  return console.log(
93
- g.red("error"),
238
+ e.red("error"),
94
239
  "api-client-generator APP_PLATFORM_URL is required in .env config or as api-client-generator argument"
95
240
  );
96
241
  if (!t.APP_PLATFORM_MODULES)
97
- return console.log(g.red("error"), "api-client-generator modules command is required");
242
+ return console.log(e.red("error"), "api-client-generator modules command is required");
98
243
  if (!t.APP_API_CLIENT_DIRECTORY)
99
- return console.log(g.red("error"), "api-client-generator outDir command is required");
100
- const i = t.APP_OUT_DIR ?? "./", n = new j(t.APP_API_CLIENT_DIRECTORY), c = t.APP_PLATFORM_MODULES.replace(/[[\]]/g, "").split(","), s = {};
101
- let u, o;
102
- if (!t.SKIP_BUILD && (u = A.join(t.APP_API_CLIENT_DIRECTORY, "tsconfig.json"), o = {
103
- extends: "@vc-shell/ts-config/tsconfig.json",
104
- compilerOptions: {
105
- baseUrl: ".",
106
- declarationDir: A.join(i, "types"),
107
- outDir: i,
108
- rootDir: "./"
109
- },
110
- files: [],
111
- include: ["*.ts"]
112
- }, D(u))) {
113
- const r = JSON.parse(C(u, "utf-8"));
114
- o = { ...o, ...r }, o && (o.compilerOptions = { ...o.compilerOptions, ...r.compilerOptions }, o.files = Array.from(new Set(o.files.concat(r.files || []))), o.include = Array.from(new Set(o.include.concat(r.include || [])))), console.log("api-client-generator %s Generated tsconfig.json", g.greenBright("success"));
115
- }
116
- for (const r of c) {
117
- const a = n.resolveApiClientPaths(r);
244
+ return console.log(e.red("error"), "api-client-generator outDir command is required");
245
+ const n = t.APP_OUT_DIR ?? "./", a = t.APP_BUILD_DIR ?? "dist", i = new k(t.APP_API_CLIENT_DIRECTORY);
246
+ if (!w(t.APP_API_CLIENT_DIRECTORY))
247
+ try {
248
+ I(t.APP_API_CLIENT_DIRECTORY, { recursive: !0 }), console.log(
249
+ "api-client-generator %s Created directory %s",
250
+ e.greenBright("success"),
251
+ e.whiteBright(t.APP_API_CLIENT_DIRECTORY)
252
+ );
253
+ } catch (c) {
254
+ console.error(
255
+ "api-client-generator %s Failed to create directory %s",
256
+ e.red("error"),
257
+ e.whiteBright(t.APP_API_CLIENT_DIRECTORY),
258
+ c
259
+ );
260
+ return;
261
+ }
262
+ const l = h.join(t.APP_API_CLIENT_DIRECTORY, a);
263
+ if (!w(l))
264
+ try {
265
+ I(l, { recursive: !0 }), console.log(
266
+ "api-client-generator %s Created build directory %s",
267
+ e.greenBright("success"),
268
+ e.whiteBright(l)
269
+ );
270
+ } catch (c) {
271
+ console.error(
272
+ "api-client-generator %s Failed to create build directory %s",
273
+ e.red("error"),
274
+ e.whiteBright(l),
275
+ c
276
+ );
277
+ }
278
+ const o = t.APP_PLATFORM_MODULES.replace(/[[\]]/g, "").split(","), s = [];
279
+ for (const c of o) {
280
+ const g = i.resolveApiClientPaths(c);
118
281
  console.log(
119
282
  "api-client-generator %s Generating API client for %s module on %s environment",
120
- g.green("info"),
121
- g.whiteBright(r),
122
- g.whiteBright(e)
283
+ e.green("info"),
284
+ e.whiteBright(c),
285
+ e.whiteBright(r)
123
286
  );
124
- const l = O(
125
- "npx nswag",
126
- [
127
- "run",
128
- n.nswagPaths.configuration,
129
- `/variables:APP_PLATFORM_URL=${e},APP_PLATFORM_MODULE=${r},APP_AUTH_API_BASE_PATH=${n.nswagPaths.authApiBase},APP_TEMPLATE_DIRECTORY=${n.nswagPaths.templates},APP_API_CLIENT_PATH=${a.nswag}`,
130
- "/runtime:Net60"
131
- ],
132
- {
133
- stdio: ["ignore", "inherit", "ignore"],
134
- shell: !0
135
- }
287
+ const f = [
288
+ "run",
289
+ i.nswagPaths.configuration,
290
+ `/variables:APP_PLATFORM_URL=${r},APP_PLATFORM_MODULE=${c},APP_AUTH_API_BASE_PATH=${i.nswagPaths.authApiBase},APP_TEMPLATE_DIRECTORY=${i.nswagPaths.templates},APP_API_CLIENT_PATH=${g.nswag}`,
291
+ "/runtime:Net60"
292
+ ];
293
+ p && console.log("api-client-generator %s Running command: npx nswag %s", e.blue("debug"), f.join(" ")), D("npx nswag", f, {
294
+ stdio: ["ignore", "inherit", "ignore"],
295
+ shell: !0
296
+ }).status === 0 ? (console.log(
297
+ "api-client-generator %s Successfully generated %s",
298
+ e.greenBright("success"),
299
+ e.whiteBright(g.console)
300
+ ), t.SKIP_BUILD || s.push(`${c.toLowerCase()}.ts`)) : console.error(
301
+ "api-client-generator %s Failed to generate %s",
302
+ e.red("error"),
303
+ e.whiteBright(g.console)
136
304
  );
137
- if (l.status === 0) {
138
- if (console.log(
139
- "api-client-generator %s Successfully generated %s",
140
- g.greenBright("success"),
141
- g.whiteBright(a.console)
142
- ), !t.SKIP_BUILD && o) {
143
- const f = r.toLowerCase();
144
- o.files.includes(`${f}.ts`) || o.files.push(`${f}.ts`);
145
- const P = {
146
- import: i !== "./" ? `./${i}/${f}.js` : `./${f}.js`,
147
- types: i !== "./" ? `./${i}/types/${f}.d.ts` : `./types/${f}.d.ts`
148
- }, h = Object.keys(s).find(
149
- (p) => s[p].import.includes(`${f}.js`)
150
- );
151
- h ? s[h] = P : s[`./${f}`] = P;
152
- }
153
- } else
154
- console.error(
155
- "api-client-generator %s Failed to generate %s",
156
- g.red("error"),
157
- g.whiteBright(a.console),
158
- l
159
- );
160
305
  }
161
- if (!t.SKIP_BUILD && u && o) {
162
- T(u, JSON.stringify(o, null, 2)), console.log("api-client-generator %s Compiling TypeScript files to JavaScript", g.green("info")), O("npx tsc", ["--project", u], {
306
+ if (!t.SKIP_BUILD) {
307
+ const c = h.join(t.APP_API_CLIENT_DIRECTORY, "tsconfig.json"), g = M(c, s, n, a);
308
+ R(c, JSON.stringify(g, null, 2)), console.log("api-client-generator %s Compiling TypeScript files to JavaScript", e.green("info")), D("npx tsc", ["--project", c], {
163
309
  stdio: ["ignore", "inherit", "ignore"],
164
310
  shell: !0
165
- }).status === 0 ? console.log("api-client-generator %s Successfully compiled TypeScript files", g.greenBright("success")) : console.error("api-client-generator %s Failed to compile TypeScript files", g.red("error"));
166
- const a = A.join(t.APP_API_CLIENT_DIRECTORY, "package.json");
167
- let l = {
168
- name: t.APP_PACKAGE_NAME || "api-client",
169
- version: t.APP_PACKAGE_VERSION || "1.0.0",
170
- files: i !== "./" ? [i, "package.json"] : ["package.json"],
171
- exports: s
172
- };
173
- if (D(a)) {
174
- const f = JSON.parse(C(a, "utf-8"));
175
- l = { ...l, ...f }, l.files = Array.from(new Set(l.files.concat(f.files || []))), l.exports = { ...f.exports, ...l.exports };
176
- for (const [P, h] of Object.entries(s)) {
177
- const p = h.import, d = A.basename(p), w = Object.keys(l.exports).find(
178
- (x) => l.exports[x].import.includes(d)
179
- );
180
- w ? l.exports[w] = h : l.exports[P] = h;
181
- }
182
- }
183
- T(a, JSON.stringify(l, null, 2)), console.log("api-client-generator %s Generated package.json", g.greenBright("success"));
311
+ }).status === 0 ? console.log("api-client-generator %s Successfully compiled TypeScript files", e.greenBright("success")) : console.error("api-client-generator %s Failed to compile TypeScript files", e.red("error"));
312
+ const m = h.join(t.APP_API_CLIENT_DIRECTORY, "package.json"), P = $(m, s, t);
313
+ R(m, JSON.stringify(P, null, 2)), console.log("api-client-generator %s Generated package.json", e.greenBright("success"));
184
314
  }
185
315
  }
186
- M();
316
+ U();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vc-shell/api-client-generator",
3
3
  "description": "Tool for API clients generation",
4
- "version": "1.1.7",
4
+ "version": "1.1.9",
5
5
  "type": "module",
6
6
  "bin": "./dist/api-client-generator.js",
7
7
  "files": [
@@ -13,12 +13,13 @@
13
13
  },
14
14
  "devDependencies": {
15
15
  "@types/cross-spawn": "^6.0.6",
16
- "@vc-shell/ts-config": "^1.1.7",
16
+ "@vc-shell/ts-config": "^1.1.9",
17
17
  "typescript": "^5.8.3"
18
18
  },
19
19
  "dependencies": {
20
20
  "chalk": "^2.4.2",
21
21
  "cross-spawn": "^7.0.3",
22
+ "mri": "^1.2.0",
22
23
  "nswag": "^13.20.0",
23
24
  "vite": "^6.3.3",
24
25
  "vite-plugin-dts": "^3.6.4"