Permalink
Browse files

Removing vendor sprintf implementation and templating feature. Not it…

… looks more concise.
  • Loading branch information...
1 parent 34c5e3c commit a34f6a143bafe18a5de7727b77def10b9fe9500e Rafael Rinaldi committed Mar 30, 2013
Showing with 51 additions and 165 deletions.
  1. +51 −33 bin/yuglify
  2. +0 −132 lib/sprintf.js
View
@@ -8,30 +8,29 @@ 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,
- sprintf = require('../lib/sprintf.js').sprintf,
defaults = {
- name: '%(name)s.min%(ext)s'
+ name: '%s.min%s'
},
known = {
type: ['css', 'js'],
output: path,
terminal: Boolean,
help: Boolean,
version: Boolean,
- name: String,
- compile: String
+ combine: String
},
shorts = {
'o': ['--output'],
't': ['--type'],
'h': ['--help'],
'v': ['--version'],
- 'n': ['--name'],
- 'c': ['--compile']
+ 'c': ['--combine']
},
filename = defaults.name,
parsed = nopt(known, shorts);
@@ -42,25 +41,20 @@ if (parsed.version) {
process.exit(0);
}
-if(parsed.name) {
- filename = parsed.name;
-}
-
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',
- ' --name/-n <template> custom sprintf-based file name template',
- ' --compile/-c <file> compiles and compresses all files into a single one.',
+ ' --combine/-c <file> compresses and combines all files into a single one.',
' <files to compress>'
];
console.log(help.join('\n'));
@@ -91,14 +85,14 @@ if (parsed.terminal) {
});
} else {
- console.log('Compressing:', parsed.argv.remain.join(', '));
-
- var buffer = '',
- files = parsed.argv.remain;
+ console.log('Compressing ' + parsed.argv.remain.join(', ') + '...');
- //Uglify is blocking, so we will do this in sync..
- files.forEach(function(file) {
+ var buffer = {js: '', css: ''},
+ files = parsed.argv.remain;
+ // Uglify is blocking, so we will do this in sync.
+ files.forEach(function(file, index) {
+
file = path.resolve(file);
if (exists(file)) {
@@ -107,37 +101,61 @@ if (parsed.terminal) {
destination = path.dirname(file) + '/',
extension = path.extname(file),
basename = path.basename(file).replace(extension, ''),
- compressed = destination.concat(sprintf(filename, {name: basename, ext: 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);
}
- if(parsed.compile) {
- buffer += smashed;
+ if(parsed.combine) {
+
+ // Fill the buffer content by its type.
+ buffer[extension.slice(1)] += smashed;
+
} else {
+
fs.writeFileSync(compressed, smashed, 'utf8');
- console.log('Compressed file:', compressed);
+
+ // 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.compile) {
-
- var compiled = parsed.compile === 'true' ? sprintf(filename, {name: 'main', ext: '.js'}) : parsed.compile;
+ 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');
- fs.writeFileSync(compiled, buffer.replace(/(\n|\r)/gi, ''), 'utf8');
- console.log('Single compiled and compressed file:', compiled);
- buffer = '';
+ // 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] = '';
+
+ });
}
View
@@ -1,132 +0,0 @@
-/**
-*
-* Basic sprintf implementation for JavaScript. Created for personal learning purposes.
-* This implementation has string (%s w/ padding), decimal (%d w/ padding) and object (%(foo)s) support.
-*
-* @example
-* console.log(sprintf('http://%s.%s', 'rinaldi', 'nu')); // http://rinaldi.nu
-* console.log(sprintf('%(lorem)s %(ipsum)s', {lorem: 'Lorem', ipsum: 'Ipsum'})); // Lorem Ipsum
-* console.log(sprintf('%30s', 'foo')); // foo
-*
-* @author Rafael Rinaldi (rafaelrinaldi.com)
-* @since Mar 17, 2010
-*
-*/
-
-var raw = '',
- buffer = raw,
- args = [],
- BASIC_REGEX = /%[d|s]|%\d+[d|s]/gi,
- COMPLEX_REGEX = /%(\(([\w_\d]+)\))s/gi;
-
-/**
-* Fill the buffer with a new entry.
-* @method pad
-* @param p_match {String} Match.
-* @param p_substitution {String} Substitution value.
-* @param p_padding {Number} Padding.
-* @private
-*/
-function pad( p_match, p_substitution, p_padding ) {
-
- /** Tip by Miller Medeiros (millermedeiros.com). **/
- var padding = new Array(p_padding + 1).join(' ');
-
- /** Computes the result to string buffer. **/
- buffer = buffer.replace(p_match, padding + p_substitution);
-
-}
-
-/**
-* @method hasBasicFormatter
-* @return {Boolean} True if the second parameter is a number or a string, false otherwise.
-* @private
-*/
-function hasBasicFormatter() {
- return (typeof args[1] === 'string' || typeof args[1] === 'number');
-}
-
-/**
-* @method hasComplexFormatter
-* @return {Boolean} True if the second parameter is a pure object, false otherwise.
-* @private
-*/
-function hasComplexFormatter() {
- return (typeof args[1] === 'object');
-}
-
-/**
-* Process values in the format: %s, %d, %ns and %nd.
-* @method resolveBasic
-* @private
-*/
-function resolveBasic() {
-
- var index = 0,
- matches = raw.match(BASIC_REGEX),
- match,
- substitution,
- padding;
-
- do {
- match = matches[index];
- substitution = args[index + 1];
- padding = parseInt(match.substring(1, match.length - 1), 10) || 0;
-
- pad(match, substitution, padding);
-
- index++;
- }
-
- while(index < matches.length);
-
-}
-
-
-/**
-* Process values in the format: %(complex)s.
-* @method resolveComplex
-* @private
-*/
-function resolveComplex() {
-
- var index = 0,
- matches = raw.match(COMPLEX_REGEX),
- match,
- id,
- substitution;
-
- do {
- match = matches[index];
- id = match.substring(2, match.length - 2);
- substitution = args[1][id];
-
- pad(match, substitution, 0);
-
- index++;
- }
-
- while(index < matches.length);
-
-}
-
-/**
-* @method sprintf
-* @param p_raw {String} String to be sprintfied.
-* @return Sprintfied string.
-*/
-exports.sprintf = function ( p_raw ) {
-
- raw = p_raw;
- buffer = raw;
- args = arguments;
-
- if(hasBasicFormatter()) {
- resolveBasic();
- } else if(hasComplexFormatter()) {
- resolveComplex();
- }
-
- return !raw ? null : buffer;
-
-};

0 comments on commit a34f6a1

Please sign in to comment.