Skip to content

Commit

Permalink
Merge pull request #394 from srod/cli-remove-all
Browse files Browse the repository at this point in the history
feat(cli): remove all from compressors
  • Loading branch information
srod authored Aug 9, 2018
2 parents 2f726ff + 11983bd commit 84b1fd7
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 166 deletions.
2 changes: 1 addition & 1 deletion __tests__/cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('cli error', function() {
test('should minify to throw with all compressors', function() {
var spy = jest.spyOn(nodeMinify, 'minify');
return cli({
compressor: 'all',
compressor: 'gcc-java',
input: 'examples/public/js/sample.js',
output: 'examples/public/js-dist/babili-es6.js'
}).catch(function(err) {
Expand Down
14 changes: 2 additions & 12 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,10 @@

You can compress files using the command line.

Usage for one or multiple compressors :
Usage:

```bash
node-minify --compressor babel-minify --input 'input.js' --output 'output.js'
```

```bash
node-minify --compressor 'babel-minify, uglifyjs, gcc' --input 'input.js' --output 'output.js'
```

Usage for all the compressors :

```bash
node-minify --compressor all --input 'input.js' --output 'output.js'
```

<img src="../static/cli.png" width="784" height="433" alt="cli">
<img src="../static/cli.png" width="749" height="322" alt="cli">
131 changes: 17 additions & 114 deletions lib/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,101 +11,31 @@
*/

var chalk = require('chalk');
var table = require('text-table');
var deprecate = require('depd')('node-minify');
var compress = require('./compress');
var spinner = require('./spinner');

/**
* Module variables.
*/

var all = ['babel-minify', 'butternut', 'gcc', 'uglifyjs', 'uglify-es', 'yui'];
var cliOptions;
var selectedCompressors;
var processed = 0;

/**
* Last step after minify.
*/

function finalize(results, smallers) {
if (processed === selectedCompressors.length) {
var _table = table(results);

console.log('');
console.log(chalk.bgBlue.black(' INFO '), 'Result');
console.log('');
console.log(_table);
console.log('');
console.log(chalk.bgBlue.black(' INFO '), formatSmallers(smallers));

console.log('\nCompressing with ' + chalk.green(smallers[0].compressor) + ' to ' + chalk.yellow(cliOptions.output));
runOne(cliOptions, smallers[0].compressor, results, smallers).then(function() {
console.log('');
console.log(chalk.bgGreen.black(' DONE '), chalk.green('All done!'));
console.log('');
});
}
}

/**
* Format the output.
*/

function formatSmallers(smallers) {
var sizeGzip = 0;
var output = 'The smallest ' + (smallers.length > 1 ? 'are' : 'is') + ': ';
smallers.forEach(function(item) {
output += chalk.green(item.compressor) + ' ';
sizeGzip = chalk.green(item.sizeGzip);
});
output += 'with ' + sizeGzip;
return output;
}

/**
* Set the smallers compressor result.
*/

function setSmaller(result, smallers) {
if (!smallers[0].sizeGzip) {
smallers[0] = result;
return;
}
if (smallers[0].sizeGzip > result.sizeGzip) {
smallers = [];
smallers[0] = result;
return;
}
if (smallers[0].sizeGzip === result.sizeGzip) {
smallers.push(result);
}
}

/**
* Run one compressor.
*/

function runOne(cli, compressor, results, smallers) {
function runOne(cli, compressor) {
return new Promise(function(resolve, reject) {
var options = {
compressor: compressor || cli.compressor,
input: cli.input.split(','),
output: cli.output
};

spinner.start(compressor || cli.compressor);
spinner.start(options);

return compress(options)
.then(function(result) {
results.push([result.compressor, result.size, result.sizeGzip]);
processed++;
setSmaller(result, smallers);
spinner.stop(compressor);
resolve();
spinner.stop(result);
resolve(result);
})
.catch(function(err) {
spinner.error(compressor);
spinner.error(options);
reject(err);
});
});
Expand All @@ -116,52 +46,25 @@ function runOne(cli, compressor, results, smallers) {
*/

function run(cli) {
var results = [['Compressor', 'Size minified', 'Size Gzipped']];
var smallers = [{ sizeGzip: undefined }];
cliOptions = cli;

console.log('');
console.log(chalk.bgBlue.black(' INFO '), 'Starting compression...');
console.log('');

return new Promise(function(resolve, reject) {
if (cli.compressor === 'all') {
selectedCompressors = all;
} else {
selectedCompressors = cli.compressor.split(',');
selectedCompressors = selectedCompressors.map(item => item.trim().toLowerCase());
deprecate('all is deprecated, babel-minify will be used be default');
console.log('');
cli.compressor = 'babel-minify';
}

const isMultipleCompressors = selectedCompressors.length > 1;

if (isMultipleCompressors) {
var sequence = Promise.resolve();
selectedCompressors.forEach(function(compressor) {
sequence = sequence
.then(function() {
return runOne(cli, compressor, results, smallers);
})
.catch(function() {
processed++;
});
});
sequence
.then(function() {
finalize(results, smallers);
resolve();
})
.catch(reject);
return sequence;
} else {
runOne(cli, cli.compressor, results, smallers)
.then(function() {
console.log('');
console.log(chalk.bgGreen.black(' DONE '), chalk.green('Done!'));
console.log('');
})
.then(resolve)
.catch(reject);
}
runOne(cli, cli.compressor)
.then(function() {
console.log('');
console.log(chalk.bgGreen.black(' DONE '), chalk.green('Done!'));
console.log('');
})
.then(resolve)
.catch(reject);
});
}

Expand Down
19 changes: 13 additions & 6 deletions lib/cli/spinner.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@ module.exports.error = error;

var spinner = ora();

function start(compressor) {
spinner.text = 'Compressing file(s) with ' + chalk.green(compressor) + '...';
function start(options) {
spinner.text = 'Compressing file(s) with ' + chalk.green(options.compressor) + '...';
spinner.start();
}

function stop(compressor) {
spinner.text = 'File(s) compressed successfully with ' + chalk.green(compressor);
function stop(result) {
spinner.text =
'File(s) compressed successfully with ' +
chalk.green(result.compressor) +
' (' +
chalk.green(result.size) +
' minified, ' +
chalk.green(result.sizeGzip) +
' gzipped)';
spinner.succeed();
}

function error(compressor) {
spinner.text = 'Error - file(s) not compressed with ' + chalk.red(compressor);
function error(options) {
spinner.text = 'Error - file(s) not compressed with ' + chalk.red(options.compressor);
spinner.fail();
}
Loading

0 comments on commit 84b1fd7

Please sign in to comment.