3
3
import { rimrafSync } from 'rimraf' ;
4
4
import { join } from 'node:path' ;
5
5
import fs from 'node:fs' ;
6
+ import colors from '../utils/colors' ;
6
7
7
- const resetColor = '\x1b[0m' ;
8
-
9
- export const Colors = {
10
- reset : ( text : string ) => `${ text } ${ resetColor } ` ,
11
- bright : ( text : string ) => `\x1b[1m${ text } ${ resetColor } ` ,
12
- dim : ( text : string ) => `\x1b[2m${ text } ${ resetColor } ` ,
13
- underscore : ( text : string ) => `\x1b[4m${ text } ${ resetColor } ` ,
14
- blink : ( text : string ) => `\x1b[5m${ text } ${ resetColor } ` ,
15
- reverse : ( text : string ) => `\x1b[7m${ text } ${ resetColor } ` ,
16
- hidden : ( text : string ) => `\x1b[8m${ text } ${ resetColor } ` ,
17
-
18
- black : ( text : string ) => `\x1b[30m${ text } ${ resetColor } ` ,
19
- red : ( text : string ) => `\x1b[31m${ text } ${ resetColor } ` ,
20
- green : ( text : string ) => `\x1b[32m${ text } ${ resetColor } ` ,
21
- yellow : ( text : string ) => `\x1b[33m${ text } ${ resetColor } ` ,
22
- blue : ( text : string ) => `\x1b[34m${ text } ${ resetColor } ` ,
23
- magenta : ( text : string ) => `\x1b[35m${ text } ${ resetColor } ` ,
24
- cyan : ( text : string ) => `\x1b[36m${ text } ${ resetColor } ` ,
25
- white : ( text : string ) => `\x1b[37m${ text } ${ resetColor } ` ,
26
-
27
- bgBlack : ( text : string ) => `\x1b[40m${ text } ${ resetColor } ` ,
28
- bgRed : ( text : string ) => `\x1b[41m${ text } ${ resetColor } ` ,
29
- bgGreen : ( text : string ) => `\x1b[42m${ text } ${ resetColor } ` ,
30
- bgYellow : ( text : string ) => `\x1b[43m${ text } ${ resetColor } ` ,
31
- bgBlue : ( text : string ) => `\x1b[44m${ text } ${ resetColor } ` ,
32
- bgMagenta : ( text : string ) => `\x1b[45m${ text } ${ resetColor } ` ,
33
- bgCyan : ( text : string ) => `\x1b[46m${ text } ${ resetColor } ` ,
34
- bgWhite : ( text : string ) => `\x1b[47m${ text } ${ resetColor } ` ,
35
- } ;
8
+ let ts : typeof import ( 'typescript' ) | undefined ;
36
9
37
10
export function write ( message : any ) {
38
11
process . stdout . write ( message ) ;
@@ -42,8 +15,8 @@ export function write(message: any) {
42
15
/**
43
16
* @returns {never }
44
17
*/
45
- export function panic ( message : any ) {
46
- write ( Colors . red ( `Error: ${ message } ` ) ) ;
18
+ export function panic ( message : any ) : never {
19
+ write ( colors . red ( `Error: ${ message } ` ) ) ;
47
20
process . exit ( 1 ) ;
48
21
}
49
22
@@ -59,17 +32,10 @@ export function findPackageJSON() {
59
32
}
60
33
61
34
const possibleFileNames = [
62
- 'commandkit.json' ,
63
- 'commandkit.config.json' ,
64
35
'commandkit.js' ,
65
- 'commandkit.config.js' ,
66
36
'commandkit.mjs' ,
67
- 'commandkit.config.mjs' ,
68
37
'commandkit.cjs' ,
69
- 'commandkit.config.cjs' ,
70
38
'commandkit.ts' ,
71
- 'commandkit.mts' ,
72
- 'commandkit.cts' ,
73
39
] ;
74
40
75
41
export async function findCommandKitConfig ( src : string ) {
@@ -89,36 +55,71 @@ export async function findCommandKitConfig(src: string) {
89
55
panic ( `Could not locate commandkit config from ${ cwd } ` ) ;
90
56
}
91
57
92
- function ensureTypeScript ( target : string ) {
93
- const isTypeScript = / \. ( c | m ) t s x ? $ / . test ( target ) ;
58
+ async function ensureTypeScript ( target : string ) {
59
+ const isTypeScript = / \. ( c | m ) ? t s x ? $ / . test ( target ) ;
94
60
95
- if ( isTypeScript && ! process . features . typescript ) {
96
- panic (
97
- 'You are trying to load commandkit config file that is written in typescript. The current Node.js version does not have TypeScript feature enabled.' ,
98
- ) ;
61
+ if ( ! isTypeScript ) return false ;
62
+ if ( process . features . typescript ) return true ;
63
+
64
+ if ( ! ts ) {
65
+ try {
66
+ ts = await import ( 'typescript' ) ;
67
+ } catch {
68
+ panic ( 'TypeScript must be installed to use TypeScript config files.' ) ;
69
+ }
99
70
}
71
+
72
+ return true ;
100
73
}
101
74
102
75
async function loadConfigInner ( target : string ) {
103
- const isJSON = target . endsWith ( '.json' ) ;
104
-
105
76
await ensureExists ( target ) ;
106
77
107
- ensureTypeScript ( target ) ;
78
+ const isTs = await ensureTypeScript ( target ) ;
79
+
80
+ if ( isTs && ts ) {
81
+ const { transpileModule } = ts ;
82
+ const src = fs . readFileSync ( target , 'utf8' ) ;
83
+ const { outputText } = transpileModule ( src , {
84
+ compilerOptions : {
85
+ module : ts . ModuleKind . ESNext ,
86
+ target : ts . ScriptTarget . ESNext ,
87
+ moduleResolution : ts . ModuleResolutionKind . NodeNext ,
88
+ } ,
89
+ fileName : target ,
90
+ } ) ;
91
+
92
+ const nodeModulesPath = join (
93
+ process . cwd ( ) ,
94
+ 'node_modules' ,
95
+ '.commandkit_tmp' ,
96
+ ) ;
97
+
98
+ fs . mkdirSync ( nodeModulesPath , { recursive : true } ) ;
99
+
100
+ const tmpFile = join ( nodeModulesPath , 'compiled-commandkit.config.mjs' ) ;
101
+
102
+ fs . writeFileSync ( tmpFile , outputText ) ;
103
+
104
+ target = tmpFile ;
105
+ }
108
106
109
107
/**
110
108
* @type {import('..').CommandKitConfig }
111
109
*/
112
- // @ts -ignore
113
- const config = await import ( `file://${ target } ` , {
114
- with : isJSON ? { type : 'json' } : undefined ,
115
- } ) . then ( ( conf ) => conf . default || conf ) ;
110
+ const config = await import ( `file://${ target } ` )
111
+ . then ( ( conf ) => conf . default || conf )
112
+ . catch ( console . log ) ;
116
113
117
114
return config ;
118
115
}
119
116
120
117
async function ensureExists ( loc : string ) {
121
- await fs . promises . access ( loc , fs . constants . F_OK ) ;
118
+ const exists = fs . existsSync ( loc ) ;
119
+
120
+ if ( ! exists ) {
121
+ throw new Error ( `File not found: ${ loc } ` ) ;
122
+ }
122
123
}
123
124
124
125
export function erase ( dir : string ) {
0 commit comments