Skip to content

Commit

Permalink
Metalsmith#match: adapt signature, move match body to helpers, enhanc…
Browse files Browse the repository at this point in the history
…e tests (see #338)
  • Loading branch information
webketje committed Jan 4, 2022
1 parent 828b17e commit f01c724
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
11 changes: 11 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs')
const rmrf = require('rimraf')
const micromatch = require('micromatch')

/**
* Type-checkers
Expand All @@ -20,6 +21,15 @@ function isUndefined(u) {
return typeof u === 'undefined'
}

function match(input, patterns, options) {
input = input || Object.keys(this._files)
if (!(input && input.length)) return []
options = Object.assign({ dot: true }, options || {}, {
// required to convert forward to backslashes on Windows and match the file keys properly
format: normalize
})
return micromatch(input, patterns, options).sort()
}
/**
* Recursively remove a directory
* @param {string} p
Expand Down Expand Up @@ -56,6 +66,7 @@ const helpers = {
isString,
isObject,
isUndefined,
match,
rm
}

Expand Down
9 changes: 2 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ const matter = require('gray-matter')
const Mode = require('stat-mode')
const path = require('path')
let readdir = require('recursive-readdir')
const { rm, isString, isBoolean, isObject, isNumber, isUndefined } = require('./helpers')
const { rm, isString, isBoolean, isObject, isNumber, isUndefined, match } = require('./helpers')
const thunkify = require('thunkify')
const unyield = require('unyield')
const utf8 = require('is-utf8')
const Ware = require('ware')
const match = require('micromatch')

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

Metalsmith.prototype.match = function (patterns, options, input) {
Metalsmith.prototype.match = function (patterns, input, options) {
input = input || Object.keys(this._files)
if (!(input && input.length)) return []
options = Object.assign({ dot: true }, options || {}, {
// required to convert forward to backslashes on Windows and match the file keys properly
format: path.normalize
})
return match(input, patterns, options)
}

Expand Down
20 changes: 10 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,22 +265,22 @@ describe('Metalsmith', function () {
const m = Metalsmith(fixture('match'))
m.process(function (err) {
if (err) done(err)
const negationMatches = m.match('!index.md').join(',')
const orMatches = m.match('*.{jpg,md}').join(',')
assert.equal(negationMatches, `.htaccess,team.jpg,${path.join('nested', 'index.md')}`)
assert.equal(orMatches, 'index.md,team.jpg')
const negationMatches = m.match('!index.md')
const orMatches = m.match('*.{jpg,md}')
assert.deepStrictEqual(negationMatches, ['.htaccess',path.join('nested', 'index.md'),'team.jpg'])
assert.deepStrictEqual(orMatches, ['index.md','team.jpg'])
done()
})
})

it('should include dotfiles, unless specified otherwise', function (done) {
const m = Metalsmith(fixture('match'))
m.process(function (err) {
m.process(function (err, files) {
if (err) done(err)
const matchesAll = m.match('**')
const matchesNoDot = m.match('**', { dot: false })
assert.deepStrictEqual(matchesAll.sort(), ['.htaccess','index.md',path.join('nested', 'index.md'),'team.jpg'])
assert.deepStrictEqual(matchesNoDot.sort(), ['index.md',path.join('nested', 'index.md'),'team.jpg'])
const matchesNoDot = m.match('**', Object.keys(files), { dot: false })
assert.deepStrictEqual(matchesAll, ['.htaccess','index.md',path.join('nested', 'index.md'),'team.jpg'])
assert.deepStrictEqual(matchesNoDot, ['index.md',path.join('nested', 'index.md'),'team.jpg'])
done()
})
})
Expand All @@ -295,8 +295,8 @@ describe('Metalsmith', function () {
})
}).process(function (err) {
if (err) done(err)
const matches = m.match('**/*.md').join(',')
assert.equal(matches, 'index.md,nested\\index.md')
const matches = m.match('**/*.md')
assert.deepStrictEqual(matches, ['index.md','nested\\index.md'])
done()
})
})
Expand Down

0 comments on commit f01c724

Please sign in to comment.