UNPKG

@nestia/core

Version:

Super-fast validation decorators of NestJS

89 lines 4.12 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.load_controllers = void 0; const url_1 = require("url"); const SourceFinder_1 = require("../../utils/SourceFinder"); const load_controllers = (path, isTsNode) => __awaiter(void 0, void 0, void 0, function* () { var _a; const include = Array.isArray(path) ? path : typeof path === "object" ? path.include : [path]; const exclude = typeof path === "object" && !Array.isArray(path) ? ((_a = path.exclude) !== null && _a !== void 0 ? _a : []) : []; const filter = isTsNode === true ? isTypeScriptSource : isJavaScriptModule; const sources = yield SourceFinder_1.SourceFinder.find({ include, exclude, filter, }); const controllers = yield mount(sources); if (controllers.length !== 0 || isTsNode === true) return controllers; // No compiled `.js` controllers were found. Under `ttsx`, the project runs // straight from its TypeScript sources: `__dirname` still points at the // source tree (the runtime hooks serve the emitted JS under the source // URLs), so the controllers on disk are `.ts`, not `.js`. Detect that // source-run context and retry with the TypeScript filter; `import()` of each // `.ts` file is then served as the transformed emit by the hooks. if (!isTsxRuntime()) return controllers; const fallback = yield SourceFinder_1.SourceFinder.find({ include, exclude, filter: isTypeScriptSource, }); return fallback.length === 0 ? controllers : mount(fallback); }); exports.load_controllers = load_controllers; /** @internal */ function mount(sources) { return __awaiter(this, void 0, void 0, function* () { const controllers = []; for (const file of sources) { const external = yield dynamicImport((0, url_1.pathToFileURL)(file).href); for (const key in external) { const instance = external[key]; if (instance === null || (typeof instance !== "function" && typeof instance !== "object")) continue; if (Reflect.getMetadata("path", instance) !== undefined) controllers.push(instance); } } return controllers; }); } const dynamicImport = Function("specifier", "return import(specifier);"); /** * Whether the process is running from TypeScript source under `ttsx`. * * `ttsx` runs a TypeScript entry from source: it builds the owning project to a * temporary directory and installs runtime module hooks that serve that emit * under the original source URLs, exporting the manifest path through * `TTSX_RUNTIME_MANIFEST`. Its presence is the reliable signal that the * controllers on disk are `.ts` (the `.js` glob will be empty) yet `import()` * of those `.ts` files resolves to transformed JavaScript. */ function isTsxRuntime() { return (typeof process.env.TTSX_RUNTIME_MANIFEST === "string" && process.env.TTSX_RUNTIME_MANIFEST.length !== 0); } const isJavaScriptModule = (file) => /\.(?:[cm]?js)$/.test(file.toLowerCase()); const isTypeScriptSource = (file) => { const lower = file.toLowerCase(); return (/\.(?:[cm]?ts)$/.test(lower) && /\.(?:d\.[cm]?ts|d\.ts)$/.test(lower) === false); }; //# sourceMappingURL=load_controller.js.map