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