dprint 0.32.0 → 0.32.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/bin.js CHANGED
@@ -8,25 +8,38 @@ const fs = require("fs");
8
8
 
9
9
  const exePath = path.join(__dirname, os.platform() === "win32" ? "dprint.exe" : "dprint");
10
10
 
11
- try {
12
- const result = child_process.spawnSync(
13
- exePath,
14
- process.argv.slice(2),
15
- { stdio: "inherit" },
16
- );
11
+ if (!fs.existsSync(exePath)) {
12
+ require("./install_api").runInstall().then(() => {
13
+ runDprintExe();
14
+ }).catch(err => {
15
+ console.error(err);
16
+ process.exit(child_process.exitCode || 1);
17
+ });
18
+ } else {
19
+ runDprintExe();
20
+ }
21
+
22
+ function runDprintExe() {
23
+ try {
24
+ const result = child_process.spawnSync(
25
+ exePath,
26
+ process.argv.slice(2),
27
+ { stdio: "inherit" },
28
+ );
29
+ if (result.error) {
30
+ throw result.error;
31
+ }
17
32
 
18
- if (result.status !== 0) {
19
33
  throwIfNoExePath();
20
- }
21
34
 
22
- process.exitCode = result.status;
23
- } catch (err) {
24
- throwIfNoExePath();
25
- throw err;
35
+ process.exitCode = result.status;
36
+ } catch (err) {
37
+ throw err;
38
+ }
26
39
  }
27
40
 
28
41
  function throwIfNoExePath() {
29
42
  if (!fs.existsSync(exePath)) {
30
- throw new Error("Could not find exe at path '" + exePath + "'. Please ensure the dprint 'postinstall' script runs on install.");
43
+ throw new Error("Could not find exe at path '" + exePath + "'. Maybe try running dprint again.");
31
44
  }
32
45
  }
package/info.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
- "version": "0.32.0",
2
+ "version": "0.32.2",
3
3
  "checksums": {
4
- "windows-x86_64": "910c047191d1d2ff6177713dd3b199b02c76ac5e9f202128b52f2a015057488e",
5
- "darwin-x86_64": "6447c2bbcb61aca4d32bba3b677b5a741d7d0b36f001ec0d12c71982ff5c51ad",
6
- "darwin-aarch64": "11596c9edb64ecec199268477c6938960ae2c7e5b0a6776d4c97bd2f453fd479",
7
- "linux-x86_64": "c339d67de7b2b119590629440b9809deb4f220b38c2f9bfd4c41af330731b901",
8
- "linux-aarch64": "2e164b3d1481df5b87bfab3c3dc91cbc34127fade224ba3b90e747090a81b2fc"
4
+ "windows-x86_64": "e4c0916438a11b18b1d6a3ce7c4e23f18998648d39247fa0b617c9863dea6d11",
5
+ "darwin-x86_64": "5abcc7e2f99b98a629b73ded47ca84bc851b4f6ec35ff6df496c2eefd569c55c",
6
+ "darwin-aarch64": "ec909acfc179b064e7d19e130c1aa1bdf0350d929b9a04dc6f6b5ce84432eb69",
7
+ "linux-x86_64": "8e8d0f32e5522a61f6251fb3b9c6be106584ccdfdb13008b276c1ee8bfdc6ffa",
8
+ "linux-aarch64": "ddff520568958b6dfeac83105a913db7247188035a0e1af12d4100d0c0d631e4"
9
9
  }
10
10
  }
package/install.js CHANGED
@@ -1,180 +1,4 @@
1
1
  // @ts-check
2
2
  "use strict";
3
3
 
4
- const https = require("https");
5
- const fs = require("fs");
6
- const crypto = require("crypto");
7
- const os = require("os");
8
- const path = require("path");
9
- const yauzl = require("yauzl");
10
-
11
- const info = JSON.parse(fs.readFileSync(path.join(__dirname, "info.json"), "utf8"));
12
- const executableFilePath = path.join(
13
- __dirname,
14
- os.platform() === "win32" ? "dprint.exe" : "dprint",
15
- );
16
- const zipFilePath = path.join(__dirname, "dprint.zip");
17
-
18
- if (fs.existsSync(executableFilePath)) {
19
- process.exit(0);
20
- }
21
-
22
- const target = getTarget();
23
- const url = "https://github.com/dprint/dprint/releases/download/"
24
- + info.version
25
- + "/dprint-" + target + ".zip";
26
-
27
- // remove the old zip file if it exists
28
- try {
29
- fs.unlinkSync(zipFilePath);
30
- } catch (err) {
31
- // ignore
32
- }
33
-
34
- // now try to download it
35
- downloadZipFile(url).then(() => {
36
- verifyZipChecksum();
37
- extractZipFile().then(() => {
38
- // todo: how to just +x? does it matter?
39
- fs.chmodSync(executableFilePath, 0o755);
40
-
41
- // delete the zip file
42
- try {
43
- fs.unlinkSync(zipFilePath);
44
- } catch (err) {
45
- // ignore
46
- }
47
- }).catch(err => {
48
- console.error("Error extracting dprint zip file.", err);
49
- process.exit(1);
50
- });
51
- }).catch(err => {
52
- console.error("Error downloading dprint zip file.", err);
53
- process.exit(1);
54
- });
55
-
56
- function getTarget() {
57
- if (os.platform() === "win32") {
58
- return "x86_64-pc-windows-msvc";
59
- } else if (os.platform() === "darwin") {
60
- if (os.arch() === "arm64") {
61
- return "aarch64-apple-darwin";
62
- } else if (os.arch() === "x64") {
63
- return "x86_64-apple-darwin";
64
- } else {
65
- throw new Error("Unsupported architecture " + os.arch() + ". Only x64 and M1 binaries are available.");
66
- }
67
- } else {
68
- if (os.arch() === "arm64") {
69
- return "aarch64-unknown-linux-gnu";
70
- } else if (os.arch() === "x64") {
71
- return "x86_64-unknown-linux-gnu";
72
- } else {
73
- throw new Error("Unsupported architecture " + os.arch() + ". Only x64 and aarch64 binaries are available.");
74
- }
75
- }
76
- }
77
-
78
- function downloadZipFile(url) {
79
- return new Promise((resolve, reject) => {
80
- https.get(url, function(response) {
81
- if (response.statusCode >= 200 && response.statusCode <= 299) {
82
- downloadResponse(response).then(resolve).catch(reject);
83
- } else if (response.headers.location) {
84
- downloadZipFile(response.headers.location).then(resolve).catch(reject);
85
- } else {
86
- reject(new Error("Unknown status code " + response.statusCode + " : " + response.statusMessage));
87
- }
88
- }).on("error", function(err) {
89
- try {
90
- fs.unlinkSync(zipFilePath);
91
- } catch (err) {
92
- // ignore
93
- }
94
- reject(err);
95
- });
96
- });
97
-
98
- /** @param response {import("http").IncomingMessage} */
99
- function downloadResponse(response) {
100
- return new Promise((resolve, reject) => {
101
- const file = fs.createWriteStream(zipFilePath);
102
- response.pipe(file);
103
- file.on("finish", function() {
104
- file.close((err) => {
105
- if (err) {
106
- reject(err);
107
- } else {
108
- resolve();
109
- }
110
- });
111
- });
112
- });
113
- }
114
- }
115
-
116
- function verifyZipChecksum() {
117
- const fileData = fs.readFileSync(zipFilePath);
118
- const actualZipChecksum = crypto.createHash("sha256").update(fileData).digest("hex").toLowerCase();
119
- const expectedZipChecksum = getExpectedZipChecksum().toLowerCase();
120
-
121
- if (actualZipChecksum !== expectedZipChecksum) {
122
- console.error(
123
- "Downloaded dprint zip checksum did not match the expected checksum (Actual: "
124
- + actualZipChecksum
125
- + ", Expected: "
126
- + expectedZipChecksum
127
- + ").",
128
- );
129
- process.exit(1);
130
- }
131
-
132
- function getExpectedZipChecksum() {
133
- switch (os.platform()) {
134
- case "win32":
135
- return info.checksums["windows-x86_64"];
136
- case "darwin":
137
- if (os.arch() === "arm64") {
138
- return info.checksums["darwin-aarch64"];
139
- } else {
140
- return info.checksums["darwin-x86_64"];
141
- }
142
- default:
143
- if (os.arch() === "arm64") {
144
- return info.checksums["linux-aarch64"];
145
- } else {
146
- return info.checksums["linux-x86_64"];
147
- }
148
- }
149
- }
150
- }
151
-
152
- function extractZipFile() {
153
- return new Promise((resolve, reject) => {
154
- // code adapted from: https://github.com/thejoshwolfe/yauzl#usage
155
- yauzl.open(zipFilePath, { autoClose: true }, (err, zipFile) => {
156
- if (err) {
157
- reject(err);
158
- return;
159
- }
160
-
161
- zipFile.on("entry", (entry) => {
162
- if (!/\/$/.test(entry.fileName)) {
163
- // file entry
164
- zipFile.openReadStream(entry, (err, readStream) => {
165
- if (err) {
166
- reject(err);
167
- return;
168
- }
169
- const destination = path.join(__dirname, entry.fileName);
170
- readStream.pipe(fs.createWriteStream(destination));
171
- });
172
- }
173
- });
174
-
175
- zipFile.once("close", function() {
176
- resolve();
177
- });
178
- });
179
- });
180
- }
4
+ require("./install_api").runInstall();
package/install_api.js ADDED
@@ -0,0 +1,209 @@
1
+ // @ts-check
2
+ "use strict";
3
+
4
+ const https = require("https");
5
+ const fs = require("fs");
6
+ const crypto = require("crypto");
7
+ const os = require("os");
8
+ const path = require("path");
9
+ const yauzl = require("yauzl");
10
+
11
+ function install() {
12
+ const executableFilePath = path.join(
13
+ __dirname,
14
+ os.platform() === "win32" ? "dprint.exe" : "dprint",
15
+ );
16
+
17
+ if (fs.existsSync(executableFilePath)) {
18
+ return Promise.resolve();
19
+ }
20
+
21
+ const info = JSON.parse(fs.readFileSync(path.join(__dirname, "info.json"), "utf8"));
22
+ const zipFilePath = path.join(__dirname, "dprint.zip");
23
+
24
+ const target = getTarget();
25
+ const url = "https://github.com/dprint/dprint/releases/download/"
26
+ + info.version
27
+ + "/dprint-" + target + ".zip";
28
+
29
+ // remove the old zip file if it exists
30
+ try {
31
+ fs.unlinkSync(zipFilePath);
32
+ } catch (err) {
33
+ // ignore
34
+ }
35
+
36
+ // now try to download it
37
+ return downloadZipFile(url).then(() => {
38
+ verifyZipChecksum();
39
+ return extractZipFile().then(() => {
40
+ // todo: how to just +x? does it matter?
41
+ fs.chmodSync(executableFilePath, 0o755);
42
+
43
+ // delete the zip file
44
+ try {
45
+ fs.unlinkSync(zipFilePath);
46
+ } catch (err) {
47
+ // ignore
48
+ }
49
+ }).catch(err => {
50
+ throw new Error("Error extracting dprint zip file.\n\n" + err);
51
+ });
52
+ }).catch(err => {
53
+ throw new Error("Error downloading dprint zip file.\n\n" + err);
54
+ });
55
+
56
+ function getTarget() {
57
+ if (os.platform() === "win32") {
58
+ return "x86_64-pc-windows-msvc";
59
+ } else if (os.platform() === "darwin") {
60
+ if (os.arch() === "arm64") {
61
+ return "aarch64-apple-darwin";
62
+ } else if (os.arch() === "x64") {
63
+ return "x86_64-apple-darwin";
64
+ } else {
65
+ throw new Error("Unsupported architecture " + os.arch() + ". Only x64 and M1 binaries are available.");
66
+ }
67
+ } else {
68
+ if (os.arch() === "arm64") {
69
+ return "aarch64-unknown-linux-gnu";
70
+ } else if (os.arch() === "x64") {
71
+ return "x86_64-unknown-linux-gnu";
72
+ } else {
73
+ throw new Error("Unsupported architecture " + os.arch() + ". Only x64 and aarch64 binaries are available.");
74
+ }
75
+ }
76
+ }
77
+
78
+ function downloadZipFile(url) {
79
+ return new Promise((resolve, reject) => {
80
+ https.get(url, function(response) {
81
+ if (response.statusCode >= 200 && response.statusCode <= 299) {
82
+ downloadResponse(response).then(resolve).catch(reject);
83
+ } else if (response.headers.location) {
84
+ downloadZipFile(response.headers.location).then(resolve).catch(reject);
85
+ } else {
86
+ reject(new Error("Unknown status code " + response.statusCode + " : " + response.statusMessage));
87
+ }
88
+ }).on("error", function(err) {
89
+ try {
90
+ fs.unlinkSync(zipFilePath);
91
+ } catch (err) {
92
+ // ignore
93
+ }
94
+ reject(err);
95
+ });
96
+ });
97
+
98
+ /** @param response {import("http").IncomingMessage} */
99
+ function downloadResponse(response) {
100
+ return new Promise((resolve, reject) => {
101
+ const file = fs.createWriteStream(zipFilePath);
102
+ response.pipe(file);
103
+ file.on("finish", function() {
104
+ file.close((err) => {
105
+ if (err) {
106
+ reject(err);
107
+ } else {
108
+ resolve();
109
+ }
110
+ });
111
+ });
112
+ });
113
+ }
114
+ }
115
+
116
+ function verifyZipChecksum() {
117
+ const fileData = fs.readFileSync(zipFilePath);
118
+ const actualZipChecksum = crypto.createHash("sha256").update(fileData).digest("hex").toLowerCase();
119
+ const expectedZipChecksum = getExpectedZipChecksum().toLowerCase();
120
+
121
+ if (actualZipChecksum !== expectedZipChecksum) {
122
+ throw new Error(
123
+ "Downloaded dprint zip checksum did not match the expected checksum (Actual: "
124
+ + actualZipChecksum
125
+ + ", Expected: "
126
+ + expectedZipChecksum
127
+ + ").",
128
+ );
129
+ }
130
+
131
+ function getExpectedZipChecksum() {
132
+ switch (os.platform()) {
133
+ case "win32":
134
+ return info.checksums["windows-x86_64"];
135
+ case "darwin":
136
+ if (os.arch() === "arm64") {
137
+ return info.checksums["darwin-aarch64"];
138
+ } else {
139
+ return info.checksums["darwin-x86_64"];
140
+ }
141
+ default:
142
+ if (os.arch() === "arm64") {
143
+ return info.checksums["linux-aarch64"];
144
+ } else {
145
+ return info.checksums["linux-x86_64"];
146
+ }
147
+ }
148
+ }
149
+ }
150
+
151
+ function extractZipFile() {
152
+ return new Promise((resolve, reject) => {
153
+ // code adapted from: https://github.com/thejoshwolfe/yauzl#usage
154
+ yauzl.open(zipFilePath, { autoClose: true }, (err, zipFile) => {
155
+ if (err) {
156
+ reject(err);
157
+ return;
158
+ }
159
+
160
+ const pendingWrites = [];
161
+
162
+ zipFile.on("entry", (entry) => {
163
+ if (!/\/$/.test(entry.fileName)) {
164
+ // file entry
165
+
166
+ // note: reject at the top level, but resolve this promise when finished
167
+ pendingWrites.push(
168
+ new Promise((resolve) => {
169
+ zipFile.openReadStream(entry, (err, readStream) => {
170
+ if (err) {
171
+ reject(err);
172
+ return;
173
+ }
174
+ const destination = path.join(__dirname, entry.fileName);
175
+ const writeStream = fs.createWriteStream(destination);
176
+ readStream.pipe(writeStream);
177
+
178
+ writeStream.on("error", (err) => {
179
+ reject(err);
180
+ });
181
+ writeStream.on("finish", () => {
182
+ resolve();
183
+ });
184
+ });
185
+ }),
186
+ );
187
+ }
188
+ });
189
+
190
+ zipFile.once("close", function() {
191
+ Promise.all(pendingWrites).then(resolve).catch(reject);
192
+ });
193
+ });
194
+ });
195
+ }
196
+ }
197
+
198
+ module.exports = {
199
+ runInstall() {
200
+ return install().catch(err => {
201
+ if (err !== undefined && typeof err.message === "string") {
202
+ console.error(err.message);
203
+ } else {
204
+ console.error(err);
205
+ }
206
+ process.exit(1);
207
+ });
208
+ },
209
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dprint",
3
- "version": "0.32.0",
3
+ "version": "0.32.2",
4
4
  "description": "Pluggable and configurable code formatting platform written in Rust.",
5
5
  "bin": "bin.js",
6
6
  "scripts": {