UNPKG

nx-serverless-cdk

Version:

nx-serverless-cdk is an Nx plugin for creating AWS CDK applications and libraries inside an Nx monorepo. It offers the possibility to test and debug CDK applications as well as AWS Lambda functions locally. The plugin provides the full flexibility of the

132 lines 6.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.normalizeProjectOptions = void 0; const devkit_1 = require("@nx/devkit"); const workspace_1 = require("@nx/workspace"); const node_path_1 = require("node:path"); const normalizeProjectOptions = (tree, options) => { validateProjectName(options.name); return createNormalizedProjectOptions(tree, options); }; exports.normalizeProjectOptions = normalizeProjectOptions; const validateProjectName = (name) => { /** * Matches two types of project names: * * 1. Valid npm package names (e.g., '@scope/name' or 'name'). * 2. Names starting with a letter and can contain any character except whitespace and ':'. * * The second case is to support the legacy behavior (^[a-zA-Z].*$) with the difference * that it doesn't allow the ":" character. It was wrong to allow it because it would * conflict with the notation for tasks. */ const pattern = '(?:^@[a-zA-Z0-9-*~][a-zA-Z0-9-*._~]*\\/[a-zA-Z0-9-~][a-zA-Z0-9-._~]*|^[a-zA-Z][^:]*)$'; const validationRegExp = new RegExp(pattern); if (!validationRegExp.test(name)) { throw new Error(`The provided project name '${name}' is invalid. It must match the pattern '${pattern}'.`); } }; const createNormalizedProjectOptions = (tree, options) => { const projectName = options.name; const projectDirectory = getProjectDirectory(projectName, options); const projectFileName = getProjectFileName(projectName); const normalizedOptions = { projectName, projectFileName, projectRoot: projectDirectory, }; if (options.projectType === workspace_1.ProjectType.Library) { normalizedOptions.importPath = getProjectImportPath(tree, projectName, options); } return normalizedOptions; }; const getProjectDirectory = (projectName, options) => { let projectDirectory; const relativeDirectoryUnix = options.directory ? removeTrailingSlash((0, devkit_1.normalizePath)((0, node_path_1.normalize)(options.directory))) : undefined; const relativeCwdUnix = removeTrailingSlash((0, devkit_1.normalizePath)((0, node_path_1.normalize)((0, node_path_1.relative)(devkit_1.workspaceRoot, getCwd())))); if (relativeDirectoryUnix) { // If a directory has been given, it is used to determine the project directory. if (relativeDirectoryUnix === relativeCwdUnix || relativeDirectoryUnix.startsWith(`${relativeCwdUnix}/`)) { // If the relative CWD is part of the given directory, then use the directory as project directory. projectDirectory = relativeDirectoryUnix; } else { // Otherwise, append the given directory to the CWD and use the resulting path as project directory. projectDirectory = (0, devkit_1.joinPathFragments)(relativeCwdUnix, relativeDirectoryUnix); } } else { // If no directory has been given, determine the project directory with the help of the CWD. if (relativeCwdUnix.endsWith(projectName)) { // If the CWD does end with the project name, then use the CWD as project directory. projectDirectory = relativeCwdUnix; } else { // If the CWD doesn't end with the project name, then append the project name to the CWD and use the resulting path as project directory. projectDirectory = (0, devkit_1.joinPathFragments)(relativeCwdUnix, projectName); } } return projectDirectory; }; const getProjectFileName = (projectName) => { let projectFileName = projectName; // If the project name contains a npm scope. if (projectName.startsWith('@')) { const projectNameWithoutScope = projectName.split('/')[1]; projectFileName = projectNameWithoutScope; } return projectFileName; }; const getProjectImportPath = (tree, projectName, options) => { let projectImportPath; // If the project name contains a npm scope. if (projectName.startsWith('@')) { // Use the project name as npm package name, if no import path has been defined. projectImportPath = options.importPath ?? projectName; } else { // If the project name doesn't contain a npm scope. if (options.importPath) { // Use the import path as package name, if it is defined. projectImportPath = options.importPath; } else { const workspacePackageJsonName = getWorkspacePackageJsonName(tree); const workspacePackageJsonScope = workspacePackageJsonName?.startsWith('@') ? workspacePackageJsonName?.split('/')[0] : undefined; // Append the project name to the workspace npm scope and use it as package name. // If the workspace npm scope isn't defined, then use the project name as package name. projectImportPath = workspacePackageJsonScope ? `${workspacePackageJsonScope}/${projectName}` : projectName; } } return projectImportPath; }; const removeTrailingSlash = (pathUnix) => { return pathUnix.replace(/\/$/, ''); }; const getWorkspacePackageJsonName = (tree) => { if (tree.exists('package.json')) { return (0, devkit_1.readJson)(tree, 'package.json').name; } return undefined; }; /** * When running a script with the package manager (e.g. `npm run`), the package manager will * traverse the directory tree upwards until it finds a `package.json` and will set `process.cwd()` * to the folder where it found it. The actual working directory is stored in the INIT_CWD * environment variable (see here: https://docs.npmjs.com/cli/v9/commands/npm-run-script#description). */ const getCwd = () => { return process.env['INIT_CWD']?.startsWith(devkit_1.workspaceRoot) ? process.env['INIT_CWD'] : process.cwd(); }; exports.default = exports.normalizeProjectOptions; //# sourceMappingURL=normalize-project-options.js.map