Skip to content

Commit 449066e

Browse files
committedJul 7, 2023
fix: determine parser based on target filename
1 parent 7300da4 commit 449066e

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed
 

‎lib/util/files.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const getParsers = (dir, files, options) => {
3535
return new (parser(Parser.Parsers))(target, file, options, { clean })
3636
}
3737

38-
return new (Parser(file))(target, file, options, { clean })
38+
return new (Parser(target))(target, file, options, { clean })
3939
})
4040

4141
return parsers.filter(Boolean)

‎lib/util/parser.js

+30-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
const fs = require('fs/promises')
2-
const { basename, extname, dirname } = require('path')
2+
const { dirname } = require('path')
33
const yaml = require('yaml')
44
const NpmPackageJson = require('@npmcli/package-json')
55
const jsonParse = require('json-parse-even-better-errors')
66
const Diff = require('diff')
77
const { unset } = require('lodash')
88
const ini = require('ini')
9+
const { minimatch } = require('minimatch')
910
const template = require('./template.js')
1011
const jsonDiff = require('./json-diff')
1112
const merge = require('./merge.js')
@@ -167,17 +168,17 @@ class Base {
167168
}
168169

169170
class Gitignore extends Base {
170-
static types = ['codeowners', 'gitignore']
171+
static types = ['codeowners', '.gitignore']
171172
comment = (c) => `# ${c}`
172173
}
173174

174175
class Js extends Base {
175-
static types = ['js']
176+
static types = ['*.js']
176177
comment = (c) => `/* ${c} */`
177178
}
178179

179180
class Ini extends Base {
180-
static types = ['ini']
181+
static types = ['*.ini']
181182
comment = (c) => `; ${c}`
182183

183184
toString (s) {
@@ -202,17 +203,17 @@ class Ini extends Base {
202203
}
203204

204205
class IniMerge extends Ini {
205-
static types = ['npmrc']
206+
static types = ['.npmrc']
206207
merge = (t, s) => merge(t, s)
207208
}
208209

209210
class Markdown extends Base {
210-
static types = ['md']
211+
static types = ['*.md']
211212
comment = (c) => `<!-- ${c} -->`
212213
}
213214

214215
class Yml extends Base {
215-
static types = ['yml']
216+
static types = ['*.yml']
216217
comment = (c) => ` ${c}`
217218

218219
toString (s) {
@@ -274,7 +275,7 @@ class YmlMerge extends Yml {
274275
}
275276

276277
class Json extends Base {
277-
static types = ['json']
278+
static types = ['*.json']
278279
// its a json comment! not really but we do add a special key
279280
// to json objects
280281
comment = (c) => ({ [`//${this.options.config.__NAME__}`]: c })
@@ -306,7 +307,7 @@ class JsonMerge extends Json {
306307
}
307308

308309
class PackageJson extends JsonMerge {
309-
static types = ['pkg.json']
310+
static types = ['package.json']
310311

311312
async prepare (s, t) {
312313
// merge new source with current pkg content
@@ -348,15 +349,28 @@ const Parsers = {
348349
PackageJson,
349350
}
350351

351-
const parserLookup = Object.values(Parsers)
352+
// Create an order to lookup parsers based on filename the only important part
353+
// of ordering is that we want to match types by exact match first, then globs,
354+
// so we always sort globs to the bottom
355+
const parserLookup = []
356+
for (const parser of Object.values(Parsers)) {
357+
for (const type of parser.types) {
358+
const parserEntry = [type, parser]
359+
if (type.includes('*')) {
360+
parserLookup.push(parserEntry)
361+
} else {
362+
parserLookup.unshift(parserEntry)
363+
}
364+
}
365+
}
352366

353367
const getParser = (file) => {
354-
const base = basename(file).toLowerCase()
355-
const ext = extname(file).slice(1).toLowerCase()
356-
357-
return parserLookup.find((p) => p.types.includes(base))
358-
|| parserLookup.find((p) => p.types.includes(ext))
359-
|| Parsers.Base
368+
for (const [type, parser] of parserLookup) {
369+
if (minimatch(file, type, { nocase: true, dot: true, matchBase: true })) {
370+
return parser
371+
}
372+
}
373+
return Parsers.Base
360374
}
361375

362376
module.exports = getParser

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"just-deep-map-values": "^1.1.1",
5151
"just-diff": "^6.0.0",
5252
"lodash": "^4.17.21",
53+
"minimatch": "^9.0.2",
5354
"npm-package-arg": "^10.0.0",
5455
"proc-log": "^3.0.0",
5556
"release-please": "npm:@npmcli/release-please@^14.2.6",

0 commit comments

Comments
 (0)
Please sign in to comment.