1
1
const fs = require ( 'fs/promises' )
2
- const { basename , extname , dirname } = require ( 'path' )
2
+ const { dirname } = require ( 'path' )
3
3
const yaml = require ( 'yaml' )
4
4
const NpmPackageJson = require ( '@npmcli/package-json' )
5
5
const jsonParse = require ( 'json-parse-even-better-errors' )
6
6
const Diff = require ( 'diff' )
7
7
const { unset } = require ( 'lodash' )
8
8
const ini = require ( 'ini' )
9
+ const { minimatch } = require ( 'minimatch' )
9
10
const template = require ( './template.js' )
10
11
const jsonDiff = require ( './json-diff' )
11
12
const merge = require ( './merge.js' )
@@ -167,17 +168,17 @@ class Base {
167
168
}
168
169
169
170
class Gitignore extends Base {
170
- static types = [ 'codeowners' , 'gitignore' ]
171
+ static types = [ 'codeowners' , '. gitignore' ]
171
172
comment = ( c ) => `# ${ c } `
172
173
}
173
174
174
175
class Js extends Base {
175
- static types = [ 'js' ]
176
+ static types = [ '*. js' ]
176
177
comment = ( c ) => `/* ${ c } */`
177
178
}
178
179
179
180
class Ini extends Base {
180
- static types = [ 'ini' ]
181
+ static types = [ '*. ini' ]
181
182
comment = ( c ) => `; ${ c } `
182
183
183
184
toString ( s ) {
@@ -202,17 +203,17 @@ class Ini extends Base {
202
203
}
203
204
204
205
class IniMerge extends Ini {
205
- static types = [ 'npmrc' ]
206
+ static types = [ '. npmrc' ]
206
207
merge = ( t , s ) => merge ( t , s )
207
208
}
208
209
209
210
class Markdown extends Base {
210
- static types = [ 'md' ]
211
+ static types = [ '*. md' ]
211
212
comment = ( c ) => `<!-- ${ c } -->`
212
213
}
213
214
214
215
class Yml extends Base {
215
- static types = [ 'yml' ]
216
+ static types = [ '*. yml' ]
216
217
comment = ( c ) => ` ${ c } `
217
218
218
219
toString ( s ) {
@@ -274,7 +275,7 @@ class YmlMerge extends Yml {
274
275
}
275
276
276
277
class Json extends Base {
277
- static types = [ 'json' ]
278
+ static types = [ '*. json' ]
278
279
// its a json comment! not really but we do add a special key
279
280
// to json objects
280
281
comment = ( c ) => ( { [ `//${ this . options . config . __NAME__ } ` ] : c } )
@@ -306,7 +307,7 @@ class JsonMerge extends Json {
306
307
}
307
308
308
309
class PackageJson extends JsonMerge {
309
- static types = [ 'pkg .json' ]
310
+ static types = [ 'package .json' ]
310
311
311
312
async prepare ( s , t ) {
312
313
// merge new source with current pkg content
@@ -348,15 +349,28 @@ const Parsers = {
348
349
PackageJson,
349
350
}
350
351
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
+ }
352
366
353
367
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
360
374
}
361
375
362
376
module . exports = getParser
0 commit comments