Skip to content
This repository has been archived by the owner on Jun 22, 2020. It is now read-only.

Commit

Permalink
fix(file): determine correct source file root
Browse files Browse the repository at this point in the history
Use the returned configuration parsed files to determine
the common root of the source files. This will represent
the out directory tree
  • Loading branch information
mattyclarkson committed Feb 6, 2018
1 parent 5fb4a7b commit 48c0180
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
26 changes: 23 additions & 3 deletions lib/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,35 @@ const writeFile = promisify(fs.writeFile);
export interface IOptions {
path: string | Path;
options: ts.CompilerOptions;
config: ts.ParsedCommandLine;
}

function commonPathPrefix(paths: IterableIterator<string>): string {
const { done, value: left } = paths.next();
if (done) {
return '';
}
let index = left.length;
for (const right of paths) {
for (let i = 0; i < index; ++i) {
if (left.charAt(i) !== right.charAt(i)) {
index = i;
break;
}
}
}
return left.substring(0, index);
}

export default class File {
readonly source: Path;
readonly root: Path;
readonly options: ts.CompilerOptions;
private program: estree.Program | undefined;

constructor({ path, options }: IOptions) {
constructor({ path, options, config: { fileNames } }: IOptions) {
this.source = new Path(path.toString());
this.root = new Path(commonPathPrefix(fileNames[Symbol.iterator]()));
this.options = options;
}

Expand Down Expand Up @@ -94,14 +114,14 @@ export default class File {
}

get destination(): Path {
const { outDir, rootDir } = this.options;
const { outDir } = this.options;

if (!outDir) {
throw new TypeError(`Only 'outDir' is supported`);
}

const out = new Path(outDir);
const destination = out.join(this.source.relative(rootDir));
const destination = out.join(this.source.relative(this.root));
destination.extension = '.js';
return destination;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class Mapper {

async *files(): AsyncIterableIterator<File> {
const { options } = this.parsed;
yield* this.parsed.fileNames.map(path => new File({ path, options }));
yield* this.parsed.fileNames.map(path => new File({ path, options, config: this.parsed }));
}

async *map(): AsyncIterableIterator<Import | Export> {
Expand Down

0 comments on commit 48c0180

Please sign in to comment.