Skip to content

Commit

Permalink
fix: ignore hidden and non .js files in loader (#113)
Browse files Browse the repository at this point in the history
* fix: ignore hidden and non .js files in loader (#110)

* style: renaming variables for consistentcy

* refactor: export `isJSFile` helper straight from `fs` package

* refactor: import isJSFile from fs instead
  • Loading branch information
neojp authored and zacharygolba committed May 21, 2016
1 parent 6d97ca7 commit 1b84009
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/packages/fs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ import { promisifyAll } from 'bluebird';
promisifyAll(fs);

export default fs;
export isJSFile from './utils/is-js-file';
4 changes: 4 additions & 0 deletions src/packages/fs/utils/is-js-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// filter out hidden files && non .js files
export default function isJSFile(file) {
return /^(?!\.).+\.js$/.test(file);
}
3 changes: 2 additions & 1 deletion src/packages/loader/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from '../fs';
import fs, { isJSFile } from '../fs';

export default async function loader(appPath, type) {
if (type === 'routes') {
Expand All @@ -8,6 +8,7 @@ export default async function loader(appPath, type) {
} else {
return new Map(
(await fs.readdirAsync(`${appPath}/app/${type}`))
.filter(isJSFile)
.map(file => {
return [
file.replace('.js', ''),
Expand Down
34 changes: 34 additions & 0 deletions test/unit/packages/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { expect } from 'chai';

import { isJSFile } from '../../../src/packages/fs';

describe('Unit: class fs ', () => {
describe('Unit: util isJSFile', () => {
const subject = {
a: 'author.js',
b: '.gitkeep',
c: 'author.js~'
};

it('is a JavaScript file', () => {
const result = isJSFile(subject.a);

expect(result).to.be.a('boolean');
expect(result).to.equal(true);
});

it('filter out hidden files', () => {
const result = isJSFile(subject.b);

expect(result).to.be.a('boolean');
expect(result).to.equal(false);
});

it('filter out non JavaScript files', () => {
const result = isJSFile(subject.c);

expect(result).to.be.a('boolean');
expect(result).to.equal(false);
});
});
});

0 comments on commit 1b84009

Please sign in to comment.