@zainriazdev/zpatch 0.6.4 → 0.6.5
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/README.md +4 -0
- package/build/zpatch.js +65 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,6 +26,10 @@ The launcher checks your system and install location, downloads the matching nat
|
|
|
26
26
|
| `zpatch update` | Downloads and verifies the current native release. It reuses a valid sign-in when available. |
|
|
27
27
|
| `zpatch --version` | Prints the installed native version, which can differ from the npm launcher version. |
|
|
28
28
|
| `zpatch --help` | Shows the installed CLI's commands and options. |
|
|
29
|
+
| `zpatch config get default-target --json` | Shows the effective archive member and its source. Works offline before login. |
|
|
30
|
+
| `zpatch config set default-target NAME` | Saves the local default. `zpatch config reset default-target` clears it. |
|
|
31
|
+
|
|
32
|
+
Use `zpatch run --file sample.zip --replace replacement.txt --dry-run --json` for a single archive. The target comes from `--target`, `ZPATCH_DEFAULT_TARGET`, local config, or `123.txt`, in that order.
|
|
29
33
|
|
|
30
34
|
The launcher stores the native release under `~/.zpatch/bin/` by default and checks the installed binary's SHA-256 before running it. The npm package has no install-time scripts, runtime dependencies, native binaries, or credentials. GitHub Actions publishes the launcher through npm Trusted Publishing with provenance. [Launcher source](https://github.com/zainriaz-dev/zpatch-cli)
|
|
31
35
|
|
package/build/zpatch.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { createHash, randomBytes } from "node:crypto";
|
|
3
3
|
import { createReadStream, createWriteStream } from "node:fs";
|
|
4
|
-
import { access, lstat, mkdir, mkdtemp, readFile, rename, rm, statfs, writeFile, realpath } from "node:fs/promises";
|
|
4
|
+
import { access, lstat, mkdir, mkdtemp, open, readFile, rename, rm, statfs, writeFile, realpath } from "node:fs/promises";
|
|
5
5
|
import { createServer } from "node:http";
|
|
6
6
|
import { availableParallelism, homedir, machine, release, totalmem } from "node:os";
|
|
7
7
|
import { constants } from "node:fs";
|
|
@@ -477,10 +477,74 @@ async function installed(config, platform) {
|
|
|
477
477
|
throw Error("installed binary checksum mismatch");
|
|
478
478
|
return executable;
|
|
479
479
|
}
|
|
480
|
+
function validateTarget(value) {
|
|
481
|
+
if (typeof value !== "string" || Buffer.from(value).toString("utf8") !== value || Buffer.byteLength(value, "utf8") > 65535 ||
|
|
482
|
+
/[\\:\p{Cc}]/u.test(value) || value.split("/").some(part => !part.trim() || part === "." || part === ".."))
|
|
483
|
+
throw Error("invalid ZIP target: use a filename or a relative internal path");
|
|
484
|
+
}
|
|
485
|
+
async function archiveConfig(args) {
|
|
486
|
+
const [action, key, value] = args;
|
|
487
|
+
if (key !== "default-target" || !(action === "set" && args.length === 3 ||
|
|
488
|
+
action === "reset" && args.length === 2 ||
|
|
489
|
+
action === "get" && (args.length === 2 || args.length === 3 && value === "--json")))
|
|
490
|
+
throw Error("Usage: zpatch config get default-target [--json], set default-target <name>, or reset default-target");
|
|
491
|
+
const configured = join(base, "config.json");
|
|
492
|
+
const readConfig = async () => {
|
|
493
|
+
const raw = await readFile(configured, "utf8").catch((error) => {
|
|
494
|
+
if (error.code === "ENOENT")
|
|
495
|
+
return "{}";
|
|
496
|
+
throw error;
|
|
497
|
+
});
|
|
498
|
+
const data = JSON.parse(raw);
|
|
499
|
+
if (!data || typeof data !== "object" || Array.isArray(data))
|
|
500
|
+
throw Error("config.json must contain a JSON object");
|
|
501
|
+
return data;
|
|
502
|
+
};
|
|
503
|
+
if (action === "get") {
|
|
504
|
+
let target = process.env.ZPATCH_DEFAULT_TARGET ?? process.env.ZPATCH_TARGET;
|
|
505
|
+
let source = "env";
|
|
506
|
+
if (target === undefined) {
|
|
507
|
+
const config = await readConfig();
|
|
508
|
+
const present = Object.hasOwn(config, "default_target");
|
|
509
|
+
target = present ? config.default_target : "123.txt";
|
|
510
|
+
source = present ? "config" : "builtin_default";
|
|
511
|
+
}
|
|
512
|
+
validateTarget(target);
|
|
513
|
+
console.log(value === "--json" ? JSON.stringify({ target, target_source: source }) : target);
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
if (action === "set")
|
|
517
|
+
validateTarget(value);
|
|
518
|
+
await privateDirectory(base, true);
|
|
519
|
+
const data = await readConfig();
|
|
520
|
+
if (action === "set")
|
|
521
|
+
data.default_target = value;
|
|
522
|
+
else if (!Object.hasOwn(data, "default_target"))
|
|
523
|
+
return;
|
|
524
|
+
else
|
|
525
|
+
delete data.default_target;
|
|
526
|
+
const tmp = join(base, ".config-" + randomBytes(12).toString("hex"));
|
|
527
|
+
try {
|
|
528
|
+
const out = await open(tmp, "wx", 0o600);
|
|
529
|
+
try {
|
|
530
|
+
await out.writeFile(JSON.stringify(data, null, 2) + "\n");
|
|
531
|
+
await out.sync();
|
|
532
|
+
}
|
|
533
|
+
finally {
|
|
534
|
+
await out.close();
|
|
535
|
+
}
|
|
536
|
+
await rename(tmp, configured);
|
|
537
|
+
}
|
|
538
|
+
finally {
|
|
539
|
+
await rm(tmp, { force: true });
|
|
540
|
+
}
|
|
541
|
+
}
|
|
480
542
|
async function main() {
|
|
481
543
|
if (Number(process.versions.node.split(".")[0]) < 22)
|
|
482
544
|
throw Error("Install Node.js 22 or newer to run zpatch.");
|
|
483
545
|
const args = process.argv.slice(2);
|
|
546
|
+
if (args[0] === "config")
|
|
547
|
+
return archiveConfig(args.slice(1));
|
|
484
548
|
const system = await device();
|
|
485
549
|
const noBrowser = args.length === 1 && args[0] === "--no-browser" ||
|
|
486
550
|
args.length === 2 && ["login", "update"].includes(args[0]) && args[1] === "--no-browser";
|