angular-cli/packages/@angular/cli/utilities/find-parent-module.ts
Stephen Cavaliere 3515c3b472 fix(@angular/cli): fix issue with console prompt bailing early (#5218)
* fix(@angular/cli): fix issue with console prompt bailing early

fixes #4614

* fix(@angular/cli): fix declarable types not finding closest module

fixes #5127
2017-03-07 22:12:18 -05:00

38 lines
1.2 KiB
TypeScript

import * as fs from 'fs';
import * as path from 'path';
const SilentError = require('silent-error');
export default function findParentModule(
projectRoot: string, appRoot: string, currentDir: string): string {
const sourceRoot = path.join(projectRoot, appRoot, 'app');
// trim currentDir
currentDir = currentDir.replace(path.join(appRoot, 'app'), '');
let pathToCheck = path.join(sourceRoot, currentDir);
while (pathToCheck.length >= sourceRoot.length) {
if (!fs.existsSync(pathToCheck)) {
pathToCheck = path.dirname(pathToCheck);
continue;
}
// 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());
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, '')}`);
}
// move to parent directory
pathToCheck = path.dirname(pathToCheck);
}
throw new SilentError('No module files found');
};