voinznext 0.2.1 → 0.3.1

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/cli.js +136 -14
  2. package/install.js +2 -89
  3. package/package.json +3 -2
package/cli.js CHANGED
@@ -1,21 +1,143 @@
1
1
  #!/usr/bin/env node
2
- const { spawn } = require("child_process");
3
- const { existsSync } = require("fs");
2
+ module.exports = { downloadLatest };
3
+
4
+ const { spawnSync } = require("child_process");
5
+ const { createWriteStream, existsSync, mkdirSync, unlinkSync, readFileSync, writeFileSync, chmodSync, renameSync } = require("fs");
6
+ const https = require("https");
4
7
  const { join } = require("path");
5
- const { platform } = require("process");
8
+ const { platform, arch } = require("process");
9
+
10
+ const REPO = "VoinzzZ/VoinzNext";
11
+ const APP = "voinznext";
12
+ const CACHE_TTL = 3600000;
13
+
14
+ const PLATFORM_MAP = { win32: "windows", darwin: "darwin", linux: "linux" };
15
+ const ARCH_MAP = { x64: "amd64", arm64: "arm64" };
16
+
17
+ function getBinaryName() {
18
+ const p = PLATFORM_MAP[platform];
19
+ const a = ARCH_MAP[arch];
20
+ if (!p || !a) return null;
21
+ const name = `${APP}-${p}-${a}`;
22
+ return platform === "win32" ? `${name}.exe` : name;
23
+ }
24
+
25
+ function getBinaryPath() {
26
+ return join(__dirname, "bin", platform === "win32" ? "voinznext.exe" : "voinznext");
27
+ }
6
28
 
7
- const binaryName = platform === "win32" ? "voinznext.exe" : "voinznext";
8
- const binaryPath = join(__dirname, "bin", binaryName);
29
+ function getVersionPath() {
30
+ return join(__dirname, "bin", ".version");
31
+ }
32
+
33
+ function fetchJson(url) {
34
+ return new Promise((resolve, reject) => {
35
+ https.get(url, { headers: { "Accept": "application/vnd.github.v3+json", "User-Agent": "voinznext-updater" } }, (res) => {
36
+ let data = "";
37
+ res.on("data", (chunk) => (data += chunk));
38
+ res.on("end", () => {
39
+ try { resolve(JSON.parse(data)); }
40
+ catch { reject(new Error("Failed to parse response")); }
41
+ });
42
+ }).on("error", reject);
43
+ });
44
+ }
45
+
46
+ function download(url, dest) {
47
+ return new Promise((resolve, reject) => {
48
+ https.get(url, { headers: { "User-Agent": "voinznext-updater" } }, (res) => {
49
+ if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
50
+ download(res.headers.location, dest).then(resolve).catch(reject);
51
+ return;
52
+ }
53
+ if (res.statusCode !== 200) {
54
+ reject(new Error(`HTTP ${res.statusCode}`));
55
+ return;
56
+ }
57
+ const file = createWriteStream(dest);
58
+ res.pipe(file);
59
+ file.on("finish", () => file.close(resolve));
60
+ file.on("error", reject);
61
+ }).on("error", reject);
62
+ });
63
+ }
64
+
65
+ function readVersionCache() {
66
+ try {
67
+ const data = readFileSync(getVersionPath(), "utf8");
68
+ return JSON.parse(data);
69
+ } catch { return null; }
70
+ }
9
71
 
10
- if (!existsSync(binaryPath)) {
11
- console.error(" ✘ Binary not found. Run `npm install` or `npx voinznext` to download it.");
12
- process.exit(1);
72
+ function writeVersionCache(tag) {
73
+ try {
74
+ mkdirSync(join(__dirname, "bin"), { recursive: true });
75
+ writeFileSync(getVersionPath(), JSON.stringify({ tag, checked: Date.now() }));
76
+ } catch {}
13
77
  }
14
78
 
15
- const child = spawn(binaryPath, process.argv.slice(2), { stdio: "inherit" });
79
+ async function downloadLatest() {
80
+ const binaryPath = getBinaryPath();
81
+ const binaryName = getBinaryName();
82
+ if (!binaryName) return;
83
+
84
+ const release = await fetchJson(`https://api.github.com/repos/${REPO}/releases/latest`);
85
+ const version = release.tag_name;
86
+ const url = `https://github.com/${REPO}/releases/download/${version}/${binaryName}`;
87
+ const tmpPath = binaryPath + ".new";
88
+
89
+ mkdirSync(join(__dirname, "bin"), { recursive: true });
90
+ await download(url, tmpPath);
91
+ if (platform !== "win32") chmodSync(tmpPath, 0o755);
92
+
93
+ try {
94
+ if (platform === "win32") unlinkSync(binaryPath);
95
+ renameSync(tmpPath, binaryPath);
96
+ } catch {}
97
+ writeVersionCache(version);
98
+ }
99
+
100
+ async function ensureBinary() {
101
+ const binaryPath = getBinaryPath();
102
+ const cache = readVersionCache();
103
+ const binaryExists = existsSync(binaryPath);
104
+
105
+ if (!cache || !binaryExists) {
106
+ console.log(" ● Downloading voinznext binary...");
107
+ await downloadLatest();
108
+ return;
109
+ }
110
+
111
+ if (Date.now() - cache.checked > CACHE_TTL) {
112
+ try {
113
+ const release = await fetchJson(`https://api.github.com/repos/${REPO}/releases/latest`);
114
+ if (release.tag_name !== cache.tag) {
115
+ console.log(" ● Updating voinznext binary...");
116
+ await downloadLatest();
117
+ return;
118
+ }
119
+ writeVersionCache(cache.tag);
120
+ } catch {}
121
+ }
122
+ }
123
+
124
+ async function main() {
125
+ if (!process.env.VOINZNEXT_INSTALL) {
126
+ await ensureBinary().catch(() => {});
127
+ }
128
+
129
+ if (process.env.VOINZNEXT_INSTALL) {
130
+ process.exit(0);
131
+ }
132
+
133
+ const binaryPath = getBinaryPath();
134
+ if (!existsSync(binaryPath)) {
135
+ console.error(" ✘ Binary not found. Run `npm install -g voinznext@latest --force`.");
136
+ process.exit(1);
137
+ }
138
+
139
+ const result = spawnSync(binaryPath, process.argv.slice(2), { stdio: "inherit" });
140
+ process.exit(result.status);
141
+ }
16
142
 
17
- child.on("exit", (code) => process.exit(code));
18
- child.on("error", (err) => {
19
- console.error(" ✘ Failed to run voinznext:", err.message);
20
- process.exit(1);
21
- });
143
+ if (require.main === module) main();
package/install.js CHANGED
@@ -1,89 +1,2 @@
1
- #!/usr/bin/env node
2
- const { createWriteStream, existsSync, mkdirSync, chmodSync } = require("fs");
3
- const https = require("https");
4
- const { join } = require("path");
5
- const { platform, arch } = require("process");
6
-
7
- const REPO = "VoinzzZ/VoinzNext";
8
- const APP = "voinznext";
9
-
10
- const PLATFORM_MAP = { win32: "windows", darwin: "darwin", linux: "linux" };
11
- const ARCH_MAP = { x64: "amd64", arm64: "arm64" };
12
-
13
- function getPlatform() {
14
- const p = PLATFORM_MAP[platform];
15
- if (!p) throw new Error(`Unsupported platform: ${platform}`);
16
- return p;
17
- }
18
-
19
- function getArch() {
20
- const a = ARCH_MAP[arch];
21
- if (!a) throw new Error(`Unsupported architecture: ${arch}`);
22
- return a;
23
- }
24
-
25
- function getBinaryName() {
26
- const p = getPlatform();
27
- const a = getArch();
28
- const name = `${APP}-${p}-${a}`;
29
- return p === "windows" ? `${name}.exe` : name;
30
- }
31
-
32
- function fetchJson(url) {
33
- return new Promise((resolve, reject) => {
34
- https.get(url, { headers: { "Accept": "application/vnd.github.v3+json", "User-Agent": "voinznext-installer" } }, (res) => {
35
- let data = "";
36
- res.on("data", (chunk) => (data += chunk));
37
- res.on("end", () => {
38
- try { resolve(JSON.parse(data)); }
39
- catch { reject(new Error(`Failed to parse response: ${data}`)); }
40
- });
41
- }).on("error", reject);
42
- });
43
- }
44
-
45
- function getLatestVersion() {
46
- return fetchJson(`https://api.github.com/repos/${REPO}/releases/latest`).then((json) => json.tag_name);
47
- }
48
-
49
- function download(url, dest) {
50
- return new Promise((resolve, reject) => {
51
- https.get(url, { headers: { "User-Agent": "voinznext-installer" } }, (res) => {
52
- if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
53
- download(res.headers.location, dest).then(resolve).catch(reject);
54
- return;
55
- }
56
- if (res.statusCode !== 200) {
57
- reject(new Error(`Download failed with status ${res.statusCode}`));
58
- return;
59
- }
60
- const file = createWriteStream(dest);
61
- res.pipe(file);
62
- file.on("finish", () => file.close(resolve));
63
- file.on("error", reject);
64
- }).on("error", reject);
65
- });
66
- }
67
-
68
- async function install() {
69
- const binDir = join(__dirname, "bin");
70
- if (!existsSync(binDir)) mkdirSync(binDir, { recursive: true });
71
-
72
- const binaryName = getBinaryName();
73
- const targetPath = join(binDir, "voinznext.exe");
74
-
75
- console.log(` ● Downloading ${APP} binary for ${platform}-${arch}...`);
76
-
77
- const version = await getLatestVersion();
78
- const downloadUrl = `https://github.com/${REPO}/releases/download/${version}/${binaryName}`;
79
-
80
- await download(downloadUrl, targetPath);
81
-
82
- if (platform !== "win32") chmodSync(targetPath, 0o755);
83
- console.log(` ✔ Installed to ${targetPath}`);
84
- }
85
-
86
- install().catch((err) => {
87
- console.error(" ✘ Installation failed:", err.message);
88
- process.exit(1);
89
- });
1
+ process.env.VOINZNEXT_INSTALL = "1";
2
+ require("./cli.js").downloadLatest().catch(() => {});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "voinznext",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "description": "Interactive CLI tool for scaffolding Next.js projects with your preferred tech stack",
5
5
  "keywords": [
6
6
  "nextjs",
@@ -20,7 +20,8 @@
20
20
  "voinznext": "cli.js"
21
21
  },
22
22
  "scripts": {
23
- "postinstall": "node install.js"
23
+ "postinstall": "node install.js",
24
+ "postupdate": "node -e \"require('./cli.js').downloadLatest()\""
24
25
  },
25
26
  "files": [
26
27
  "cli.js",