Skip to content

Commit

Permalink
Merge pull request #26 from stefanpenner/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
jasonmit committed May 12, 2015
2 parents ba51411 + 6d47d2d commit 3c3e89e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 55 deletions.
47 changes: 16 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,12 @@ var mkdirp = require('mkdirp')
var includePathSearcher = require('include-path-searcher')
var CachingWriter = require('broccoli-caching-writer')
var less = require('less')
var _ = require('lodash')
var merge = require('lodash-node/modern/object/merge')
var RSVP = require('rsvp');

function writeFilePromise(filename, data, options) {
return new RSVP.Promise(function(resolve, reject) {
fs.writeFile(filename, data, options, function (err) {
if (err) {
return reject(err);
}
return resolve(data);
});
});
}
var writeFile = RSVP.denodeify(fs.writeFile);

module.exports = LessCompiler;

LessCompiler.prototype = Object.create(CachingWriter.prototype)
LessCompiler.prototype.constructor = LessCompiler

Expand Down Expand Up @@ -55,30 +46,24 @@ LessCompiler.prototype.updateCache = function (srcDir, destDir) {
paths: srcDir
}

_.merge(lessOptions, this.lessOptions)
merge(lessOptions, this.lessOptions)

lessOptions.paths = [path.dirname(lessOptions.filename)].concat(lessOptions.paths);
data = fs.readFileSync(lessOptions.filename, 'utf8');

return new RSVP.Promise(function(resolve, reject) {

less.render(data, lessOptions).then(function (output) {

var fileWriterPromises = [ writeFilePromise(destFile, output.css, { encoding: 'utf8' }) ];
return less.render(data, lessOptions).
catch(function(err) {
less.writeError(err, lessOptions);
throw err;
}).
then(function (output) {
var fileWriterPromises = [writeFile(destFile, output.css, { encoding: 'utf8' }) ];
var sourceMapURL = lessOptions.sourceMap && lessOptions.sourceMap.sourceMapURL;

if (sourceMapURL) {
fileWriterPromises.push( writeFilePromise(destDir + '/' + sourceMapURL, output.map, { encoding: 'utf8' }) );
fileWriterPromises.push( writeFile(destDir + '/' + sourceMapURL, output.map, { encoding: 'utf8' }) );
}

RSVP.all(fileWriterPromises).then(function() {
return resolve(output);
}, function(err) {
return reject(err);
});

}, function (err) {
less.writeError(err, lessOptions);
reject(err);
});

});
return RSVP.Promise.all(fileWriterPromises);
})
}
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@
"css"
],
"scripts": {
"test": "mocha test/runner.js --reporter spec"
"test": "mocha test/runner.js --reporter spec",
"test:debug": "mocha debug test/runner.js --reporter spec"
},
"dependencies": {
"mkdirp": "^0.5.0",
"lodash": "^2.4.1",
"broccoli-caching-writer": "^0.5.4",
"include-path-searcher": "^0.1.0",
"less": "^2.5.0",
"rsvp": "^3.0.17",
"broccoli-caching-writer": "^0.5.4"
"lodash-node": "^3.8.0",
"mkdirp": "^0.5.0",
"rsvp": "^3.0.17"
},
"readmeFilename": "README.md",
"bugs": {
Expand All @@ -49,6 +50,6 @@
"homepage": "https://github.com/gabrielgrant/broccoli-less-single",
"devDependencies": {
"broccoli": "^0.13.3",
"mocha": "^2.2.1"
"mocha": "^2.2.4"
}
}
19 changes: 9 additions & 10 deletions test/tests/compare-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@ var read = require('../utils/read')
var lessCompile = require('../utils/build');
var expected = path.join(__dirname, '..', 'expected');

function compare (name, done, lessOptions) {
lessCompile('../less', name + '.less', name + '.css', lessOptions).then(function (result) {
function compare (name, lessOptions) {
return lessCompile('../less', name + '.less', name + '.css', lessOptions).then(function (result) {
assert.equal(result.css, read(path.join(expected, result.outputFile)));
done();
}).catch(done);
});
}

describe('lessCompiler', function () {
it('basic less preprocessing', function (done) {
compare('basic', done);
it('basic less preprocessing', function () {
return compare('basic');
});

it('import statements functioning', function (done) {
compare('import', done);
it('import statements functioning', function () {
return compare('import');
});

it('`lessOption` discovers paths', function (done) {
compare('paths', done, {
it('`lessOption` discovers paths', function () {
return compare('paths', {
paths: ['../less/branch']
});
});
Expand Down
14 changes: 6 additions & 8 deletions test/utils/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ module.exports = function (inputTrees, inputFile, outputFile, lessOptions) {

var less = compileLess.apply(this, arguments);

return new RSVP.Promise(function (resolve, reject) {
return new broccoli.Builder(less).build().then(function (results) {
resolve({
css: read(path.join(results.directory, outputFile)),
directory: results.directory,
outputFile: outputFile
});
});
return new broccoli.Builder(less).build().then(function (results) {
return {
css: read(path.join(results.directory, outputFile)),
directory: results.directory,
outputFile: outputFile
};
});
}

0 comments on commit 3c3e89e

Please sign in to comment.