endform 0.8.7 → 0.8.9

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 (2) hide show
  1. package/bin/endform +134 -31
  2. package/package.json +5 -5
package/bin/endform CHANGED
@@ -6,8 +6,10 @@
6
6
  * It supports only macOS and Linux on x86 (x64) or arm64.
7
7
  */
8
8
 
9
- const child_process = require("child_process");
10
- const path = require("path");
9
+ const child_process = require("node:child_process");
10
+ const path = require("node:path");
11
+ const fs = require("node:fs");
12
+ const https = require("node:https");
11
13
 
12
14
  // Define supported platforms and architectures.
13
15
  const supportedPlatforms = ["darwin", "linux"];
@@ -15,18 +17,18 @@ const supportedArchs = ["x64", "arm64"];
15
17
 
16
18
  const platform = process.platform;
17
19
  if (!supportedPlatforms.includes(platform)) {
18
- console.error(
19
- `Unsupported platform: ${platform}. This CLI supports only macOS and Linux.`,
20
- );
21
- process.exit(1);
20
+ console.error(
21
+ `Unsupported platform: ${platform}. This CLI supports only macOS and Linux.`,
22
+ );
23
+ process.exit(1);
22
24
  }
23
25
 
24
26
  const arch = process.arch;
25
27
  if (!supportedArchs.includes(arch)) {
26
- console.error(
27
- `Unsupported architecture: ${arch}. This CLI supports only x86 and arm64.`,
28
- );
29
- process.exit(1);
28
+ console.error(
29
+ `Unsupported architecture: ${arch}. This CLI supports only x86 and arm64.`,
30
+ );
31
+ process.exit(1);
30
32
  }
31
33
 
32
34
  // Map Node's "x64" to our package naming convention "x86"
@@ -39,25 +41,126 @@ const packageName = `endform-${platform}-${archSuffix}`;
39
41
  // The binary is expected to reside at "bin/endform" inside the package.
40
42
  const binaryRelativePath = "bin/endform";
41
43
 
42
- let binaryPath;
43
- try {
44
- // Attempt to resolve the binary from the installed dependency.
45
- binaryPath = require.resolve(`${packageName}/${binaryRelativePath}`);
46
- } catch (err) {
47
- console.error(
48
- `Error: Could not find the Endform binary for ${platform} (${arch}).`,
49
- );
50
- console.error(
51
- `Ensure that the package "${packageName}" is installed as an optional dependency.`,
52
- );
53
- process.exit(1);
54
- }
44
+ // Get version from package.json
45
+ const packageJsonPath = path.join(__dirname, "..", "package.json");
46
+ const packageJson = require(packageJsonPath);
47
+ const version = packageJson.version;
55
48
 
56
- // Execute the binary and forward any command-line arguments.
57
- try {
58
- child_process.execFileSync(binaryPath, process.argv.slice(2), {
59
- stdio: "inherit",
60
- });
61
- } catch (err) {
62
- process.exit(err.status || 1);
63
- }
49
+ console.log(version);
50
+
51
+ // Define download URLs and binary names
52
+ const MAC_ARM_URL = `https://cli.endform.dev/${version}/endform-aarch64-apple-darwin/endform`;
53
+ const MAC_X86_URL = `https://cli.endform.dev/${version}/endform-x86_64-apple-darwin/endform`;
54
+ const LINUX_ARM_URL = `https://cli.endform.dev/${version}/endform-aarch64-unknown-linux-gnu/endform`;
55
+ const LINUX_X86_URL = `https://cli.endform.dev/${version}/endform-x86_64-unknown-linux-gnu/endform`;
56
+
57
+ // Get the appropriate URL and binary name based on platform and architecture
58
+ const getDownloadInfo = () => {
59
+ if (platform === "darwin") {
60
+ return {
61
+ url: arch === "arm64" ? MAC_ARM_URL : MAC_X86_URL,
62
+ binaryName:
63
+ arch === "arm64"
64
+ ? "endform-aarch64-apple-darwin"
65
+ : "endform-x86_64-apple-darwin",
66
+ };
67
+ }
68
+ return {
69
+ url: arch === "arm64" ? LINUX_ARM_URL : LINUX_X86_URL,
70
+ binaryName:
71
+ arch === "arm64"
72
+ ? "endform-aarch64-unknown-linux-gnu"
73
+ : "endform-x86_64-unknown-linux-gnu",
74
+ };
75
+ };
76
+
77
+ // Function to download the binary
78
+ const downloadBinary = () => {
79
+ const { url, binaryName } = getDownloadInfo();
80
+ const currentDir = __dirname;
81
+ const binaryPath = path.join(currentDir, binaryName);
82
+
83
+ console.log("Endform was not installed as an optional dependency...");
84
+ console.log(`Downloading endform cli for ${platform} (${arch})...`);
85
+
86
+ return new Promise((resolve, reject) => {
87
+ const file = fs.createWriteStream(binaryPath);
88
+ https
89
+ .get(url, (response) => {
90
+ if (response.statusCode !== 200) {
91
+ file.close();
92
+ fs.unlink(binaryPath, () => {});
93
+ reject(
94
+ new Error(
95
+ `Failed to download binary: ${response.statusCode} ${response.statusMessage}`,
96
+ ),
97
+ );
98
+ return;
99
+ }
100
+ response.pipe(file);
101
+ file.on("finish", () => {
102
+ file.close();
103
+ // Make the binary executable
104
+ fs.chmodSync(binaryPath, "755");
105
+ resolve(binaryPath);
106
+ });
107
+ file.on("error", (err) => {
108
+ fs.unlink(binaryPath, () => {});
109
+ reject(err);
110
+ });
111
+ })
112
+ .on("error", (err) => {
113
+ fs.unlink(binaryPath, () => {}); // Clean up binary file on error
114
+ console.error("Error downloading binary:", err);
115
+ reject(err);
116
+ });
117
+ });
118
+ };
119
+
120
+ // Main execution function
121
+ const main = async () => {
122
+ let binaryPath;
123
+ try {
124
+ // First try to find the binary in the current directory
125
+ const { binaryName } = getDownloadInfo();
126
+ const localBinaryPath = path.join(__dirname, binaryName);
127
+ if (fs.existsSync(localBinaryPath)) {
128
+ binaryPath = localBinaryPath;
129
+ } else {
130
+ // If not found locally, try to resolve from installed optional dependency
131
+ try {
132
+ binaryPath = require.resolve(`${packageName}/${binaryRelativePath}`);
133
+ } catch (err) {
134
+ // If still not found, download the binary
135
+ try {
136
+ binaryPath = await downloadBinary();
137
+ } catch (err) {
138
+ console.error(
139
+ `Error: Could not download the Endform binary for ${platform} (${arch}).`,
140
+ );
141
+ process.exit(1);
142
+ }
143
+ }
144
+ }
145
+
146
+ // Execute the binary and forward any command-line arguments.
147
+ try {
148
+ child_process.execFileSync(binaryPath, process.argv.slice(2), {
149
+ stdio: "inherit",
150
+ });
151
+ } catch (err) {
152
+ process.exit(err.status || 1);
153
+ }
154
+ } catch (err) {
155
+ console.error(
156
+ `Error: Could not find or download the Endform binary for ${platform} (${arch}).`,
157
+ );
158
+ process.exit(1);
159
+ }
160
+ };
161
+
162
+ // Run the main function
163
+ main().catch((err) => {
164
+ console.error("Unexpected error:", err);
165
+ process.exit(1);
166
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "endform",
3
- "version": "0.8.7",
3
+ "version": "0.8.9",
4
4
  "description": "Endform CLI",
5
5
  "repository": "https://github.com/endformdev/npm",
6
6
  "license": "UNLICENSED",
@@ -12,9 +12,9 @@
12
12
  "bin"
13
13
  ],
14
14
  "optionalDependencies": {
15
- "endform-darwin-arm64": "0.8.7",
16
- "endform-darwin-x86": "0.8.7",
17
- "endform-linux-arm64": "0.8.7",
18
- "endform-linux-x86": "0.8.7"
15
+ "endform-darwin-arm64": "0.8.9",
16
+ "endform-darwin-x86": "0.8.9",
17
+ "endform-linux-arm64": "0.8.9",
18
+ "endform-linux-x86": "0.8.9"
19
19
  }
20
20
  }