Skip to content

Commit

Permalink
Merge branch 'pieroxy:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
HelloLudger authored Jan 22, 2024
2 parents 73bcd2c + ef6b696 commit 684a4e3
Show file tree
Hide file tree
Showing 52 changed files with 569 additions and 265 deletions.
2 changes: 1 addition & 1 deletion LICENSE → LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2013 pieroxy
Copyright (c) 2013 Pieroxy <pieroxy@pieroxy.net>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ $ lz-string input.txt > output.txt

Home page for this program with examples, documentation and a live demo: http://pieroxy.net/blog/pages/lz-string/index.html

## Command line

If installed globally there is a command line tool available, and a test suite that can use it to show things are working properly. If other langauges build a command line tool that supports the same arguments then the test suite can be run against them too.

```console
$ lz-string -h
Usage: lz-string [options] [input-file]

Use lz-string to compress or decompress a file

Arguments:
input-file file to process, if no file then read from stdin

Options:
-V, --version output the version number
-d, --decompress if unset then this will compress
-f, --format <type> formatter to use (choices: "base64", "encodeduri", "raw", "uint8array", "utf16", default: "raw")
-v, --validate validate before returning (default: true)
-o, --output <output-file> output file, otherwise write to stdout
-q, --quiet don't print any error messages
-h, --help display help for command
```

> [!WARNING]
> Currently I cannot get NodeJS to read binary files correctly, so `raw` and `uint8array` are both compress-only, see [#139](https://github.com/pieroxy/lz-string/issues/139) - Ryc
## Other languages

This lib has numerous ports to other languages, for server side processing, mostly. Here they are:
Expand Down
48 changes: 31 additions & 17 deletions bin/cli.cjs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ function decompressContent(format, content) {
}
}

function loadFile(file) {
try {
return fs.readFileSync(file).toString();
} catch {
// TODO better error handling
}
}

program
.version(pkg.version)
.description("Use lz-string to compress or decompress a file")
Expand All @@ -48,22 +56,32 @@ program
.default("raw"),
)
.addOption(new Option("-v, --validate", "validate before returning").default(true))
.addOption(new Option("-o, --output <output-file>", "output file"))
.addOption(new Option("-o, --output <output-file>", "output file, otherwise write to stdout"))
.addOption(new Option("-q, --quiet", "don't print any error messages"))
.argument("<input-file>", "file to process")
.argument("[input-file]", "file to process, if no file then read from stdin")
.showHelpAfterError()
.action((file, { format, decompress, output, quiet, validate }) => {
if (!fs.existsSync(file)) {
if (!quiet) process.stderr.write(`Unable to find ${file}`);
.action((file = process.stdin.fd, { format, decompress, output = process.stdout.fd, quiet, validate }) => {
if (file !== process.stdin.fd) {
if (!fs.existsSync(file)) {
if (!quiet) process.stderr.write(`Unable to find ${file}\n`);
process.exit(1);
}
try {
fs.accessSync(file, fs.constants.R_OK);
} catch {
if (!quiet) process.stderr.write(`Unable to access ${file}\n`);
process.exit(1);
}
}
if (decompress && ["raw", "uint8array"].includes(format)) {
if (!quiet) process.stderr.write(`Decompressing ${format} is currently unsupported\n`);
process.exit(1);
}
try {
fs.accessSync(file, fs.constants.R_OK);
} catch {
if (!quiet) process.stderr.write(`Unable to read ${file}`);
const unprocessed = loadFile(file, quiet);
if (unprocessed === undefined) {
if (!quiet) process.stderr.write(`Unable to read ${file === process.stdin.fd ? "from stdin" : file}\n`);
process.exit(1);
}
const unprocessed = fs.readFileSync(file).toString();
const processed = decompress ? decompressContent(format, unprocessed) : compressContent(format, unprocessed);

if (validate) {
Expand All @@ -76,18 +94,14 @@ program
}
}
if (!valid) {
if (!quiet) process.stderr.write(`Unable to validate ${file}`);
if (!quiet) process.stderr.write(`Unable to validate ${file}\n`);
process.exit(1);
}
}
if (processed == null) {
if (!quiet) process.stderr.write(`Unable to process ${file}`);
if (!quiet) process.stderr.write(`Unable to process ${file}\n`);
process.exit(1);
}
if (output) {
fs.writeFileSync(output, processed, null);
} else {
process.stdout.write(processed);
}
fs.writeFileSync(output, processed, null);
})
.parse();
Loading

0 comments on commit 684a4e3

Please sign in to comment.