Skip to content

Commit 8e9edee

Browse files
committed
Rework muliple urls param, rename params indexFile -> defaultFilename, srcToLoad -> sources
1 parent 4a5610c commit 8e9edee

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

lib/config/defaults.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var config = {
2-
indexFile: 'index.html',
2+
defaultFilename: 'index.html',
33
log: false,
4-
srcToLoad: [
4+
sources: [
55
{
66
selector: 'img',
77
attr: 'src'

lib/load.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ var Loader = function (data) {
4848

4949
function getDirectoryByExtension(ext) {
5050
var dirObj = _.chain(options.subdirectories)
51-
.filter(function (dir) {
52-
return _.indexOf(dir.extensions, ext) >= 0
53-
})
51+
.filter(function (dir) { return _.contains(dir.extensions, ext); })
5452
.first()
5553
.value();
5654
return (!dirObj) ? '' : dirObj.directory;
@@ -109,7 +107,7 @@ var Loader = function (data) {
109107
return loadCssSources(currentOptions.url, absoluteLocalPath, html);
110108
});
111109

112-
_.each(options.srcToLoad, function (src) {
110+
_.each(options.sources, function (src) {
113111
p = p.then(function loadSource(html) {
114112
return loadHtmlSources(currentOptions.url, absoluteLocalPath, html, src.selector, src.attr);
115113
});
@@ -194,36 +192,33 @@ var Loader = function (data) {
194192
});
195193
}
196194

195+
function adaptLoadOptions (loadOptions) {
196+
loadOptions = _.isArray(loadOptions) ? loadOptions : [loadOptions];
197+
198+
return _.reduce(loadOptions, function (memo, loadOption) {
199+
var url = _.isObject(loadOption) && _.has(loadOption, 'url') ? loadOption.url : loadOption.toString();
200+
var filename = _.isObject(loadOption) && _.has(loadOption, 'filename') ? loadOption.filename : options.defaultFilename;
201+
202+
memo.push({
203+
url: url,
204+
filename: filename
205+
});
206+
return memo;
207+
}, []);
208+
}
209+
197210
function beforeLoad() {
198211
fs.ensureDirSync(options.directory);
199212

200-
// Create map { url -> local filename } for downloading
201-
var loadOptions = (function () {
202-
if (!_.isEmpty(options.paths)) {
203-
return _.map(options.paths, function (path) {
204-
return {
205-
filename: path.filename,
206-
url: url.resolve(path.url || options.url, path.path || '/')
207-
};
208-
});
209-
} else {
210-
return [
211-
{
212-
filename: options.indexFile,
213-
url: options.url
214-
}
215-
];
216-
}
217-
})();
218-
219213
// Set static subdirectories as loaded to avoid saving file with subdirectory's name
220214
_.map(options.subdirectories, function (dir) {
221215
setLoadedFilename(dir.directory, path.resolve(options.directory, dir.directory));
222216
});
223217

224218
logger = new Logger(options.log);
225219

226-
return Promise.resolve(loadOptions);
220+
// Create map { url -> local filename } for downloading
221+
return Promise.resolve(adaptLoadOptions(options.urls));
227222
}
228223

229224
function load(loadOptions) {

0 commit comments

Comments
 (0)