Skip to content

Commit

Permalink
Replace strip-ansi with --no-color flag (svg#1588)
Browse files Browse the repository at this point in the history
nanocolors checks for --color and --no-color flags to force or disable
output coloring. Which can be used instead of strip-ansi package which
btw introduced vulnerability recently.
  • Loading branch information
TrySound authored Sep 30, 2021
1 parent acd9183 commit 203db9a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
36 changes: 19 additions & 17 deletions lib/svgo/coa.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const FS = require('fs');
const PATH = require('path');
const fs = require('fs');
const path = require('path');
const { green, red } = require('nanocolors');
const { loadConfig, optimize } = require('../svgo-node.js');
const pluginsMap = require('../../plugins/plugins.js');
Expand All @@ -16,7 +16,7 @@ const regSVGFile = /\.svg$/i;
*/
function checkIsDir(path) {
try {
return FS.lstatSync(path).isDirectory();
return fs.lstatSync(path).isDirectory();
} catch (e) {
return false;
}
Expand Down Expand Up @@ -73,6 +73,8 @@ module.exports = function makeProgram(program) {
'Only output error messages, not regular status messages'
)
.option('--show-plugins', 'Show available plugins and exit')
// used by nanocolors internally
.option('--no-color', 'Output plain text without color')
.action(action);
};

Expand Down Expand Up @@ -218,7 +220,7 @@ async function action(args, opts, command) {
for (var i = 0; i < input.length; i++) {
output[i] = checkIsDir(input[i])
? input[i]
: PATH.resolve(dir, PATH.basename(input[i]));
: path.resolve(dir, path.basename(input[i]));
}
} else if (output.length < input.length) {
output = output.concat(input.slice(output.length));
Expand Down Expand Up @@ -283,7 +285,7 @@ function optimizeFolder(config, dir, output) {
if (!config.quiet) {
console.log(`Processing directory '${dir}':\n`);
}
return FS.promises
return fs.promises
.readdir(dir)
.then((files) => processDirectory(config, dir, files, output));
}
Expand Down Expand Up @@ -331,19 +333,19 @@ function getFilesDescriptions(config, dir, files, output) {
!config.exclude.some((regExclude) => regExclude.test(name))
)
.map((name) => ({
inputPath: PATH.resolve(dir, name),
outputPath: PATH.resolve(output, name),
inputPath: path.resolve(dir, name),
outputPath: path.resolve(output, name),
}));

return config.recursive
? [].concat(
filesInThisFolder,
files
.filter((name) => checkIsDir(PATH.resolve(dir, name)))
.filter((name) => checkIsDir(path.resolve(dir, name)))
.map((subFolderName) => {
const subFolderPath = PATH.resolve(dir, subFolderName);
const subFolderFiles = FS.readdirSync(subFolderPath);
const subFolderOutput = PATH.resolve(output, subFolderName);
const subFolderPath = path.resolve(dir, subFolderName);
const subFolderFiles = fs.readdirSync(subFolderPath);
const subFolderOutput = path.resolve(output, subFolderName);
return getFilesDescriptions(
config,
subFolderPath,
Expand All @@ -364,7 +366,7 @@ function getFilesDescriptions(config, dir, files, output) {
* @return {Promise}
*/
function optimizeFile(config, file, output) {
return FS.promises.readFile(file, 'utf8').then(
return fs.promises.readFile(file, 'utf8').then(
(data) =>
processSVGData(config, { input: 'file', path: file }, data, output, file),
(error) => checkOptimizeFileError(config, file, output, error)
Expand Down Expand Up @@ -398,7 +400,7 @@ function processSVGData(config, info, data, output, input) {
function () {
if (!config.quiet && output != '-') {
if (input) {
console.log(`\n${PATH.basename(input)}:`);
console.log(`\n${path.basename(input)}:`);
}
printTimeInfo(processingTime);
printProfitInfo(prevFileSize, resultFileSize);
Expand Down Expand Up @@ -428,9 +430,9 @@ function writeOutput(input, output, data) {
return Promise.resolve();
}

FS.mkdirSync(PATH.dirname(output), { recursive: true });
fs.mkdirSync(path.dirname(output), { recursive: true });

return FS.promises
return fs.promises
.writeFile(output, data, 'utf8')
.catch((error) => checkWriteFileError(input, output, data, error));
}
Expand Down Expand Up @@ -491,8 +493,8 @@ function checkOptimizeFileError(config, input, output, error) {
*/
function checkWriteFileError(input, output, data, error) {
if (error.code == 'EISDIR' && input) {
return FS.promises.writeFile(
PATH.resolve(output, PATH.basename(input)),
return fs.promises.writeFile(
path.resolve(output, path.basename(input)),
data,
'utf8'
);
Expand Down
43 changes: 35 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"css-select": "^4.1.3",
"css-tree": "^1.1.3",
"csso": "^4.2.0",
"nanocolors": "^0.1.12",
"nanocolors": "^0.2.1",
"stable": "^0.1.8"
},
"devDependencies": {
Expand All @@ -123,7 +123,6 @@
"prettier": "^2.4.0",
"rollup": "^2.56.3",
"rollup-plugin-terser": "^7.0.2",
"strip-ansi": "^6.0.0",
"tar-stream": "^2.2.0",
"typescript": "^4.4.3"
},
Expand Down
5 changes: 2 additions & 3 deletions test/cli/cli.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';

const { spawn } = require('child_process');
const stripAnsi = require('strip-ansi');

test('should exit with 1 code on syntax error', async () => {
const proc = spawn('node', ['../../bin/svgo', 'invalid.svg'], {
const proc = spawn('node', ['../../bin/svgo', '--no-color', 'invalid.svg'], {
cwd: __dirname,
});
const [code, stderr] = await Promise.all([
Expand All @@ -20,7 +19,7 @@ test('should exit with 1 code on syntax error', async () => {
}),
]);
expect(code).toEqual(1);
expect(stripAnsi(stderr))
expect(stderr)
.toEqual(`SvgoParserError: invalid.svg:2:27: Unquoted attribute value
1 | <svg>
Expand Down

0 comments on commit 203db9a

Please sign in to comment.