Skip to content

Commit

Permalink
Merge pull request #2 from jackw/develop
Browse files Browse the repository at this point in the history
Updated Image Dimensions to use Eyeglass 0.8.
  • Loading branch information
jackw committed Feb 21, 2016
2 parents 78bcd33 + 5b5dfd7 commit f59108a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 70 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/node_modules
/node_modules
.DS_Store
test/assets/.eyeglass_cache
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- "0.12"
- "4.0"
- "4.1"
- "4.2"
- "5.0"
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[![Build Status](https://travis-ci.org/jackw/eyeglass-image-dimensions.svg?branch=master)](https://travis-ci.org/jackw/eyeglass-image-dimensions)
[![NPM Downloads](https://img.shields.io/npm/dm/eyeglass-image-dimensions.svg)](http://npm-stat.com/charts.html?package=eyeglass-image-dimensions&author=&from=&to=)
[![devDependency Status](https://david-dm.org/jackw/eyeglass-image-dimensions/dev-status.svg)](https://david-dm.org/jackw/eyeglass-image-dimensions#info=devDependencies)
# eyeglass-image-dimensions
An eyeglass module that provides LibSass the same image dimensions functionality found in ruby compass.
An eyeglass module to give LibSass the same image dimension functionality found in ruby compass.

## Setup

Expand Down
37 changes: 5 additions & 32 deletions eyeglass-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@ module.exports = function(eyeglass, sass) {
function ImageDimensions (assetPath, registeredAssets, callback) {
sassUtils.assertType(assetPath, "string");
sassUtils.assertType(registeredAssets, "map");

var self = this;

var getPath = self.checkImagePath(assetPath.getValue(), registeredAssets);
getPath.then(function (success) {

var imageDimensions = self.getDimensions(success);

imageDimensions.then(function (dimensions) {
callback(null, dimensions);
}, function (err) {
callback(err, null);
});

}, function (err) {
callback(err, null);
});
Expand All @@ -43,19 +38,15 @@ module.exports = function(eyeglass, sass) {
*/
ImageDimensions.prototype.getDimensions = function (assetPath) {
return new Promise(function (resolve, reject) {

sizeOf(assetPath, function (err, dimensions) {

if (err) {
return reject(err);
}

return resolve({
height: new sassUtils.SassDimension(dimensions.height, "px"),
width: new sassUtils.SassDimension(dimensions.width, "px")
});
});

});
};

Expand All @@ -66,81 +57,63 @@ module.exports = function(eyeglass, sass) {
* @return {String | Object} Either the path or the error object
*/
ImageDimensions.prototype.checkImagePath = function(assetPath, registeredAssets) {

registeredAssets = sassUtils.castToJs(registeredAssets);

return new Promise(function (resolve, reject) {

// Given an absolute path make sure we're looking in the right location.
if (path.isAbsolute(assetPath)) {
assetPath = path.join(eyeglass.options.root, assetPath);
assetPath = path.join(eyeglass.options.eyeglass.root, assetPath);
}

fs.lstat(assetPath, function(err, stats) {

if (err) {
// can"t find file from path provided so check assets
// can't find file from path provided so check assets
if (err.code === "ENOENT") {
// loop assets and check for a matching file.
registeredAssets.forEach(function(asset) {
asset = sassUtils.castToJs(asset);
var assetData = asset.coerce.get(assetPath);

if (assetData) {
return resolve(assetData.coerce.get("filepath"));
}

});
}

return reject(err);
}

return resolve(assetPath);
});

});

};

return {
sassDir: path.join(__dirname, "sass"),
functions: {
"eg-image-width($assetPath, $registeredAssets)":
function(assetPath, registeredAssets, done) {

var imageDim = new ImageDimensions(assetPath, registeredAssets, function(err, dimensions) {
new ImageDimensions(assetPath, registeredAssets, function(err, dimensions) {
if (err) {
done(sass.types.Error(err.message));
}
done(sassUtils.castToSass(dimensions.width));
});

},
"eg-image-height($assetPath, $registeredAssets)":
function(assetPath, registeredAssets, done) {

var imageDim = new ImageDimensions(assetPath, registeredAssets, function(err, dimensions) {
new ImageDimensions(assetPath, registeredAssets, function(err, dimensions) {
if (err) {
done(sass.types.Error(err.message));
}
done(sassUtils.castToSass(dimensions.height));
});

},
"eg-image-dimensions($assetPath, $registeredAssets)":
function(assetPath, registeredAssets, done) {

var imageDim = new ImageDimensions(assetPath, registeredAssets, function(err, dimensions) {
new ImageDimensions(assetPath, registeredAssets, function(err, dimensions) {
if (err) {
done(sass.types.Error(err.message));
}
dimensions = sassUtils.castToSass([dimensions.width, dimensions.height]);
dimensions.setSeparator(false);

done(dimensions);
});

}
}
};
Expand Down
25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "eyeglass-image-dimensions",
"description": "An eyeglass module that provides libsass the same image dimensions functionality found in ruby compass.",
"version": "0.1.1",
"version": "0.2.0",
"author": {
"name": "Jack Westbrook",
"email": "[email protected]",
Expand All @@ -16,26 +16,27 @@
"keywords": [
"sass",
"eyeglass-module",
"image dimensions"
"image dimensions",
"image size"
],
"eyeglass": {
"name": "image-dimensions",
"exports": "eyeglass-exports.js",
"needs": "^0.7.0"
"needs": "^0.8.0"
},
"devDependencies": {
"eyeglass-dev-eslint": "^1.0.1",
"eyeglass": "^0.8.0",
"eyeglass-dev-eslint": "^2.0.0",
"gulp": "^3.9.0",
"gulp-eslint": "^0.15.0",
"gulp-mocha": "^2.1.3",
"node-sass": "^3.2.0",
"should": "^7.0.2"
"gulp-eslint": "^1.1.1",
"gulp-mocha": "^2.2.0",
"node-sass": "^3.4.0",
"should": "^8.2.0"
},
"dependencies": {
"eyeglass": "*",
"image-size": "^0.3.5",
"node-sass-utils": "^1.1.1",
"promise": "^7.0.3"
"image-size": "^0.4.0",
"node-sass-utils": "^1.1.0",
"promise": "^7.1.0"
},
"scripts": {
"test": "./node_modules/.bin/gulp test"
Expand Down
61 changes: 37 additions & 24 deletions test/test_dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var fs = require("fs");
var path = require('path');
var sass = require("node-sass");
var Eyeglass = require("eyeglass").Eyeglass;
var eyeglass = require("eyeglass");
var assert = require("assert");


Expand All @@ -19,13 +19,17 @@ describe("image dimensions module", function () {
" background-height: 550px;\n" +
" background-width: 400px; }\n";

var eyeglass = new Eyeglass({
root: path.join(__dirname, 'assets'),
data: sassInput
}, sass);

sass.render(eyeglass.sassOptions(), function(error, result) {
// console.log(result.css.toString());
var options = {
data: sassInput,
eyeglass: {
engines: {
sass: sass
},
root: path.join(__dirname, 'assets')
}
};

sass.render(eyeglass(options), function(error, result) {
assert(!error, error && error.message);
assert.equal(cssOutput, result.css.toString());
done();
Expand All @@ -44,12 +48,17 @@ describe("image dimensions module", function () {
" background-height: 300px;\n" +
" background-width: 150px; }\n";

var eyeglass = new Eyeglass({
root: path.join(__dirname, 'assets'),
data: sassInput
}, sass);

sass.render(eyeglass.sassOptions(), function(error, result) {
var options = {
data: sassInput,
eyeglass: {
engines: {
sass: sass
},
root: path.join(__dirname, 'assets')
}
};

sass.render(eyeglass(options), function(error, result) {
assert(!error, error && error.message);
assert.equal(cssOutput, result.css.toString());
done();
Expand All @@ -70,21 +79,25 @@ describe("image dimensions module", function () {

var rootDir = path.join(__dirname, 'assets');

var eyeglass = new Eyeglass({
root: rootDir,
data: sassInput
}, sass);

eyeglass.assets.addSource(path.join(rootDir, 'images'));

sass.render(eyeglass.sassOptions(), function(error, result) {
var options = {
data: sassInput,
eyeglass: {
engines: {
sass: sass
},
root: rootDir,
assets: {
sources: [{directory: path.join(rootDir, 'images')}]
}
}
};

sass.render(eyeglass(options), function(error, result) {
assert(!error, error && error.message);
assert.equal(cssOutput, result.css.toString());
done();
});

});



});

0 comments on commit f59108a

Please sign in to comment.