@hypequery/cli 0.0.3 → 0.0.5

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 +1 @@
1
- {"version":3,"file":"load-api.d.ts","sourceRoot":"","sources":["../../src/utils/load-api.ts"],"names":[],"mappings":"AAOA,wBAAsB,aAAa,CAAC,UAAU,EAAE,MAAM,gBAgFrD"}
1
+ {"version":3,"file":"load-api.d.ts","sourceRoot":"","sources":["../../src/utils/load-api.ts"],"names":[],"mappings":"AAaA,wBAAsB,aAAa,CAAC,UAAU,EAAE,MAAM,gBAuErD"}
@@ -34,15 +34,21 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
34
34
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
35
  }
36
36
  };
37
+ var _a, _b, _c;
37
38
  import { pathToFileURL } from 'node:url';
38
- import { access } from 'node:fs/promises';
39
+ import { access, mkdtemp, writeFile, rm } from 'node:fs/promises';
40
+ import os from 'node:os';
39
41
  import path from 'node:path';
40
- import { ensureTypeScriptRuntime } from './ensure-ts-runtime.js';
42
+ import { build } from 'esbuild';
43
+ if (typeof process.setMaxListeners === 'function') {
44
+ process.setMaxListeners(0);
45
+ }
41
46
  var TYPESCRIPT_EXTENSIONS = new Set(['.ts', '.tsx', '.mts', '.cts']);
47
+ var tsconfigCache = new Map();
42
48
  export function loadApiModule(modulePath) {
43
49
  return __awaiter(this, void 0, void 0, function () {
44
- var resolved, _a, relativePath, extension, isTypeScript, error_1, moduleUrl, mod, error_2, relativePath, api, relativePath, availableExports;
45
- var _b, _c;
50
+ var resolved, _a, relativePath, extension, isTypeScript, moduleUrl, _b, mod, error_1, relativePath, api, relativePath, availableExports;
51
+ var _c;
46
52
  return __generator(this, function (_d) {
47
53
  switch (_d.label) {
48
54
  case 0:
@@ -64,39 +70,35 @@ export function loadApiModule(modulePath) {
64
70
  case 4:
65
71
  extension = path.extname(resolved).toLowerCase();
66
72
  isTypeScript = TYPESCRIPT_EXTENSIONS.has(extension);
67
- if (!isTypeScript) return [3 /*break*/, 8];
68
- _d.label = 5;
73
+ if (!isTypeScript) return [3 /*break*/, 6];
74
+ return [4 /*yield*/, bundleTypeScriptModule(resolved)];
69
75
  case 5:
70
- _d.trys.push([5, 7, , 8]);
71
- return [4 /*yield*/, ensureTypeScriptRuntime()];
76
+ _b = _d.sent();
77
+ return [3 /*break*/, 7];
72
78
  case 6:
73
- _d.sent();
74
- return [3 /*break*/, 8];
79
+ _b = "".concat(pathToFileURL(resolved).href, "?t=").concat(Date.now());
80
+ _d.label = 7;
75
81
  case 7:
76
- error_1 = _d.sent();
77
- throw new Error("Failed to load TypeScript support. This should never happen because the CLI bundles tsx.\n" +
78
- "Original error: ".concat((_b = error_1 === null || error_1 === void 0 ? void 0 : error_1.message) !== null && _b !== void 0 ? _b : error_1));
82
+ moduleUrl = _b;
83
+ _d.label = 8;
79
84
  case 8:
80
- moduleUrl = "".concat(pathToFileURL(resolved).href, "?t=").concat(Date.now());
81
- _d.label = 9;
82
- case 9:
83
- _d.trys.push([9, 11, , 12]);
85
+ _d.trys.push([8, 10, , 11]);
84
86
  return [4 /*yield*/, import(moduleUrl)];
85
- case 10:
87
+ case 9:
86
88
  mod = _d.sent();
87
- return [3 /*break*/, 12];
88
- case 11:
89
- error_2 = _d.sent();
89
+ return [3 /*break*/, 11];
90
+ case 10:
91
+ error_1 = _d.sent();
90
92
  relativePath = path.relative(process.cwd(), resolved);
91
93
  throw new Error("Failed to load module: ".concat(relativePath, "\n\n") +
92
- "Error: ".concat(error_2.message, "\n\n") +
93
- (error_2.code === 'ERR_MODULE_NOT_FOUND'
94
+ "Error: ".concat(error_1.message, "\n\n") +
95
+ (error_1.code === 'ERR_MODULE_NOT_FOUND'
94
96
  ? "This usually means:\n" +
95
97
  " \u2022 A dependency is missing (run 'npm install')\n" +
96
98
  " \u2022 An import path is incorrect\n"
97
99
  : "") +
98
- (error_2.stack ? "\nStack trace:\n".concat(error_2.stack, "\n") : ''));
99
- case 12:
100
+ (error_1.stack ? "\nStack trace:\n".concat(error_1.stack, "\n") : ''));
101
+ case 11:
100
102
  api = (_c = mod.api) !== null && _c !== void 0 ? _c : mod.default;
101
103
  if (!api || typeof api.handler !== 'function') {
102
104
  relativePath = path.relative(process.cwd(), resolved);
@@ -126,3 +128,181 @@ export function loadApiModule(modulePath) {
126
128
  });
127
129
  });
128
130
  }
131
+ var globalState = globalThis;
132
+ var tempDirPromise = (_a = globalState.__hypequeryCliTempDirPromise) !== null && _a !== void 0 ? _a : null;
133
+ var tempFiles = (_b = globalState.__hypequeryCliTempFiles) !== null && _b !== void 0 ? _b : new Set();
134
+ var cleanupHooksInstalled = (_c = globalState.__hypequeryCliCleanupInstalled) !== null && _c !== void 0 ? _c : false;
135
+ if (!globalState.__hypequeryCliTempFiles) {
136
+ globalState.__hypequeryCliTempFiles = tempFiles;
137
+ }
138
+ function ensureTempDir() {
139
+ installCleanupHooks();
140
+ if (!tempDirPromise) {
141
+ tempDirPromise = mkdtemp(path.join(os.tmpdir(), 'hypequery-cli-'));
142
+ globalState.__hypequeryCliTempDirPromise = tempDirPromise;
143
+ }
144
+ return tempDirPromise;
145
+ }
146
+ function cleanupTempFiles() {
147
+ return __awaiter(this, void 0, void 0, function () {
148
+ var _this = this;
149
+ return __generator(this, function (_a) {
150
+ switch (_a.label) {
151
+ case 0:
152
+ if (tempFiles.size === 0)
153
+ return [2 /*return*/];
154
+ return [4 /*yield*/, Promise.all(Array.from(tempFiles).map(function (file) { return __awaiter(_this, void 0, void 0, function () {
155
+ var _a;
156
+ return __generator(this, function (_b) {
157
+ switch (_b.label) {
158
+ case 0:
159
+ _b.trys.push([0, 2, , 3]);
160
+ return [4 /*yield*/, rm(file, { force: true })];
161
+ case 1:
162
+ _b.sent();
163
+ return [3 /*break*/, 3];
164
+ case 2:
165
+ _a = _b.sent();
166
+ return [3 /*break*/, 3];
167
+ case 3: return [2 /*return*/];
168
+ }
169
+ });
170
+ }); }))];
171
+ case 1:
172
+ _a.sent();
173
+ tempFiles.clear();
174
+ return [2 /*return*/];
175
+ }
176
+ });
177
+ });
178
+ }
179
+ function installCleanupHooks() {
180
+ if (cleanupHooksInstalled)
181
+ return;
182
+ cleanupHooksInstalled = true;
183
+ globalState.__hypequeryCliCleanupInstalled = true;
184
+ process.once('exit', function () {
185
+ cleanupTempFiles().catch(function () { return undefined; });
186
+ });
187
+ ['SIGINT', 'SIGTERM'].forEach(function (signal) {
188
+ process.once(signal, function () {
189
+ cleanupTempFiles().catch(function () { return undefined; });
190
+ process.exit();
191
+ });
192
+ });
193
+ }
194
+ function bundleTypeScriptModule(entryPath) {
195
+ return __awaiter(this, void 0, void 0, function () {
196
+ var relativePath, tsconfigPath, result, output, contents, tempDir, tempFile, error_2;
197
+ var _a, _b, _c, _d;
198
+ return __generator(this, function (_e) {
199
+ switch (_e.label) {
200
+ case 0:
201
+ relativePath = path.relative(process.cwd(), entryPath);
202
+ return [4 /*yield*/, findNearestTsconfig(entryPath)];
203
+ case 1:
204
+ tsconfigPath = _e.sent();
205
+ _e.label = 2;
206
+ case 2:
207
+ _e.trys.push([2, 6, , 7]);
208
+ return [4 /*yield*/, build({
209
+ entryPoints: [entryPath],
210
+ bundle: true,
211
+ format: 'esm',
212
+ platform: 'node',
213
+ target: ['node18'],
214
+ sourcemap: 'inline',
215
+ write: false,
216
+ logLevel: 'silent',
217
+ absWorkingDir: process.cwd(),
218
+ packages: 'external',
219
+ tsconfig: tsconfigPath !== null && tsconfigPath !== void 0 ? tsconfigPath : undefined,
220
+ loader: {
221
+ '.ts': 'ts',
222
+ '.tsx': 'tsx',
223
+ '.mts': 'ts',
224
+ '.cts': 'ts',
225
+ },
226
+ })];
227
+ case 3:
228
+ result = _e.sent();
229
+ output = (_b = (_a = result.outputFiles) === null || _a === void 0 ? void 0 : _a.find(function (file) { return file.path.endsWith('.js'); })) !== null && _b !== void 0 ? _b : (_c = result.outputFiles) === null || _c === void 0 ? void 0 : _c[0];
230
+ if (!output) {
231
+ throw new Error('esbuild produced no output');
232
+ }
233
+ contents = "".concat(output.text, "\n//# sourceURL=").concat(pathToFileURL(entryPath).href);
234
+ return [4 /*yield*/, ensureTempDir()];
235
+ case 4:
236
+ tempDir = _e.sent();
237
+ tempFile = path.join(tempDir, "".concat(path.basename(entryPath).replace(/[^a-zA-Z0-9_-]/g, '_'), "-").concat(Date.now(), "-").concat(Math.random().toString(36).slice(2), ".mjs"));
238
+ return [4 /*yield*/, writeFile(tempFile, contents, 'utf8')];
239
+ case 5:
240
+ _e.sent();
241
+ tempFiles.add(tempFile);
242
+ return [2 /*return*/, "".concat(pathToFileURL(tempFile).href, "?t=").concat(Date.now())];
243
+ case 6:
244
+ error_2 = _e.sent();
245
+ throw new Error("Failed to compile ".concat(relativePath, " with esbuild.\n") +
246
+ "Original error: ".concat((_d = error_2 === null || error_2 === void 0 ? void 0 : error_2.message) !== null && _d !== void 0 ? _d : error_2));
247
+ case 7: return [2 /*return*/];
248
+ }
249
+ });
250
+ });
251
+ }
252
+ function findNearestTsconfig(filePath) {
253
+ return __awaiter(this, void 0, void 0, function () {
254
+ var dir, visited, _loop_1, state_1;
255
+ var _a;
256
+ return __generator(this, function (_b) {
257
+ switch (_b.label) {
258
+ case 0:
259
+ dir = path.dirname(filePath);
260
+ visited = [];
261
+ _loop_1 = function () {
262
+ var cached_1, candidate, _c, parent_1;
263
+ return __generator(this, function (_d) {
264
+ switch (_d.label) {
265
+ case 0:
266
+ if (tsconfigCache.has(dir)) {
267
+ cached_1 = (_a = tsconfigCache.get(dir)) !== null && _a !== void 0 ? _a : null;
268
+ visited.forEach(function (pathname) { return tsconfigCache.set(pathname, cached_1); });
269
+ return [2 /*return*/, { value: cached_1 }];
270
+ }
271
+ visited.push(dir);
272
+ candidate = path.join(dir, 'tsconfig.json');
273
+ _d.label = 1;
274
+ case 1:
275
+ _d.trys.push([1, 3, , 4]);
276
+ return [4 /*yield*/, access(candidate)];
277
+ case 2:
278
+ _d.sent();
279
+ tsconfigCache.set(dir, candidate);
280
+ visited.forEach(function (pathname) { return tsconfigCache.set(pathname, candidate); });
281
+ return [2 /*return*/, { value: candidate }];
282
+ case 3:
283
+ _c = _d.sent();
284
+ parent_1 = path.dirname(dir);
285
+ if (parent_1 === dir) {
286
+ visited.forEach(function (pathname) { return tsconfigCache.set(pathname, null); });
287
+ return [2 /*return*/, { value: null }];
288
+ }
289
+ dir = parent_1;
290
+ return [3 /*break*/, 4];
291
+ case 4: return [2 /*return*/];
292
+ }
293
+ });
294
+ };
295
+ _b.label = 1;
296
+ case 1:
297
+ if (!true) return [3 /*break*/, 3];
298
+ return [5 /*yield**/, _loop_1()];
299
+ case 2:
300
+ state_1 = _b.sent();
301
+ if (typeof state_1 === "object")
302
+ return [2 /*return*/, state_1.value];
303
+ return [3 /*break*/, 1];
304
+ case 3: return [2 /*return*/];
305
+ }
306
+ });
307
+ });
308
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hypequery/cli",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Command-line interface for Hypequery",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -21,10 +21,10 @@
21
21
  "chalk": "^5.3.0",
22
22
  "commander": "^12.0.0",
23
23
  "dotenv": "^16.4.7",
24
+ "esbuild": "^0.24.0",
24
25
  "open": "^10.0.0",
25
26
  "ora": "^8.0.1",
26
- "prompts": "^2.4.2",
27
- "tsx": "^4.19.2"
27
+ "prompts": "^2.4.2"
28
28
  },
29
29
  "peerDependencies": {
30
30
  "@hypequery/clickhouse": "*",
@@ -47,4 +47,4 @@
47
47
  "publishConfig": {
48
48
  "access": "public"
49
49
  }
50
- }
50
+ }