create-guardio 0.0.8 → 0.0.10

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 +86 -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,73 @@ 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
+ // Copy only the dashboard package contents into guardioPath/dashboard (never the whole repo)
77
+ await mkdir(dashboardPath, { recursive: true });
78
+ const dashboardEntries = readdirSync(srcDashboard, { withFileTypes: true });
79
+ for (const e of dashboardEntries) {
80
+ cpSync(join(srcDashboard, e.name), join(dashboardPath, e.name), {
81
+ recursive: true,
82
+ });
83
+ }
84
+ const dashboardPkgJson = join(dashboardPath, "package.json");
85
+ if (!existsSync(dashboardPkgJson)) {
86
+ throw new Error(
87
+ "Dashboard package missing package.json after copy. Wrong repo layout?",
88
+ );
89
+ }
90
+ console.log("Installing dashboard dependencies...");
91
+ const installResult = spawnSync(
92
+ "pnpm",
93
+ ["install"],
94
+ { cwd: dashboardPath, stdio: "inherit", shell: true },
95
+ );
96
+ if (installResult.status !== 0) {
97
+ const npmResult = spawnSync(
98
+ "npm",
99
+ ["install"],
100
+ { cwd: dashboardPath, stdio: "inherit", shell: true },
101
+ );
102
+ if (npmResult.status !== 0) {
103
+ throw new Error("Failed to install dashboard dependencies (tried pnpm and npm).");
104
+ }
105
+ }
106
+ }
107
+
30
108
  async function main() {
31
109
  console.log("\nCreate Guardio\n");
32
110
 
@@ -114,8 +192,7 @@ async function main() {
114
192
  },
115
193
  };
116
194
  if (installDashboard) {
117
- packageJson.dependencies["@guardiojs/dashboard"] = "*";
118
- packageJson.scripts.dashboard = "dashboard";
195
+ packageJson.scripts.dashboard = "cd dashboard && npm run dev";
119
196
  }
120
197
  await writeFile(
121
198
  resolve(guardioPath, "package.json"),
@@ -123,6 +200,10 @@ async function main() {
123
200
  "utf-8",
124
201
  );
125
202
 
203
+ if (installDashboard) {
204
+ await setupDashboardFromTarball(guardioPath);
205
+ }
206
+
126
207
  const tsconfig = {
127
208
  compilerOptions: {
128
209
  target: "ES2022",
@@ -216,7 +297,7 @@ Import types from "@guardiojs/guardio".${addExamplePlugin ? " See example/ for a
216
297
  console.log(" npm run guardio");
217
298
  console.log("");
218
299
  if (installDashboard) {
219
- console.log("Run dashboard (point it at Guardio URL):");
300
+ console.log("Run dashboard (standalone copy from GitHub; point at Guardio URL):");
220
301
  console.log(" pnpm run dashboard # or: npm run dashboard");
221
302
  console.log(" # Guardio base URL: http://127.0.0.1:" + port);
222
303
  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.10",
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
  }