Skip to content

Commit 82c329e

Browse files
committed
fix: allow for non-existent source directories
1 parent 15ec1b3 commit 82c329e

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

src/extracter.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ export default class Extracter {
136136
extractApi(): API {
137137
debug(`Extracting API for ${ this.dir }`);
138138
let sourceType = this.detectSourceType();
139-
debug(`Source type for this project seems to be ${ sourceType }`);
140139
if (!sourceType) {
141140
throw new Error('Cannot extract API docs from this directory: unknown source type. Source must be Typescript or JavaScript');
142141
}
@@ -145,11 +144,21 @@ export default class Extracter {
145144
}
146145

147146
detectSourceType(): 'typescript' | 'javascript' | null {
147+
debug(`Checking source type for project located in ${ this.dir }`);
148+
let basePattern: string;
149+
if (this.sourceDirs.length > 1) {
150+
basePattern = `{${ this.sourceDirs.join(',') }}`;
151+
} else {
152+
basePattern = this.sourceDirs[0];
153+
}
154+
basePattern = path.join(basePattern, '**', '*');
148155
// Typescript
149-
if (glob(path.join(this.dir, `{${ this.sourceDirs.join(',') }}`, '**', '*.ts'))) {
156+
if (glob(`${ basePattern }.ts`).length > 0) {
157+
debug(`Found .ts files in ${ this.dir }, assuming Typescript`);
150158
return 'typescript';
151159
// JavaScript
152-
} else if (glob(path.join(this.dir, `{${ this.sourceDirs.join(',') }}`, '**', '*.js'))) {
160+
} else if (glob(`${ basePattern }.js`).length > 0) {
161+
debug(`Found no .ts files ${ this.dir }, assuming JavaScript`);
153162
return 'javascript';
154163
}
155164
return null;

src/source-extracters/javascript.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as Y from 'yuidocjs';
22
import { forEach } from 'lodash';
33
import { dirSync as tmp } from 'tmp';
44
import { inspect } from 'util';
5+
import { existsSync as exists } from 'fs';
56
import API, { FreeFunction, Package, Class, Method, Property } from '../api';
67
import ui from '../ui';
78
import * as createDebug from 'debug';
@@ -23,10 +24,11 @@ export default class JavascriptSourceExtracter extends SourceExtracter {
2324
runYuidoc(): YUIDoc.Project {
2425
debug(`Running Yuidoc to extract inline documentation`);
2526
let outDir = tmp({ prefix: 'yuidoc-output-', unsafeCleanup: true }).name;
27+
let paths = this.sourceDirs.filter(exists);
2628
let config = {
2729
quiet: true,
2830
outdir: outDir,
29-
paths: this.sourceDirs,
31+
paths,
3032
parseOnly: true
3133
};
3234
let yuidoc = new Y.YUIDoc(config);

test/javascript.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ test('javascript projects', async (t) => {
77
let extracter = new Extracter({
88
dir: path.join(__dirname, 'fixtures', 'javascript-project'),
99
pagesDir: 'guides',
10-
sourceDirs: [ 'src' ],
10+
sourceDirs: [ 'src', 'non-existent-folder' ],
1111
projectName: 'javascript-project',
1212
projectVersion: '1.0.0'
1313
});
1414
let result = extracter.extract();
1515
t.deepEqual(result, output('js'));
16-
});
16+
});

test/typescript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ test('typescript projects', async (t) => {
77
let extracter = new Extracter({
88
dir: path.join(__dirname, 'fixtures', 'typescript-project'),
99
pagesDir: 'guides',
10-
sourceDirs: [ 'src' ],
10+
sourceDirs: [ 'src', 'non-existent-folder' ],
1111
projectName: 'typescript-project',
1212
projectVersion: '1.0.0'
1313
});

0 commit comments

Comments
 (0)