-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (49 loc) · 1.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict'
const path = require('path')
const metalsmithAssets = require('metalsmith-assets-2')
const async = require('async')
module.exports = function (opts) {
// Default configuration.
opts = opts || {}
opts.extname = opts.extname || '.assets'
// Execute the plugin.
return function (files, metalsmith, done) {
/**
* Check if the given file is a .concat file. Call done() with result.
*
* @param {string} file The file to filter on.
* @param {function} callback A asyncronous callback that's made once the processing is complete.
*/
function filterFile(file, callback) {
// Ensure it matches the extension.
const correctExtention = path.extname(file) === opts.extname
// Make sure it has defined files.
const source = files[file].source || false
const destination = files[file].destination || '.'
callback(null, correctExtention && source && destination)
}
/**
* Tell Metalsmith Assets to process the data.
*
* @param {string} filename The file to filter on.
* @param {function} callback A asyncronous callback that's made once the processing is complete.
*/
function assetFile(filename, callback) {
const data = {
source: files[filename].source,
destination: files[filename].destination || path.join(path.dirname(filename), '.')
}
delete files[filename]
metalsmithAssets(data)(files, metalsmith, callback)
}
// Find all the .concat files.
async.filter(Object.keys(files), filterFile, (err, assets) => {
if (err) {
done(err)
} else {
// Use async to process each concat object.
async.each(assets, assetFile, done)
}
})
}
}