1
1
import { BaseContext } from "clipanion/lib/advanced" ;
2
+ import { promises as fsp } from "fs" ;
2
3
import * as _path from "path" ;
3
4
import { cliInit } from "./cli-init" ;
4
5
import { HarkCliStartOptions , HarkfileFunction } from "./models" ;
5
- import { existsSync , promises as fsp } from "fs" ;
6
6
7
7
export async function cliStart < T > ( args : string [ ] , options ?: HarkCliStartOptions < T > ) {
8
- const harkfilePath = _path . resolve ( options ?. harkfilePath || "./harkfile" ) ;
9
8
const cliVars = await cliInit ( options ) ;
10
- const extensions = [ ".js" , ".jsx" , ".csj" , ".mjs" , ".ts" , ".tsx" ] ;
9
+ const extensions : string [ ] = options ?. registerBabelOptions ?? [
10
+ //
11
+ ".ts" ,
12
+ ".tsx" ,
13
+ ".mjs" ,
14
+ ".es" ,
15
+ ".es6" ,
16
+ ".csj" ,
17
+ ".js" ,
18
+ ".jsx" ,
19
+ ] ;
20
+ if ( extensions . length === 0 ) {
21
+ throw new Error ( "No extensions given" ) ;
22
+ }
11
23
if ( options ?. registerBabelOptions != null ) {
12
- require ( "@babel/register" ) ( {
24
+ let babelRegister ;
25
+ try {
26
+ babelRegister = require ( "@babel/register" ) ;
27
+ } catch ( ex ) {
28
+ console . error (
29
+ "\n\n@hark/cli: PeerDependency for @babel/register not met. Please set HARK_REGISTER_BABEL=0 if this is intended.\n\n" ,
30
+ ) ;
31
+ throw ex ;
32
+ }
33
+ babelRegister ( {
13
34
babelrc : true ,
14
35
extensions,
15
36
rootMode : "root" ,
16
37
...options ?. registerBabelOptions ,
17
38
} ) ;
18
39
}
19
- let harkFileFn : HarkfileFunction < any , any > ;
20
40
21
- if ( await fileExists ( harkfilePath , [ ...extensions , "" ] ) ) {
22
- try {
23
- harkFileFn = ( await import ( harkfilePath ) ) . default ;
24
- } catch ( ex ) {
25
- console . error ( `\nError while loading harkfile at "${ harkfilePath } ".\n` ) ;
26
- throw ex ;
27
- }
28
- } else {
29
- console . error ( `\nNo harkfile detected at "${ harkfilePath } ".\n` ) ;
30
- harkFileFn = ( ) => undefined ;
41
+ const harkfilePath = _path . resolve ( options ?. harkfilePath || "./harkfile" ) ;
42
+ const possibleHarkfiles = [ harkfilePath , harkfilePath + _path . sep + "index" ] . flatMap ( ( p ) => extensions . map ( ( e ) => p + e ) ) ;
43
+ const foundHarkfile = await findFirstExistingFile ( possibleHarkfiles ) ;
44
+ if ( foundHarkfile == null ) {
45
+ throw new Error ( `\nNo harkfile detected at any of these paths:\n- ` + possibleHarkfiles . join ( "- \n" ) ) ;
46
+ }
47
+
48
+ let harkFileFn : HarkfileFunction < any , any > ;
49
+ try {
50
+ harkFileFn = ( await import ( foundHarkfile ) ) . default ;
51
+ } catch ( ex ) {
52
+ console . error ( `\nError while loading harkfile at "${ harkfilePath } ".\n` ) ;
53
+ throw ex ;
31
54
}
32
55
33
56
let harkfileContext : any ;
@@ -52,12 +75,12 @@ export async function cliStartExit<T>(args: string[], options?: HarkCliStartOpti
52
75
process . exit ( code ) ;
53
76
}
54
77
55
- async function fileExists ( filepath : string , extensions : string [ ] = [ "" ] ) {
56
- for ( const extension of extensions ) {
78
+ async function findFirstExistingFile ( filepaths : string [ ] ) : Promise < string | undefined > {
79
+ for ( const filepath of filepaths ) {
57
80
try {
58
- await fsp . access ( ` ${ filepath } ${ extension } ` ) ;
59
- return true ;
81
+ await fsp . access ( filepath ) ;
82
+ return filepath ;
60
83
} catch { }
61
84
}
62
- return false ;
85
+ return undefined ;
63
86
}
0 commit comments