Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris committed Feb 11, 2015
0 parents commit a7ca555
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules/
3 changes: 3 additions & 0 deletions .travis.yml
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "0.10"
58 changes: 58 additions & 0 deletions index.js
@@ -0,0 +1,58 @@
var Promise = typeof Promise === 'undefined' ? require('promise') : Promise,
promisify = require('promisify-node'),
globby = require('globby'),
path = require('path'),
eol = require('os').EOL,
globbyPromise = promisify(globby);

function isLess(file) {
return path.extname(file) === '.less';
}

function processPaths(paths) {
return paths.filter(function(path) {
if(!isLess(path)) {
console.warn('Here is non-less file: ' + path + ', ignored');
return false;
}
return true;
});
}

module.exports = {
install: function(less, pluginManager) {
function GlobFileManager() {

}
GlobFileManager.prototype = new less.FileManager();
GlobFileManager.prototype.constructor = GlobFileManager;
GlobFileManager.prototype.supports = function(filename, currentDirectory, options, environment) {
return (filename).indexOf('*') > -1;
};
GlobFileManager.prototype.loadFile = function(filename, currentDirectory, options, environment) {
var self = this;
return Promise.all(options.paths.map(function(basePath) {
return globbyPromise(path.join(currentDirectory, filename), {cwd: basePath});
})).then(function(paths) {
paths = Array.prototype.concat.apply([], paths);
paths = processPaths(paths);
return Promise.all(paths.map(function(file) {
return less.FileManager.prototype.loadFile.call(self, file, currentDirectory, options, environment);
}));
}).then(function(files) {
return {contents: files.map(function(file) {
return file.contents;
}).join(eol), filename: filename};
});
};
GlobFileManager.prototype.loadFileSync = function(filename, currentDirectory, options, environment, encoding) {
var paths = globby.sync(filename, {cwd: currentDirectory});
paths = processPaths(paths);
return paths.map(function(file) {
return less.FileManager.prototype.loadFileSync.call(this, path.basename(file), path.dirname(file), options, environment, encoding);
}, this).join(eol);
};
GlobFileManager.prototype.supportsSync = GlobFileManager.prototype.supports;
pluginManager.addFileManager(new GlobFileManager());
}
};
21 changes: 21 additions & 0 deletions package.json
@@ -0,0 +1,21 @@
{
"name": "less-glob",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"author": "",
"license": "MIT",
"dependencies": {
"globby": "^1.1.0",
"promise": "^6.1.0",
"promisify-node": "^0.1.5"
},
"devDependencies": {
"chai": "^1.10.0",
"less": "^2.4.0",
"mocha": "^2.1.0"
}
}
1 change: 1 addition & 0 deletions test/fixtures/all-less.less
@@ -0,0 +1 @@
@import "one/*.less";
1 change: 1 addition & 0 deletions test/fixtures/include-non-less.less
@@ -0,0 +1 @@
@import "two/**";
1 change: 1 addition & 0 deletions test/fixtures/no-glob.less
@@ -0,0 +1 @@
@import "one/one";
4 changes: 4 additions & 0 deletions test/fixtures/one/one-sub.less
@@ -0,0 +1,4 @@
/* file:one-sub.less */
.one-sub {
color: red;
}
4 changes: 4 additions & 0 deletions test/fixtures/one/one.less
@@ -0,0 +1,4 @@
/* file:one.less */
.one {
color: blue;
}
2 changes: 2 additions & 0 deletions test/fixtures/two/resource.txt
@@ -0,0 +1,2 @@
/* file:resource.txt */
just text resource
4 changes: 4 additions & 0 deletions test/fixtures/two/two.less
@@ -0,0 +1,4 @@
/* file:two.less */
.two {
font-size: 20px;
}
58 changes: 58 additions & 0 deletions test/index.spec.js
@@ -0,0 +1,58 @@
var lessGlob = require('../'),
less = require('less'),
fs = require('fs'),
expect = require('chai').expect;

function readResource(filename) {
return fs.readFileSync(filename, 'utf-8');
}

function assertFilesToBeIncluded(output, files) {
var pattern = "file:([\\w\\.\\-]+)",
includes = output.match(new RegExp(pattern, 'g'));
includes.forEach(function(include) {
include = new RegExp(pattern).exec(include)[1];
expect(files).to.contain(include);
files.splice(files.indexOf(include), 1);
});
expect(files).to.have.length(0, 'all includes should be found');
}

var options = {
paths: ['test/fixtures'],
plugins: [lessGlob]
};

describe('less-glob', function() {
it('should import files by glob', function(done) {
less.render(readResource('test/fixtures/all-less.less'), options)
.then(function(output) {
assertFilesToBeIncluded(output.css, [
'one.less',
'one-sub.less'
]);
})
.then(done, done);
});

it('should ignore non-less files', function(done) {
less.render(readResource('test/fixtures/include-non-less.less'), options)
.then(function(output) {
assertFilesToBeIncluded(output.css, [
'two.less'
]);
expect(output.css).to.not.contain('file:resource.txt');
})
.then(done, done);
});

it('should not break standard imports', function(done) {
less.render(readResource('test/fixtures/no-glob.less'), options)
.then(function(output) {
assertFilesToBeIncluded(output.css, [
'one.less'
]);
})
.then(done, done);
});
});

0 comments on commit a7ca555

Please sign in to comment.