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