Skip to content

Commit

Permalink
Starting project
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Peybernes committed Jul 4, 2016
0 parents commit 161e9ac
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.idea
14 changes: 14 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Copyright (c) 2016 Vincent Peybernes <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# gulp-less-json-import

This is a gulp preprocessor for the less compilation. It provide a directive **@json-import** to import variables
defined in a json file.

It inject the Less formated data in place of the directive in the file buffer without write it on disk.

## Exemple

In less
```Less
@json-import "../relative/path.json";
@json-import "../relative/path/extOmit";
```

In gulp
```javascript
var gulp = require('gulp');
var less = require('gulp-less');
var lessJsonImport = require('gulp-less-json-import');

gulp.src(['./less/**/*.less', '!./less/**/_*.less'])
.pipe(lessJsonImport())
.pipe(less())
.pipe(gulp.dest('./css'));
```
66 changes: 66 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict';

var NAME = 'gulp-less-json-import';

var through2 = require('through2');
var gutil = require('gulp-util');
var StringDecoder = require('string_decoder').StringDecoder;
var Buffer = require('buffer').Buffer;
var path = require('path');
var fs = require('fs');

var PluginError = gutil.PluginError;

var importMatcher = /^\s?@json-import "(.*?)";/;

module.exports = function () {
return through2.obj(function(file, encoding, callback){

if(file.isStream()){
return callback(new PluginError(NAME, 'Stream mode not supported'));
}

var decoder = new StringDecoder(encoding);
var fileDir = path.dirname(file.path);

var content = decoder.write(file.contents).split('\n');

for(var i = 0; i < content.length; i++){
var match = importMatcher.exec(content[i]);
if(!match) continue;

var jsonPath = path.resolve(path.join(fileDir, match[1]));

if(path.parse(jsonPath).ext != '.json'){
jsonPath += '.json';
}

var jsonContent = {};

try{
jsonContent = require(jsonPath);
} catch (e){
gutil.log(NAME, 'can\'t load "'+jsonPath+'"');
}

var lessContent = [];
for(var key in jsonContent){
lessContent.push('@');
lessContent.push(key);
lessContent.push(': ');
lessContent.push(jsonContent[key]);
lessContent.push(';\n');
}

content[i] = lessContent.join('');


}

file.contents = new Buffer(content.join('\n'));

this.push(file);

callback();
});
};
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "gulp-less-json-import",
"version": "1.0.0",
"description": "Import Less variables from json files",
"main": "index.js",
"author":{
"name": "Vincent Peybernes",
"email": "[email protected]"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/Techniv/gulp-less-json-import.git"
},
"dependencies": {
"through2": "^2.0.0",
"gulp-util": "^3.0.6"
},

"engines": {
"node": ">=0.12.0"
}
}

0 comments on commit 161e9ac

Please sign in to comment.