@@ -10,100 +10,8 @@ import * as path from "path";
10
10
import * as fs from "fs" ;
11
11
import * as url from 'url' ;
12
12
13
- type AppOptions = {
14
- 'public-dir' : string | undefined ,
15
- 'static-dir' : string | undefined ,
16
- spa : string | null | undefined ,
17
- 'not-found-page' : string | null | ( ( options : AppOptions ) => string | undefined ) | undefined ,
18
- 'auto-index' : string [ ] | null | undefined ,
19
- 'auto-ext' : string [ ] | null | undefined ,
20
- name : string ,
21
- author : string ,
22
- description : string ,
23
- 'service-id' : string | undefined ,
24
- } ;
25
-
26
- type Preset = {
27
- name : string ,
28
- inherit ?: string ,
29
- check ?: PresetCheckFunc | undefined ,
30
- defaultOptions ?: Partial < AppOptions > ,
31
- } ;
32
-
33
- type PresetCheckFunc = ( packageJson : any | null , options : AppOptions ) => boolean ;
34
-
35
- const presets : Record < string , Preset | string > = {
36
- 'cra' : {
37
- name : 'Create React App' ,
38
- defaultOptions : {
39
- 'public-dir' : './build' ,
40
- 'static-dir' : './build/static' ,
41
- spa : undefined ,
42
- name : 'my-create-react-app' ,
43
- description : 'Compute@Edge static site from create-react-app' ,
44
- } ,
45
- check : ( packageJson , options ) => {
46
- if ( packageJson == null ) {
47
- console . error ( "❌ Can't read/parse package.json" ) ;
48
- console . error ( "Run this from a create-react-app project directory." ) ;
49
- return false ;
50
- }
51
- if ( packageJson ?. dependencies ?. [ 'react-scripts' ] == null ) {
52
- console . error ( "❌ Can't find react-scripts in dependencies" ) ;
53
- console . error ( "Run this from a create-react-app project directory." ) ;
54
- console . log ( "If this is a project created with create-react-app and has since been ejected, specify preset cra-eject to skip this check." )
55
- return false ;
56
- }
57
- return true ;
58
- } ,
59
- } ,
60
- 'cra-eject' : {
61
- name : 'Create React App (Ejected)' ,
62
- inherit : 'cra' ,
63
- check : undefined ,
64
- } ,
65
- 'create-react-app' : 'cra' ,
66
- 'vite' : {
67
- name : 'Vite' ,
68
- defaultOptions : {
69
- 'public-dir' : './dist' ,
70
- 'static-dir' : undefined ,
71
- spa : undefined ,
72
- } ,
73
- } ,
74
- 'sveltekit' : {
75
- name : 'SvelteKit' ,
76
- defaultOptions : {
77
- 'public-dir' : './dist' ,
78
- 'static-dir' : undefined ,
79
- spa : undefined ,
80
- } ,
81
- } ,
82
- 'next' : {
83
- name : 'Next.js' ,
84
- defaultOptions : {
85
- 'public-dir' : './out' ,
86
- 'static-dir' : undefined ,
87
- spa : undefined ,
88
- } ,
89
- } ,
90
- 'gatsby' : {
91
- name : 'Gatsby' ,
92
- defaultOptions : {
93
- 'public-dir' : './public' ,
94
- 'static-dir' : undefined ,
95
- spa : undefined ,
96
- } ,
97
- } ,
98
- 'docusaurus' : {
99
- name : 'Docusaurus' ,
100
- defaultOptions : {
101
- 'public-dir' : './build' ,
102
- 'static-dir' : undefined ,
103
- spa : undefined ,
104
- } ,
105
- } ,
106
- } ;
13
+ import { AppOptions , IPresetBase } from './presets/preset-base.js' ;
14
+ import { presets } from './presets/index.js' ;
107
15
108
16
const defaultOptions : AppOptions = {
109
17
'public-dir' : undefined ,
@@ -137,23 +45,18 @@ function pickKeys(keys: string[], object: Record<string, any>): Record<string, a
137
45
export function initApp ( commandLineValues : CommandLineOptions ) {
138
46
139
47
let options : AppOptions = defaultOptions ;
140
- let check : PresetCheckFunc | undefined = undefined ;
48
+ let preset : IPresetBase | null = null ;
141
49
142
50
const presetName = ( commandLineValues [ 'preset' ] as string | null ) ?? 'none' ;
143
51
if ( presetName !== 'none' ) {
144
- if ( presets [ presetName ] == null ) {
52
+ const presetClass = presets [ presetName ] ;
53
+ if ( presetClass == null ) {
145
54
console . error ( 'Unknown preset name.' ) ;
146
55
console . error ( "--preset must be one of: none, " + ( Object . keys ( presets ) . join ( ', ' ) ) ) ;
147
56
process . exit ( 1 ) ;
57
+ return ;
148
58
}
149
-
150
- let presetDef : string | Preset = presetName ;
151
- while ( typeof presetDef === 'string' ) {
152
- presetDef = presets [ presetDef ] ;
153
- }
154
-
155
- options = { ...options , ...presetDef . defaultOptions } ;
156
- check = presetDef . check ;
59
+ preset = new presetClass ( ) ;
157
60
}
158
61
159
62
let packageJson ;
@@ -167,6 +70,7 @@ export function initApp(commandLineValues: CommandLineOptions) {
167
70
168
71
options = {
169
72
...options ,
73
+ ...( preset != null ? preset . defaultOptions : { } ) ,
170
74
...pickKeys ( [ 'author' , 'name' , 'description' ] , packageJson ?? { } ) ,
171
75
...pickKeys ( [ 'public-dir' , 'static-dir' , 'spa' , 'not-found-page' , 'auto-index' , 'auto-ext' , 'author' , 'name' , 'description' , 'service-id' ] , commandLineValues )
172
76
} ;
@@ -176,10 +80,11 @@ export function initApp(commandLineValues: CommandLineOptions) {
176
80
options [ 'not-found-page' ] = options [ 'not-found-page' ] ( options ) ;
177
81
}
178
82
179
- if ( check != null ) {
180
- if ( ! check ( packageJson , options ) ) {
83
+ if ( preset != null ) {
84
+ if ( ! preset . check ( packageJson , options ) ) {
181
85
console . log ( "Failed preset check." ) ;
182
86
process . exit ( 1 ) ;
87
+ return ;
183
88
}
184
89
}
185
90
0 commit comments