diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5d3f7033..e3014f87 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -39,9 +39,6 @@ jobs: - name: Install Node Packages run: npm ci - - name: Decompress compressed files - run: npm run decompress - - name: Cache jsvu Binaries uses: actions/cache@v4 with: diff --git a/package.json b/package.json index 689f6ce9..7b348193 100644 --- a/package.json +++ b/package.json @@ -17,19 +17,20 @@ "scripts": { "server": "node tests/server.mjs", "compress": "node utils/compress.mjs", - "decompress": "node utils/compress.mjs -d -k", + "decompress": "node utils/compress.mjs --decompress --keep ", "lint:check": "eslint **/*.{js,mjs,jsx,ts,tsx}", "pretty:check": "prettier --check ./", "pretty:format": "prettier --write ./", "format:check": "npm run pretty:check && npm run lint:check", - "test:chrome": "node tests/run-browser.mjs --browser chrome", - "test:firefox": "node tests/run-browser.mjs --browser firefox", - "test:safari": "node tests/run-browser.mjs --browser safari", - "test:edge": "node tests/run-browser.mjs --browser edge", - "test:shell": "npm run test:v8 && npm run test:jsc && npm run test:spidermonkey", - "test:v8": "node tests/run-shell.mjs --shell v8", - "test:jsc": "node tests/run-shell.mjs --shell jsc", - "test:spidermonkey": "node tests/run-shell.mjs --shell spidermonkey" + "test:prepare": "npm run decompress -- --quiet", + "test:chrome": "npm run test:prepare && node tests/run-browser.mjs --browser chrome", + "test:firefox": "npm run test:prepare && node tests/run-browser.mjs --browser firefox", + "test:safari": "npm run test:prepare && node tests/run-browser.mjs --browser safari", + "test:edge": "npm run test:prepare && node tests/run-browser.mjs --browser edge", + "test:shell": "npm run test:prepare && npm run test:v8 && npm run test:jsc && npm run test:spidermonkey", + "test:v8": "npm run test:prepare && node tests/run-shell.mjs --shell v8", + "test:jsc": "npm run test:prepare && node tests/run-shell.mjs --shell jsc", + "test:spidermonkey": "npm run test:prepare && node tests/run-shell.mjs --shell spidermonkey" }, "devDependencies": { "@actions/core": "^1.11.1", diff --git a/utils/compress.mjs b/utils/compress.mjs index fb91d7bc..4e5c5cc6 100644 --- a/utils/compress.mjs +++ b/utils/compress.mjs @@ -5,11 +5,14 @@ import zlib from 'zlib'; import fs from 'fs'; import path from 'path'; +let log = console.log + function parseCommandLineArgs() { const optionDefinitions = [ { name: 'decompress', alias: 'd', type: Boolean, description: 'Decompress files (default: compress).' }, { name: 'keep', alias: 'k', type: Boolean, description: 'Keep input files after processing (default: delete).' }, { name: 'help', alias: 'h', type: Boolean, description: 'Print this usage guide.' }, + { name: 'quiet', alias: 'q', type: Boolean, description: 'Quite output, only print errors.' }, { name: 'globs', type: String, multiple: true, defaultOption: true, description: 'Glob patterns of files to process.' }, ]; const options = commandLineArgs(optionDefinitions); @@ -32,10 +35,14 @@ function parseCommandLineArgs() { process.exit(0); } + if (options.quiet) { + log = () => {}; + } + if (options.globs === undefined) { if (options.decompress) { const defaultGlob = '**/*.z'; - console.log(`No input glob pattern given, using default: ${defaultGlob}`); + log(`No input glob pattern given, using default: ${defaultGlob}`); options.globs = [defaultGlob]; } else { // For compression, require the user to specify explicit input file patterns. @@ -61,9 +68,9 @@ function compress(inputData) { const originalSize = inputData.length; const compressedSize = compressedData.length; const compressionRatio = calculateCompressionRatio(originalSize, compressedSize); - 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)}%`); + log(` Original size: ${String(originalSize).padStart(8)} bytes`); + log(` Compressed size: ${String(compressedSize).padStart(8)} bytes`); + log(` Compression ratio: ${compressionRatio.toFixed(2).padStart(8)}%`); return compressedData; } @@ -74,18 +81,19 @@ function decompress(inputData) { const compressedSize = inputData.length; const decompressedSize = decompressedData.length; const expansionRatio = calculateExpansionRatio(compressedSize, decompressedSize); - 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)}%`); + log(` Compressed size: ${String(compressedSize).padStart(8)} bytes`); + log(` Decompressed size: ${String(decompressedSize).padStart(8)} bytes`); + log(` Expansion ratio: ${expansionRatio.toFixed(2).padStart(8)}%`); return decompressedData; } function globsToFiles(globs) { let files = []; - console.assert(globs.length > 0); + console.assert(globs.length > 0) ; + const globtions = { nodir: true, ignore: '**/node_modules/**' }; for (const glob of globs) { - const matches = globSync(glob, { nodir: true }); + const matches = globSync(glob, globtions); files = files.concat(matches); } files = Array.from(new Set(files)).sort(); @@ -94,7 +102,7 @@ function globsToFiles(globs) { function processFiles(files, isDecompress, keep) { const verb = isDecompress ? 'decompress' : 'compress'; - console.log(`Found ${files.length} files to ${verb}` + (files.length ? ':' : '.')); + log(`Found ${files.length} files to ${verb}` + (files.length ? ':' : '.')); // For printing overall statistics at the end. let totalInputSize = 0; @@ -111,7 +119,7 @@ function processFiles(files, isDecompress, keep) { } else { outputFilename = inputFilename.slice(0, -2); } - console.log(` Decompressing to: ${outputFilename}`); + log(` Decompressing to: ${outputFilename}`); } else { if (path.extname(inputFilename) === '.z') { console.warn(` Warning: Input file already has a .z extension.`); @@ -130,7 +138,7 @@ function processFiles(files, isDecompress, keep) { if (!keep) { fs.unlinkSync(inputFilename); - console.log(` Deleted input file.`); + log(` Deleted input file.`); } } catch (err) { console.error(`Error ${verb}ing ${inputFilename}:`, err); @@ -140,14 +148,14 @@ function processFiles(files, isDecompress, keep) { if (files.length > 1) { if (isDecompress) { const totalExpansionRatio = calculateExpansionRatio(totalInputSize, totalOutputSize); - console.log(`Total compressed sizes: ${String(totalInputSize).padStart(9)} bytes`); - console.log(`Total decompressed sizes: ${String(totalOutputSize).padStart(9)} bytes`); - console.log(`Average expansion ratio: ${totalExpansionRatio.toFixed(2).padStart(9)}%`); + log(`Total compressed sizes: ${String(totalInputSize).padStart(9)} bytes`); + log(`Total decompressed sizes: ${String(totalOutputSize).padStart(9)} bytes`); + log(`Average expansion ratio: ${totalExpansionRatio.toFixed(2).padStart(9)}%`); } else { const totalCompressionRatio = calculateCompressionRatio(totalInputSize, totalOutputSize); - console.log(`Total original sizes: ${String(totalInputSize).padStart(9)} bytes`); - console.log(`Total compressed sizes: ${String(totalOutputSize).padStart(9)} bytes`); - console.log(`Average compression ratio: ${totalCompressionRatio.toFixed(2).padStart(9)}%`); + log(`Total original sizes: ${String(totalInputSize).padStart(9)} bytes`); + log(`Total compressed sizes: ${String(totalOutputSize).padStart(9)} bytes`); + log(`Average compression ratio: ${totalCompressionRatio.toFixed(2).padStart(9)}%`); } } }