33import { rimrafSync } from 'rimraf' ;
44import { join } from 'node:path' ;
55import fs from 'node:fs' ;
6+ import colors from '../utils/colors' ;
67
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 ;
369
3710export function write ( message : any ) {
3811 process . stdout . write ( message ) ;
@@ -42,8 +15,8 @@ export function write(message: any) {
4215/**
4316 * @returns {never }
4417 */
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 } ` ) ) ;
4720 process . exit ( 1 ) ;
4821}
4922
@@ -59,17 +32,10 @@ export function findPackageJSON() {
5932}
6033
6134const possibleFileNames = [
62- 'commandkit.json' ,
63- 'commandkit.config.json' ,
6435 'commandkit.js' ,
65- 'commandkit.config.js' ,
6636 'commandkit.mjs' ,
67- 'commandkit.config.mjs' ,
6837 'commandkit.cjs' ,
69- 'commandkit.config.cjs' ,
7038 'commandkit.ts' ,
71- 'commandkit.mts' ,
72- 'commandkit.cts' ,
7339] ;
7440
7541export async function findCommandKitConfig ( src : string ) {
@@ -89,36 +55,71 @@ export async function findCommandKitConfig(src: string) {
8955 panic ( `Could not locate commandkit config from ${ cwd } ` ) ;
9056}
9157
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 ) ;
9460
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+ }
9970 }
71+
72+ return true ;
10073}
10174
10275async function loadConfigInner ( target : string ) {
103- const isJSON = target . endsWith ( '.json' ) ;
104-
10576 await ensureExists ( target ) ;
10677
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+ }
108106
109107 /**
110108 * @type {import('..').CommandKitConfig }
111109 */
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 ) ;
116113
117114 return config ;
118115}
119116
120117async 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+ }
122123}
123124
124125export function erase ( dir : string ) {
0 commit comments