1+ #!/usr/bin/env node
2+
13/**
24 * This script initializes an empty Mastro project. Usage:
3- * Deno: `deno run -A jsr:@mastrojs/[email protected] /init` or 4- * Node.js: `npx xjsr @mastrojs/mastro/init`.
5- * @module
5+ *
6+ * Deno: `deno run -A npm:@mastrojs/init@latest`
7+ * Node.js: `npx @mastrojs/init@latest`
8+ *
9+ * This is an npm package until JSR has a way to create a "bin" field in the generated package.json
10+ * See https://github.com/jsr-io/jsr-npm/issues/76
11+ * and https://gitlab.com/soapbox-pub/xjsr/-/issues/2
612 */
713
814import { exec } from "node:child_process" ;
15+ import { createWriteStream } from "node:fs" ;
916import fs from "node:fs/promises" ;
1017import { stdin , stdout } from "node:process" ;
1118import { createInterface } from "node:readline/promises" ;
12- import { writeFile } from "./node/writeFile.ts" ;
13-
14- const isDeno = typeof Deno === "object" ;
15- const repoName = `template-basic-${ isDeno ? "deno" : "node" } ` ;
16- const repoUrl = `https://github.com/mastrojs/${ repoName } /archive/refs/heads/main.zip` ;
17- const zipFilePromise = fetch ( repoUrl ) ;
19+ import { Readable } from "node:stream" ;
1820
19- const rl = createInterface ( { input : stdin , output : stdout , crlfDelay : Infinity } ) ;
20- const dir = await rl . question ( "What folder should we create for your new project?\n" ) ;
21- rl . close ( ) ;
22- stdin . destroy ( ) ;
21+ const writeFile = ( path , data ) => {
22+ if ( typeof Deno === "object" ) {
23+ return Deno . writeFile ( path , data ) ;
24+ } else {
25+ return new Promise ( ( resolve , reject ) =>
26+ Readable . fromWeb ( data )
27+ . pipe ( createWriteStream ( path ) )
28+ . on ( "finish" , resolve )
29+ . on ( "error" , reject )
30+ ) ;
31+ }
32+ }
2333
24- const execCmd = (
25- cmd : string ,
26- ) : Promise < { code : number ; stdout : string ; stderr : string } > =>
34+ const execCmd = ( cmd ) =>
2735 new Promise ( ( resolve ) =>
2836 exec ( cmd , ( error , stdout , stderr ) =>
2937 resolve ( {
@@ -33,6 +41,16 @@ const execCmd = (
3341 } ) )
3442 ) ;
3543
44+ const isDeno = typeof Deno === "object" ;
45+ const repoName = `template-basic-${ isDeno ? "deno" : "node" } ` ;
46+ const repoUrl = `https://github.com/mastrojs/${ repoName } /archive/refs/heads/main.zip` ;
47+ const zipFilePromise = fetch ( repoUrl ) ;
48+
49+ const rl = createInterface ( { input : stdin , output : stdout , crlfDelay : Infinity } ) ;
50+ const dir = await rl . question ( "What folder should we create for your new project?\n" ) ;
51+ rl . close ( ) ;
52+ stdin . destroy ( ) ;
53+
3654if ( dir ) {
3755 const outDir = repoName + "-main" ; // this cannot be changed and is determined by the zip file
3856 const zipFileName = outDir + ".zip" ;
@@ -58,7 +76,7 @@ if (dir) {
5876 `
5977Success!
6078
61- Enter your project directory with: %ccd ${ dir } ${ isDeno ? "" : "\n\nThen install dependencies with: pnpm install\n \n" }
79+ Enter the newly created folder with: %ccd ${ dir } ${ isDeno ? "" : "\n\nThen install dependencies with: pnpm install\n" }
6280%cThen start the dev server with: %c${ isDeno ? "deno task" : "pnpm run" } start` ,
6381 codeStyle ,
6482 "" ,
0 commit comments