UNPKG

@equinor/fusion-framework-cli

Version:

--- title: Fusion Framework CLI ---

81 lines 3.53 kB
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()); }); }; import { existsSync } from 'node:fs'; import { dirname, relative } from 'node:path'; import { readPackageUp, } from 'read-package-up'; import { assert } from './utils/assert.js'; // implementation export function defineAppPackage(fnOrObject) { return fnOrObject; } /** * Resolves the entry point of a given package. * * This function attempts to find the entry point of a package by checking several * common properties in the package's `package.json` file, such as `entrypoint`, `main`, * and `module`. If none of these properties are found, it defaults to checking for * common entry files like `src/index.ts`, `src/index.tsx`, `src/index.js`, and `src/index.jsx`. * * @param pkg - The resolved application package containing the package.json and path information. * @returns The relative path to the resolved entry point. * @throws Will throw an error if no entry point can be resolved. */ export const resolveEntryPoint = (packageJson, pkgPath = '') => { const entrypoint = [ packageJson.entrypoint, packageJson.main, packageJson.module, 'src/index.ts', 'src/index.tsx', 'src/index.js', 'src/index.jsx', ] .filter((x) => !!x) .map((x) => relative(dirname(pkgPath), x)) .find((entry) => existsSync(entry)); assert(entrypoint, 'failed to resolve entrypoint'); return entrypoint; }; /** * Resolves the application key from the given package.json object. * * @param packageJson - An object containing the 'name' property from the package.json. * @returns The resolved application key, which is the package name with any leading '@' or scope removed. * @throws Will throw an error if the 'name' property is not present in the packageJson. */ export const resolveAppKey = (packageJson) => { assert(packageJson.name, 'expected [name] in packageJson'); return packageJson.name.replace(/^@|\w.*\//gm, ''); }; /** * Asserts the validity of a given package by resolving its application key and entry point. * * @param pkg - A partial representation of the application's package JSON. */ export const assertPackage = (pkg) => { assert(resolveAppKey(pkg)); assert(resolveEntryPoint(pkg)); }; /** * Resolves the application package by searching for the nearest `package.json` file. * * @param options - Optional parameters to customize the search behavior. * @returns A promise that resolves to the found package information. * @throws Will throw an error if the `package.json` file is not found. */ export const resolveAppPackage = (options) => __awaiter(void 0, void 0, void 0, function* () { const result = yield readPackageUp(options); if (!result) { throw Error('failed to find package.json'); } return result; }); export default resolveAppPackage; //# sourceMappingURL=app-package.js.map