@@ -10,100 +10,8 @@ import * as path from "path";
1010import * as fs from "fs" ;
1111import * as url from 'url' ;
1212
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' ;
10715
10816const defaultOptions : AppOptions = {
10917 'public-dir' : undefined ,
@@ -137,23 +45,18 @@ function pickKeys(keys: string[], object: Record<string, any>): Record<string, a
13745export function initApp ( commandLineValues : CommandLineOptions ) {
13846
13947 let options : AppOptions = defaultOptions ;
140- let check : PresetCheckFunc | undefined = undefined ;
48+ let preset : IPresetBase | null = null ;
14149
14250 const presetName = ( commandLineValues [ 'preset' ] as string | null ) ?? 'none' ;
14351 if ( presetName !== 'none' ) {
144- if ( presets [ presetName ] == null ) {
52+ const presetClass = presets [ presetName ] ;
53+ if ( presetClass == null ) {
14554 console . error ( 'Unknown preset name.' ) ;
14655 console . error ( "--preset must be one of: none, " + ( Object . keys ( presets ) . join ( ', ' ) ) ) ;
14756 process . exit ( 1 ) ;
57+ return ;
14858 }
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 ( ) ;
15760 }
15861
15962 let packageJson ;
@@ -167,6 +70,7 @@ export function initApp(commandLineValues: CommandLineOptions) {
16770
16871 options = {
16972 ...options ,
73+ ...( preset != null ? preset . defaultOptions : { } ) ,
17074 ...pickKeys ( [ 'author' , 'name' , 'description' ] , packageJson ?? { } ) ,
17175 ...pickKeys ( [ 'public-dir' , 'static-dir' , 'spa' , 'not-found-page' , 'auto-index' , 'auto-ext' , 'author' , 'name' , 'description' , 'service-id' ] , commandLineValues )
17276 } ;
@@ -176,10 +80,11 @@ export function initApp(commandLineValues: CommandLineOptions) {
17680 options [ 'not-found-page' ] = options [ 'not-found-page' ] ( options ) ;
17781 }
17882
179- if ( check != null ) {
180- if ( ! check ( packageJson , options ) ) {
83+ if ( preset != null ) {
84+ if ( ! preset . check ( packageJson , options ) ) {
18185 console . log ( "Failed preset check." ) ;
18286 process . exit ( 1 ) ;
87+ return ;
18388 }
18489 }
18590
0 commit comments