asset-resolver
Advanced tools
Comparing version 2.0.2 to 2.0.3
#!/usr/bin/env node | ||
'use strict'; | ||
const meow = require('meow'); | ||
@@ -13,3 +15,3 @@ const resolver = require('.'); | ||
'Options', | ||
' -b --base List of directories/urls where we should start looking for assets. [Default: process.cwd()]', | ||
' -b --base List of directories/URLs where we should start looking for assets. [Default: process.cwd()]', | ||
'', | ||
@@ -16,0 +18,0 @@ 'Examples', |
28
index.js
'use strict'; | ||
const os = require('os'); | ||
const toarray = require('lodash/toArray'); | ||
const defaults = require('lodash/defaults'); | ||
const map = require('lodash/map'); | ||
const debug = require('debug')('asset-resolver'); | ||
@@ -10,9 +8,8 @@ const Bluebird = require('bluebird'); | ||
module.exports.getResource = function(file, opts) { | ||
opts = defaults(opts || {}, { | ||
module.exports.getResource = function(file, options = {}) { | ||
const opts = { | ||
base: [process.cwd()], | ||
filter() { | ||
return true; | ||
} | ||
}); | ||
filter: () => true, | ||
...options | ||
}; | ||
@@ -23,12 +20,11 @@ if (typeof opts.base === 'string') { | ||
opts.base = resolver.glob(toarray(opts.base)); | ||
opts.base = resolver.glob([...opts.base]); | ||
return Bluebird.any( | ||
map(opts.base, base => { | ||
return resolver.getResource(base, file, opts); | ||
}) | ||
).catch(Bluebird.AggregateError, errs => { | ||
const promises = (opts.base || []).map(base => { | ||
return resolver.getResource(base, file, opts); | ||
}); | ||
return Bluebird.any(promises).catch(Bluebird.AggregateError, errs => { | ||
const msg = [ | ||
'The file "' + file + '" could not be resolved because of:' | ||
].concat(map(errs, 'message')); | ||
].concat(errs.map(err => err.message)); | ||
debug(msg); | ||
@@ -35,0 +31,0 @@ return Bluebird.reject(new Error(msg.join(os.EOL))); |
'use strict'; | ||
const path = require('path'); | ||
@@ -9,4 +10,2 @@ const url = require('url'); | ||
const debug = require('debug')('asset-resolver'); | ||
const result = require('lodash/result'); | ||
const reduce = require('lodash/reduce'); | ||
const globby = require('globby'); | ||
@@ -83,5 +82,6 @@ | ||
const mimeType = | ||
result(resp, 'headers.content-type') || mime.getType(resource); | ||
const {headers = {}} = resp; | ||
const mimeType = headers['content-type'] || mime.getType(resource); | ||
resolve({ | ||
@@ -132,30 +132,26 @@ contents: body, | ||
function glob(base) { | ||
return reduce( | ||
base, | ||
(res, val) => { | ||
if (isUrl(val)) { | ||
res.push(val); | ||
return res; | ||
} | ||
return base.reduce((res, val) => { | ||
if (isUrl(val)) { | ||
res.push(val); | ||
return res; | ||
} | ||
if (fs.existsSync(val) && fs.lstatSync(val).isDirectory()) { | ||
res.push(val); | ||
return res; | ||
} | ||
if (fs.existsSync(val) && fs.lstatSync(val).isDirectory()) { | ||
res.push(val); | ||
return res; | ||
} | ||
let files = []; | ||
try { | ||
files = globby.sync([val], { | ||
nodir: false, | ||
onlyDirectories: true, | ||
cwd: process.cwd() | ||
}); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
let files = []; | ||
try { | ||
files = globby.sync([val], { | ||
nodir: false, | ||
onlyDirectories: true, | ||
cwd: process.cwd() | ||
}); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
return res.concat(files); | ||
}, | ||
[] | ||
); | ||
return [...res, ...files]; | ||
}, []); | ||
} | ||
@@ -162,0 +158,0 @@ |
{ | ||
"name": "asset-resolver", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "Find an asset in a set of locations", | ||
@@ -17,3 +17,5 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "xo && nyc ava", | ||
"ava": "nyc ava", | ||
"xo": "xo", | ||
"test": "npm run xo && npm run ava", | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls" | ||
@@ -38,3 +40,2 @@ }, | ||
"hash": "^0.2.1", | ||
"lodash": "^4.17.15", | ||
"meow": "5.0.0", | ||
@@ -46,3 +47,3 @@ "mime": "^2.4.4", | ||
"devDependencies": { | ||
"ava": "^2.2.0", | ||
"ava": "^2.3.0", | ||
"coveralls": "^3.0.6", | ||
@@ -49,0 +50,0 @@ "es6-promise": "^4.2.8", |
[![NPM version][npm-image]][npm-url] [![Linux Build Status][travis-image]][travis-url] [![Windows Build status][appveyor-image]][appveyor-url] [![Coverage Status][coveralls-image]][coveralls-url] [![dependencies Status][depstat-image]][depstat-url] [![devDependencies Status Status][deVdepstat-image]][deVdepstat-url] | ||
# asset-resolver | ||
# asset-resolver | ||
@@ -18,8 +18,8 @@ > Helper module to find an asset in a set of locations | ||
```js | ||
var resolver = require('asset-resolver'); | ||
const resolver = require('asset-resolver'); | ||
resolver.getResource('my.svg',{ | ||
base: ['some/directory','http://some.domain/assets'] | ||
resolver.getResource('my.svg', { | ||
base: ['some/directory', 'http://some.domain/assets'] | ||
}).then(function(resource) { | ||
console.log(resource) | ||
console.log(resource) | ||
}); | ||
@@ -36,3 +36,3 @@ //=> { path: http://some.domain/assets/my.svg', mime: 'image/svg+xml', contents: ' ... ' } | ||
Type: `string` | ||
* Type: `string` | ||
@@ -45,29 +45,30 @@ The filename | ||
Type: `string`,`array` | ||
Default: `[process.cwd()]` | ||
Required: `false` | ||
Example: `['http://domain.de/', 'http://domain.de/styles', 'app/images', '**/images/']` | ||
* Type: `string`, `array` | ||
* Default: `[process.cwd()]` | ||
* Required: `false` | ||
* Example: `['http://domain.de/', 'http://domain.de/styles', 'app/images', '**/images/']` | ||
List of directories/urls where we should start looking for assets (supports globbing on directories). | ||
List of directories/URLs where we should start looking for assets (supports globbing on directories). | ||
##### filter | ||
Type: `function` | ||
Default: `function(){ return true; }` | ||
Required: `false` | ||
Example: | ||
```javascript | ||
resolver.getResource('my.svg',{ | ||
base: ['some/directory','http://some.domain/assets'], | ||
filter: function (resource) { | ||
return filesize(resource) < maxFileSize; | ||
} | ||
}).then(function(resource) { | ||
console.log(resource) | ||
}); | ||
``` | ||
* Type: `function` | ||
* Default: `function(){ return true; }` | ||
* Required: `false` | ||
* Example: | ||
List of directories/urls where we should start looking for assets. | ||
```js | ||
resolver.getResource('my.svg',{ | ||
base: ['some/directory', 'http://some.domain/assets'], | ||
filter: function (resource) { | ||
return filesize(resource) < maxFileSize; | ||
} | ||
}).then(function(resource) { | ||
console.log(resource) | ||
}); | ||
``` | ||
List of directories/URLs where we should start looking for assets. | ||
## CLI | ||
@@ -86,3 +87,3 @@ | ||
Options | ||
-b --base List of directories/urls where we should start looking for assets. [Default: process.cwd()] | ||
-b --base List of directories/URLs where we should start looking for assets. [Default: process.cwd()] | ||
@@ -100,5 +101,5 @@ Examples | ||
MIT © [Ben Zörb](http://sommerlaune.com) | ||
MIT © [Ben Zörb](http://sommerlaune.com) | ||
[npm-url]: https://www.npmjs.com/package/asset-resolver | ||
@@ -108,6 +109,6 @@ [npm-image]: https://img.shields.io/npm/v/asset-resolver.svg | ||
[travis-url]: https://travis-ci.org/bezoerb/asset-resolver | ||
[travis-image]: https://travis-ci.org/bezoerb/asset-resolver.svg?branch=master&label=Linux%20build | ||
[travis-image]: https://img.shields.io/travis/bezoerb/asset-resolver/master.svg?label=Linux%20build | ||
[appveyor-url]: https://ci.appveyor.com/project/bezoerb/asset-resolver/branch/master | ||
[appveyor-image]: https://ci.appveyor.com/api/projects/status/yr4kfmv5vcbb03yn/branch/master?svg=true&label=Windows%20build | ||
[appveyor-image]: https://img.shields.io/appveyor/ci/bezoerb/asset-resolver/master.svg?label=Windows%20build | ||
@@ -119,4 +120,4 @@ [depstat-url]: https://david-dm.org/bezoerb/asset-resolver | ||
[deVdepstat-image]: https://img.shields.io/david/dev/bezoerb/asset-resolver.svg | ||
[coveralls-url]: https://coveralls.io/github/bezoerb/asset-resolver?branch=master | ||
[coveralls-image]: https://coveralls.io/repos/github/bezoerb/asset-resolver/badge.svg?branch=master |
11
117
12081
201
- Removedlodash@^4.17.15
- Removedlodash@4.17.21(transitive)