Skip to content

Commit

Permalink
fix: determine parser based on target filename
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Jul 7, 2023
1 parent 7300da4 commit 449066e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/util/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const getParsers = (dir, files, options) => {
return new (parser(Parser.Parsers))(target, file, options, { clean })
}

return new (Parser(file))(target, file, options, { clean })
return new (Parser(target))(target, file, options, { clean })
})

return parsers.filter(Boolean)
Expand Down
46 changes: 30 additions & 16 deletions lib/util/parser.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const fs = require('fs/promises')
const { basename, extname, dirname } = require('path')
const { dirname } = require('path')
const yaml = require('yaml')
const NpmPackageJson = require('@npmcli/package-json')
const jsonParse = require('json-parse-even-better-errors')
const Diff = require('diff')
const { unset } = require('lodash')
const ini = require('ini')
const { minimatch } = require('minimatch')
const template = require('./template.js')
const jsonDiff = require('./json-diff')
const merge = require('./merge.js')
Expand Down Expand Up @@ -167,17 +168,17 @@ class Base {
}

class Gitignore extends Base {
static types = ['codeowners', 'gitignore']
static types = ['codeowners', '.gitignore']
comment = (c) => `# ${c}`
}

class Js extends Base {
static types = ['js']
static types = ['*.js']
comment = (c) => `/* ${c} */`
}

class Ini extends Base {
static types = ['ini']
static types = ['*.ini']
comment = (c) => `; ${c}`

toString (s) {
Expand All @@ -202,17 +203,17 @@ class Ini extends Base {
}

class IniMerge extends Ini {
static types = ['npmrc']
static types = ['.npmrc']
merge = (t, s) => merge(t, s)
}

class Markdown extends Base {
static types = ['md']
static types = ['*.md']
comment = (c) => `<!-- ${c} -->`
}

class Yml extends Base {
static types = ['yml']
static types = ['*.yml']
comment = (c) => ` ${c}`

toString (s) {
Expand Down Expand Up @@ -274,7 +275,7 @@ class YmlMerge extends Yml {
}

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

class PackageJson extends JsonMerge {
static types = ['pkg.json']
static types = ['package.json']

async prepare (s, t) {
// merge new source with current pkg content
Expand Down Expand Up @@ -348,15 +349,28 @@ const Parsers = {
PackageJson,
}

const parserLookup = Object.values(Parsers)
// Create an order to lookup parsers based on filename the only important part
// of ordering is that we want to match types by exact match first, then globs,
// so we always sort globs to the bottom
const parserLookup = []
for (const parser of Object.values(Parsers)) {
for (const type of parser.types) {
const parserEntry = [type, parser]
if (type.includes('*')) {
parserLookup.push(parserEntry)
} else {
parserLookup.unshift(parserEntry)
}
}
}

const getParser = (file) => {
const base = basename(file).toLowerCase()
const ext = extname(file).slice(1).toLowerCase()

return parserLookup.find((p) => p.types.includes(base))
|| parserLookup.find((p) => p.types.includes(ext))
|| Parsers.Base
for (const [type, parser] of parserLookup) {
if (minimatch(file, type, { nocase: true, dot: true, matchBase: true })) {
return parser
}
}
return Parsers.Base
}

module.exports = getParser
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"just-deep-map-values": "^1.1.1",
"just-diff": "^6.0.0",
"lodash": "^4.17.21",
"minimatch": "^9.0.2",
"npm-package-arg": "^10.0.0",
"proc-log": "^3.0.0",
"release-please": "npm:@npmcli/release-please@^14.2.6",
Expand Down

0 comments on commit 449066e

Please sign in to comment.