mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-19 12:34:32 +08:00
34 lines
1.1 KiB
TypeScript
34 lines
1.1 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) {
|
|
// 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');
|
|
};
|