UNPKG

@nestia/sdk

Version:

Nestia SDK and Swagger generator

100 lines 4.24 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EmittedJavaScriptPatcher = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); var EmittedJavaScriptPatcher; (function (EmittedJavaScriptPatcher) { EmittedJavaScriptPatcher.importMetaUrl = (root) => __awaiter(this, void 0, void 0, function* () { const files = yield collect(root); yield Promise.all(files.map(patch)); }); })(EmittedJavaScriptPatcher || (exports.EmittedJavaScriptPatcher = EmittedJavaScriptPatcher = {})); const TARGET = "import.meta.url"; // TypeScript can preserve this token even in CommonJS output. Node 24 then // syntax-detects the temporary `.js` file as ESM before require() can load it. const REPLACEMENT = 'require("url").pathToFileURL(__filename).href'; const collect = (location) => __awaiter(void 0, void 0, void 0, function* () { const entries = yield fs_1.default.promises.readdir(location, { withFileTypes: true, }); const nested = yield Promise.all(entries.map((entry) => __awaiter(void 0, void 0, void 0, function* () { const next = path_1.default.join(location, entry.name); if (entry.isDirectory()) return collect(next); if (entry.isFile() && /\.(?:cjs|js)$/i.test(entry.name)) return [next]; return []; }))); return nested.flat(); }); const patch = (file) => __awaiter(void 0, void 0, void 0, function* () { const before = yield fs_1.default.promises.readFile(file, "utf8"); if (before.includes(TARGET) === false) return; const after = replaceImportMetaUrl(before); if (after !== before) yield fs_1.default.promises.writeFile(file, after, "utf8"); }); const replaceImportMetaUrl = (input) => { let output = ""; let cursor = 0; for (let i = 0; i < input.length;) { if (isTarget(input, i)) { output += input.slice(cursor, i); output += REPLACEMENT; i += TARGET.length; cursor = i; continue; } const ch = input[i]; const next = input[i + 1]; if (ch === '"' || ch === "'") i = skipQuoted(input, i, ch); else if (ch === "/" && next === "/") i = skipLineComment(input, i); else if (ch === "/" && next === "*") i = skipBlockComment(input, i); else ++i; } return output + input.slice(cursor); }; const isTarget = (input, index) => input.startsWith(TARGET, index) && isBoundary(input[index - 1]) && isBoundary(input[index + TARGET.length]); const isBoundary = (ch) => ch === undefined || /[^A-Za-z0-9_$]/.test(ch); const skipQuoted = (input, index, quote) => { let escaped = false; for (let i = index + 1; i < input.length; ++i) { const ch = input[i]; if (escaped) escaped = false; else if (ch === "\\") escaped = true; else if (ch === quote) return i + 1; } return input.length; }; const skipLineComment = (input, index) => { const found = input.indexOf("\n", index + 2); return found === -1 ? input.length : found + 1; }; const skipBlockComment = (input, index) => { const found = input.indexOf("*/", index + 2); return found === -1 ? input.length : found + 2; }; //# sourceMappingURL=EmittedJavaScriptPatcher.js.map