@mastrojs/create-mastro 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +9 -0
  2. package/index.js +98 -0
  3. package/package.json +24 -0
package/README.md ADDED
@@ -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
package/index.js ADDED
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env node
2
+
3
+ //@ts-check
4
+
5
+ /**
6
+ * This script initializes an empty Mastro project. Usage:
7
+ *
8
+ * Deno: `deno run -A npm:@mastrojs/init@latest`
9
+ * Node.js: `npx @mastrojs/init@latest`
10
+ *
11
+ * This is an npm package until JSR has a way to create a "bin" field in the generated package.json
12
+ * See https://github.com/jsr-io/jsr-npm/issues/76
13
+ * and https://gitlab.com/soapbox-pub/xjsr/-/issues/2
14
+ */
15
+
16
+ import { exec } from "node:child_process";
17
+ import { createWriteStream } from "node:fs";
18
+ import fs from "node:fs/promises";
19
+ import { stdin, stdout } from "node:process";
20
+ import { createInterface } from "node:readline/promises";
21
+ import { Readable } from "node:stream";
22
+
23
+ /**
24
+ * @param {string} path
25
+ * @param {ReadableStream<Uint8Array<ArrayBuffer>>} data
26
+ * @returns {Promise<void>}
27
+ */
28
+ const writeFile = (path, data) => {
29
+ if (typeof Deno === "object") {
30
+ return Deno.writeFile(path, data);
31
+ } else {
32
+ return new Promise((resolve, reject) =>
33
+ // @ts-ignore
34
+ Readable.fromWeb(data)
35
+ .pipe(createWriteStream(path))
36
+ .on("finish", resolve)
37
+ .on("error", reject)
38
+ );
39
+ }
40
+ }
41
+
42
+ /**
43
+ * @param {string} cmd
44
+ * @returns {Promise<{code: number; stdout: string; stderr: string;}>}
45
+ */
46
+ const execCmd = (cmd) =>
47
+ new Promise((resolve) =>
48
+ exec(cmd, (error, stdout, stderr) =>
49
+ resolve({
50
+ code: error ? (error.code || -1) : 0,
51
+ stdout,
52
+ stderr,
53
+ }))
54
+ );
55
+
56
+ const isDeno = typeof Deno === "object";
57
+ const repoName = `template-basic-${isDeno ? "deno" : "node"}`;
58
+ const repoUrl = `https://github.com/mastrojs/${repoName}/archive/refs/heads/main.zip`;
59
+ const zipFilePromise = fetch(repoUrl);
60
+
61
+ const rl = createInterface({ input: stdin, output: stdout, crlfDelay: Infinity });
62
+ const dir = await rl.question("What folder should we create for your new project?\n");
63
+ rl.close();
64
+ stdin.destroy();
65
+
66
+ if (dir) {
67
+ const outDir = repoName + "-main"; // this cannot be changed and is determined by the zip file
68
+ const zipFileName = outDir + ".zip";
69
+ const res = await zipFilePromise;
70
+ if (res.ok && res.body) {
71
+ await writeFile(zipFileName, res.body);
72
+ }
73
+
74
+ // unzip using the tar command (should work on unix and Windows 10 and later)
75
+ const { code, stdout, stderr } = await execCmd(`tar -xf ${zipFileName}`);
76
+ const unzipSuccess = code === 0;
77
+ if (!unzipSuccess) {
78
+ console.log(stdout);
79
+ console.error(stderr);
80
+ }
81
+ await fs.rm(zipFileName, { force: true, recursive: true });
82
+
83
+ if (unzipSuccess) {
84
+ await fs.rename(outDir, dir);
85
+
86
+ const codeStyle = "color: blue";
87
+ console.log(
88
+ `
89
+ Success!
90
+
91
+ Enter the newly created folder with: %ccd ${dir}${isDeno ? "" : "\n\nThen install dependencies with: pnpm install\n"}
92
+ %cThen start the dev server with: %c${isDeno ? "deno task" : "pnpm run"} start`,
93
+ codeStyle,
94
+ "",
95
+ codeStyle,
96
+ );
97
+ }
98
+ }
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@mastrojs/create-mastro",
3
+ "version": "0.0.2",
4
+ "type": "module",
5
+ "scripts": {
6
+ "publish": "deno check && npm publish --access public"
7
+ },
8
+ "bin": {
9
+ "create-mastro": "index.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
+ }