1
1
import chalk from 'chalk' ;
2
2
import enquirer from 'enquirer' ;
3
3
import fs from 'fs-extra' ;
4
- import gittar from 'gittar ' ;
4
+ import url from 'node:url ' ;
5
5
import ora from 'ora' ;
6
6
import path from 'path' ;
7
7
import shell from 'shelljs' ;
8
8
import util from 'util' ;
9
9
import Constants from './constants.js' ;
10
+ import { setupProject } from './helpers.js' ;
11
+
12
+ const __filename = url . fileURLToPath ( import . meta. url ) ;
13
+ const __dirname = path . dirname ( __filename ) ;
10
14
11
15
const shellExec = util . promisify ( shell . exec ) ;
12
16
@@ -18,6 +22,11 @@ const shellExec = util.promisify(shell.exec);
18
22
* @return {Promise<void> }
19
23
*/
20
24
export async function example ( example ) {
25
+ if ( ! shell . which ( 'git' ) ) {
26
+ console . error ( chalk . red ( 'Please ensure Git is installed, then try again.' ) ) ;
27
+ shell . exit ( 1 ) ;
28
+ }
29
+
21
30
if ( ! example ) {
22
31
const res = await enquirer . prompt ( {
23
32
type : 'select' ,
@@ -44,20 +53,18 @@ export async function example(example) {
44
53
}
45
54
46
55
const dir = findUniqueDir ( example ) ;
47
- const lang = 'ts' ;
48
56
const isWindows = process . platform === 'win32' ;
49
57
50
- if ( ! ( await fetchProjectTemplate ( dir , lang ) ) ) shell . exit ( 1 ) ;
51
- if ( ! ( await extractExample ( example , dir , lang ) ) ) shell . exit ( 1 ) ;
58
+ if ( ! ( await setupProject ( path . join ( shell . pwd ( ) . toString ( ) , dir ) ) ) ) {
59
+ shell . exit ( 1 ) ;
60
+ }
61
+ if ( ! ( await updateExampleSources ( example , dir ) ) ) {
62
+ shell . exit ( 1 ) ;
63
+ }
52
64
53
65
// Set dir for shell commands. Doesn't change user's dir in their CLI.
54
66
shell . cd ( dir ) ;
55
67
56
- if ( ! shell . which ( 'git' ) ) {
57
- console . error ( chalk . red ( 'Please ensure Git is installed, then try again.' ) ) ;
58
- shell . exit ( 1 ) ;
59
- }
60
-
61
68
await step ( 'Initialize Git repo' , 'git init -q' ) ;
62
69
63
70
await step (
@@ -90,48 +97,6 @@ export async function example(example) {
90
97
process . exit ( 0 ) ;
91
98
}
92
99
93
- /**
94
- * Fetch project template.
95
- * @param {string } example Name of the destination dir.
96
- * @param {string } lang ts or js
97
- * @returns {Promise<boolean> } True if successful; false if not.
98
- */
99
- async function fetchProjectTemplate ( name , lang ) {
100
- const projectName = lang === 'ts' ? 'project-ts' : 'project' ;
101
- const step = 'Fetch project template' ;
102
- const spin = ora ( `${ step } ...` ) . start ( ) ;
103
-
104
- try {
105
- const TEMP = '.temp-dir' ;
106
- const templatePath = `templates/${ projectName } ` ;
107
-
108
- if ( process . env . CI ) {
109
- shell . mkdir ( '-p' , path . join ( TEMP , templatePath ) ) ;
110
- shell . cp ( '-r' , `${ templatePath } /.` , path . join ( TEMP , templatePath ) ) ;
111
- } else {
112
- const src = 'github:o1-labs/zkapp-cli#main' ;
113
- await gittar . fetch ( src , { force : true } ) ;
114
-
115
- // Note: Extract will overwrite any existing dir's contents. But we're
116
- // using an always-unique name.
117
- await gittar . extract ( src , TEMP , {
118
- filter ( path ) {
119
- return path . includes ( templatePath ) ;
120
- } ,
121
- } ) ;
122
- }
123
-
124
- shell . mv ( path . join ( TEMP , templatePath ) , name ) ;
125
- shell . rm ( '-r' , TEMP ) ;
126
- spin . succeed ( chalk . green ( step ) ) ;
127
- return true ;
128
- } catch ( err ) {
129
- spin . fail ( step ) ;
130
- console . error ( err ) ;
131
- return false ;
132
- }
133
- }
134
-
135
100
/**
136
101
* Helper for any steps that need to call a shell command.
137
102
* @param {string } step Name of step to show user
@@ -210,46 +175,40 @@ export function kebabCase(str) {
210
175
}
211
176
212
177
/**
213
- * Fetch an example & place in the `src` directory .
214
- * @param {string } example Name of the example, as found in our GitHub repo .
178
+ * Updates the example sources .
179
+ * @param {string } example Name of the example.
215
180
* @param {string } name Destination dir name.
216
- * @param {string } lang ts or js
181
+ * @param {string } lang ts (default) or js
217
182
* @returns {Promise<boolean> } True if successful; false if not.
218
183
*/
219
- export async function extractExample ( example , name , lang ) {
220
- const step = 'Extract example' ;
184
+ export async function updateExampleSources ( example , name , lang = 'ts' ) {
185
+ const step = 'Update example sources ' ;
221
186
const spin = ora ( `${ step } ...` ) . start ( ) ;
222
187
223
188
try {
224
- const TEMP = '.temp-dir' ;
225
- const examplePath = `examples/${ example } /${ lang } /src` ;
226
-
227
- if ( process . env . CI ) {
228
- shell . mkdir ( '-p' , path . join ( TEMP , examplePath ) ) ;
229
- shell . cp ( '-r' , `${ examplePath } /.` , path . join ( TEMP , examplePath ) ) ;
230
- } else {
231
- const src = 'github:o1-labs/zkapp-cli#main' ;
232
-
233
- // Note: Extract will overwrite any existing dir's contents. That's ok here.
234
- await gittar . extract ( src , TEMP , {
235
- filter ( path ) {
236
- return path . includes ( examplePath ) ;
237
- } ,
238
- } ) ;
239
- }
189
+ const examplePath = path . resolve (
190
+ __dirname ,
191
+ '..' ,
192
+ '..' ,
193
+ 'examples' ,
194
+ example ,
195
+ lang ,
196
+ 'src'
197
+ ) ;
240
198
241
199
// Example not found. Delete the project template & temp dir to clean up.
242
- if ( isEmpty ( TEMP ) ) {
200
+ if ( isEmpty ( examplePath ) ) {
243
201
spin . fail ( step ) ;
244
202
console . error ( chalk . red ( 'Example not found' ) ) ;
245
- shell . rm ( '-r' , `${ process . cwd ( ) } /${ name } ` , TEMP ) ;
246
203
return false ;
247
204
}
248
205
249
206
// Delete the project template's `src` & use the example's `src` instead.
250
- shell . rm ( '-r' , `${ name } /src` ) ;
251
- shell . mv ( `${ TEMP } /${ examplePath } ` , `${ name } /src` ) ;
252
- shell . rm ( '-r' , TEMP ) ;
207
+ const srcPath = path . resolve ( name , 'src' ) ;
208
+ shell . rm ( '-r' , srcPath ) ;
209
+ // `node:fs.cpSync` instead of the `shell.cp` because `ShellJS` does not implement `cp -a`
210
+ // https://github.com/shelljs/shelljs/issues/79#issuecomment-30821277
211
+ fs . cpSync ( `${ examplePath } /` , `${ srcPath } /` , { recursive : true } ) ;
253
212
spin . succeed ( chalk . green ( step ) ) ;
254
213
return true ;
255
214
} catch ( err ) {
0 commit comments