Skip to content

Commit 8fdcf49

Browse files
committed
Use npm instead of JSR for init script
1 parent 4f1238e commit 8fdcf49

File tree

5 files changed

+69
-19
lines changed

5 files changed

+69
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ If you prefer the command line, or need to run a server, install either [Deno](h
3030

3131
After [installing Deno](https://docs.deno.com/runtime/getting_started/installation/), either use the [Mastro template for Deno](https://github.com/mastrojs/template-basic-deno) or run:
3232

33-
deno run -A jsr:@mastrojs/[email protected]/init
33+
deno run -A npm:@mastrojs/init@latest
3434

3535
#### Node.js
3636

deno.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"exports": {
55
".": "./src/core/index.ts",
66
"./generator": "./src/generator.ts",
7-
"./init": "./src/init.ts",
87
"./server": "./src/server.ts"
98
},
109
"compilerOptions": {

src/node/init/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Create a new [Mastro](https://mastrojs.github.io/) project. Usage:
2+
3+
Deno:
4+
5+
deno run -A npm:@mastrojs/init@latest
6+
7+
Node.js
8+
9+
npx @mastrojs/init@latest

src/init.ts renamed to src/node/init/init.js

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
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

814
import { exec } from "node:child_process";
15+
import { createWriteStream } from "node:fs";
916
import fs from "node:fs/promises";
1017
import { stdin, stdout } from "node:process";
1118
import { 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+
3654
if (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
`
5977
Success!
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
"",

src/node/init/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "@mastrojs/init",
3+
"version": "0.0.2",
4+
"type": "module",
5+
"scripts": {
6+
"publish": "npm publish --access public"
7+
},
8+
"bin": {
9+
"init": "init.js"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/mastrojs/mastro.git"
14+
},
15+
"author": "Mauro Bieg",
16+
"license": "MIT",
17+
"bugs": {
18+
"url": "https://github.com/mastrojs/mastro/issues"
19+
},
20+
"homepage": "https://mastrojs.github.io/",
21+
"volta": {
22+
"node": "24.9.0"
23+
}
24+
}

0 commit comments

Comments
 (0)