Skip to content

Commit

Permalink
feat: Add a --disableAutoModuleName flag
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen committed Jun 5, 2020
1 parent efcc02a commit a8561e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
7 changes: 6 additions & 1 deletion index.js
@@ -1,5 +1,10 @@
var plugin = require('./typedoc-plugin-external-module-name');
module.exports = function(PluginHost) {
module.exports = function (PluginHost) {
var app = PluginHost.owner;
/**
* used like so:
* --disableAutoModuleName true
*/
app.options.addDeclaration({ name: 'disableAutoModuleName', defaultValue: false });
app.converter.addComponent('external-module-name', plugin.ExternalModuleNamePlugin);
};
28 changes: 18 additions & 10 deletions typedoc-plugin-external-module-name.ts
Expand Up @@ -62,6 +62,7 @@ export class ExternalModuleNamePlugin extends ConverterComponent {
private entryPoints = [];
private customGetModuleNameFn: CustomModuleNameMappingFn;
private defaultGetModuleNameFn: CustomModuleNameMappingFn = (match, guess) => match || guess;
private disableAutoModuleName = false;

initialize() {
this.listenTo(this.owner, {
Expand All @@ -83,12 +84,14 @@ export class ExternalModuleNamePlugin extends ConverterComponent {
}
}

/**
* Get the program entry points
*/
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)));

/** Process options */
const option = this.application.options.getValue('disableAutoModuleName');
this.disableAutoModuleName = option === 'true' || option === true;
}

/**
Expand All @@ -104,15 +107,20 @@ export class ExternalModuleNamePlugin extends ConverterComponent {
let preferred = /@preferred/.exec(comment) != null;
// Look for @module
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 pathsRelativeToEntrypoints = this.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
const guess = pathsRelativeToEntrypoints.pop(); //.replace(/[/\\]/g, '_');
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();

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

0 comments on commit a8561e0

Please sign in to comment.