1
- import execa , { ExecaError } from 'execa' ;
2
- import load from '@commitlint/load' ;
3
- import lint from '@commitlint/lint' ;
4
- import read from '@commitlint/read' ;
5
- import isFunction from 'lodash.isfunction' ;
6
- import resolveFrom from 'resolve-from' ;
7
- import resolveGlobal from 'resolve-global' ;
8
- import yargs , { Arguments } from 'yargs' ;
1
+ import { createRequire } from 'module' ;
2
+ import path from 'path' ;
3
+ import { fileURLToPath , pathToFileURL } from 'url' ;
9
4
import util from 'util' ;
10
5
11
- import { CliFlags } from './types' ;
12
- import {
6
+ import lint from '@commitlint/lint' ;
7
+ import load from '@commitlint/load' ;
8
+ import read from '@commitlint/read' ;
9
+ import type {
10
+ Formatter ,
13
11
LintOptions ,
14
12
LintOutcome ,
15
- ParserOptions ,
16
13
ParserPreset ,
17
14
QualifiedConfig ,
18
- Formatter ,
19
15
UserConfig ,
20
16
} from '@commitlint/types' ;
21
- import { CliError } from './cli-error' ;
17
+ import type { Options } from 'conventional-commits-parser' ;
18
+ import execa , { ExecaError } from 'execa' ;
19
+ import resolveFrom from 'resolve-from' ;
20
+ import resolveGlobal from 'resolve-global' ;
21
+ import yargs , { type Arguments } from 'yargs' ;
22
+
23
+ import { CliFlags } from './types.js' ;
24
+
25
+ import { CliError } from './cli-error.js' ;
26
+
27
+ const require = createRequire ( import . meta. url ) ;
28
+
29
+ const __dirname = path . resolve ( fileURLToPath ( import . meta. url ) , '..' ) ;
22
30
23
- const pkg = require ( '../package' ) ;
31
+ const dynamicImport = async < T > ( id : string ) : Promise < T > => {
32
+ const imported = await import (
33
+ path . isAbsolute ( id ) ? pathToFileURL ( id ) . toString ( ) : id
34
+ ) ;
35
+ return ( 'default' in imported && imported . default ) || imported ;
36
+ } ;
37
+
38
+ const pkg : typeof import ( '../package.json' ) = require ( '../package.json' ) ;
24
39
25
40
const gitDefaultCommentChar = '#' ;
26
41
27
- const cli = yargs
42
+ const cli = yargs ( process . argv . slice ( 2 ) )
28
43
. options ( {
29
44
color : {
30
45
alias : 'c' ,
@@ -206,7 +221,7 @@ async function main(args: MainArgs): Promise<void> {
206
221
'[input] is required: supply via stdin, or --env or --edit or --from and --to' ,
207
222
pkg . name
208
223
) ;
209
- yargs . showHelp ( 'log' ) ;
224
+ yargs ( ) . showHelp ( 'log' ) ;
210
225
console . log ( err . message ) ;
211
226
throw err ;
212
227
}
@@ -216,7 +231,7 @@ async function main(args: MainArgs): Promise<void> {
216
231
file : flags . config ,
217
232
} ) ;
218
233
const parserOpts = selectParserOpts ( loaded . parserPreset ) ;
219
- const opts : LintOptions & { parserOpts : ParserOptions } = {
234
+ const opts : LintOptions & { parserOpts : Options } = {
220
235
parserOpts : { } ,
221
236
plugins : { } ,
222
237
ignores : [ ] ,
@@ -234,7 +249,7 @@ async function main(args: MainArgs): Promise<void> {
234
249
if ( loaded . defaultIgnores === false ) {
235
250
opts . defaultIgnores = false ;
236
251
}
237
- const format = loadFormatter ( loaded , flags ) ;
252
+ const format = await loadFormatter ( loaded , flags ) ;
238
253
239
254
// If reading from `.git/COMMIT_EDIT_MSG`, strip comments using
240
255
// core.commentChar from git configuration, falling back to '#'.
@@ -422,21 +437,18 @@ function selectParserOpts(parserPreset: ParserPreset | undefined) {
422
437
return parserPreset . parserOpts ;
423
438
}
424
439
425
- function loadFormatter ( config : QualifiedConfig , flags : CliFlags ) : Formatter {
440
+ function loadFormatter (
441
+ config : QualifiedConfig ,
442
+ flags : CliFlags
443
+ ) : Promise < Formatter > {
426
444
const moduleName = flags . format || config . formatter || '@commitlint/format' ;
427
445
const modulePath =
428
446
resolveFrom . silent ( __dirname , moduleName ) ||
429
447
resolveFrom . silent ( flags . cwd , moduleName ) ||
430
448
resolveGlobal . silent ( moduleName ) ;
431
449
432
450
if ( modulePath ) {
433
- const moduleInstance = require ( modulePath ) ;
434
-
435
- if ( isFunction ( moduleInstance . default ) ) {
436
- return moduleInstance . default ;
437
- }
438
-
439
- return moduleInstance ;
451
+ return dynamicImport < Formatter > ( modulePath ) ;
440
452
}
441
453
442
454
throw new Error ( `Using format ${ moduleName } , but cannot find the module.` ) ;
0 commit comments