Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update path search to not require the string 'app'
  • Loading branch information
dopry committed Feb 11, 2017
commit e47ac840f8f9ca190e08dc7250af51b9ee444d8f
31 changes: 22 additions & 9 deletions packages/@angular/cli/utilities/find-parent-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,38 @@ import * as fs from 'fs';
import * as path from 'path';
const SilentError = require('silent-error');

/**
* Find the path to the first module, moving up through the directory tree from
* currentDir.
*
* @export
* @param {*} project - the project definition.
* @param {string} currentDir - a relative path from the project root.
* @returns {string} the relative path to the first module above currentDir from the project root.
*/
export default function findParentModule(project: any, currentDir: string): string {
const sourceRoot = path.join(project.root, project.ngConfig.apps[0].root, 'app');

// trim currentDir
currentDir = currentDir.replace(path.join(project.ngConfig.apps[0].root, 'app'), '');
currentDir = currentDir.replace(project.root, '');

let pathToCheck = path.join(sourceRoot, currentDir);
// normalize to project.root
let pathToCheck = path.join(project.root, currentDir);

while (pathToCheck.length >= sourceRoot.length) {
function isModule(fileName: string) {
// TODO: refactor to not be based upon file name
const files = fs.readdirSync(pathToCheck)
.filter(fileName => !fileName.endsWith('routing.module.ts'))
.filter(fileName => fileName.endsWith('.module.ts'))
.filter(fileName => fs.statSync(path.join(pathToCheck, fileName)).isFile());
return !fileName.endsWith('routing.module.ts') &&
fileName.endsWith('.module.ts') &&
fs.statSync(path.join(pathToCheck, fileName)).isFile();
}

while (pathToCheck.length >= project.root.length) {
const files = fs.readdirSync(pathToCheck).filter(isModule);

if (files.length === 1) {
return path.join(pathToCheck, files[0]);
} else if (files.length > 1) {
throw new SilentError(`Multiple module files found: ${pathToCheck.replace(sourceRoot, '')}`);
throw new SilentError(`Multiple module files found:
${pathToCheck.replace(project.root, '')}`);
}

// move to parent directory
Expand Down