Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/rafaelrinaldi/yuglify into …
Browse files Browse the repository at this point in the history
…rafaelrinaldi-master
  • Loading branch information
davglass committed Apr 2, 2013
2 parents 43923fd + a34f6a1 commit c572fbd
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 53 deletions.
96 changes: 75 additions & 21 deletions bin/yuglify
Expand Up @@ -8,46 +8,53 @@ http://yuilibrary.com/license/
var nopt = require('nopt'),
path = require('path'),
fs = require('fs'),
util = require('util'),
pwd = path.resolve(),
exists = fs.existsSync || path.existsSync,
pack = require('../package.json'),
jsmin = require('../').jsmin,
cssmin = require('../').cssmin,
defaults = {
name: '%s.min%s'
},
known = {
type: ['css', 'js'],
output: path,
terminal: Boolean,
help: Boolean,
version: Boolean
version: Boolean,
combine: String
},
shorts = {
'o': ['--output'],
't': ['--type'],
'h': ['--help'],
'v': ['--version']
'v': ['--version'],
'c': ['--combine']
},
filename = defaults.name,
parsed = nopt(known, shorts);

parsed.type = parsed.type || 'js'; //Default to JS files
parsed.type = parsed.type || 'js'; //Default to JS files

if (parsed.version) {
console.log(pack.version);
process.exit(0);
}


if (parsed.help || (!parsed.argv.remain.length && !parsed.terminal)) {
var help = [
'yuglify@' + pack.version,
'',
' yuglify ./foo.js #Auto saves to ./foo-min.js',
' yuglify ./foo/foo.js ./bar/bar.js #Auto saves to ./foo/foo-min.js and ./bar/bar-min.js',
' yuglify ./foo/foo.css ./bar/bar.js #Auto saves to ./foo/foo-min.css and ./bar/bar-min.js',
' yuglify ./foo.js #Auto saves to ./foo.min.js',
' yuglify ./foo/foo.js ./bar/bar.js #Auto saves to ./foo/foo.min.js and ./bar/bar.min.js',
' yuglify ./foo/foo.css ./bar/bar.js #Auto saves to ./foo/foo.min.css and ./bar/bar.min.js',
'',
' --help/-h show this',
' --version/-v show version',
' --type js|css what type of file to compress (used with stdin)',
' --terminal use stdin and stdout instead of files',
' --output <file> use this file when using --terminal',
' --combine/-c <file> compresses and combines all files into a single one.',
' <files to compress>'
];
console.log(help.join('\n'));
Expand All @@ -58,7 +65,7 @@ if (parsed.terminal) {
var stdin = process.openStdin(),
data = '';

stdin.setEncoding("utf8");
stdin.setEncoding('utf8');
stdin.on('data', function(buffer) {
data += buffer;
});
Expand All @@ -77,32 +84,79 @@ if (parsed.terminal) {
});
});
} else {
console.log('compressing..');
console.log(parsed.argv.remain.join(', '));

var out = parsed.out,
console.log('Compressing ' + parsed.argv.remain.join(', ') + '...');

var buffer = {js: '', css: ''},
files = parsed.argv.remain;

//Uglify is blocking, so we will do this in sync..
files.forEach(function(file) {
// Uglify is blocking, so we will do this in sync.
files.forEach(function(file, index) {

file = path.resolve(file);

if (exists(file)) {

var data = fs.readFileSync(file, 'utf8'),
ext = path.extname(file),
out = path.join(path.dirname(file), path.basename(file).replace(ext, '') + '-min' + ext),
compfn = (ext === '.css' ? cssmin : jsmin);
destination = path.dirname(file) + '/',
extension = path.extname(file),
basename = path.basename(file).replace(extension, ''),
compressed = destination.concat(util.format(filename, basename, extension)),
compfn = (extension === '.css' ? cssmin : jsmin);

compfn(data, function(err, smashed) {

if (err) {
throw(err);
process.exit(1);
}
fs.writeFileSync(out, smashed, 'utf8');
console.log('compressed file', out);

if(parsed.combine) {

// Fill the buffer content by its type.
buffer[extension.slice(1)] += smashed;

} else {

fs.writeFileSync(compressed, smashed, 'utf8');

// Those bizarre sets of characters are for coloring the output.
console.log('Successfully generated ' + extension.slice(1).toUpperCase() + ' file: \u001b[32m' + path.relative(pwd, compressed) + '\033[0m');

}

});
} else {
console.log('Failed to find', file);
console.log('Failed to find:', file);
process.exit(1);
}
});
}

if(parsed.combine) {

var combined;

// Looping through kown types.
known.type.forEach(function( type ) {

if(buffer[type] !== '') {

// It uses the default file name (which is main) in case user doesn't give a custom one.
// The file is always generated on the current folder.
combined = util.format(filename, parsed.combine !== 'true' ? parsed.combine : 'main', '.'.concat(type));

fs.writeFileSync(combined, buffer[type].replace(/(\n|\r)/gi, ''), 'utf8');

// Those bizarre sets of characters are for coloring the output.
console.log('Generated combined ' + type.toUpperCase() + ' file: \u001b[32m' + combined + '\033[0m');

}

// Cleaning up buffer.
buffer[type] = '';

});

}

}
66 changes: 34 additions & 32 deletions package.json
@@ -1,35 +1,37 @@
{
"name": "yuglify",
"description": "cli wrapper for uglify and cssmin used by YUI",
"version": "0.1.2",
"dependencies": {
"uglify-js": "~1.3.3",
"ycssmin": "~1.0.0",
"nopt": "*"
},
"devDependencies": {
"yui-lint": "~0.1.1",
"jshint": "~0.9.0",
"vows": "*"
},
"main": "./lib/index.js",
"bin": {
"yuglify": "./bin/yuglify"
},
"scripts": {
"pretest": "jshint --config ./node_modules/yui-lint/jshint.json ./lib/*.js ./bin/yuglify",
"test": "vows --spec ./tests/*.js"
},
"preferGlobal": "true",
"bugs": { "url" : "http://github.com/yui/yuglify/issues" },
"licenses":[
{
"type" : "BSD",
"url" : "https://github.com/yui/yuglify/blob/master/LICENSE"
}
],
"repository": {
"type":"git",
"url":"http://github.com/yui/yuglify.git"
"name": "yuglify",
"description": "cli wrapper for uglify and cssmin used by YUI",
"version": "0.1.2",
"dependencies": {
"uglify-js": "~1.3.4",
"ycssmin": "~1.0.1",
"nopt": "~2.1.1"
},
"devDependencies": {
"yui-lint": "~0.1.1",
"jshint": "~0.9.0",
"vows": "*"
},
"main": "./lib/index.js",
"bin": {
"yuglify": "./bin/yuglify"
},
"scripts": {
"pretest": "jshint --config ./node_modules/yui-lint/jshint.json ./lib/*.js ./bin/yuglify",
"test": "vows --spec ./tests/*.js"
},
"preferGlobal": "true",
"bugs": {
"url": "http://github.com/yui/yuglify/issues"
},
"licenses": [
{
"type": "BSD",
"url": "https://github.com/yui/yuglify/blob/master/LICENSE"
}
],
"repository": {
"type": "git",
"url": "http://github.com/yui/yuglify.git"
}
}

0 comments on commit c572fbd

Please sign in to comment.