Skip to content

Commit

Permalink
Merge pull request svg#778 from LISBON11/fix_coa
Browse files Browse the repository at this point in the history
Fix coa
  • Loading branch information
GreLI authored Sep 5, 2017
2 parents d70f6fc + b96982f commit b367c87
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 127 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"phantom": false,
"describe": false,
"before": false,
"it": false
"it": false,
"beforeEach": false
},
"node": true,
"strict": "global",
Expand Down
259 changes: 136 additions & 123 deletions lib/svgo/coa.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ module.exports = require('coa').Cmd()
// --show-plugins
if (opts['show-plugins']) {
showAvailablePlugins();
process.exit(0);
return;
}

// w/o anything
Expand Down Expand Up @@ -243,48 +243,56 @@ module.exports = require('coa').Cmd()

// --folder
if (opts.folder) {
optimizeFolder(opts.folder, config, output);

return;
return optimizeFolder(opts.folder, config, output);
}

// --input
if (input) {
// STDIN
if (input === '-') {
var data = '';

process.stdin.pause();

process.stdin
.on('data', function(chunk) {
data += chunk;
})
.once('end', function() {
optimizeFromString(data, config, opts.datauri, input, output);
})
.resume();
// file
} else {
FS.readFile(input, 'utf8', function(err, data) {
if (err) {
if (err.code === 'EISDIR')
optimizeFolder(input, config, output);
else if (err.code === 'ENOENT')
console.error(`Error: no such file or directory '${input}'.`);
else
console.error(err);
return;
}
optimizeFromString(data, config, opts.datauri, input, output);
});
}
return new Promise((resolve, reject) => {
// STDIN
if (input === '-') {
var data = '';

process.stdin.pause();

process.stdin
.on('data', function(chunk) {
data += chunk;
})
.once('end', function() {
optimizeFromString(data, config, opts.datauri, input, output)
.then(resolve)
.catch(reject);
})
.resume();
// file
} else {
FS.readFile(input, 'utf8', function(err, data) {
if (err) {
switch(err.code) {
case 'EISDIR':
return optimizeFolder(input, config, output)
.then(resolve)
.catch(reject);
case 'ENOENT':
return reject(`Error: no such file or directory '${input}'.`);
default:
return reject(err);
}
}

optimizeFromString(data, config, opts.datauri, input, output)
.then(resolve)
.catch(reject);
});
}
});

// --string
} else if (opts.string) {
opts.string = decodeSVGDatauri(opts.string);

optimizeFromString(opts.string, config, opts.datauri, input, output);
return optimizeFromString(opts.string, config, opts.datauri, input, output);
}
});

Expand All @@ -295,7 +303,7 @@ function optimizeFromString(svgstr, config, datauri, input, output) {
outBytes,
svgo = new SVGO(config);

svgo.optimize(svgstr).then(function(result) {
return svgo.optimize(svgstr).then(function(result) {
if (datauri) {
result.data = encodeSVGDatauri(result.data, datauri);
}
Expand All @@ -305,7 +313,7 @@ function optimizeFromString(svgstr, config, datauri, input, output) {

// stdout
if (output === '-' || (!input || input === '-') && !output) {
process.stdout.write(result.data + '\n');
console.log(result.data + '\n');
// file
} else {
// overwrite input file if there is no output
Expand All @@ -317,23 +325,26 @@ function optimizeFromString(svgstr, config, datauri, input, output) {
console.log('\r');
}

saveFileAndPrintInfo(config, result.data, output, inBytes, outBytes, time);
return saveFileAndPrintInfo(config, result.data, output, inBytes, outBytes, time);
}
}, error => console.error(error));
}).catch(console.error);
}

function saveFileAndPrintInfo(config, data, path, inBytes, outBytes, time) {
FS.writeFile(path, data, 'utf8', function() {
if (config.quiet) {
return;
}
return new Promise((resolve, reject) => {
FS.writeFile(path, data, 'utf8', function(err) {
if (err) return reject(err);

// print time info
printTimeInfo(time);
if (!config.quiet) {
// print time info
printTimeInfo(time);

// print optimization profit info
printProfitInfo(inBytes, outBytes);
// print optimization profit info
printProfitInfo(inBytes, outBytes);
}

resolve();
});
});
}

Expand Down Expand Up @@ -411,94 +422,96 @@ function optimizeFolder(dir, config, output) {
var path = PATH.resolve(dir);

// list folder content
FS.readdir(path, function(err, files) {
if (err) {
console.error(err);
return;
}

if (!files.length) {
console.log(`Directory '${dir}' is empty.`);
return;
}

var i = 0,
found = false;

function optimizeFile(file) {
// absoluted file path
var filepath = PATH.resolve(path, file);
var fileStat = FS.lstatSync(filepath);
var outfilepath = output ? PATH.resolve(output, file) : filepath;

if (config.recursive && fileStat.isDirectory()) {
optimizeFolder(filepath, config, output);
return new Promise((resolve, reject) => {
FS.readdir(path, function(err, files) {
if (err) {
return reject(err);
}

// check if file name matches *.svg
if (regSVGFile.test(filepath)) {
found = true;
FS.readFile(filepath, 'utf8', function(err, data) {
if (err) {
console.error(err);
return;
}

var startTime = Date.now(),
time,
inBytes = Buffer.byteLength(data, 'utf8'),
outBytes;
if (!files.length) {
console.log(`Directory '${dir}' is empty.`);
return resolve();
}

svgo.optimize(data).then(function(result) {
outBytes = Buffer.byteLength(result.data, 'utf8');
time = Date.now() - startTime;
var found = false;

writeOutput();
Promise.all(files.map(optimizeFile))
.then(() => {
if (!found) {
console.log('No SVG files have been found.');
}
resolve();
})
.catch(reject);

function optimizeFile(file) {
// absoluted file path
var filepath = PATH.resolve(path, file);
var fileStat = FS.lstatSync(filepath);
var outfilepath = output ? PATH.resolve(output, file) : filepath;

if (config.recursive && fileStat.isDirectory()) {
return optimizeFolder(filepath, config, output);
}

function writeOutput() {
FS.writeFile(outfilepath, result.data, 'utf8', report);
}
// check if file name matches *.svg
if (regSVGFile.test(file)) {
found = true;

function report(err) {
return new Promise((resolve, reject) => {
FS.readFile(filepath, 'utf8', function(err, data) {
if (err) {
if (err.code === 'ENOENT') {
mkdirp(output, writeOutput);
return;
} else if (err.code === 'ENOTDIR') {
console.error(`Error: output '${output}' is not a directory.`);
return;
}
console.error(err);
reject(err);
return;
}

if (!config.quiet) {
console.log(file + ':');

// print time info
printTimeInfo(time);

// print optimization profit info
printProfitInfo(inBytes, outBytes);
}

//move on to the next file
if (++i < files.length) {
optimizeFile(files[i]);
}
}
}, error => console.error(error));
});
}
//move on to the next file
else if (++i < files.length) {
optimizeFile(files[i]);
} else if (!found) {
console.log('No SVG files have been found.');
var startTime = Date.now(),
time,
inBytes = Buffer.byteLength(data, 'utf8'),
outBytes;

svgo.optimize(data)
.then(function(result) {
outBytes = Buffer.byteLength(result.data, 'utf8');
time = Date.now() - startTime;

writeOutput();

function writeOutput() {
FS.writeFile(outfilepath, result.data, 'utf8', report);
}

function report(err) {
if (err) {
switch(err.code) {
case 'ENOENT':
return mkdirp(output, writeOutput);
case 'ENOTDIR':
return reject(`Error: output '${output}' is not a directory.`);
default:
return reject(err);
}
}

if (!config.quiet) {
console.log(file + ':');

// print time info
printTimeInfo(time);

// print optimization profit info
printProfitInfo(inBytes, outBytes);
}

resolve();
}
})
.catch(reject);
});
});
}
}
}

optimizeFile(files[i]);
});
});
}

Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@
"jshint": "jshint --show-non-errors ."
},
"dependencies": {
"sax": "~1.2.1",
"coa": "~1.0.1",
"js-yaml": "~3.7.0",
"colors": "~1.1.2",
"csso": "~2.3.1",
"fs-extra": "^4.0.1",
"js-yaml": "~3.7.0",
"mkdirp": "~0.5.1",
"csso": "~2.3.1"
"mock-stdin": "^0.3.1",
"sax": "~1.2.1"
},
"devDependencies": {
"mocha": "~3.2.0",
Expand Down
Loading

0 comments on commit b367c87

Please sign in to comment.