@open-norantec/herbal 1.0.2-alpha.11 → 1.0.2-alpha.13

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.
@@ -35,10 +35,20 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
36
  }
37
37
  };
38
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
39
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
40
+ if (ar || !(i in from)) {
41
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
42
+ ar[i] = from[i];
43
+ }
44
+ }
45
+ return to.concat(ar || Array.prototype.slice.call(from));
46
+ };
38
47
  Object.defineProperty(exports, "__esModule", { value: true });
39
48
  exports.ClientUtil = void 0;
40
49
  var ts = require("typescript");
41
50
  var path = require("node:path");
51
+ var fs = require("fs-extra");
42
52
  var _ = require("lodash");
43
53
  var reflect_declaration_1 = require("../transformers/reflect-declaration");
44
54
  var esbuild = require("esbuild");
@@ -46,11 +56,28 @@ var requireFromString = require("require-from-string");
46
56
  var utilities_1 = require("@open-norantec/utilities");
47
57
  var create_1 = require("../create");
48
58
  var zod_1 = require("zod");
59
+ var es_module_lexer_1 = require("es-module-lexer");
60
+ var babel = require("@babel/core");
61
+ var module = require("node:module");
49
62
  var OPTIONS_SCHEMA = zod_1.z.object({
50
63
  entry: zod_1.z.string(),
51
64
  outputFile: zod_1.z.string(),
52
65
  group: zod_1.z.string().optional(),
53
66
  });
67
+ function maybeESModule(code) {
68
+ return __awaiter(this, void 0, void 0, function () {
69
+ var _a, imports, exports;
70
+ return __generator(this, function (_b) {
71
+ switch (_b.label) {
72
+ case 0: return [4, es_module_lexer_1.init];
73
+ case 1:
74
+ _b.sent();
75
+ _a = (0, es_module_lexer_1.parse)(code), imports = _a[0], exports = _a[1];
76
+ return [2, imports.length > 0 || exports.length > 0];
77
+ }
78
+ });
79
+ });
80
+ }
54
81
  var ClientUtil = (function () {
55
82
  function ClientUtil(inputOptions, onFile, onLog) {
56
83
  this.inputOptions = inputOptions;
@@ -59,12 +86,12 @@ var ClientUtil = (function () {
59
86
  this.options = OPTIONS_SCHEMA.parse(this.inputOptions);
60
87
  }
61
88
  ClientUtil.prototype.generateClientCode = function () {
62
- var _a, _b, _c, _d, _e;
89
+ var _a, _b, _c, _d, _e, _f;
63
90
  return __awaiter(this, void 0, void 0, function () {
64
91
  var configPath, configFile, parsed, program, outputMap, outputFile, esbuildResult, text, client, requireResult, clientCode, absoluteOutputFile;
65
92
  var _this = this;
66
- return __generator(this, function (_f) {
67
- switch (_f.label) {
93
+ return __generator(this, function (_g) {
94
+ switch (_g.label) {
68
95
  case 0:
69
96
  configPath = ts.findConfigFile('.', ts.sys.fileExists, 'tsconfig.json');
70
97
  configFile = ts.readConfigFile(configPath, ts.sys.readFile);
@@ -97,6 +124,11 @@ var ClientUtil = (function () {
97
124
  entryPoints: [path.resolve(outputFile)],
98
125
  bundle: true,
99
126
  platform: 'node',
127
+ loader: {
128
+ '.node': 'base64',
129
+ },
130
+ logLevel: 'silent',
131
+ packages: 'external',
100
132
  format: 'cjs',
101
133
  write: false,
102
134
  plugins: [
@@ -112,41 +144,68 @@ var ClientUtil = (function () {
112
144
  return null;
113
145
  }
114
146
  var resolvedModule = ts.nodeModuleNameResolver(args.path, args.importer, parsed.options || {}, ts.sys).resolvedModule;
115
- if (!resolvedModule) {
147
+ if (!resolvedModule)
116
148
  return null;
117
- }
118
149
  var resolvedFileName = resolvedModule.resolvedFileName;
119
- if (!resolvedFileName || resolvedFileName.endsWith('.d.ts')) {
150
+ if (!resolvedFileName || resolvedFileName.endsWith('.d.ts'))
120
151
  return null;
121
- }
122
152
  var resolved = ts.sys.resolvePath(resolvedFileName);
123
153
  (_b = _this.onLog) === null || _b === void 0 ? void 0 : _b.call(_this, 'info', "Resolved file using TypeScript paths: ".concat(args.path, " -> ").concat(resolved, ")"));
124
- return {
125
- path: resolved,
126
- };
154
+ return { path: resolved };
127
155
  });
128
156
  },
129
157
  },
130
158
  {
131
- name: 'vfs',
159
+ name: 'herbal',
132
160
  setup: function (build) {
133
- build.onResolve({ filter: /.*/ }, function (args) {
134
- var fullPath = path.resolve(path.dirname(args.importer), args.path);
135
- var attemptPaths = [];
136
- if (!fullPath.endsWith('.js')) {
137
- attemptPaths.push("".concat(fullPath, ".js"));
138
- attemptPaths.push(path.resolve(fullPath, 'index.js'));
139
- }
140
- else {
141
- attemptPaths.push(fullPath);
142
- }
143
- for (var _i = 0, attemptPaths_1 = attemptPaths; _i < attemptPaths_1.length; _i++) {
144
- var attemptPath = attemptPaths_1[_i];
145
- if (outputMap.has(attemptPath))
146
- return { path: attemptPath, namespace: 'vfs' };
147
- }
148
- return { path: args.path, external: true };
149
- });
161
+ build.onResolve({ filter: /.*/ }, function (args) { return __awaiter(_this, void 0, void 0, function () {
162
+ var targetPaths, absoluteImportPath, _i, targetPaths_1, targetPath, requiredPath;
163
+ return __generator(this, function (_a) {
164
+ if (args.path.startsWith('node:') ||
165
+ module.builtinModules.some(function (moduleName) { return args.path.startsWith(moduleName) || args.path.startsWith("".concat(moduleName, "/")); })) {
166
+ return [2, { path: args.path, external: true }];
167
+ }
168
+ if (outputMap.has(args.path))
169
+ return [2, { path: args.path, namespace: 'vfs' }];
170
+ if (outputMap.has(args.importer)) {
171
+ targetPaths = [];
172
+ absoluteImportPath = path.resolve(path.dirname(args.importer), args.path);
173
+ if (!['.js', '.cjs'].includes(path.extname(absoluteImportPath))) {
174
+ targetPaths.push(absoluteImportPath + '.js');
175
+ targetPaths.push(absoluteImportPath + '.cjs');
176
+ targetPaths.push(path.resolve(absoluteImportPath, 'index.js'));
177
+ targetPaths.push(path.resolve(absoluteImportPath, 'index.cjs'));
178
+ }
179
+ else {
180
+ targetPaths.push(absoluteImportPath);
181
+ }
182
+ for (_i = 0, targetPaths_1 = targetPaths; _i < targetPaths_1.length; _i++) {
183
+ targetPath = targetPaths_1[_i];
184
+ if (outputMap.has(targetPath)) {
185
+ return [2, { path: targetPath, namespace: 'vfs' }];
186
+ }
187
+ }
188
+ }
189
+ requiredPath = _.attempt(function () {
190
+ return require.resolve(args.path, {
191
+ paths: __spreadArray(__spreadArray([], (function () {
192
+ var result = [];
193
+ var currentDir = path.dirname(args.importer);
194
+ result.push(currentDir);
195
+ while (currentDir !== path.dirname(currentDir)) {
196
+ result.push(path.dirname(currentDir));
197
+ currentDir = path.dirname(currentDir);
198
+ }
199
+ return result;
200
+ })(), true), (require.resolve.paths('') || []), true),
201
+ });
202
+ });
203
+ if (!(requiredPath instanceof Error)) {
204
+ return [2, { path: requiredPath, namespace: outputMap.has(requiredPath) ? 'vfs' : undefined }];
205
+ }
206
+ return [2, { path: args.path, external: true }];
207
+ });
208
+ }); });
150
209
  build.onLoad({ filter: /.*/, namespace: 'vfs' }, function (args) {
151
210
  var contents = outputMap.get(args.path);
152
211
  return {
@@ -154,12 +213,43 @@ var ClientUtil = (function () {
154
213
  loader: 'js',
155
214
  };
156
215
  });
216
+ build.onLoad({ filter: /node_modules\/.*.(mjs|js)$/ }, function (args) { return __awaiter(_this, void 0, void 0, function () {
217
+ var code, transformed;
218
+ return __generator(this, function (_a) {
219
+ switch (_a.label) {
220
+ case 0:
221
+ code = fs.readFileSync(args.path, { encoding: 'utf-8' });
222
+ return [4, maybeESModule(code)];
223
+ case 1:
224
+ if (_a.sent()) {
225
+ transformed = babel.transformSync(code, {
226
+ plugins: [require.resolve('@babel/plugin-transform-modules-commonjs')],
227
+ });
228
+ return [2, {
229
+ contents: (transformed === null || transformed === void 0 ? void 0 : transformed.code) || code,
230
+ loader: 'js',
231
+ }];
232
+ }
233
+ return [2, {
234
+ contents: code,
235
+ loader: 'js',
236
+ }];
237
+ }
238
+ });
239
+ }); });
240
+ build.onLoad({ filter: /.*/, namespace: 'json' }, function (args) {
241
+ var contents = fs.readJsonSync(args.path, { encoding: 'utf-8' });
242
+ return {
243
+ contents: "module.exports = ".concat(JSON.stringify(contents)),
244
+ loader: 'js',
245
+ };
246
+ });
157
247
  },
158
248
  },
159
249
  ],
160
250
  }))];
161
251
  case 1:
162
- esbuildResult = _f.sent();
252
+ esbuildResult = _g.sent();
163
253
  if (esbuildResult instanceof Error) {
164
254
  (_c = this.onLog) === null || _c === void 0 ? void 0 : _c.call(this, 'error', "Failed to build client code: ".concat(esbuildResult.message));
165
255
  return [2];
@@ -170,20 +260,26 @@ var ClientUtil = (function () {
170
260
  }
171
261
  text = esbuildResult.outputFiles[0].text;
172
262
  client = undefined;
263
+ fs.writeFileSync(path.resolve('dist/test.js'), text, { encoding: 'utf-8' });
173
264
  try {
174
265
  requireResult = requireFromString(text, { appendPaths: [path.resolve(process.cwd())] });
175
266
  client = requireResult;
176
267
  if (!(client instanceof create_1.Client))
177
268
  client = client === null || client === void 0 ? void 0 : client.default;
178
269
  }
179
- catch (_g) { }
180
- if (!(client instanceof create_1.Client)) {
181
- (_d = this.onLog) === null || _d === void 0 ? void 0 : _d.call(this, 'error', 'Failed to load client code');
270
+ catch (error) {
271
+ if (error instanceof Error) {
272
+ (_d = this.onLog) === null || _d === void 0 ? void 0 : _d.call(this, 'error', "Failed to load client code: ".concat(error.message));
273
+ }
274
+ }
275
+ console.log('LENCONDA:test', client instanceof create_1.Client);
276
+ if (!(typeof (client === null || client === void 0 ? void 0 : client.generateClientSourceFile) === 'function')) {
277
+ (_e = this.onLog) === null || _e === void 0 ? void 0 : _e.call(this, 'error', 'Failed to load client code');
182
278
  return [2];
183
279
  }
184
280
  clientCode = _.attempt(function () { return client.generateClientSourceFile(_this.options.group); });
185
281
  if (clientCode instanceof Error) {
186
- (_e = this.onLog) === null || _e === void 0 ? void 0 : _e.call(this, 'error', "Failed to generate client code: ".concat(clientCode.message));
282
+ (_f = this.onLog) === null || _f === void 0 ? void 0 : _f.call(this, 'error', "Failed to generate client code: ".concat(clientCode.message));
187
283
  return [2];
188
284
  }
189
285
  absoluteOutputFile = path.resolve(this.options.outputFile);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-norantec/herbal",
3
- "version": "1.0.2-alpha.11",
3
+ "version": "1.0.2-alpha.13",
4
4
  "description": "Herbal is a builder and toolchain for Nest.js applications",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -35,12 +35,16 @@
35
35
  "typescript": ">=5.1.0 <5.2.0"
36
36
  },
37
37
  "dependencies": {
38
+ "@alcalzone/esm2cjs": "^1.4.2",
39
+ "@babel/core": "^7.29.0",
40
+ "@babel/plugin-transform-modules-commonjs": "^7.28.6",
38
41
  "@nestjs/common": "^10.0.0",
39
42
  "@nestjs/core": "^10.4.19",
40
43
  "@nestjs/sequelize": "^10.0.1",
41
44
  "@open-norantec/forge": "latest",
42
45
  "@open-norantec/utilities": "latest",
43
46
  "commander": "^12.1.0",
47
+ "es-module-lexer": "^2.0.0",
44
48
  "esbuild": "^0.28.0",
45
49
  "fs-extra": "^11.3.4",
46
50
  "lodash": "^4.17.21",
@@ -60,6 +64,7 @@
60
64
  },
61
65
  "devDependencies": {
62
66
  "@stylistic/eslint-plugin": "^2.12.1",
67
+ "@types/babel__core": "^7.20.5",
63
68
  "@types/express": "^5.0.3",
64
69
  "@types/fs-extra": "^11.0.4",
65
70
  "@types/lodash": "^4.17.16",