11const fs = require ( 'fs/promises' )
2- const { basename , extname , dirname } = require ( 'path' )
2+ const { dirname } = require ( 'path' )
33const yaml = require ( 'yaml' )
44const NpmPackageJson = require ( '@npmcli/package-json' )
55const jsonParse = require ( 'json-parse-even-better-errors' )
66const Diff = require ( 'diff' )
77const { unset } = require ( 'lodash' )
88const ini = require ( 'ini' )
9+ const { minimatch } = require ( 'minimatch' )
910const template = require ( './template.js' )
1011const jsonDiff = require ( './json-diff' )
1112const merge = require ( './merge.js' )
@@ -167,17 +168,17 @@ class Base {
167168}
168169
169170class Gitignore extends Base {
170- static types = [ 'codeowners' , 'gitignore' ]
171+ static types = [ 'codeowners' , '. gitignore' ]
171172 comment = ( c ) => `# ${ c } `
172173}
173174
174175class Js extends Base {
175- static types = [ 'js' ]
176+ static types = [ '*. js' ]
176177 comment = ( c ) => `/* ${ c } */`
177178}
178179
179180class 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
204205class IniMerge extends Ini {
205- static types = [ 'npmrc' ]
206+ static types = [ '. npmrc' ]
206207 merge = ( t , s ) => merge ( t , s )
207208}
208209
209210class Markdown extends Base {
210- static types = [ 'md' ]
211+ static types = [ '*. md' ]
211212 comment = ( c ) => `<!-- ${ c } -->`
212213}
213214
214215class 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
276277class 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
308309class 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
353367const 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
362376module . exports = getParser
0 commit comments