@equinor/fusion-framework-cli
Version:
[](./LICENSE)
92 lines • 4.12 kB
JavaScript
import { existsSync } from 'node:fs';
import { dirname, relative } from 'node:path';
import { readPackageUp, } from 'read-package-up';
import { assert } from '@equinor/fusion-framework-cli/utils';
// Implementation for defineAppPackage, returns the input as-is.
export function defineAppPackage(fnOrObject) {
// This function is intentionally a passthrough for both object and function types.
return fnOrObject;
}
/**
* Resolves the entry point of a given package.
*
* This function checks for common entry point fields in the package.json (entrypoint, main, module),
* and falls back to typical source files if not found. It returns the first existing entry.
*
* @param packageJson - The package.json object to resolve from.
* @param pkgPath - The path to the package.json file (used for relative resolution).
* @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 = '') => {
// List of possible entry points, prioritized by common convention.
const entrypoint = [
packageJson.entrypoint,
packageJson.main,
packageJson.module,
'src/index.ts',
'src/index.tsx',
'src/index.js',
'src/index.jsx',
]
// Filter out undefined/null values.
.filter((x) => !!x)
// Map to relative paths from the package root.
.map((x) => relative(dirname(pkgPath), x))
// Find the first entry that actually exists on disk.
.find((entry) => existsSync(entry));
// Assert that an entry point was found, otherwise throw.
assert(entrypoint, 'failed to resolve entrypoint');
return entrypoint;
};
/**
* Resolves the application key from the given package.json object.
*
* This function strips any leading '@' or scope from the package name, returning a normalized key.
*
* @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) => {
// Ensure the package has a name property.
assert(packageJson.name, 'expected [name] in packageJson');
// Remove leading '@' or scope from the name for normalization.
return packageJson.name.replace(/^@|\w.*\//gm, '');
};
/**
* Asserts the validity of a given package by resolving its application key and entry point.
*
* This function is useful for validating that a package is correctly structured and can be used by the framework.
*
* @param pkg - A partial representation of the application's package JSON.
* @throws Will throw if the package is missing a valid name or entry point.
*/
export const assertPackage = (pkg) => {
// Validate that the package has a valid key and entry point.
assert(resolveAppKey(pkg));
assert(resolveEntryPoint(pkg));
};
/**
* Resolves the application package by searching for the nearest `package.json` file.
*
* This function uses `read-package-up` to traverse up the directory tree and find the closest package.json.
* It returns the package contents, its path, and the root directory.
*
* @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 = async (options) => {
// Attempt to find the nearest package.json using read-package-up.
const pkg = await readPackageUp(options);
if (!pkg) {
// Throw if no package.json is found in the directory tree.
throw Error('failed to find package.json');
}
// Return the resolved package, including its root directory.
return { ...pkg, root: dirname(pkg.path) };
};
// Export the main package resolver as the default export for convenience.
export default resolveAppPackage;
//# sourceMappingURL=app-package.js.map