From 7230251a51181214f9cb8b2ae4601b96cbe693b4 Mon Sep 17 00:00:00 2001 From: Stanislav Termosa Date: Wed, 7 Feb 2018 19:43:05 +0200 Subject: [PATCH] Add `cwd` option (#6) --- index.js | 6 ++++-- readme.md | 13 +++++++++++++ test.js | 8 +++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 2fa1293..f403673 100644 --- a/index.js +++ b/index.js @@ -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); }; diff --git a/readme.md b/readme.md index 614ca37..69ec18e 100644 --- a/readme.md +++ b/readme.md @@ -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'] @@ -68,6 +75,12 @@ Type: `Array` Only glob for certain files. +##### cwd + +Type: `string` + +Test in specific directory. + ## License diff --git a/test.js b/test.js index ef8642a..79e97a3 100644 --- a/test.js +++ b/test.js @@ -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 => { @@ -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 => { @@ -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']); });