@@ -8,7 +8,27 @@ import logSymbols from 'log-symbols';
8
8
import ora from 'ora' ;
9
9
import { tree } from './tree.js' ;
10
10
11
- const init = async ( name ) => {
11
+ const getLatestVersionOfTag = async ( packageName , tag ) => {
12
+ const response = await fetch (
13
+ `https://registry.npmjs.org/${ packageName } /${ tag } ` ,
14
+ ) ;
15
+ const data = await response . json ( ) ;
16
+
17
+ if ( typeof data === 'string' && data . startsWith ( 'version not found' ) ) {
18
+ console . error ( `Tag ${ tag } does not exist for ${ packageName } .` ) ;
19
+ process . exit ( 1 ) ;
20
+ }
21
+
22
+ const { version } = data ;
23
+
24
+ if ( ! / ^ \d + \. \d + \. \d + .* $ / . test ( version ) ) {
25
+ console . error ( 'Invalid version received, something has gone very wrong.' ) ;
26
+ }
27
+
28
+ return version ;
29
+ } ;
30
+
31
+ const init = async ( name , { tag } ) => {
12
32
let projectPath = name ;
13
33
14
34
if ( ! projectPath ) {
@@ -39,25 +59,18 @@ const init = async (name) => {
39
59
resolvedProjectPath ,
40
60
'./package.json' ,
41
61
) ;
42
- const templatePackageJson = JSON . parse (
43
- fse . readFileSync ( templatePackageJsonPath , 'utf8' ) ,
44
- ) ;
45
- for ( const key in templatePackageJson . dependencies ) {
46
- // We remove any workspace prefix that might have been added for the purposes
47
- // of being used locally
48
- templatePackageJson . dependencies [ key ] = templatePackageJson . dependencies [
49
- key
50
- ] . replace ( 'workspace:' , '' ) ;
51
- }
52
- for ( const key in templatePackageJson . devDependencies ) {
53
- // We remove any workspace prefix that might have been added for the purposes
54
- // of being used locally
55
- templatePackageJson . devDependencies [ key ] =
56
- templatePackageJson . devDependencies [ key ] . replace ( 'workspace:' , '' ) ;
57
- }
62
+ const templatePackageJson = fse . readFileSync ( templatePackageJsonPath , 'utf8' ) ;
58
63
fse . writeFileSync (
59
64
templatePackageJsonPath ,
60
- JSON . stringify ( templatePackageJson , null , 2 ) ,
65
+ templatePackageJson
66
+ . replace (
67
+ 'INSERT_COMPONENTS_VERSION' ,
68
+ await getLatestVersionOfTag ( '@react-email/components' , tag ) ,
69
+ )
70
+ . replace (
71
+ 'INSERT_REACT_EMAIL_VERSION' ,
72
+ await getLatestVersionOfTag ( 'react-email' , tag ) ,
73
+ ) ,
61
74
'utf8' ,
62
75
) ;
63
76
@@ -80,6 +93,7 @@ new Command()
80
93
. name ( 'create-email' )
81
94
. version ( '0.0.30-canary.0' )
82
95
. description ( 'The easiest way to get started with React Email' )
83
- . arguments ( '[dir]' , 'path to initialize the project' )
96
+ . arguments ( '[dir]' , 'Path to initialize the project' )
97
+ . option ( '-t, --tag <tag>' , 'Tag of React Email versions to use' , 'latest' )
84
98
. action ( init )
85
99
. parse ( process . argv ) ;
0 commit comments