attnmd 0.1.0

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/attn.js ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { existsSync } = require("node:fs");
4
+ const { join } = require("node:path");
5
+ const { spawnSync } = require("node:child_process");
6
+
7
+ const binaryPath = join(__dirname, "..", "bin-runtime", "attn");
8
+
9
+ if (!existsSync(binaryPath)) {
10
+ console.error("attn: runtime binary is missing.");
11
+ console.error(
12
+ "Reinstall with npm or set ATTN_SKIP_DOWNLOAD=0 so postinstall can fetch binaries."
13
+ );
14
+ process.exit(1);
15
+ }
16
+
17
+ const child = spawnSync(binaryPath, process.argv.slice(2), {
18
+ stdio: "inherit",
19
+ });
20
+
21
+ if (child.error) {
22
+ console.error(`attn: failed to launch binary: ${child.error.message}`);
23
+ process.exit(1);
24
+ }
25
+
26
+ if (typeof child.status === "number") {
27
+ process.exit(child.status);
28
+ }
29
+
30
+ process.exit(1);
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "attnmd",
3
+ "version": "0.1.0",
4
+ "description": "A beautiful markdown viewer that launches from the CLI",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/lightsofapollo/attn.git"
9
+ },
10
+ "homepage": "https://github.com/lightsofapollo/attn",
11
+ "bugs": {
12
+ "url": "https://github.com/lightsofapollo/attn/issues"
13
+ },
14
+ "bin": {
15
+ "attn": "bin/attn.js"
16
+ },
17
+ "files": [
18
+ "bin/attn.js",
19
+ "scripts/postinstall.mjs"
20
+ ],
21
+ "scripts": {
22
+ "postinstall": "node scripts/postinstall.mjs"
23
+ },
24
+ "engines": {
25
+ "node": ">=18"
26
+ },
27
+ "keywords": [
28
+ "markdown",
29
+ "viewer",
30
+ "cli",
31
+ "desktop"
32
+ ]
33
+ }
@@ -0,0 +1,95 @@
1
+ import { chmodSync, createWriteStream, existsSync, mkdirSync, readFileSync, renameSync, rmSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import https from "node:https";
5
+
6
+ const __dirname = dirname(fileURLToPath(import.meta.url));
7
+ const packageDir = join(__dirname, "..");
8
+ const runtimeDir = join(packageDir, "bin-runtime");
9
+ const runtimeBinaryPath = join(runtimeDir, "attn");
10
+ const tempPath = `${runtimeBinaryPath}.tmp`;
11
+
12
+ if (process.env.ATTN_SKIP_DOWNLOAD === "1") {
13
+ console.log("attn: skipping binary download (ATTN_SKIP_DOWNLOAD=1).");
14
+ process.exit(0);
15
+ }
16
+
17
+ const { version } = JSON.parse(readFileSync(join(packageDir, "package.json"), "utf8"));
18
+ const assetSuffix = resolveAssetSuffix(process.platform, process.arch);
19
+
20
+ if (!assetSuffix) {
21
+ console.warn(
22
+ `attn: unsupported platform ${process.platform}/${process.arch}. ` +
23
+ "Currently supported: darwin-arm64, darwin-x64."
24
+ );
25
+ process.exit(0);
26
+ }
27
+
28
+ const url = `https://github.com/lightsofapollo/attn/releases/download/v${version}/attn-v${version}-${assetSuffix}`;
29
+ console.log(`attn: downloading ${url}`);
30
+
31
+ mkdirSync(runtimeDir, { recursive: true });
32
+ safeRemove(tempPath);
33
+
34
+ download(url, tempPath)
35
+ .then(() => {
36
+ chmodSync(tempPath, 0o755);
37
+ renameSync(tempPath, runtimeBinaryPath);
38
+ console.log(`attn: installed ${runtimeBinaryPath}`);
39
+ })
40
+ .catch((error) => {
41
+ safeRemove(tempPath);
42
+ console.error(`attn: failed to download release binary: ${error.message}`);
43
+ process.exit(1);
44
+ });
45
+
46
+ function resolveAssetSuffix(platform, arch) {
47
+ if (platform === "darwin" && arch === "arm64") {
48
+ return "darwin-arm64";
49
+ }
50
+ if (platform === "darwin" && arch === "x64") {
51
+ return "darwin-x64";
52
+ }
53
+ return null;
54
+ }
55
+
56
+ function safeRemove(path) {
57
+ if (existsSync(path)) {
58
+ rmSync(path);
59
+ }
60
+ }
61
+
62
+ function download(url, destination) {
63
+ return new Promise((resolve, reject) => {
64
+ const request = https.get(url, (response) => {
65
+ if (
66
+ response.statusCode &&
67
+ response.statusCode >= 300 &&
68
+ response.statusCode < 400 &&
69
+ response.headers.location
70
+ ) {
71
+ download(response.headers.location, destination).then(resolve).catch(reject);
72
+ return;
73
+ }
74
+
75
+ if (response.statusCode !== 200) {
76
+ reject(
77
+ new Error(
78
+ `HTTP ${response.statusCode ?? "unknown"} while downloading ${url}`
79
+ )
80
+ );
81
+ return;
82
+ }
83
+
84
+ const out = createWriteStream(destination, { mode: 0o755 });
85
+ response.pipe(out);
86
+ out.on("finish", () => {
87
+ out.close();
88
+ resolve();
89
+ });
90
+ out.on("error", reject);
91
+ });
92
+
93
+ request.on("error", reject);
94
+ });
95
+ }