create-guardio 0.0.8 → 0.0.9

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 (2) hide show
  1. package/index.mjs +74 -5
  2. package/package.json +5 -2
package/index.mjs CHANGED
@@ -1,11 +1,22 @@
1
1
  #!/usr/bin/env node
2
2
  import { createInterface } from "node:readline";
3
- import { mkdir, writeFile } from "node:fs/promises";
4
- import { resolve, dirname } from "node:path";
3
+ import { mkdir, writeFile, mkdtemp } from "node:fs/promises";
4
+ import { createWriteStream } from "node:fs";
5
+ import { readdirSync, cpSync, existsSync } from "node:fs";
6
+ import { resolve, dirname, join } from "node:path";
5
7
  import { fileURLToPath } from "node:url";
8
+ import { pipeline } from "node:stream/promises";
9
+ import { tmpdir } from "node:os";
10
+ import { spawnSync } from "node:child_process";
11
+ import decompress from "decompress";
6
12
 
7
13
  const __dirname = dirname(fileURLToPath(import.meta.url));
8
14
 
15
+ const DASHBOARD_REPO =
16
+ process.env.GUARDIO_DASHBOARD_REPO || "radoslaw-sz/guardio";
17
+ const DASHBOARD_BRANCH = process.env.GUARDIO_DASHBOARD_BRANCH || "main";
18
+ const DASHBOARD_TARBALL_URL = `https://github.com/${DASHBOARD_REPO}/archive/refs/heads/${DASHBOARD_BRANCH}.tar.gz`;
19
+
9
20
  const rl = createInterface({ input: process.stdin, output: process.stdout });
10
21
 
11
22
  function ask(question, defaultAnswer = "") {
@@ -27,6 +38,61 @@ function askYesNo(question, defaultAnswer = "n") {
27
38
  );
28
39
  }
29
40
 
41
+ async function setupDashboardFromTarball(guardioPath) {
42
+ const dashboardPath = join(guardioPath, "dashboard");
43
+ console.log("Downloading dashboard from GitHub...");
44
+ let res;
45
+ try {
46
+ res = await fetch(DASHBOARD_TARBALL_URL, { redirect: "follow" });
47
+ } catch (err) {
48
+ console.error(
49
+ "Could not download dashboard template. Check network and GUARDIO_DASHBOARD_REPO.",
50
+ );
51
+ throw err;
52
+ }
53
+ if (!res.ok) {
54
+ throw new Error(
55
+ `Dashboard download failed: ${res.status} ${res.statusText}. Check repo (${DASHBOARD_REPO}) and branch (${DASHBOARD_BRANCH}).`,
56
+ );
57
+ }
58
+ const tmpDir = await mkdtemp(join(tmpdir(), "guardio-dashboard-"));
59
+ const tarPath = join(tmpDir, "archive.tar.gz");
60
+ const w = createWriteStream(tarPath);
61
+ await pipeline(res.body, w);
62
+ console.log("Extracting...");
63
+ const extractDir = join(tmpDir, "extract");
64
+ await mkdir(extractDir, { recursive: true });
65
+ await decompress(tarPath, extractDir);
66
+ const topLevel = readdirSync(extractDir)[0];
67
+ if (!topLevel) {
68
+ throw new Error("Dashboard tarball had no top-level directory.");
69
+ }
70
+ const srcDashboard = join(extractDir, topLevel, "packages", "dashboard");
71
+ if (!existsSync(srcDashboard)) {
72
+ throw new Error(
73
+ "Dashboard tarball missing packages/dashboard. Wrong repo or branch?",
74
+ );
75
+ }
76
+ await mkdir(dashboardPath, { recursive: true });
77
+ cpSync(srcDashboard, dashboardPath, { recursive: true });
78
+ console.log("Installing dashboard dependencies...");
79
+ const installResult = spawnSync(
80
+ "pnpm",
81
+ ["install"],
82
+ { cwd: dashboardPath, stdio: "inherit", shell: true },
83
+ );
84
+ if (installResult.status !== 0) {
85
+ const npmResult = spawnSync(
86
+ "npm",
87
+ ["install"],
88
+ { cwd: dashboardPath, stdio: "inherit", shell: true },
89
+ );
90
+ if (npmResult.status !== 0) {
91
+ throw new Error("Failed to install dashboard dependencies (tried pnpm and npm).");
92
+ }
93
+ }
94
+ }
95
+
30
96
  async function main() {
31
97
  console.log("\nCreate Guardio\n");
32
98
 
@@ -114,8 +180,7 @@ async function main() {
114
180
  },
115
181
  };
116
182
  if (installDashboard) {
117
- packageJson.dependencies["@guardiojs/dashboard"] = "*";
118
- packageJson.scripts.dashboard = "dashboard";
183
+ packageJson.scripts.dashboard = "cd dashboard && npm run dev";
119
184
  }
120
185
  await writeFile(
121
186
  resolve(guardioPath, "package.json"),
@@ -123,6 +188,10 @@ async function main() {
123
188
  "utf-8",
124
189
  );
125
190
 
191
+ if (installDashboard) {
192
+ await setupDashboardFromTarball(guardioPath);
193
+ }
194
+
126
195
  const tsconfig = {
127
196
  compilerOptions: {
128
197
  target: "ES2022",
@@ -216,7 +285,7 @@ Import types from "@guardiojs/guardio".${addExamplePlugin ? " See example/ for a
216
285
  console.log(" npm run guardio");
217
286
  console.log("");
218
287
  if (installDashboard) {
219
- console.log("Run dashboard (point it at Guardio URL):");
288
+ console.log("Run dashboard (standalone copy from GitHub; point at Guardio URL):");
220
289
  console.log(" pnpm run dashboard # or: npm run dashboard");
221
290
  console.log(" # Guardio base URL: http://127.0.0.1:" + port);
222
291
  console.log("");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-guardio",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "Scaffold a Guardio project",
5
5
  "type": "module",
6
6
  "bin": {
@@ -18,5 +18,8 @@
18
18
  "agents"
19
19
  ],
20
20
  "author": "Radoslaw Szymkiewicz",
21
- "license": "Apache-2.0"
21
+ "license": "Apache-2.0",
22
+ "dependencies": {
23
+ "decompress": "^4.2.1"
24
+ }
22
25
  }