UNPKG

@lenne.tech/cli

Version:

lenne.Tech CLI: lt

129 lines (128 loc) 5.22 kB
"use strict"; /** * Framework-detection helpers for @lenne.tech/nuxt-extensions consumer projects. * * lenne.tech frontend projects can consume the framework in two modes: * * - **npm mode** (classic): `@lenne.tech/nuxt-extensions` is installed as an npm * dependency. Framework source lives in * `node_modules/@lenne.tech/nuxt-extensions/`. The Nuxt config references * the module via `modules: ['@lenne.tech/nuxt-extensions']`. * * - **vendored mode**: The framework's source is copied directly * into the project at `<app-root>/app/core/` as first-class project code. * There is **no** `@lenne.tech/nuxt-extensions` dependency in `package.json`. * The Nuxt config references `modules: ['./app/core/module']`. * * The detection is driven by the presence of `<app-root>/app/core/VENDOR.md` * (a baseline + patch-log file written by the vendoring pipeline). * * This module centralizes the detection logic so that every CLI command which * emits or patches nuxt-extensions-aware code can branch consistently. */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.detectFrontendFrameworkMode = detectFrontendFrameworkMode; exports.findAppDir = findAppDir; exports.getFrontendFrameworkRootPath = getFrontendFrameworkRootPath; exports.isVendoredAppProject = isVendoredAppProject; const node_fs_1 = require("node:fs"); const path = __importStar(require("node:path")); /** * Determines the current frontend framework consumption mode of the given project. * * Returns `'vendor'` if `VENDOR.md` indicates vendored mode. Otherwise * returns `'npm'` (the classic mode where `@lenne.tech/nuxt-extensions` is an * npm dependency). */ function detectFrontendFrameworkMode(appDir) { return isVendoredAppProject(appDir) ? 'vendor' : 'npm'; } /** * Walks up from `startDir` looking for the nearest `nuxt.config.ts` (or * `nuxt.config.js`), returning the directory that contains it. Used by * commands invoked from a sub-directory of a frontend project. * * Returns `undefined` if no Nuxt config is found up to the filesystem root. */ function findAppDir(startDir) { let current = path.resolve(startDir); const root = path.parse(current).root; while (current !== root) { if ((0, node_fs_1.existsSync)(path.join(current, 'nuxt.config.ts')) || (0, node_fs_1.existsSync)(path.join(current, 'nuxt.config.js'))) { return current; } current = path.dirname(current); } return undefined; } /** * Returns the filesystem root of the frontend framework source for the project. * * - npm mode: `<appDir>/node_modules/@lenne.tech/nuxt-extensions` * - vendor mode: `<appDir>/app/core` * * Consumers that need to introspect framework source files should use this * instead of hard-coding either path. */ function getFrontendFrameworkRootPath(appDir) { return isVendoredAppProject(appDir) ? path.join(appDir, 'app', 'core') : path.join(appDir, 'node_modules', '@lenne.tech', 'nuxt-extensions'); } /** * Detects whether the given frontend project directory runs in vendored mode. * * A project is considered vendored when: * 1. `<appDir>/app/core/VENDOR.md` exists, AND * 2. The VENDOR.md content references `@lenne.tech/nuxt-extensions` (guards * against coincidental unrelated `VENDOR.md` files). * * @param appDir Absolute path to the frontend project (the directory that * contains `nuxt.config.ts` and `app/`). */ function isVendoredAppProject(appDir) { const vendorMd = path.join(appDir, 'app', 'core', 'VENDOR.md'); if (!(0, node_fs_1.existsSync)(vendorMd)) { return false; } try { const content = (0, node_fs_1.readFileSync)(vendorMd, 'utf-8'); return content.includes('@lenne.tech/nuxt-extensions'); } catch (_a) { return false; } }