Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files option #3

Merged
merged 7 commits into from
Jul 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '8'
- '6'
- '4'
52 changes: 35 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
'use strict';
const path = require('path');
const arrify = require('arrify');
const isDirectory = require('is-directory');
const pify = require('pify');
const pathType = require('path-type');

const getGlob = (fp, ext) => path.join(fp, '**', ext || '');
const getExt = ext => Array.isArray(ext) ? `*.{${ext.join(',')}}` : `*.${ext}`;
const getPath = fp => fp[0] === '!' ? fp.slice(1) : fp;
const getExtensions = extensions => extensions.length > 1 ? `{${extensions.join(',')}}` : extensions[0];
const getPath = filepath => filepath[0] === '!' ? filepath.slice(1) : filepath;

module.exports = (input, opts) => {
opts = opts || {};

if (opts.ext) {
opts.ext = getExt(opts.ext);
const addExtensions = (file, extensions) => {
if (path.extname(file)) {
return `**/${file}`;
}

return Promise.all(arrify(input).map(x => pify(isDirectory)(getPath(x))
.then(isDir => isDir ? getGlob(x, opts.ext) : x)));
return `**/${file}.${getExtensions(extensions)}`;
};

module.exports.sync = (input, opts) => {
opts = opts || {};
const getGlob = (dir, opts) => {
opts = Object.assign({}, opts);

if (opts.files && !Array.isArray(opts.files)) {
throw new TypeError(`\`options.files\` must be an \`Array\`, not \`${typeof opts.files}\``);
}

if (opts.extensions && !Array.isArray(opts.extensions)) {
throw new TypeError(`\`options.extensions\` must be an \`Array\`, not \`${typeof opts.extensions}\``);
}

if (opts.ext) {
opts.ext = getExt(opts.ext);
if (opts.files && opts.extensions) {
return opts.files.map(x => path.join(dir, addExtensions(x, opts.extensions)));
} else if (opts.files) {
return opts.files.map(x => path.join(dir, `**/${x}`));
} else if (opts.extensions) {
return [path.join(dir, `**/*.${getExtensions(opts.extensions)}`)];
}

return input.map(x => isDirectory.sync(getPath(x)) ? getGlob(x, opts.ext) : x);
return [path.join(dir, '**')];
};

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

module.exports.sync = (input, opts) => {
const globs = arrify(input).map(x => pathType.dirSync(getPath(x)) ? getGlob(x, opts) : x);
return [].concat.apply([], globs);
};
22 changes: 5 additions & 17 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Kevin Martensson <kevinmartensson@gmail.com> (github.com/kevva)
Copyright (c) Kevin Mårtensson <kevinmartensson@gmail.com> (github.com/kevva)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
76 changes: 37 additions & 39 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
{
"name": "dir-glob",
"version": "1.1.0",
"description": "Convert directories to glob compatible strings",
"license": "MIT",
"repository": "kevva/dir-glob",
"author": {
"name": "Kevin Martensson",
"email": "kevinmartensson@gmail.com",
"url": "github.com/kevva"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"convert",
"directory",
"glob"
],
"dependencies": {
"arrify": "^1.0.1",
"is-directory": "^0.3.1",
"pify": "^2.3.0"
},
"devDependencies": {
"ava": "*",
"make-dir": "^1.0.0",
"mkdirp": "^0.5.1",
"rimraf": "^2.5.0",
"xo": "*"
},
"xo": {
"esnext": true
}
"name": "dir-glob",
"version": "1.1.0",
"description": "Convert directories to glob compatible strings",
"license": "MIT",
"repository": "kevva/dir-glob",
"author": {
"name": "Kevin Mårtensson",
"email": "kevinmartensson@gmail.com",
"url": "github.com/kevva"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"convert",
"directory",
"extensions",
"files",
"glob"
],
"dependencies": {
"arrify": "^1.0.1",
"path-type": "^3.0.0"
},
"devDependencies": {
"ava": "*",
"del": "^3.0.0",
"make-dir": "^1.0.0",
"rimraf": "^2.5.0",
"xo": "*"
}
}
48 changes: 23 additions & 25 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
## Install

```
$ npm install --save dir-glob
$ npm install dir-glob
```


Expand All @@ -20,14 +20,20 @@ dirGlob(['index.js', 'test.js', 'fixtures']).then(files => {
//=> ['index.js', 'test.js', 'fixtures/**']
});

dirGlob(['lib/**', 'fixtures'], {ext: 'js'}).then(files => {
dirGlob(['lib/**', 'fixtures'], {
files: ['test', 'unicorn']
extensions: ['js']
}).then(files => {
console.log(files);
//=> ['lib/**', 'fixtures/**/*.js']
//=> ['lib/**', 'fixtures/**/test.js', 'fixtures/**/unicorn.js']
});

dirGlob(['lib/**', 'fixtures'], {ext: ['js', 'png']}).then(files => {
dirGlob(['lib/**', 'fixtures'], {
files: ['test', 'unicorn', '*.jsx'],
extensions: ['js', 'png']
}).then(files => {
console.log(files);
//=> ['lib/**', 'fixtures/**/*.{js,png}']
//=> ['lib/**', 'fixtures/**/test.{js,png}', 'fixtures/**/unicorn.{js,png}', 'fixtures/**/*.jsx']
});
```

Expand All @@ -36,21 +42,7 @@ dirGlob(['lib/**', 'fixtures'], {ext: ['js', 'png']}).then(files => {

### dirGlob(input, [options])

Returns a promise for an array of glob strings.

#### input

Type: `Array` `string`

A `string` or an `Array` of paths.

#### options

##### ext

Type: `Array` `string`

Append extension to the end of your glob.
Returns a `Promise` for an array of glob strings.

### dirGlob.sync(input, [options])

Expand All @@ -60,17 +52,23 @@ Returns an array of glob strings.

Type: `Array` `string`

A `string` or an `array` of paths.
A `string` or an `Array` of paths.

#### options

##### ext
##### extensions

Type: `Array` `string`
Type: `Array`

Append extensions to the end of your globs.

##### files

Type: `Array`

Append extension to the end of your glob.
Only glob for certain files.


## License

MIT © [Kevin Martensson](http://github.com/kevva)
MIT © [Kevin Mårtensson](https://github.com/kevva)
22 changes: 14 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import del from 'del';
import makeDir from 'make-dir';
import rimraf from 'rimraf';
import test from 'ava';
import m from '.';

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

test('convert directories to glob - async', async t => {
t.deepEqual(await m(['index.js', 'tmp']), ['index.js', 'tmp/**']);
t.deepEqual(await m(['index.js', 'tmp'], {ext: 'js'}), ['index.js', 'tmp/**/*.js']);
t.deepEqual(await m(['index.js', 'tmp'], {ext: ['js', 'png']}), ['index.js', 'tmp/**/*.{js,png}']);
t.deepEqual(await m(['index.js', 'tmp'], {extensions: ['js']}), ['index.js', 'tmp/**/*.js']);
t.deepEqual(await m(['index.js', 'tmp'], {extensions: ['js', 'png']}), ['index.js', 'tmp/**/*.{js,png}']);
t.deepEqual(await m(['foo/**', 'tmp']), ['foo/**', 'tmp/**']);
t.deepEqual(await m(['index.js', '!tmp']), ['index.js', '!tmp/**']);
t.deepEqual(await m(['index.js', '!tmp'], {ext: ['js', 'png']}), ['index.js', '!tmp/**/*.{js,png}']);
t.deepEqual(await m(['index.js', '!tmp'], {extensions: ['js', 'png']}), ['index.js', '!tmp/**/*.{js,png}']);
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}']);
});

test('convert directories to glob - sync', t => {
t.deepEqual(m.sync(['index.js', 'tmp']), ['index.js', 'tmp/**']);
t.deepEqual(m.sync(['index.js', 'tmp'], {ext: 'js'}), ['index.js', 'tmp/**/*.js']);
t.deepEqual(m.sync(['index.js', 'tmp'], {ext: ['js', 'png']}), ['index.js', 'tmp/**/*.{js,png}']);
t.deepEqual(m.sync(['index.js', 'tmp'], {extensions: ['js']}), ['index.js', 'tmp/**/*.js']);
t.deepEqual(m.sync(['index.js', 'tmp'], {extensions: ['js', 'png']}), ['index.js', 'tmp/**/*.{js,png}']);
t.deepEqual(m.sync(['foo/**', 'tmp']), ['foo/**', 'tmp/**']);
t.deepEqual(m.sync(['index.js', '!tmp']), ['index.js', '!tmp/**']);
t.deepEqual(m.sync(['index.js', '!tmp'], {ext: ['js', 'png']}), ['index.js', '!tmp/**/*.{js,png}']);
t.deepEqual(m.sync(['index.js', '!tmp'], {extensions: ['js', 'png']}), ['index.js', '!tmp/**/*.{js,png}']);
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}']);
});