fix: support trailing slash in basePath (#10533)

This commit is contained in:
Miško Hevery 2016-08-05 11:11:24 -07:00 committed by GitHub
parent 83e2d3d1cb
commit 0d1f3c3b07
2 changed files with 14 additions and 8 deletions

View File

@ -30,11 +30,17 @@ export class ReflectorHost implements StaticReflectorHost, ImportGenerator {
private metadataCollector = new MetadataCollector(); private metadataCollector = new MetadataCollector();
private context: ReflectorHostContext; private context: ReflectorHostContext;
private isGenDirChildOfRootDir: boolean; private isGenDirChildOfRootDir: boolean;
private basePath: string;
private genDir: string;
constructor( constructor(
private program: ts.Program, private compilerHost: ts.CompilerHost, private program: ts.Program, private compilerHost: ts.CompilerHost,
private options: AngularCompilerOptions, context?: ReflectorHostContext) { private options: AngularCompilerOptions, context?: ReflectorHostContext) {
// normalize the path so that it never ends with '/'.
this.basePath = path.normalize(path.join(this.options.basePath, '.'));
this.genDir = path.normalize(path.join(this.options.genDir, '.'));
this.context = context || new NodeReflectorHostContext(); this.context = context || new NodeReflectorHostContext();
var genPath: string = path.relative(options.basePath, options.genDir); var genPath: string = path.relative(this.basePath, this.genDir);
this.isGenDirChildOfRootDir = genPath === '' || !genPath.startsWith('..'); this.isGenDirChildOfRootDir = genPath === '' || !genPath.startsWith('..');
} }
@ -108,7 +114,7 @@ export class ReflectorHost implements StaticReflectorHost, ImportGenerator {
// rewrite to genDir path // rewrite to genDir path
if (importModule) { if (importModule) {
// it is generated, therefore we do a relative path to the factory // it is generated, therefore we do a relative path to the factory
return this.dotRelative(containingDir, this.options.genDir + NODE_MODULES + importModule); return this.dotRelative(containingDir, this.genDir + NODE_MODULES + importModule);
} else { } else {
// assume that import is also in `genDir` // assume that import is also in `genDir`
importedFile = this.rewriteGenDirPath(importedFile); importedFile = this.rewriteGenDirPath(importedFile);
@ -121,7 +127,7 @@ export class ReflectorHost implements StaticReflectorHost, ImportGenerator {
} else { } else {
if (!this.isGenDirChildOfRootDir) { if (!this.isGenDirChildOfRootDir) {
// assume that they are on top of each other. // assume that they are on top of each other.
importedFile = importedFile.replace(this.options.basePath, this.options.genDir); importedFile = importedFile.replace(this.basePath, this.genDir);
} }
return this.dotRelative(containingDir, importedFile); return this.dotRelative(containingDir, importedFile);
} }
@ -140,11 +146,11 @@ export class ReflectorHost implements StaticReflectorHost, ImportGenerator {
var nodeModulesIndex = filepath.indexOf(NODE_MODULES); var nodeModulesIndex = filepath.indexOf(NODE_MODULES);
if (nodeModulesIndex !== -1) { if (nodeModulesIndex !== -1) {
// If we are in node_modulse, transplant them into `genDir`. // If we are in node_modulse, transplant them into `genDir`.
return path.join(this.options.genDir, filepath.substring(nodeModulesIndex)); return path.join(this.genDir, filepath.substring(nodeModulesIndex));
} else { } else {
// pretend that containing file is on top of the `genDir` to normalize the paths. // pretend that containing file is on top of the `genDir` to normalize the paths.
// we apply the `genDir` => `rootDir` delta through `rootDirPrefix` later. // we apply the `genDir` => `rootDir` delta through `rootDirPrefix` later.
return filepath.replace(this.options.basePath, this.options.genDir); return filepath.replace(this.basePath, this.genDir);
} }
} }
@ -156,7 +162,7 @@ export class ReflectorHost implements StaticReflectorHost, ImportGenerator {
throw new Error('Resolution of relative paths requires a containing file.'); throw new Error('Resolution of relative paths requires a containing file.');
} }
// Any containing file gives the same result for absolute imports // Any containing file gives the same result for absolute imports
containingFile = path.join(this.options.basePath, 'index.ts'); containingFile = path.join(this.basePath, 'index.ts');
} }
try { try {

View File

@ -35,7 +35,7 @@ describe('reflector_host', () => {
} }
reflectorNestedGenDir = new ReflectorHost( reflectorNestedGenDir = new ReflectorHost(
program, host, { program, host, {
genDir: '/tmp/project/src/gen', genDir: '/tmp/project/src/gen/',
basePath: '/tmp/project/src', basePath: '/tmp/project/src',
skipMetadataEmit: false, skipMetadataEmit: false,
skipTemplateCodegen: false, skipTemplateCodegen: false,
@ -45,7 +45,7 @@ describe('reflector_host', () => {
reflectorSiblingGenDir = new ReflectorHost( reflectorSiblingGenDir = new ReflectorHost(
program, host, { program, host, {
genDir: '/tmp/project/gen', genDir: '/tmp/project/gen',
basePath: '/tmp/project/src', basePath: '/tmp/project/src/',
skipMetadataEmit: false, skipMetadataEmit: false,
skipTemplateCodegen: false, skipTemplateCodegen: false,
trace: false trace: false