snipetype 0.0.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.
- package/bin/snipetype +40 -0
- package/install.js +85 -0
- package/package.json +22 -0
package/bin/snipetype
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const { execFileSync } = require("child_process");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
|
|
5
|
+
const PLATFORMS = {
|
|
6
|
+
"darwin-arm64": "@snipetype/darwin-arm64",
|
|
7
|
+
"darwin-x64": "@snipetype/darwin-x64",
|
|
8
|
+
"linux-arm64": "@snipetype/linux-arm64",
|
|
9
|
+
"linux-x64": "@snipetype/linux-x64",
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
function getBinaryPath() {
|
|
13
|
+
const platform = `${process.platform}-${process.arch}`;
|
|
14
|
+
const pkg = PLATFORMS[platform];
|
|
15
|
+
|
|
16
|
+
if (!pkg) {
|
|
17
|
+
console.error(`snipetype: Unsupported platform: ${platform}`);
|
|
18
|
+
console.error("Please open an issue at https://github.com/snipeship/tuityper/issues");
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const bin = "snipetype";
|
|
23
|
+
|
|
24
|
+
// Try optionalDependency first
|
|
25
|
+
try {
|
|
26
|
+
return require.resolve(`${pkg}/bin/${bin}`);
|
|
27
|
+
} catch {
|
|
28
|
+
// Fallback to postinstall-downloaded binary
|
|
29
|
+
return path.join(__dirname, "..", bin);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
execFileSync(getBinaryPath(), process.argv.slice(2), { stdio: "inherit" });
|
|
35
|
+
} catch (e) {
|
|
36
|
+
if (e.status !== undefined) {
|
|
37
|
+
process.exit(e.status);
|
|
38
|
+
}
|
|
39
|
+
throw e;
|
|
40
|
+
}
|
package/install.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const zlib = require("zlib");
|
|
4
|
+
const https = require("https");
|
|
5
|
+
|
|
6
|
+
const PLATFORMS = {
|
|
7
|
+
"darwin-arm64": "@snipetype/darwin-arm64",
|
|
8
|
+
"darwin-x64": "@snipetype/darwin-x64",
|
|
9
|
+
"linux-arm64": "@snipetype/linux-arm64",
|
|
10
|
+
"linux-x64": "@snipetype/linux-x64",
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const VERSION = require("./package.json").version;
|
|
14
|
+
const platform = `${process.platform}-${process.arch}`;
|
|
15
|
+
const pkg = PLATFORMS[platform];
|
|
16
|
+
|
|
17
|
+
if (!pkg) {
|
|
18
|
+
console.warn(`snipetype: Unsupported platform ${platform}, skipping binary download`);
|
|
19
|
+
process.exit(0);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const bin = "snipetype";
|
|
23
|
+
|
|
24
|
+
// Check if already installed via optionalDependencies
|
|
25
|
+
try {
|
|
26
|
+
require.resolve(`${pkg}/bin/${bin}`);
|
|
27
|
+
process.exit(0); // Already installed
|
|
28
|
+
} catch {
|
|
29
|
+
// Not installed, need to download
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
console.log(`snipetype: Downloading binary for ${platform}...`);
|
|
33
|
+
|
|
34
|
+
function fetch(url) {
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
https
|
|
37
|
+
.get(url, (res) => {
|
|
38
|
+
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
|
39
|
+
return fetch(res.headers.location).then(resolve, reject);
|
|
40
|
+
}
|
|
41
|
+
if (res.statusCode !== 200) {
|
|
42
|
+
return reject(new Error(`HTTP ${res.statusCode}`));
|
|
43
|
+
}
|
|
44
|
+
const chunks = [];
|
|
45
|
+
res.on("data", (c) => chunks.push(c));
|
|
46
|
+
res.on("end", () => resolve(Buffer.concat(chunks)));
|
|
47
|
+
res.on("error", reject);
|
|
48
|
+
})
|
|
49
|
+
.on("error", reject);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function extractFromTarball(tarball, filepath) {
|
|
54
|
+
let offset = 0;
|
|
55
|
+
while (offset < tarball.length) {
|
|
56
|
+
const name = tarball.toString("utf8", offset, offset + 100).replace(/\0.*/, "");
|
|
57
|
+
const size = parseInt(
|
|
58
|
+
tarball.toString("utf8", offset + 124, offset + 136).replace(/\0.*/, ""),
|
|
59
|
+
8
|
|
60
|
+
);
|
|
61
|
+
offset += 512;
|
|
62
|
+
if (name === filepath) {
|
|
63
|
+
return tarball.subarray(offset, offset + size);
|
|
64
|
+
}
|
|
65
|
+
offset += Math.ceil(size / 512) * 512;
|
|
66
|
+
}
|
|
67
|
+
throw new Error(`${filepath} not found in tarball`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
(async () => {
|
|
71
|
+
const pkgName = pkg.split("/")[1]; // e.g., "darwin-arm64"
|
|
72
|
+
const tgz = await fetch(
|
|
73
|
+
`https://registry.npmjs.org/${pkg}/-/${pkgName}-${VERSION}.tgz`
|
|
74
|
+
);
|
|
75
|
+
const tar = zlib.gunzipSync(tgz);
|
|
76
|
+
const binary = extractFromTarball(tar, `package/bin/${bin}`);
|
|
77
|
+
const dest = path.join(__dirname, bin);
|
|
78
|
+
fs.writeFileSync(dest, binary, { mode: 0o755 });
|
|
79
|
+
console.log(`snipetype: Binary installed successfully`);
|
|
80
|
+
})().catch((e) => {
|
|
81
|
+
console.error(`snipetype: Failed to download binary: ${e.message}`);
|
|
82
|
+
console.error("You may need to install the platform-specific package manually:");
|
|
83
|
+
console.error(` npm install ${pkg}`);
|
|
84
|
+
process.exit(1);
|
|
85
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "snipetype",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A terminal typing test",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/snipeship/tuityper"
|
|
9
|
+
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"snipetype": "bin/snipetype"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"postinstall": "node install.js"
|
|
15
|
+
},
|
|
16
|
+
"optionalDependencies": {
|
|
17
|
+
"@snipetype/darwin-arm64": "0.0.1",
|
|
18
|
+
"@snipetype/darwin-x64": "0.0.1",
|
|
19
|
+
"@snipetype/linux-arm64": "0.0.1",
|
|
20
|
+
"@snipetype/linux-x64": "0.0.1"
|
|
21
|
+
}
|
|
22
|
+
}
|