Skip to content

Commit

Permalink
Add cwd option (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
termosa authored and kevva committed Feb 7, 2018
1 parent a4d824f commit 7230251
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ const getGlob = (dir, opts) => {
};

module.exports = (input, opts) => {
return Promise.all([].concat(input).map(x => pathType.dir(getPath(x))
const cwd = opts && opts.cwd ? opts.cwd : process.cwd();
return Promise.all([].concat(input).map(x => pathType.dir(path.join(cwd, getPath(x)))
.then(isDir => isDir ? getGlob(x, opts) : x)))
.then(globs => [].concat.apply([], globs));
};

module.exports.sync = (input, opts) => {
const globs = [].concat(input).map(x => pathType.dirSync(getPath(x)) ? getGlob(x, opts) : x);
const cwd = opts && opts.cwd ? opts.cwd : process.cwd();
const globs = [].concat(input).map(x => pathType.dirSync(path.join(cwd, getPath(x))) ? getGlob(x, opts) : x);
return [].concat.apply([], globs);
};
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ dirGlob(['index.js', 'test.js', 'fixtures']).then(files => {
//=> ['index.js', 'test.js', 'fixtures/**']
});

dirGlob(['index.js', 'inner_folder'], {
cwd: 'fixtures'
}).then(files => {
console.log(files);
//=> ['index.js', 'inner_folder/**']
});

dirGlob(['lib/**', 'fixtures'], {
files: ['test', 'unicorn']
extensions: ['js']
Expand Down Expand Up @@ -68,6 +75,12 @@ Type: `Array`

Only glob for certain files.

##### cwd

Type: `string`

Test in specific directory.


## License

Expand Down
8 changes: 7 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import makeDir from 'make-dir';
import test from 'ava';
import m from '.';

test.before(() => makeDir.sync('tmp'));
test.before(() => makeDir.sync('tmp/inner_tmp'));
test.after(() => del.sync('tmp'));

test('convert directories to glob - async', async t => {
Expand All @@ -17,6 +17,9 @@ test('convert directories to glob - async', async t => {
t.deepEqual(await m(['index.js', 'tmp'], {files: ['unicorn', '*.png'], extensions: ['js']}), ['index.js', 'tmp/**/unicorn.js', 'tmp/**/*.png']);
t.deepEqual(await m(['index.js', 'tmp'], {files: ['unicorn', '*.png'], extensions: ['js', 'png']}), ['index.js', 'tmp/**/unicorn.{js,png}', 'tmp/**/*.png']);
t.deepEqual(await m(['index.js', 'tmp'], {files: ['test', 'unicorn'], extensions: ['js', 'png']}), ['index.js', 'tmp/**/test.{js,png}', 'tmp/**/unicorn.{js,png}']);
t.deepEqual(await m('inner_tmp', {cwd: 'tmp'}), ['inner_tmp/**']);
t.deepEqual(await m(['index.js', 'inner_tmp'], {cwd: 'tmp'}), ['index.js', 'inner_tmp/**']);
t.deepEqual(await m(['index.js', 'inner_tmp'], {cwd: 'tmp', files: ['unicorn', '*.png'], extensions: ['js', 'png']}), ['index.js', 'inner_tmp/**/unicorn.{js,png}', 'inner_tmp/**/*.png']);
});

test('convert directories to glob - sync', t => {
Expand All @@ -30,4 +33,7 @@ test('convert directories to glob - sync', t => {
t.deepEqual(m.sync(['index.js', 'tmp'], {files: ['unicorn', '*.png'], extensions: ['js']}), ['index.js', 'tmp/**/unicorn.js', 'tmp/**/*.png']);
t.deepEqual(m.sync(['index.js', 'tmp'], {files: ['unicorn', '*.png'], extensions: ['js', 'png']}), ['index.js', 'tmp/**/unicorn.{js,png}', 'tmp/**/*.png']);
t.deepEqual(m.sync(['index.js', 'tmp'], {files: ['test', 'unicorn'], extensions: ['js', 'png']}), ['index.js', 'tmp/**/test.{js,png}', 'tmp/**/unicorn.{js,png}']);
t.deepEqual(m.sync('inner_tmp', {cwd: 'tmp'}), ['inner_tmp/**']);
t.deepEqual(m.sync(['index.js', 'inner_tmp'], {cwd: 'tmp'}), ['index.js', 'inner_tmp/**']);
t.deepEqual(m.sync(['index.js', 'inner_tmp'], {cwd: 'tmp', files: ['unicorn', '*.png'], extensions: ['js', 'png']}), ['index.js', 'inner_tmp/**/unicorn.{js,png}', 'inner_tmp/**/*.png']);
});

0 comments on commit 7230251

Please sign in to comment.