Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/svgo/coa.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export default function makeProgram(program) {
'-q, --quiet',
'Only output error messages, not regular status messages',
)
.option(
'-h, --hide-unchanged',
'Hide Unchanged/Unoptimized files in the output (only show Optimized files)',
)
.option('--show-plugins', 'Show available plugins and exit')
// used by picocolors internally
.option('--no-color', 'Output plain text without color')
Expand Down Expand Up @@ -166,6 +170,11 @@ async function action(args, opts, command) {
config.quiet = opts.quiet;
}

// --hide-unchanged
if (opts.hideUnchanged) {
config.hideUnchanged = opts.hideUnchanged;
}

// --recursive
if (opts.recursive) {
config.recursive = opts.recursive;
Expand Down Expand Up @@ -414,6 +423,9 @@ function processSVGData(config, info, data, output, input) {

return writeOutput(input, output, result.data).then(
function () {
if (config.hideUnchanged && prevFileSize === resultFileSize) {
return;
}
if (!config.quiet && output != '-') {
if (input) {
console.log(`\n${path.basename(input)}:`);
Expand Down
49 changes: 48 additions & 1 deletion test/coa/_index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const svgFiles = [
path.resolve(__dirname, 'testSvg/test.svg'),
path.resolve(__dirname, 'testSvg/test.1.svg'),
];
const tempFolder = 'temp';
const tempFolder = 'temp/';

/**
* @param {ReadonlyArray<string>} args
Expand Down Expand Up @@ -140,5 +140,52 @@ describe('coa', function () {
]),
).rejects.toThrow(/No SVG files have been found/);
});

it('Should not show output when svg remains unchanged (--hide-unchanged is set)', async () => {
let output = '';
const original = console.log;
console.log = function (...args) {
output += args.join(' ') + '\n';
};
try {
await expect(
runProgram([
'--folder',
path.resolve(__dirname, 'testFolderUnchangedSvg'),
'--hide-unchanged',
'--output',
tempFolder,
]),
).resolves.toBeDefined();

expect(output).not.toContain('unchanged.svg');
} finally {
console.log = original;
}
});

it('Should show output when svg gets optimized (--hide-unchanged is set)', async () => {
let output = '';
const original = console.log;
console.log = function (...args) {
output += args.join(' ') + '\n';
};
try {
await expect(
runProgram([
'--folder',
path.resolve(__dirname, 'testFolderUnchangedSvg'),
'--hide-unchanged',
'--output',
tempFolder,
]),
).resolves.toBeDefined();

expect(output).toContain('optimizable.svg');
expect(output).not.toContain('unchanged.svg');
} finally {
console.log = original;
}
});
});
});
9 changes: 9 additions & 0 deletions test/coa/testFolderUnchangedSvg/optimizable.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/coa/testFolderUnchangedSvg/unchanged.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.