swelog-cli 0.2.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/swelog.js ADDED
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { spawnSync } from "node:child_process";
4
+ import { existsSync } from "node:fs";
5
+ import { dirname, join } from "node:path";
6
+ import { fileURLToPath } from "node:url";
7
+
8
+ try {
9
+ main();
10
+ } catch (error) {
11
+ console.error("An unknown error occurred while running swelog");
12
+ console.error(error);
13
+
14
+ process.exit(1);
15
+ }
16
+
17
+ function main() {
18
+ const executablePath = getExecutablePath();
19
+
20
+ ensureExecutableExists(executablePath);
21
+
22
+ const result = spawnSync(executablePath, process.argv.slice(2), {
23
+ stdio: "inherit",
24
+ });
25
+
26
+ if (result.error) {
27
+ fail(result.error.message);
28
+ }
29
+
30
+ if (result.signal) {
31
+ process.kill(process.pid, result.signal);
32
+ }
33
+
34
+ process.exit(result.status ?? 1);
35
+ }
36
+
37
+ function getExecutablePath() {
38
+ const executableName = process.platform === "win32" ? "swelog.exe" : "swelog";
39
+
40
+ return join(getCurrentDirectory(), "..", "vendor", executableName);
41
+ }
42
+
43
+ function getCurrentDirectory() {
44
+ return dirname(fileURLToPath(import.meta.url));
45
+ }
46
+
47
+ function ensureExecutableExists(executablePath) {
48
+ if (!existsSync(executablePath)) {
49
+ fail(
50
+ "swelog binary is missing. Reinstall the package without --ignore-scripts.",
51
+ );
52
+ }
53
+ }
54
+
55
+ function fail(message) {
56
+ console.error(`Error: ${message}`);
57
+
58
+ process.exit(1);
59
+ }
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "swelog-cli",
3
+ "version": "0.2.0",
4
+ "description": "A Rust CLI to track your daily accomplishments in Obsidian",
5
+ "license": "MIT",
6
+ "homepage": "https://github.com/RudraPatel2003/swelog-cli",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/RudraPatel2003/swelog-cli.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/RudraPatel2003/swelog-cli/issues"
13
+ },
14
+ "bin": {
15
+ "swelog": "bin/swelog.js"
16
+ },
17
+ "scripts": {
18
+ "postinstall": "node scripts/install.js"
19
+ },
20
+ "files": [
21
+ "bin/",
22
+ "scripts/"
23
+ ],
24
+ "engines": {
25
+ "node": ">=22"
26
+ }
27
+ }
@@ -0,0 +1,122 @@
1
+ #!/usr/bin/env node
2
+
3
+ import {
4
+ chmodSync,
5
+ existsSync,
6
+ mkdirSync,
7
+ readFileSync,
8
+ renameSync,
9
+ rmSync,
10
+ writeFileSync,
11
+ } from "node:fs";
12
+ import { dirname, join } from "node:path";
13
+ import { fileURLToPath } from "node:url";
14
+
15
+ try {
16
+ await main();
17
+ } catch (error) {
18
+ console.error("An unknown error occurred while installing swelog");
19
+ console.error(error);
20
+
21
+ process.exit(1);
22
+ }
23
+
24
+ async function main() {
25
+ const packageJson = getPackageJson();
26
+
27
+ const vendorDirectory = getVendorDirectory();
28
+
29
+ const destination = getDestination(vendorDirectory);
30
+
31
+ const assetName = getAssetName();
32
+
33
+ const downloadUrl = getDownloadUrl(packageJson.version, assetName);
34
+
35
+ const temporaryDestination = `${destination}.download`;
36
+
37
+ mkdirSync(vendorDirectory, { recursive: true });
38
+
39
+ if (existsSync(temporaryDestination)) {
40
+ rmSync(temporaryDestination, { force: true });
41
+ }
42
+
43
+ await download(downloadUrl, temporaryDestination, packageJson);
44
+
45
+ renameSync(temporaryDestination, destination);
46
+
47
+ chmodSync(destination, 0o755);
48
+
49
+ console.log(
50
+ `Installed swelog ${packageJson.version} for ${process.platform} ${process.arch}`,
51
+ );
52
+ }
53
+
54
+ function getPackageJson() {
55
+ const packageJson = JSON.parse(
56
+ readFileSync(join(getPackageDirectory(), "package.json"), "utf8"),
57
+ );
58
+
59
+ if (!packageJson.name || !packageJson.version) {
60
+ throw new Error("npm/package.json is missing a name or version");
61
+ }
62
+
63
+ return packageJson;
64
+ }
65
+
66
+ function getPackageDirectory() {
67
+ return join(getCurrentDirectory(), "..");
68
+ }
69
+
70
+ function getCurrentDirectory() {
71
+ return dirname(fileURLToPath(import.meta.url));
72
+ }
73
+
74
+ function getVendorDirectory() {
75
+ return join(getPackageDirectory(), "vendor");
76
+ }
77
+
78
+ function getDestination(vendorDirectory) {
79
+ const executableName = process.platform === "win32" ? "swelog.exe" : "swelog";
80
+
81
+ return join(vendorDirectory, executableName);
82
+ }
83
+
84
+ function getAssetName() {
85
+ if (process.platform === "linux" && process.arch === "x64") {
86
+ return "swelog-x86_64-unknown-linux-gnu";
87
+ }
88
+
89
+ if (process.platform === "darwin" && process.arch === "x64") {
90
+ return "swelog-x86_64-apple-darwin";
91
+ }
92
+
93
+ if (process.platform === "darwin" && process.arch === "arm64") {
94
+ return "swelog-aarch64-apple-darwin";
95
+ }
96
+
97
+ if (process.platform === "win32" && process.arch === "x64") {
98
+ return "swelog-x86_64-pc-windows-msvc.exe";
99
+ }
100
+
101
+ throw new Error(`Unsupported platform: ${process.platform} ${process.arch}`);
102
+ }
103
+
104
+ function getDownloadUrl(version, assetName) {
105
+ return `https://github.com/RudraPatel2003/swelog-cli/releases/download/v${version}/${assetName}`;
106
+ }
107
+
108
+ async function download(url, filePath, packageJson) {
109
+ const response = await fetch(url, {
110
+ headers: {
111
+ "User-Agent": `${packageJson.name}/${packageJson.version}`,
112
+ },
113
+ });
114
+
115
+ if (!response.ok) {
116
+ throw new Error(`Download failed with HTTP ${response.status}: ${url}`);
117
+ }
118
+
119
+ const binary = Buffer.from(await response.arrayBuffer());
120
+
121
+ writeFileSync(filePath, binary, { mode: 0o755 });
122
+ }