Skip to content

Commit

Permalink
fix: Use the common parent directory of all entrypoints as the relati…
Browse files Browse the repository at this point in the history
…ve path to guess module names
  • Loading branch information
christopherthielen committed Jun 6, 2020
1 parent 166b5d0 commit f790eee
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions typedoc-plugin-external-module-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type CustomModuleNameMappingFn = (
export class ExternalModuleNamePlugin extends ConverterComponent {
/** List of module reflections which are models to rename */
private moduleRenames: ModuleRename[] = [];
private entryPoints = [];
private baseDir = '';
private customGetModuleNameFn: CustomModuleNameMappingFn;
private defaultGetModuleNameFn: CustomModuleNameMappingFn = (match, guess) => match || guess;
private disableAutoModuleName = false;
Expand Down Expand Up @@ -87,7 +87,23 @@ export class ExternalModuleNamePlugin extends ConverterComponent {
private onBegin(context: Context) {
/** Get the program entry points */
const dir = context.program.getCurrentDirectory();
this.entryPoints = context.program.getRootFileNames().map((entry) => path.dirname(path.resolve(dir, entry)));
const rootFileNames = context.program.getRootFileNames();
const options = context.getCompilerOptions();

function commonPrefix(string1: string, string2: string) {
let idx = 0;
while (idx < string1.length && string1[idx] === string2[idx]) {
idx++;
}
return string1.substr(0, idx);
}

const commonParent = rootFileNames.reduce(
(acc, entry) => commonPrefix(acc, path.dirname(path.resolve(dir, entry))),
rootFileNames[0],
);

this.baseDir = options.rootDir || options.baseUrl || commonParent;

/** Process options */
const option = this.application.options.getValue('disableAutoModuleName');
Expand All @@ -109,18 +125,7 @@ export class ExternalModuleNamePlugin extends ConverterComponent {
let [, match] = /@module\s+([\w\u4e00-\u9fa5\.\-_/@"]+)/.exec(comment) || [];
// Make a guess based on enclosing directory structure
const filename = reflection.sources[0].file.fullFileName;
const entryPoints = this.entryPoints;

function getAutoModuleName() {
const pathsRelativeToEntrypoints = entryPoints
.map((entry) => path.dirname(path.relative(entry, filename)))
.filter((x) => !x.includes('../'))
.sort((a, b) => a.length - b.length);
// Find shortest path relative to the entry points
return pathsRelativeToEntrypoints.pop(); //.replace(/[/\\]/g, '_');
}

const guess = this.disableAutoModuleName ? undefined : getAutoModuleName();
const guess = this.disableAutoModuleName ? undefined : path.dirname(path.relative(this.baseDir, filename));

// Try the custom function
const mapper: CustomModuleNameMappingFn = this.customGetModuleNameFn || this.defaultGetModuleNameFn;
Expand Down

0 comments on commit f790eee

Please sign in to comment.