-
Notifications
You must be signed in to change notification settings - Fork 16
Add a zlib-based compress.mjs script #172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
63eb646
Add a zlib-based compress script
danleh a6c8827
address first round comments
danleh f8c64d5
move to new utils subdir
danleh c336666
address more comments, add total sizes stats at end
danleh 8090c8a
warn against compressing .z files again
danleh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { globSync } from 'glob'; | ||
import zlib from 'zlib'; | ||
import fs from 'fs'; | ||
|
||
const isNPM = process.env.npm_config_user_agent !== undefined; | ||
const command = isNPM ? 'npm run compress --' : 'node compress.mjs'; | ||
|
||
const usage = `Usage: ${command} [options] <glob>... | ||
|
||
Options: | ||
-d, --decompress Decompress files (default: compress). | ||
-k, --keep Keep input files after processing (default: delete).`; | ||
|
||
const args = process.argv.slice(2); | ||
if (args.length === 0) { | ||
console.log(usage); | ||
process.exit(1); | ||
} | ||
|
||
const keepInputFiles = args.some(arg => arg === '-k' || arg === '--keep'); | ||
const decompressMode = args.some(arg => arg === '-d' || arg === '--decompress'); | ||
|
||
let globs = args.filter(arg => !arg.startsWith('-')); | ||
if (globs.length === 0) { | ||
if (decompressMode) { | ||
const defaultGlob = '**/*.z'; | ||
console.log(`No input glob pattern given, using default: ${defaultGlob}`); | ||
globs = [defaultGlob]; | ||
} else { | ||
// To prevent accidental compression, require explicit input file patterns. | ||
console.log(usage); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
let files = new Set(); | ||
if (globs.length > 0) { | ||
for (const glob of globs) { | ||
try { | ||
const matches = globSync(glob, { nodir: true }); | ||
for (const match of matches) { | ||
danleh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
files.add(match); | ||
} | ||
} catch (err) { | ||
console.error(`Error processing glob: ${glob}`, err); | ||
danleh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
files = Array.from(files).sort(); | ||
if (files.length === 0) { | ||
console.log(`No files found to process with the given globs: ${globs.join(', ')}`); | ||
danleh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
process.exit(0); | ||
} | ||
|
||
function compress(inputData) { | ||
const compressedData = zlib.deflateSync(inputData, { level: zlib.constants.Z_BEST_COMPRESSION }); | ||
|
||
const originalSize = inputData.length; | ||
const compressedSize = compressedData.length; | ||
const compressionRatio = (1 - compressedSize / originalSize) * 100; | ||
console.log(` Original size: ${String(originalSize).padStart(8)} bytes`); | ||
console.log(` Compressed size: ${String(compressedSize).padStart(8)} bytes`); | ||
console.log(` Compression ratio: ${compressionRatio.toFixed(2).padStart(8)}%`); | ||
danleh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
return compressedData; | ||
} | ||
|
||
function decompress(inputData) { | ||
const decompressedData = zlib.inflateSync(inputData); | ||
|
||
const compressedSize = inputData.length; | ||
const decompressedSize = decompressedData.length; | ||
const expansionRatio = (decompressedSize / compressedSize - 1) * 100; | ||
console.log(` Compressed size: ${String(compressedSize).padStart(8)} bytes`); | ||
console.log(` Decompressed size: ${String(decompressedSize).padStart(8)} bytes`); | ||
console.log(` Expansion ratio: ${expansionRatio.toFixed(2).padStart(8)}%`); | ||
danleh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
return decompressedData; | ||
} | ||
|
||
const verb = decompressMode ? 'decompress' : 'compress'; | ||
console.log(`Found ${files.length} files to ${verb}...`); | ||
|
||
for (const inputFile of files) { | ||
danleh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
try { | ||
console.log(inputFile); | ||
|
||
// Copy the mode over to avoid git status entries after a roundtrip. | ||
const { mode } = fs.statSync(inputFile); | ||
const inputData = fs.readFileSync(inputFile); | ||
const outputData = decompressMode ? decompress(inputData) : compress(inputData); | ||
let outputFile; | ||
if (decompressMode) { | ||
outputFile = inputFile.endsWith('.z') ? inputFile.slice(0, -2) : `${inputFile}.decompressed`; | ||
danleh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
danleh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} else { | ||
outputFile = `${inputFile}.z`; | ||
} | ||
fs.writeFileSync(outputFile, outputData, { mode }); | ||
|
||
if (!keepInputFiles) { | ||
fs.unlinkSync(inputFile); | ||
console.log(` Deleted input file.`); | ||
} | ||
} catch (err) { | ||
console.error(`Error ${verb}ing ${inputFile}:`, err); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.