tinyship 0.1.0 → 0.1.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.
package/README.md CHANGED
@@ -5,7 +5,9 @@ Deploy static frontend projects to Tinyship.
5
5
  ```bash
6
6
  npx tinyship login
7
7
  npx tinyship deploy
8
+ npx tinyship change
9
+ npx tinyship help
8
10
  ```
9
11
 
10
12
  This package is a small launcher. The matching native binary is installed through platform-specific optional dependencies.
11
-
13
+ It checks npm for newer Tinyship releases at most once per day and prints the update command when one exists.
package/bin/tinyship.js CHANGED
@@ -3,25 +3,23 @@
3
3
 
4
4
  const { spawnSync } = require("node:child_process");
5
5
  const { existsSync } = require("node:fs");
6
+ const { mkdir, readFile, writeFile } = require("node:fs/promises");
7
+ const https = require("node:https");
8
+ const os = require("node:os");
6
9
  const { dirname, join } = require("node:path");
7
10
 
8
11
  const packageByPlatform = {
9
- "linux-x64": "tinyship-cli-linux-x64",
10
- "linux-arm64": "tinyship-cli-linux-arm64",
11
- "darwin-x64": "tinyship-cli-darwin-x64",
12
- "darwin-arm64": "tinyship-cli-darwin-arm64",
13
- "win32-x64": "tinyship-cli-win32-x64",
14
- "win32-arm64": "tinyship-cli-win32-arm64"
12
+ "linux-x64": "tinyship-native-linux-x64",
13
+ "linux-arm64": "tinyship-native-linux-arm64",
14
+ "darwin-x64": "tinyship-native-darwin-x64",
15
+ "darwin-arm64": "tinyship-native-darwin-arm64",
16
+ "win32-x64": "tinyship-native-win32-x64",
17
+ "win32-arm64": "tinyship-native-win32-arm64"
15
18
  };
16
19
 
17
- if (process.argv.includes("--version") || process.argv.includes("-v")) {
18
- const pkg = require("../package.json");
19
- console.log(pkg.version);
20
- process.exit(0);
21
- }
22
-
23
20
  const platformKey = `${process.platform}-${process.arch}`;
24
21
  const packageName = packageByPlatform[platformKey];
22
+ const rootPackage = require("../package.json");
25
23
 
26
24
  const fail = (message) => {
27
25
  console.error(`tinyship: ${message}`);
@@ -54,13 +52,120 @@ const resolveBinary = () => {
54
52
  return binaryPath;
55
53
  };
56
54
 
57
- const result = spawnSync(resolveBinary(), process.argv.slice(2), {
58
- stdio: "inherit",
59
- windowsHide: false
60
- });
55
+ const cachePath = join(os.homedir(), ".tinyship", "npm-update-check.json");
56
+ const oneDayMs = 24 * 60 * 60 * 1000;
57
+
58
+ const shouldCheckForUpdate = async () => {
59
+ if (process.env.TINYSHIP_NO_UPDATE_CHECK) {
60
+ return false;
61
+ }
62
+ try {
63
+ const raw = await readFile(cachePath, "utf8");
64
+ const cached = JSON.parse(raw);
65
+ return Date.now() - Number(cached.checkedAt ?? 0) > oneDayMs;
66
+ } catch {
67
+ return true;
68
+ }
69
+ };
61
70
 
62
- if (result.error) {
63
- fail(result.error.message);
64
- }
71
+ const markUpdateChecked = async () => {
72
+ try {
73
+ await mkdir(dirname(cachePath), { recursive: true });
74
+ await writeFile(cachePath, JSON.stringify({ checkedAt: Date.now() }), "utf8");
75
+ } catch {
76
+ // Update check must never block CLI use.
77
+ }
78
+ };
79
+
80
+ const fetchLatestVersion = () =>
81
+ new Promise((resolve) => {
82
+ const request = https.get(
83
+ "https://registry.npmjs.org/tinyship/latest",
84
+ { timeout: 1200 },
85
+ (response) => {
86
+ if (response.statusCode !== 200) {
87
+ response.resume();
88
+ resolve("");
89
+ return;
90
+ }
91
+ let body = "";
92
+ response.setEncoding("utf8");
93
+ response.on("data", (chunk) => {
94
+ body += chunk;
95
+ });
96
+ response.on("end", () => {
97
+ try {
98
+ resolve(JSON.parse(body).version || "");
99
+ } catch {
100
+ resolve("");
101
+ }
102
+ });
103
+ }
104
+ );
105
+ request.on("timeout", () => {
106
+ request.destroy();
107
+ resolve("");
108
+ });
109
+ request.on("error", () => resolve(""));
110
+ });
111
+
112
+ const compareVersion = (left, right) => {
113
+ const parse = (version) =>
114
+ String(version)
115
+ .split("-")[0]
116
+ .split(".")
117
+ .map((part) => Number.parseInt(part, 10) || 0);
118
+ const a = parse(left);
119
+ const b = parse(right);
120
+ for (let index = 0; index < Math.max(a.length, b.length); index++) {
121
+ const diff = (a[index] || 0) - (b[index] || 0);
122
+ if (diff !== 0) {
123
+ return diff;
124
+ }
125
+ }
126
+ return 0;
127
+ };
128
+
129
+ const maybePromptForUpdate = async () => {
130
+ if (!(await shouldCheckForUpdate())) {
131
+ return;
132
+ }
133
+ const latest = await fetchLatestVersion();
134
+ await markUpdateChecked();
135
+ if (!latest || compareVersion(latest, rootPackage.version) <= 0) {
136
+ return;
137
+ }
138
+
139
+ if (process.env.TINYSHIP_AUTO_UPDATE === "1") {
140
+ console.error(`tinyship: updating ${rootPackage.version} -> ${latest}`);
141
+ const npmCommand = process.platform === "win32" ? "npm.cmd" : "npm";
142
+ const update = spawnSync(npmCommand, ["install", "-g", `tinyship@${latest}`], {
143
+ stdio: "inherit",
144
+ windowsHide: true
145
+ });
146
+ if (update.status === 0) {
147
+ console.error("tinyship: updated. Re-run command to use new CLI.");
148
+ process.exit(0);
149
+ }
150
+ }
151
+
152
+ console.error(`tinyship: update available ${rootPackage.version} -> ${latest}`);
153
+ console.error(`tinyship: run npm install -g tinyship@${latest}`);
154
+ };
155
+
156
+ const main = async () => {
157
+ await maybePromptForUpdate();
158
+
159
+ const result = spawnSync(resolveBinary(), process.argv.slice(2), {
160
+ stdio: "inherit",
161
+ windowsHide: false
162
+ });
163
+
164
+ if (result.error) {
165
+ fail(result.error.message);
166
+ }
167
+
168
+ process.exit(result.status ?? 1);
169
+ };
65
170
 
66
- process.exit(result.status ?? 1);
171
+ main().catch((error) => fail(error.message));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tinyship",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Tinyship CLI for deploying static frontends.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://tinyship.run",
@@ -18,5 +18,13 @@
18
18
  ],
19
19
  "engines": {
20
20
  "node": ">=18"
21
+ },
22
+ "optionalDependencies": {
23
+ "tinyship-native-darwin-arm64": "0.1.1",
24
+ "tinyship-native-darwin-x64": "0.1.1",
25
+ "tinyship-native-linux-arm64": "0.1.1",
26
+ "tinyship-native-linux-x64": "0.1.1",
27
+ "tinyship-native-win32-arm64": "0.1.1",
28
+ "tinyship-native-win32-x64": "0.1.1"
21
29
  }
22
30
  }