Skip to content

Commit f01c724

Browse files
committed
Metalsmith#match: adapt signature, move match body to helpers, enhance tests (see #338)
1 parent 828b17e commit f01c724

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

lib/helpers.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require('fs')
22
const rmrf = require('rimraf')
3+
const micromatch = require('micromatch')
34

45
/**
56
* Type-checkers
@@ -20,6 +21,15 @@ function isUndefined(u) {
2021
return typeof u === 'undefined'
2122
}
2223

24+
function match(input, patterns, options) {
25+
input = input || Object.keys(this._files)
26+
if (!(input && input.length)) return []
27+
options = Object.assign({ dot: true }, options || {}, {
28+
// required to convert forward to backslashes on Windows and match the file keys properly
29+
format: normalize
30+
})
31+
return micromatch(input, patterns, options).sort()
32+
}
2333
/**
2434
* Recursively remove a directory
2535
* @param {string} p
@@ -56,6 +66,7 @@ const helpers = {
5666
isString,
5767
isObject,
5868
isUndefined,
69+
match,
5970
rm
6071
}
6172

lib/index.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ const matter = require('gray-matter')
55
const Mode = require('stat-mode')
66
const path = require('path')
77
let readdir = require('recursive-readdir')
8-
const { rm, isString, isBoolean, isObject, isNumber, isUndefined } = require('./helpers')
8+
const { rm, isString, isBoolean, isObject, isNumber, isUndefined, match } = require('./helpers')
99
const thunkify = require('thunkify')
1010
const unyield = require('unyield')
1111
const utf8 = require('is-utf8')
1212
const Ware = require('ware')
13-
const match = require('micromatch')
1413

1514
/**
1615
* @typedef {Object.<String, File>} Files
@@ -295,13 +294,9 @@ Metalsmith.prototype.path = function (...paths) {
295294
* @returns {String[]} An array of matching file paths
296295
*/
297296

298-
Metalsmith.prototype.match = function (patterns, options, input) {
297+
Metalsmith.prototype.match = function (patterns, input, options) {
299298
input = input || Object.keys(this._files)
300299
if (!(input && input.length)) return []
301-
options = Object.assign({ dot: true }, options || {}, {
302-
// required to convert forward to backslashes on Windows and match the file keys properly
303-
format: path.normalize
304-
})
305300
return match(input, patterns, options)
306301
}
307302

test/index.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -265,22 +265,22 @@ describe('Metalsmith', function () {
265265
const m = Metalsmith(fixture('match'))
266266
m.process(function (err) {
267267
if (err) done(err)
268-
const negationMatches = m.match('!index.md').join(',')
269-
const orMatches = m.match('*.{jpg,md}').join(',')
270-
assert.equal(negationMatches, `.htaccess,team.jpg,${path.join('nested', 'index.md')}`)
271-
assert.equal(orMatches, 'index.md,team.jpg')
268+
const negationMatches = m.match('!index.md')
269+
const orMatches = m.match('*.{jpg,md}')
270+
assert.deepStrictEqual(negationMatches, ['.htaccess',path.join('nested', 'index.md'),'team.jpg'])
271+
assert.deepStrictEqual(orMatches, ['index.md','team.jpg'])
272272
done()
273273
})
274274
})
275275

276276
it('should include dotfiles, unless specified otherwise', function (done) {
277277
const m = Metalsmith(fixture('match'))
278-
m.process(function (err) {
278+
m.process(function (err, files) {
279279
if (err) done(err)
280280
const matchesAll = m.match('**')
281-
const matchesNoDot = m.match('**', { dot: false })
282-
assert.deepStrictEqual(matchesAll.sort(), ['.htaccess','index.md',path.join('nested', 'index.md'),'team.jpg'])
283-
assert.deepStrictEqual(matchesNoDot.sort(), ['index.md',path.join('nested', 'index.md'),'team.jpg'])
281+
const matchesNoDot = m.match('**', Object.keys(files), { dot: false })
282+
assert.deepStrictEqual(matchesAll, ['.htaccess','index.md',path.join('nested', 'index.md'),'team.jpg'])
283+
assert.deepStrictEqual(matchesNoDot, ['index.md',path.join('nested', 'index.md'),'team.jpg'])
284284
done()
285285
})
286286
})
@@ -295,8 +295,8 @@ describe('Metalsmith', function () {
295295
})
296296
}).process(function (err) {
297297
if (err) done(err)
298-
const matches = m.match('**/*.md').join(',')
299-
assert.equal(matches, 'index.md,nested\\index.md')
298+
const matches = m.match('**/*.md')
299+
assert.deepStrictEqual(matches, ['index.md','nested\\index.md'])
300300
done()
301301
})
302302
})

0 commit comments

Comments
 (0)