Permalink
Browse files

0.0.2 (refactoring)

  • Loading branch information...
1 parent 23c8ace commit 8925fb8d8502ebf15cf42e68cf1e459819cf86c7 @Panya committed Aug 1, 2011
Showing with 47 additions and 47 deletions.
  1. +1 −1 VERSION
  2. +2 −12 bin/durilka
  3. +43 −33 lib/durilka.js
  4. +1 −1 package.json
View
@@ -1 +1 @@
-Durilka v0.0.1
+Durilka v0.0.2
View
@@ -1,13 +1,3 @@
-#!/usr/bin/env bash
+#!/usr/bin/env node
-SELF_PATH=$(cd -P -- "$(dirname -- "$0")" && pwd -P) && SELF_PATH="$SELF_PATH/$(basename -- "$0")"
-
-while [ -h "$SELF_PATH" ]; do
- DIR=$(dirname -- "$SELF_PATH")
- SYM=$(readlink -- "$SELF_PATH")
- SELF_PATH=$(cd -- "$DIR" && cd -- $(dirname -- "$SYM") && pwd)/$(basename -- "$SYM")
-done
-
-DURILKA_HOME=$(dirname -- "$(dirname -- "$SELF_PATH")")
-
-node $DURILKA_HOME/lib/durilka.js $@
+require('../lib/durilka').main();
View
@@ -11,30 +11,32 @@ var url = require('url'),
'--output', '-o']).known,
srcFile, outFile, baseUrl, src, maxSize;
-if (opts['--version'] || opts['-v']) {
- printFile('VERSION');
-} else if (opts['--help'] || opts['-h']) {
- printFile('USAGE');
-} else {
- if (maxSize = (opts['--max-size'] || opts['-m'])) {
- maxSize = parseInt(maxSize[0], 10);
- }
- maxSize = maxSize || 32;
+exports.main = function() {
+ if (opts['--version'] || opts['-v']) {
+ printFile('VERSION');
+ } else if (opts['--help'] || opts['-h']) {
+ printFile('USAGE');
+ } else {
+ if (maxSize = (opts['--max-size'] || opts['-m'])) {
+ maxSize = parseInt(maxSize[0], 10);
+ }
+ maxSize = maxSize || 32;
- if (baseUrl = (opts['--base-url'] || opts['-b'])) {
- baseUrl = baseUrl[0];
- }
+ if (baseUrl = (opts['--base-url'] || opts['-b'])) {
+ baseUrl = baseUrl[0];
+ }
- if (srcFile = (opts['--input'] || opts['-i'])) {
- srcFile = fs.realpathSync(srcFile[0]);
- src = fs.readFileSync(srcFile).toString();
+ if (srcFile = (opts['--input'] || opts['-i'])) {
+ srcFile = fs.realpathSync(srcFile[0]);
+ src = fs.readFileSync(srcFile).toString();
- encodeImages(src, function(encodedSrc) {
- outFile = (opts['--output'] || opts['-o']);
- fs.writeFileSync(outFile ? outFile[0] : srcFile, encodedSrc);
- });
- } else {
- printFile('USAGE');
+ encodeImages(src, function(encodedSrc) {
+ outFile = (opts['--output'] || opts['-o']);
+ fs.writeFileSync(outFile ? outFile[0] : srcFile, encodedSrc);
+ });
+ } else {
+ printFile('USAGE');
+ }
}
}
@@ -56,22 +58,29 @@ function encodeImages(src, callback) {
}
}
-function encodeImage(path, callback) {
- if (path.length && path.substr(0, 5) != 'data:') {
- var cPath = '';
- if (path.substr(0, 2) == '//') {
- cPath = 'http:' + path;
- } else if (/^https?/.test(path)) {
- cPath = path;
+function normalizeUri(uri, baseUrl) {
+ var normUri = '';
+
+ if (uri.length && uri.substr(0, 5) != 'data:') {
+ if (uri.substr(0, 2) == '//') {
+ normUri = 'http:' + uri;
+ } else if (/^https?/.test(uri)) {
+ normUri = uri;
} else {
- cPath = baseUrl ? baseUrl + path : path;
+ normUri = baseUrl ? baseUrl + uri : uri;
}
- } else {
+ }
+ return normUri;
+}
+
+function encodeImage(uri, callback) {
+ uri = normalizeUri(uri, baseUrl);
+ if (!uri) {
callback && callback();
return;
}
- var urlObj = url.parse(cPath),
+ var urlObj = url.parse(uri),
opts = {
host: urlObj.hostname,
port: urlObj.port || 80,
@@ -89,19 +98,20 @@ function encodeImage(path, callback) {
if (response.statusCode == 200) {
response.body.push(chunk);
} else {
- console.error('Image was not loaded. Status: %s [%s]', response.statusCode, cPath);
+ console.error('Image was not loaded. Status: %s [%s]', response.statusCode, uri);
}
});
response.on('end', function() {
var b = new Buffer(response.body.join(''), 'binary'), data;
if (Buffer.byteLength(b.toString('binary')) > maxSize * 1024) {
- console.warn('Image size greater than %d kilobytes [%s]', maxSize, cPath);
+ console.warn('Image size greater than %d kilobytes [%s]', maxSize, uri);
} else {
data = prefix + b.toString('base64');
}
callback && callback(data);
+ delete response.body;
});
});
}
View
@@ -1,7 +1,7 @@
{
"name" : "durilka",
"description" : "Simple tool for making data URIs from images in CSS files",
- "version" : "0.0.1",
+ "version" : "0.0.2",
"author" : "Mikhail Korepanov <panyakor@gmail.com>",
"homepage": "http://github.com/panya/durilka",
"repository" : {

0 comments on commit 8925fb8

Please sign in to comment.