UNPKG

@nx/angular

Version:

The Nx Plugin for Angular contains executors, generators, and utilities for managing Angular applications and libraries within an Nx workspace. It provides: - Integration with libraries such as Storybook, Jest, ESLint, Tailwind CSS, Playwright and Cypre

65 lines (64 loc) 3.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findModuleFromOptions = findModuleFromOptions; const devkit_1 = require("@nx/devkit"); const path_1 = require("path"); // Adapted from https://github.com/angular/angular-cli/blob/732aab5fa7e63618c89dfbbb6f78753f706d7014/packages/schematics/angular/utility/find-module.ts#L29 // to match the logic from the Angular CLI component schematic. const MODULE_EXT = '.module.ts'; const ROUTING_MODULE_EXT = '-routing.module.ts'; function findModuleFromOptions(tree, options, projectRoot) { const moduleExt = options.moduleExt || MODULE_EXT; const routingModuleExt = options.routingModuleExt || ROUTING_MODULE_EXT; if (!options.module) { return (0, devkit_1.normalizePath)(findModule(tree, options.directory, projectRoot, moduleExt, routingModuleExt)); } else { const modulePath = (0, devkit_1.joinPathFragments)(options.directory, options.module); const componentPath = options.directory; const moduleBaseName = (0, path_1.basename)(modulePath); const candidateSet = new Set([options.directory]); const projectRootParent = (0, path_1.dirname)(projectRoot); for (let dir = modulePath; dir !== projectRootParent; dir = (0, path_1.dirname)(dir)) { candidateSet.add(dir); } for (let dir = componentPath; dir !== projectRoot; dir = (0, path_1.dirname)(dir)) { candidateSet.add(dir); } const candidatesDirs = [...candidateSet].sort((a, b) => b.length - a.length); for (const c of candidatesDirs) { const candidateFiles = [ '', moduleBaseName, `${moduleBaseName}.ts`, `${moduleBaseName}${moduleExt}`, ].map((x) => (0, devkit_1.joinPathFragments)(c, x)); for (const sc of candidateFiles) { if (tree.isFile(sc) && tree.read(sc, 'utf-8').includes('@NgModule')) { return (0, devkit_1.normalizePath)(sc); } } } throw new Error(`Specified module '${options.module}' does not exist.\n` + `Looked in the following directories:\n ${candidatesDirs.join('\n ')}`); } } function findModule(tree, generateDir, projectRoot, moduleExt = MODULE_EXT, routingModuleExt = ROUTING_MODULE_EXT) { let dir = generateDir; const projectRootParent = (0, path_1.dirname)(projectRoot); while (dir !== projectRootParent) { const allMatches = tree .children(dir) .map((path) => (0, devkit_1.joinPathFragments)(dir, path)) .filter((path) => tree.isFile(path) && path.endsWith(moduleExt)); const filteredMatches = allMatches.filter((path) => !path.endsWith(routingModuleExt)); if (filteredMatches.length == 1) { return filteredMatches[0]; } else if (filteredMatches.length > 1) { throw new Error("Found more than one candidate module to add the component to. Please specify which module the component should be added to by using the '--module' option."); } dir = (0, path_1.dirname)(dir); } throw new Error("Could not find a candidate module to add the component to. Please specify which module the component should be added to by using the '--module' option, or pass '--standalone' to generate a standalone component."); }