Skip to content
This repository has been archived by the owner on May 25, 2019. It is now read-only.

Add extensions option for TypeScript support and other extensions in the future #9

Closed
wants to merge 10 commits into from
Empty file added fixtr/ext/typescript.ts
Empty file.
68 changes: 51 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ function defaultExcludePatterns() {
];
}

function defaultIncludePatterns() {
return [
'test.js',
'test-*.js',
function defaultIncludePatterns(extensions) {
var patterns = [
'test',
'**/__tests__',
'**/*.test.js'
'**/__tests__'
];

extensions.forEach(function (ext) {
patterns = patterns.concat([
'test.' + ext,
'test-*.' + ext,
'**/*.test.' + ext
]);
});

return patterns;
}

function AvaFiles(options) {
Expand All @@ -35,13 +42,15 @@ function AvaFiles(options) {
options = options || {};

var files = options.files;
var extensions = options.extensions || ['js'];

if (!files || !files.length) {
files = defaultIncludePatterns();
files = defaultIncludePatterns(extensions);
}

this.excludePatterns = defaultExcludePatterns();
this.files = files;
this.extensions = extensions;
this.sources = options.sources || [];
this.cwd = options.cwd || process.cwd();

Expand All @@ -51,6 +60,7 @@ function AvaFiles(options) {
AvaFiles.prototype.findTestFiles = function () {
return handlePaths(this.files, this.excludePatterns, {
cwd: this.cwd,
extensions: this.extensions,
cache: Object.create(null),
statCache: Object.create(null),
realpathCache: Object.create(null),
Expand Down Expand Up @@ -93,7 +103,12 @@ AvaFiles.prototype.isSource = function (filePath) {

// Same defaults as used for Chokidar.
if (!hasPositivePattern) {
mixedPatterns = ['package.json', '**/*.js'].concat(mixedPatterns);
var patterns = ['package.json'];
this.extensions.forEach(function (ext) {
patterns.push('**/*.' + ext);
});

mixedPatterns = patterns.concat(mixedPatterns);
}

filePath = matchable(filePath);
Expand Down Expand Up @@ -125,9 +140,10 @@ AvaFiles.prototype.isSource = function (filePath) {
AvaFiles.prototype.isTest = function (filePath) {
var excludePatterns = this.excludePatterns;
var initialPatterns = this.files.concat(excludePatterns);
var extensions = this.extensions;

// Like in api.js, tests must be .js files and not start with _
if (path.extname(filePath) !== '.js' || path.basename(filePath)[0] === '_') {
if (!isValidFileName(filePath, extensions)) {
return false;
}

Expand Down Expand Up @@ -159,11 +175,14 @@ AvaFiles.prototype.isTest = function (filePath) {

// Check if any of the possible subpaths match a pattern. If so, generate a
// new pattern with **/*.js.
var recursivePatterns = subpaths.filter(function (subpath) {
var recursivePatterns = [];
subpaths.filter(function (subpath) {
return multimatch(subpath, initialPatterns).length === 1;
}).map(function (subpath) {
}).forEach(function (subpath) {
// Always use / to makes multimatch consistent across platforms.
return subpath + '/**/*.js';
extensions.forEach(function (ext) {
recursivePatterns.push(subpath + '/**/*.' + ext);
});
});

// See if the entire path matches any of the subpaths patterns, taking the
Expand Down Expand Up @@ -197,7 +216,9 @@ AvaFiles.prototype.getChokidarPatterns = function () {
ignored = getDefaultIgnorePatterns().concat(ignored, overrideDefaultIgnorePatterns);

if (paths.length === 0) {
paths = ['package.json', '**/*.js'];
paths = ['package.json'].concat(this.extensions.map(function (ext) {
return '**/*.' + ext;
}));
}

paths = paths.concat(this.files);
Expand Down Expand Up @@ -241,14 +262,17 @@ function handlePaths(files, excludePatterns, globOptions) {

searchedParents[file] = true;

var pattern = path.join(file, '**', '*.js');
var patterns = [];
globOptions.extensions.forEach(function (ext) {
patterns.push(file + '/**/*.' + ext);
});

if (process.platform === 'win32') {
// Always use / in patterns, harmonizing matching across platforms.
pattern = slash(pattern);
patterns = patterns.map(slash);
}

return handlePaths([pattern], excludePatterns, globOptions);
return handlePaths(patterns, excludePatterns, globOptions);
}

// globby returns slashes even on Windows. Normalize here so the file
Expand All @@ -257,7 +281,7 @@ function handlePaths(files, excludePatterns, globOptions) {
})
.then(flatten)
.filter(function (file) {
return file && path.extname(file) === '.js' && path.basename(file)[0] !== '_';
return isValidFileName(file, globOptions.extensions);
})
.map(function (file) {
return path.resolve(file);
Expand All @@ -269,6 +293,16 @@ function handlePaths(files, excludePatterns, globOptions) {
});
}

function isValidFileName(file, extensions) {
if (!file || path.basename(file)[0] === '_') {
return false;
}

return extensions.filter(function (ext) {
return path.extname(file) === ('.' + ext);
}).length > 0;
}

module.exports = AvaFiles;
module.exports.defaultIncludePatterns = defaultIncludePatterns;
module.exports.defaultExcludePatterns = defaultExcludePatterns;
28 changes: 28 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,31 @@ test('findFiles - finds the correct files by default', async t => {
files.sort();
t.deepEqual(files, expected);
});

test('findFiles - finds the correct files with custom extension ts', async t => {
const fixtureDir = fixture('ext');
const avaFiles = new AvaFiles({
files: ['**/*.ts'],
cwd: fixtureDir,
extensions: ['ts']
});

const files = await avaFiles.findTestFiles();
const expected = ['typescript.ts'].map(file => path.join(fixtureDir, file));

t.deepEqual(files, expected);
});

test('findFiles - finds the empty files with custom extension []', async t => {
const fixtureDir = fixture('ext');
const avaFiles = new AvaFiles({
files: ['**/*.ts'],
cwd: fixtureDir,
extensions: []
});

const files = await avaFiles.findTestFiles();
const expected = [];

t.deepEqual(files, expected);
});