From a7ca555f19501b9b6e47af50f640133e105b0fe9 Mon Sep 17 00:00:00 2001 From: just-boris Date: Wed, 11 Feb 2015 17:16:16 +0300 Subject: [PATCH] initial commit --- .gitignore | 1 + .travis.yml | 3 ++ index.js | 58 +++++++++++++++++++++++++++++ package.json | 21 +++++++++++ test/fixtures/all-less.less | 1 + test/fixtures/include-non-less.less | 1 + test/fixtures/no-glob.less | 1 + test/fixtures/one/one-sub.less | 4 ++ test/fixtures/one/one.less | 4 ++ test/fixtures/two/resource.txt | 2 + test/fixtures/two/two.less | 4 ++ test/index.spec.js | 58 +++++++++++++++++++++++++++++ 12 files changed, 158 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 index.js create mode 100644 package.json create mode 100644 test/fixtures/all-less.less create mode 100644 test/fixtures/include-non-less.less create mode 100644 test/fixtures/no-glob.less create mode 100644 test/fixtures/one/one-sub.less create mode 100644 test/fixtures/one/one.less create mode 100644 test/fixtures/two/resource.txt create mode 100644 test/fixtures/two/two.less create mode 100644 test/index.spec.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..87f8cd9 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: node_js +node_js: + - "0.10" \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..386e848 --- /dev/null +++ b/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()); + } +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..728f38b --- /dev/null +++ b/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" + } +} diff --git a/test/fixtures/all-less.less b/test/fixtures/all-less.less new file mode 100644 index 0000000..0c2c57e --- /dev/null +++ b/test/fixtures/all-less.less @@ -0,0 +1 @@ +@import "one/*.less"; \ No newline at end of file diff --git a/test/fixtures/include-non-less.less b/test/fixtures/include-non-less.less new file mode 100644 index 0000000..bb7f917 --- /dev/null +++ b/test/fixtures/include-non-less.less @@ -0,0 +1 @@ +@import "two/**"; \ No newline at end of file diff --git a/test/fixtures/no-glob.less b/test/fixtures/no-glob.less new file mode 100644 index 0000000..d498fb2 --- /dev/null +++ b/test/fixtures/no-glob.less @@ -0,0 +1 @@ +@import "one/one"; \ No newline at end of file diff --git a/test/fixtures/one/one-sub.less b/test/fixtures/one/one-sub.less new file mode 100644 index 0000000..e176c5d --- /dev/null +++ b/test/fixtures/one/one-sub.less @@ -0,0 +1,4 @@ +/* file:one-sub.less */ +.one-sub { + color: red; +} \ No newline at end of file diff --git a/test/fixtures/one/one.less b/test/fixtures/one/one.less new file mode 100644 index 0000000..33723ca --- /dev/null +++ b/test/fixtures/one/one.less @@ -0,0 +1,4 @@ +/* file:one.less */ +.one { + color: blue; +} \ No newline at end of file diff --git a/test/fixtures/two/resource.txt b/test/fixtures/two/resource.txt new file mode 100644 index 0000000..e702d9a --- /dev/null +++ b/test/fixtures/two/resource.txt @@ -0,0 +1,2 @@ +/* file:resource.txt */ +just text resource \ No newline at end of file diff --git a/test/fixtures/two/two.less b/test/fixtures/two/two.less new file mode 100644 index 0000000..d98ce25 --- /dev/null +++ b/test/fixtures/two/two.less @@ -0,0 +1,4 @@ +/* file:two.less */ +.two { + font-size: 20px; +} \ No newline at end of file diff --git a/test/index.spec.js b/test/index.spec.js new file mode 100644 index 0000000..cfbdbcf --- /dev/null +++ b/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); + }); +}); \ No newline at end of file