11import { Result } from 'meow' ;
2- import React , { useEffect , useState } from 'react' ;
2+ import React , { ReactNode , useEffect , useState } from 'react' ;
33import { Flags } from '../../lib/cli-flags.js' ;
44import { Box , Text } from 'ink' ;
55import Spinner from 'ink-spinner' ;
6- import { $ } from 'execa' ;
6+ import { $ , execa } from 'execa' ;
77import SelectInput from 'ink-select-input' ;
88import { Item } from '../../lib/types.js' ;
99import { UncontrolledTextInput } from 'ink-text-input' ;
@@ -14,11 +14,9 @@ export default function Create({cli}: {cli: Result<Flags>}) {
1414 const [ framework , setFramework ] = useState < string | undefined > ( undefined ) ;
1515 const [ projectName , setProjectName ] = useState < string | undefined > ( undefined ) ;
1616
17- const [ isCreated , setIsCreated ] = useState ( false ) ;
1817 const [ isFrameworkSelected , setIsFrameworkSelected ] = useState ( false ) ;
1918
20- const [ isPathValid , setIsPathValid ] = useState ( true ) ;
21- const [ isCloneFailed , setIsCloneFailed ] = useState ( false ) ;
19+ const [ message , setMessage ] = useState < ReactNode > ( ) ;
2220
2321 const frameworkItems : Item < string > [ ] = [
2422 {
@@ -48,31 +46,90 @@ export default function Create({cli}: {cli: Result<Flags>}) {
4846 async function createFromTemplate ( name : string ) {
4947 if ( framework === 'react' ) {
5048 // Clone the template repository
49+ setMessage (
50+ < Box >
51+ < Spinner type = "dots" />
52+ < Text >
53+ { ' ' }
54+ Creating a new Pulse Editor app using React template...
55+ </ Text >
56+ </ Box > ,
57+ ) ;
5158 try {
52- await $ `git clone https://github.com/ClayPulse/pulse-editor-extension-template.git ${ name } ` ;
53- setIsCreated ( true ) ;
59+ await $ `git clone --depth 1 https://github.com/ClayPulse/pulse-editor-extension-template.git ${ name } ` ;
5460 } catch ( error ) {
55- setIsCloneFailed ( true ) ;
61+ setMessage (
62+ < Text color = "redBright" >
63+ ❌ Failed to clone the template. Please check your internet
64+ connection and try again.
65+ </ Text > ,
66+ ) ;
5667 return ;
5768 }
5869
5970 // Modify the package.json file to update the name
71+ setMessage (
72+ < Box >
73+ < Spinner type = "dots" />
74+ < Text > Initializing project...</ Text >
75+ </ Box > ,
76+ ) ;
6077 const packageJsonPath = path . join ( process . cwd ( ) , name , 'package.json' ) ;
6178 const packageJson = JSON . parse (
6279 fs . readFileSync ( packageJsonPath , 'utf8' ) ,
6380 ) ;
64- packageJson . name = name ;
81+ packageJson . name = name . replaceAll ( '-' , '_' ) ;
6582
6683 // Write the modified package.json back to the file
6784 fs . writeFileSync ( packageJsonPath , JSON . stringify ( packageJson , null , 2 ) ) ;
85+
86+ // Remove the .git directory
87+ const gitDirPath = path . join ( process . cwd ( ) , name , '.git' ) ;
88+ if ( fs . existsSync ( gitDirPath ) ) {
89+ fs . rmSync ( gitDirPath , { recursive : true , force : true } ) ;
90+ }
91+
92+ // Remove the .github directory
93+ const githubDirPath = path . join ( process . cwd ( ) , name , '.github' ) ;
94+ if ( fs . existsSync ( githubDirPath ) ) {
95+ fs . rmSync ( githubDirPath , { recursive : true , force : true } ) ;
96+ }
97+
98+ setMessage (
99+ < Box >
100+ < Spinner type = "dots" />
101+ < Text > Installing dependencies...</ Text >
102+ </ Box > ,
103+ ) ;
104+ // Run `npm i`
105+ try {
106+ await execa ( `npm install` , {
107+ cwd : path . join ( process . cwd ( ) , name ) ,
108+ } ) ;
109+ } catch ( error ) {
110+ setMessage (
111+ < Text color = "redBright" >
112+ ❌ Failed to install dependencies. Please check your internet
113+ connection and try again.
114+ </ Text > ,
115+ ) ;
116+ return ;
117+ }
118+ setMessage (
119+ < Text > 🚀 Pulse Editor React app project created successfully!</ Text > ,
120+ ) ;
68121 }
69122 }
70123
71124 if ( projectName ) {
72125 // Check if the project already exists
73126 const projectPath = path . join ( process . cwd ( ) , projectName ) ;
74127 if ( fs . existsSync ( projectPath ) ) {
75- setIsPathValid ( false ) ;
128+ setMessage (
129+ < Text color = "redBright" >
130+ ❌ A project with same name already exists in current path.
131+ </ Text > ,
132+ ) ;
76133 return ;
77134 }
78135 createFromTemplate ( projectName ) ;
@@ -116,31 +173,7 @@ export default function Create({cli}: {cli: Result<Flags>}) {
116173
117174 { projectName && (
118175 < >
119- { framework === 'react' &&
120- ( ! isPathValid ? (
121- < Text color = "redBright" >
122- ❌ A project with same name already exists in current path.
123- </ Text >
124- ) : isCloneFailed ? (
125- < Text color = "redBright" >
126- ❌ Failed to clone the template. Please check your internet
127- connection and try again.
128- </ Text >
129- ) : isCreated ? (
130- < Text >
131- 🚀 Pulse Editor React app project created successfully!
132- </ Text >
133- ) : (
134- < >
135- < Box >
136- < Spinner type = "dots" />
137- < Text >
138- { ' ' }
139- Creating a new Pulse Editor app using React template...
140- </ Text >
141- </ Box >
142- </ >
143- ) ) }
176+ { framework === 'react' && < > { message } </ > }
144177 { framework !== 'react' && (
145178 < Text >
146179 🚧 Currently not available. We'd like to invite you to work on
0 commit comments