@@ -12,7 +12,7 @@ import { registerSpinnerAutostopping } from '../utils/register-spinner-autostopp
12
12
13
13
interface Args {
14
14
dir : string ;
15
- packageManager : string ;
15
+ packageManager ? : string ;
16
16
}
17
17
18
18
const buildPreviewApp = ( absoluteDirectory : string ) => {
@@ -38,47 +38,15 @@ const buildPreviewApp = (absoluteDirectory: string) => {
38
38
} ) ;
39
39
} ;
40
40
41
- const npmInstall = async (
42
- builtPreviewAppPath : string ,
43
- packageManager : string ,
44
- ) => {
45
- return new Promise < void > ( ( resolve , reject ) => {
46
- const childProc = spawn (
47
- packageManager ,
48
- [
49
- 'install' ,
50
- packageManager === 'deno' ? '' : '--include=dev' ,
51
- packageManager === 'deno' ? '--quiet' : '--silent' ,
52
- ] ,
53
- {
54
- cwd : builtPreviewAppPath ,
55
- shell : true ,
56
- } ,
57
- ) ;
58
- childProc . stdout . pipe ( process . stdout ) ;
59
- childProc . stderr . pipe ( process . stderr ) ;
60
- childProc . on ( 'close' , ( code ) => {
61
- if ( code === 0 ) {
62
- resolve ( ) ;
63
- } else {
64
- reject (
65
- new Error (
66
- `Unable to install the dependencies and it exited with code: ${ code } ` ,
67
- ) ,
68
- ) ;
69
- }
70
- } ) ;
71
- } ) ;
72
- } ;
73
-
74
41
const setNextEnvironmentVariablesForBuild = async (
75
42
emailsDirRelativePath : string ,
76
- builtPreviewAppPath : string ,
43
+ userProjectPath : string ,
44
+ modifiedPreviewAppPath : string ,
77
45
) => {
78
46
const nextConfigContents = `
79
47
const path = require('path');
80
48
const emailsDirRelativePath = path.normalize('${ emailsDirRelativePath } ');
81
- const userProjectLocation = '${ process . cwd ( ) } ';
49
+ const userProjectLocation = '${ userProjectPath } ';
82
50
/** @type {import('next').NextConfig} */
83
51
module.exports = {
84
52
env: {
@@ -111,7 +79,7 @@ module.exports = {
111
79
}` ;
112
80
113
81
await fs . promises . writeFile (
114
- path . resolve ( builtPreviewAppPath , './next.config.js' ) ,
82
+ path . resolve ( modifiedPreviewAppPath , './next.config.js' ) ,
115
83
nextConfigContents ,
116
84
'utf8' ,
117
85
) ;
@@ -203,19 +171,9 @@ const updatePackageJson = async (builtPreviewAppPath: string) => {
203
171
} ;
204
172
packageJson . scripts . build = 'next build' ;
205
173
packageJson . scripts . start = 'next start' ;
206
- delete packageJson . scripts . postbuild ;
207
174
208
175
packageJson . name = 'preview-server' ;
209
176
210
- // We remove this one to avoid having resolve issues on our demo build process.
211
- // This is only used in the `export` command so it's irrelevant to have it here.
212
- //
213
- // See `src/actions/render-email-by-path` for more info on how we render the
214
- // email templates without `@react-email/render` being installed.
215
- delete packageJson . devDependencies [ '@react-email/render' ] ;
216
- delete packageJson . devDependencies [ '@react-email/components' ] ;
217
- delete packageJson . scripts . prepare ;
218
-
219
177
await fs . promises . writeFile (
220
178
packageJsonPath ,
221
179
JSON . stringify ( packageJson ) ,
@@ -227,6 +185,12 @@ export const build = async ({
227
185
dir : emailsDirRelativePath ,
228
186
packageManager,
229
187
} : Args ) => {
188
+ if ( packageManager !== undefined ) {
189
+ console . warn (
190
+ 'The --packageManager flag is deprecated and is currently ignored. It will be removed in the next major version' ,
191
+ ) ;
192
+ }
193
+
230
194
try {
231
195
const previewServerLocation = await getPreviewServerLocation ( ) ;
232
196
@@ -244,61 +208,81 @@ export const build = async ({
244
208
const emailsDirPath = path . join ( process . cwd ( ) , emailsDirRelativePath ) ;
245
209
const staticPath = path . join ( emailsDirPath , 'static' ) ;
246
210
211
+ const modifiedPreviewAppPath = path . resolve (
212
+ previewServerLocation ,
213
+ '../.react-email' ,
214
+ ) ;
215
+ if ( fs . existsSync ( modifiedPreviewAppPath ) ) {
216
+ spinner . text = 'Deleting pre-existing modified preview app folder' ;
217
+ await fs . promises . rm ( modifiedPreviewAppPath , { recursive : true } ) ;
218
+ }
247
219
const builtPreviewAppPath = path . join ( process . cwd ( ) , '.react-email' ) ;
248
-
249
220
if ( fs . existsSync ( builtPreviewAppPath ) ) {
250
- spinner . text = 'Deleting pre-existing ` .react-email` folder' ;
221
+ spinner . text = 'Deleting pre-existing .react-email folder' ;
251
222
await fs . promises . rm ( builtPreviewAppPath , { recursive : true } ) ;
252
223
}
253
224
254
- spinner . text = 'Copying preview app from CLI to `.react-email` ' ;
255
- await fs . promises . cp ( previewServerLocation , builtPreviewAppPath , {
225
+ spinner . text = 'Copying preview app from CLI to modify it ' ;
226
+ await fs . promises . cp ( previewServerLocation , modifiedPreviewAppPath , {
256
227
recursive : true ,
257
228
filter : ( source : string ) => {
258
- // do not copy the CLI files
259
229
return (
260
230
! / ( \/ | \\ ) c l i ( \/ | \\ ) ? / . test ( source ) &&
261
231
! / ( \/ | \\ ) \. n e x t ( \/ | \\ ) ? / . test ( source ) &&
262
- ! / ( \/ | \\ ) \. t u r b o ( \/ | \\ ) ? / . test ( source ) &&
263
- ! / ( \/ | \\ ) n o d e _ m o d u l e s ( \/ | \\ ) ? $ / . test ( source )
232
+ ! / ( \/ | \\ ) \. t u r b o ( \/ | \\ ) ? / . test ( source )
264
233
) ;
265
234
} ,
266
235
} ) ;
267
236
268
237
if ( fs . existsSync ( staticPath ) ) {
269
- spinner . text =
270
- 'Copying `static` folder into `.react-email/public/static`' ;
271
- const builtStaticDirectory = path . resolve (
272
- builtPreviewAppPath ,
238
+ spinner . text = 'Copying static directory' ;
239
+ const modifiedPreviewAppStaticDirectory = path . resolve (
240
+ modifiedPreviewAppPath ,
273
241
'./public/static' ,
274
242
) ;
275
- await fs . promises . cp ( staticPath , builtStaticDirectory , {
243
+ await fs . promises . cp ( staticPath , modifiedPreviewAppStaticDirectory , {
276
244
recursive : true ,
277
245
} ) ;
278
246
}
279
247
280
- spinner . text =
281
- 'Setting Next environment variables for preview app to work properly' ;
248
+ spinner . text = 'Setting Next environment variables' ;
282
249
await setNextEnvironmentVariablesForBuild (
283
250
emailsDirRelativePath ,
284
- builtPreviewAppPath ,
251
+ process . cwd ( ) ,
252
+ modifiedPreviewAppPath ,
285
253
) ;
286
254
287
- spinner . text = 'Setting server side generation for the email preview pages ' ;
288
- await forceSSGForEmailPreviews ( emailsDirPath , builtPreviewAppPath ) ;
255
+ spinner . text = 'Setting up server side generation' ;
256
+ await forceSSGForEmailPreviews ( emailsDirPath , modifiedPreviewAppPath ) ;
289
257
290
258
spinner . text = "Updating package.json's build and start scripts" ;
291
- await updatePackageJson ( builtPreviewAppPath ) ;
292
-
293
- spinner . text = 'Installing dependencies on `.react-email`' ;
294
- await npmInstall ( builtPreviewAppPath , packageManager ) ;
259
+ await updatePackageJson ( modifiedPreviewAppPath ) ;
295
260
296
261
spinner . stopAndPersist ( {
297
- text : 'Successfully prepared `.react-email` for ` next build` ' ,
262
+ text : 'Ready for next build' ,
298
263
symbol : logSymbols . success ,
299
264
} ) ;
265
+ await buildPreviewApp ( modifiedPreviewAppPath ) ;
300
266
301
- await buildPreviewApp ( builtPreviewAppPath ) ;
267
+ await fs . promises . mkdir ( builtPreviewAppPath ) ;
268
+ await fs . promises . cp (
269
+ path . join ( modifiedPreviewAppPath , '.next' ) ,
270
+ path . join ( builtPreviewAppPath , '.next' ) ,
271
+ {
272
+ recursive : true ,
273
+ } ,
274
+ ) ;
275
+ await fs . promises . cp (
276
+ path . join ( modifiedPreviewAppPath , 'public' ) ,
277
+ path . join ( builtPreviewAppPath , 'public' ) ,
278
+ {
279
+ recursive : true ,
280
+ } ,
281
+ ) ;
282
+
283
+ await fs . promises . rm ( modifiedPreviewAppPath , {
284
+ recursive : true ,
285
+ } ) ;
302
286
} catch ( error ) {
303
287
console . log ( error ) ;
304
288
process . exit ( 1 ) ;
0 commit comments