1
+ import chalk from 'chalk' ;
2
+ import fs from 'fs-extra' ;
3
+ import path from 'path' ;
4
+ import { execSync } from 'child_process' ;
5
+ import yargs from 'yargs'
6
+ import { hideBin } from 'yargs/helpers'
7
+
8
+ const __dirname = path . resolve ( ) ;
9
+
10
+ const templates = {
11
+ default : path . join ( __dirname , 'templates' , 'default' ) ,
12
+ // add other templates here open the PR/issue
13
+ } ;
14
+ const createReactApp = ( name = "reactive-rust" , template ) => {
15
+ console . log ( chalk . green ( `Creating a new React Rust project "${ name } " ` ) ) ;
16
+
17
+ const templateDir = templates [ template ] ;
18
+ const targetDir = path . join ( process . cwd ( ) , name ) ;
19
+
20
+ fs . ensureDirSync ( targetDir ) ;
21
+ fs . copySync ( templateDir , targetDir ) ;
22
+ console . log ( chalk . bgGreenBright ( chalk . green ( `Installing npm dependencies...` ) ) ) ;
23
+ execSync ( `cd ${ name } && npm install` , { stdio : 'inherit' } ) ;
24
+ console . log ( chalk . bgRed ( chalk . green ( `Setting up rust env...` ) ) ) ;
25
+ execSync ( `rustup target add wasm32-unknown-unknown` ) ;
26
+ execSync ( `cd ${ name } && cargo build --target wasm32-unknown-unknown` ) ;
27
+ console . log ( chalk . bgMagenta ( chalk . green ( `Installing webassembly support...` ) ) ) ;
28
+ execSync ( `cd ${ name } && cargo install -f wasm-bindgen-cli` ) ;
29
+ execSync ( `cd ${ name } && wasm-bindgen target/wasm32-unknown-unknown/debug/rusty_react.wasm --out-dir build` )
30
+ console . log ( chalk . bgGray ( chalk . green ( `Building your app, this may take few moments...` ) ) ) ;
31
+ execSync ( `cd ${ name } && npm run build` ) ;
32
+ console . log ( chalk . green ( `Done!` ) ) ;
33
+ execSync ( `cd ${ name } && npm run dev` ) ;
34
+ } ;
35
+
36
+ yargs ( hideBin ( process . argv ) )
37
+ . command (
38
+ 'create <name>' ,
39
+ 'create a new react-rust project' ,
40
+ ( yargs ) => {
41
+ return yargs
42
+ . positional ( 'name' , {
43
+ describe : 'name of the project' ,
44
+ type : 'string'
45
+ } )
46
+ } , ( argv ) => {
47
+ createReactApp ( argv . name , argv . template )
48
+ } )
49
+ . demandCommand ( 1 )
50
+ . parse ( )
0 commit comments