diff --git a/bin/yuglify b/bin/yuglify index 65e5bf0..85c0b23 100755 --- a/bin/yuglify +++ b/bin/yuglify @@ -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 use this file when using --terminal', + ' --combine/-c compresses and combines all files into a single one.', ' ' ]; console.log(help.join('\n')); @@ -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; }); @@ -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] = ''; + + }); + + } + +} \ No newline at end of file diff --git a/package.json b/package.json index 3efc3da..cec7a90 100644 --- a/package.json +++ b/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" + } }