Skip to content

Commit

Permalink
Add support for excluding directories and refactor expandInputFiles. (
Browse files Browse the repository at this point in the history
#730)

* Add support for excluding directories and refactor `expandInputFiles` to avoid duplicated `if...else...`.

* Tests - follow refactoring done in another PR
  • Loading branch information
meowtec authored and Gerrit0 committed May 10, 2019
1 parent 14d23b8 commit 682aaf2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 35 deletions.
62 changes: 32 additions & 30 deletions src/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import { ProjectReflection } from './models/index';
import { Logger, ConsoleLogger, CallbackLogger, PluginHost, writeFile } from './utils/index';
import { createMinimatch } from './utils/paths';

import { AbstractComponent, ChildableComponent, Component, Option, DUMMY_APPLICATION_OWNER } from './utils/component';
import {
AbstractComponent,
ChildableComponent,
Component,
Option,
DUMMY_APPLICATION_OWNER
} from './utils/component';
import { Options, OptionsReadMode, OptionsReadResult } from './utils/options/index';
import { ParameterType } from './utils/options/declaration';

Expand All @@ -35,7 +41,7 @@ import { ParameterType } from './utils/options/declaration';
* and emit a series of events while processing the project. Subscribe to these Events
* to control the application flow or alter the output.
*/
@Component({name: 'application', internal: true})
@Component({ name: 'application', internal: true })
export class Application extends ChildableComponent<Application, AbstractComponent<Application>> {
options: Options;

Expand Down Expand Up @@ -63,11 +69,11 @@ export class Application extends ChildableComponent<Application, AbstractCompone

@Option({
name: 'logger',
help: 'Specify the logger that should be used, \'none\' or \'console\'',
help: "Specify the logger that should be used, 'none' or 'console'",
defaultValue: 'console',
type: ParameterType.Mixed
})
loggerType!: string|Function;
loggerType!: string | Function;

@Option({
name: 'ignoreCompilerErrors',
Expand Down Expand Up @@ -96,12 +102,12 @@ export class Application extends ChildableComponent<Application, AbstractCompone
constructor(options?: Object) {
super(DUMMY_APPLICATION_OWNER);

this.logger = new ConsoleLogger();
this.logger = new ConsoleLogger();
this.converter = this.addComponent<Converter>('converter', Converter);
this.serializer = this.addComponent<Serializer>('serializer', Serializer);
this.renderer = this.addComponent<Renderer>('renderer', Renderer);
this.plugins = this.addComponent('plugins', PluginHost);
this.options = this.addComponent('options', Options);
this.renderer = this.addComponent<Renderer>('renderer', Renderer);
this.plugins = this.addComponent('plugins', PluginHost);
this.options = this.addComponent('options', Options);

this.bootstrap(options);
}
Expand Down Expand Up @@ -156,7 +162,11 @@ export class Application extends ChildableComponent<Application, AbstractCompone
* @returns An instance of ProjectReflection on success, undefined otherwise.
*/
public convert(src: string[]): ProjectReflection | undefined {
this.logger.writeln('Using TypeScript %s from %s', this.getTypeScriptVersion(), this.getTypeScriptPath());
this.logger.writeln(
'Using TypeScript %s from %s',
this.getTypeScriptVersion(),
this.getTypeScriptPath()
);

const result = this.converter.convert(src);
if (result.errors && result.errors.length) {
Expand Down Expand Up @@ -249,37 +259,29 @@ export class Application extends ChildableComponent<Application, AbstractCompone
public expandInputFiles(inputFiles: string[] = []): string[] {
let files: string[] = [];

const exclude = this.exclude
? createMinimatch(this.exclude)
: [];
const exclude = this.exclude ? createMinimatch(this.exclude) : [];

function isExcluded(fileName: string): boolean {
return exclude.some(mm => mm.match(fileName));
}

const supportedFileRegex = this.options.getCompilerOptions().allowJs ? /\.[tj]sx?$/ : /\.tsx?$/;
function add(dirname: string) {
FS.readdirSync(dirname).forEach((file) => {
const realpath = Path.join(dirname, file);
if (FS.statSync(realpath).isDirectory()) {
add(realpath);
} else if (supportedFileRegex.test(realpath)) {
if (isExcluded(realpath.replace(/\\/g, '/'))) {
return;
}

files.push(realpath);
}
});
}
function add(file: string) {
if (isExcluded(file.replace(/\\/g, '/'))) {
return;
}

inputFiles.forEach((file) => {
file = Path.resolve(file);
if (FS.statSync(file).isDirectory()) {
add(file);
} else if (!isExcluded(file)) {
FS.readdirSync(file).forEach(next => {
add(Path.join(file, next));
});
} else if (supportedFileRegex.test(file)) {
files.push(file);
}
}

inputFiles.forEach(file => {
add(Path.resolve(file));
});

return files;
Expand Down
20 changes: 15 additions & 5 deletions src/test/typedoc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,26 @@ describe('TypeDoc', function() {
Assert(!expanded.includes(inputFiles));
});
it('Honors the exclude option even if a module is imported', () => {
application.options.setValue('exclude', '**/b.d.ts');
application.options.setValue('exclude', '**/b.ts', Assert.fail);
application.options.setValue('module', 'commonjs', Assert.fail);

function handler(context: Context) {
Assert.deepStrictEqual(context.fileNames, [
Path.resolve(__dirname, 'module', 'a.d.ts').replace(/\\/g, '/')
Path.resolve(__dirname, 'module', 'a.ts').replace(/\\/g, '/')
]);
}
application.converter.on(Converter.EVENT_END, handler);
application.convert([ Path.join(__dirname, 'module', 'a.d.ts')]);
application.converter.off(Converter.EVENT_END, handler);
application.converter.once(Converter.EVENT_END, handler);
application.convert([ Path.join(__dirname, 'module', 'a.ts')]);
});

it('supports directory excludes', function() {
const inputFiles = Path.join(__dirname, 'converter');
application.options.setValue('exclude', [ '**/access' ]);
const expanded = application.expandInputFiles([inputFiles]);

Assert.strictEqual(expanded.includes(Path.join(inputFiles, 'class', 'class.ts')), true);
Assert.strictEqual(expanded.includes(Path.join(inputFiles, 'access', 'access.ts')), false);
Assert.strictEqual(expanded.includes(inputFiles), false);
});
});
});

0 comments on commit 682aaf2

Please sign in to comment.