Skip to content
Browse files

Initial commit

  • Loading branch information...
1 parent 25b139e commit 9cff07784aeb12be25ffc3d286ec7a2d390d475e @krasimir committed
Showing with 108 additions and 3 deletions.
  1. +1 −0 .gitignore
  2. +23 −3 README.md
  3. +43 −0 index.js
  4. +13 −0 package.json
  5. +5 −0 tests/app.js
  6. +9 −0 tests/compiled/styles.css
  7. +3 −0 tests/less/footer.less
  8. +3 −0 tests/less/header.less
  9. +8 −0 tests/less/main.less
View
1 .gitignore
@@ -0,0 +1 @@
+node_modules
View
26 README.md
@@ -1,4 +1,24 @@
-less-compiler
-=============
+# less-compiler
-Watch and compile your .less files automatically
+The main purpose of the module is to compile your .less files automatically. It uses [watchr](https://npmjs.org/package/watchr) to monitor a specific folder for changes and [less](https://npmjs.org/package/less) for compilation. There are some other tools for the same purpose ([less-middleware](https://npmjs.org/package/less-middleware), [less-watch](https://npmjs.org/package/less-watch)) out there, but I needed completelly node based thingy - without the usage of the command line.
+
+## Requirements
+[NodeJS](http://nodejs.org/)
+
+## Usage
+
+ require("less-compiler").init({
+ "pathToWatch": [path to your less files],
+ "fileToCompile": [your main less file],
+ "destination": [.css file as a destination]
+ });
+
+example:
+
+ require("less-compiler").init({
+ "pathToWatch": __dirname + "/less",
+ "fileToCompile": __dirname + "/less/main.less",
+ "destination": __dirname + "/compiled/styles.css"
+ });
+
+The usage of absolute paths is recommended.
View
43 index.js
@@ -0,0 +1,43 @@
+var shell = require('shelljs');
+var watchr = require('watchr');
+var less = require("less");
+var fs = require('fs');
+
+exports.init = function(config) {
+
+ if(!config) throw new Error("Please provide configuration.");
+
+ config.lesscPath = config.lesscPath || __dirname + "/node_modules/less/bin/lessc";
+
+ for(var i in config) {
+ if(i != "destination" && !fs.existsSync(config[i])) {
+ throw new Error("Path doesn't exist (" + config[i] + ")");
+ }
+ }
+
+ var watch = {
+ path: config.pathToWatch,
+ listener: function(eventName, filePath, fileCurrentStat, filePreviousStat) {
+ shell.exec(config.lesscPath + ' ' + config.fileToCompile + ' > ' + config.destination, function(code, output) {
+ if(code === 0) {
+ console.log('less compiled successfully');
+ } else {
+ console.log('less compilation failed. ' + output);
+ }
+ });
+ },
+ next: function(err, watcher){
+ if (err) throw err;
+ console.log('less-compiler watching started ...');
+ }
+ };
+
+ if(!config.pathToWatch) throw new Error("Missing config parameter 'pathToWatch'.");
+ if(!config.lesscPath) throw new Error("Missing config parameter 'lesscPath'.");
+ if(!config.fileToCompile) throw new Error("Missing config parameter 'fileToCompile'.");
+ if(!config.destination) throw new Error("Missing config parameter 'destination'.");
+
+ watchr.watch(watch);
+};
+
+
View
13 package.json
@@ -0,0 +1,13 @@
+{
+ "name": "lesscompile",
+ "version": "0.0.0",
+ "description": "compilation of project's less",
+ "main": "index.js",
+ "author": "Krasimir Tsonev",
+ "license": "BSD",
+ "dependencies": {
+ "watchr": "latest",
+ "less": "latest",
+ "shelljs": "latest"
+ }
+}
View
5 tests/app.js
@@ -0,0 +1,5 @@
+require("../index.js").init({
+ "pathToWatch": __dirname + "/less",
+ "fileToCompile": __dirname + "/less/main.less",
+ "destination": __dirname + "/compiled/styles.css"
+});
View
9 tests/compiled/styles.css
@@ -0,0 +1,9 @@
+#header {
+ background: #3960aa;
+}
+#footer {
+ background: #3960aa;
+}
+#main-site-container {
+ background: #3960aa;
+}
View
3 tests/less/footer.less
@@ -0,0 +1,3 @@
+#footer {
+ background: @mainappcolor;
+}
View
3 tests/less/header.less
@@ -0,0 +1,3 @@
+#header {
+ background: @mainappcolor;
+}
View
8 tests/less/main.less
@@ -0,0 +1,8 @@
+@mainappcolor: #3960AA;
+
+@import "header.less";
+@import "footer.less";
+
+#main-site-container {
+ background: @mainappcolor;
+}

0 comments on commit 9cff077

Please sign in to comment.
Something went wrong with that request. Please try again.