paragma 0.2.6 → 0.2.7
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/SHA256SUMS +6 -6
- package/bin/pg +8 -1
- package/install.js +116 -10
- package/package.json +1 -1
package/SHA256SUMS
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
3925ee7b9390989cb702528ca99a89f3dbb0ba74f9b203de885c6e03d81fefd9 pg-darwin-arm64
|
|
2
|
+
99836619bfbaa7314f7b4d7b13f4756c5e2456c007d7cfb022d96efc5340098f pg-darwin-x64
|
|
3
|
+
abce83da17188c8789aebefda7e4f32de5940967d6399bb19eae026b5cd18b3f pg-linux-arm64
|
|
4
|
+
968d0da083075cf223f0fbdb04e5134d680af99c3724bc4e6aad13e6c6eff24e pg-linux-x64
|
|
5
|
+
5c5a13308acdddafaaeafd4ea1d3009f51b4478af6381b79883177e81edd0b33 pg-windows-arm64.exe
|
|
6
|
+
32f85609f703f83a3ab6aa0686e73fc2339d6204d7cf89907c2302730fb0bb30 pg-windows-x64.exe
|
package/bin/pg
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// Runs the native pg binary for this computer, downloading it first when
|
|
3
3
|
// the install did not (install scripts turned off, or no network then).
|
|
4
|
+
// On Windows the first call also points npm's shims at the binary, so the
|
|
5
|
+
// calls after it do not go through Node (see install.js, direct).
|
|
4
6
|
"use strict";
|
|
5
7
|
const { spawnSync } = require("child_process");
|
|
6
8
|
const fs = require("fs");
|
|
7
|
-
const { install, binaryPath } = require("../install.js");
|
|
9
|
+
const { install, binaryPath, direct } = require("../install.js");
|
|
8
10
|
|
|
9
11
|
async function main() {
|
|
10
12
|
let exe = binaryPath();
|
|
@@ -22,6 +24,11 @@ async function main() {
|
|
|
22
24
|
process.exit(7);
|
|
23
25
|
}
|
|
24
26
|
}
|
|
27
|
+
try {
|
|
28
|
+
direct(exe);
|
|
29
|
+
} catch {
|
|
30
|
+
// the shims stay as npm wrote them: pg still works, through Node
|
|
31
|
+
}
|
|
25
32
|
const r = spawnSync(exe, process.argv.slice(2), { stdio: "inherit" });
|
|
26
33
|
if (r.error) {
|
|
27
34
|
console.error("paragma: " + r.error.message);
|
package/install.js
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
// package's version and checks it against SHA256SUMS (packed into the
|
|
4
4
|
// package when it was published). Runs after `npm install`; when install
|
|
5
5
|
// scripts are turned off, bin/pg runs it on the first call instead.
|
|
6
|
+
//
|
|
7
|
+
// On Windows it then points npm's pg, pg.cmd and pg.ps1 straight at the
|
|
8
|
+
// binary: through Node every call took about half a second more.
|
|
6
9
|
"use strict";
|
|
7
|
-
const crypto = require("crypto");
|
|
8
10
|
const fs = require("fs");
|
|
9
|
-
const https = require("https");
|
|
10
11
|
const path = require("path");
|
|
11
|
-
const zlib = require("zlib");
|
|
12
12
|
|
|
13
13
|
const REPO = "Utku4836/Paragma";
|
|
14
14
|
const OS = { win32: "windows", linux: "linux", darwin: "darwin" };
|
|
@@ -39,6 +39,7 @@ function expectedSum(name) {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
function get(url, redirects = 5) {
|
|
42
|
+
const https = require("https");
|
|
42
43
|
return new Promise((resolve, reject) => {
|
|
43
44
|
https
|
|
44
45
|
.get(url, { headers: { "User-Agent": "paragma-installer" } }, (res) => {
|
|
@@ -67,9 +68,9 @@ async function install({ quiet = false } = {}) {
|
|
|
67
68
|
if (!quiet) process.stderr.write(`paragma: downloading pg ${version} for ${process.platform}/${process.arch}...\n`);
|
|
68
69
|
const res = await get(url);
|
|
69
70
|
const tmp = `${dest}.${process.pid}.tmp`;
|
|
70
|
-
const hash = crypto.createHash("sha256");
|
|
71
|
+
const hash = require("crypto").createHash("sha256");
|
|
71
72
|
await new Promise((resolve, reject) => {
|
|
72
|
-
const gunzip = zlib.createGunzip();
|
|
73
|
+
const gunzip = require("zlib").createGunzip();
|
|
73
74
|
const out = fs.createWriteStream(tmp, { mode: 0o755 });
|
|
74
75
|
gunzip.on("data", (chunk) => hash.update(chunk));
|
|
75
76
|
res.on("error", reject);
|
|
@@ -90,11 +91,116 @@ async function install({ quiet = false } = {}) {
|
|
|
90
91
|
return dest;
|
|
91
92
|
}
|
|
92
93
|
|
|
93
|
-
|
|
94
|
+
const MARK = "written by paragma (install.js): runs pg itself, not through Node";
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* npm's shims for pg that run it through Node, with what each becomes:
|
|
98
|
+
* pg (Git Bash and other sh), pg.cmd and pg.ps1, in the folder of a global
|
|
99
|
+
* install (<prefix>, the package in <prefix>/node_modules/paragma) or of a
|
|
100
|
+
* local one (node_modules/.bin). A shim is known by the launcher path npm
|
|
101
|
+
* wrote in it; the new one is written as npm writes them and points inside
|
|
102
|
+
* the package too, so npm still treats it as its own: an update writes it
|
|
103
|
+
* anew (the first call then points it again), an uninstall removes it.
|
|
104
|
+
*/
|
|
105
|
+
function shims(exe) {
|
|
106
|
+
const launcher = path.join(__dirname, "bin", "pg");
|
|
107
|
+
const dirs = [path.resolve(__dirname, "..", ".."), path.resolve(__dirname, "..", ".bin")];
|
|
108
|
+
const found = [];
|
|
109
|
+
for (const dir of dirs) {
|
|
110
|
+
const slash = (p) => path.relative(dir, p).split(path.sep).join("/");
|
|
111
|
+
const back = (p) => path.relative(dir, p).split(path.sep).join("\\");
|
|
112
|
+
const sh = [
|
|
113
|
+
"#!/bin/sh",
|
|
114
|
+
`# ${MARK}`,
|
|
115
|
+
'case "$0" in',
|
|
116
|
+
` *\\\\*) basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')") ;;`,
|
|
117
|
+
" */*) basedir=${0%/*} ;;",
|
|
118
|
+
" *) basedir=. ;;",
|
|
119
|
+
"esac",
|
|
120
|
+
`if [ -f "$basedir/${slash(exe)}" ]; then`,
|
|
121
|
+
` exec "$basedir/${slash(exe)}" "$@"`,
|
|
122
|
+
"fi",
|
|
123
|
+
"# the binary is gone (an update): Node's launcher downloads it again",
|
|
124
|
+
`exec node "$basedir/${slash(launcher)}" "$@"`,
|
|
125
|
+
"",
|
|
126
|
+
].join("\n");
|
|
127
|
+
const cmd = [
|
|
128
|
+
"@ECHO off",
|
|
129
|
+
`REM ${MARK}`,
|
|
130
|
+
"GOTO start",
|
|
131
|
+
":find_dp0",
|
|
132
|
+
"SET dp0=%~dp0",
|
|
133
|
+
"EXIT /b",
|
|
134
|
+
":start",
|
|
135
|
+
"SETLOCAL",
|
|
136
|
+
"CALL :find_dp0",
|
|
137
|
+
`IF NOT EXIST "%dp0%\\${back(exe)}" GOTO node`,
|
|
138
|
+
`endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%dp0%\\${back(exe)}" %*`,
|
|
139
|
+
":node",
|
|
140
|
+
"REM the binary is gone (an update): Node's launcher downloads it again",
|
|
141
|
+
`endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "node" "%dp0%\\${back(launcher)}" %*`,
|
|
142
|
+
"",
|
|
143
|
+
].join("\r\n");
|
|
144
|
+
const ps1 = [
|
|
145
|
+
"#!/usr/bin/env pwsh",
|
|
146
|
+
`# ${MARK}`,
|
|
147
|
+
"$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent",
|
|
148
|
+
`if (-not (Test-Path "$basedir/${slash(exe)}")) {`,
|
|
149
|
+
" # the binary is gone (an update): Node's launcher downloads it again",
|
|
150
|
+
` & node "$basedir/${slash(launcher)}" $args`,
|
|
151
|
+
" exit $LASTEXITCODE",
|
|
152
|
+
"}",
|
|
153
|
+
"if ($MyInvocation.ExpectingInput) {",
|
|
154
|
+
` $input | & "$basedir/${slash(exe)}" $args`,
|
|
155
|
+
"} else {",
|
|
156
|
+
` & "$basedir/${slash(exe)}" $args`,
|
|
157
|
+
"}",
|
|
158
|
+
"exit $LASTEXITCODE",
|
|
159
|
+
"",
|
|
160
|
+
].join("\n");
|
|
161
|
+
const files = [
|
|
162
|
+
["pg", `"$basedir/${slash(launcher)}" "$@"`, sh],
|
|
163
|
+
["pg.cmd", `"%dp0%\\${back(launcher)}" %*`, cmd],
|
|
164
|
+
["pg.ps1", `"$basedir/${slash(launcher)}" $args`, ps1],
|
|
165
|
+
];
|
|
166
|
+
for (const [name, viaNode, text] of files) {
|
|
167
|
+
const file = path.join(dir, name);
|
|
168
|
+
let now;
|
|
169
|
+
try {
|
|
170
|
+
now = fs.readFileSync(file, "utf8");
|
|
171
|
+
} catch {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
if (now.includes(viaNode)) found.push({ file, text });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return found;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Points npm's shims at the binary (Windows); how many were changed. */
|
|
181
|
+
function direct(exe) {
|
|
182
|
+
if (process.platform !== "win32" || !exe || !fs.existsSync(exe)) return 0;
|
|
183
|
+
let changed = 0;
|
|
184
|
+
for (const { file, text } of shims(exe)) {
|
|
185
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
186
|
+
try {
|
|
187
|
+
fs.writeFileSync(tmp, text);
|
|
188
|
+
fs.renameSync(tmp, file);
|
|
189
|
+
changed++;
|
|
190
|
+
} catch {
|
|
191
|
+
fs.rmSync(tmp, { force: true });
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return changed;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
module.exports = { install, binaryPath, assetName, direct };
|
|
94
198
|
|
|
95
199
|
if (require.main === module) {
|
|
96
|
-
install()
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
200
|
+
install()
|
|
201
|
+
.then(direct)
|
|
202
|
+
.catch((e) => {
|
|
203
|
+
// the install itself does not fail: bin/pg tries again on the first call
|
|
204
|
+
process.stderr.write(`paragma: ${e.message}; pg will try again when it first runs.\n`);
|
|
205
|
+
});
|
|
100
206
|
}
|
package/package.json
CHANGED