@solidpixels/less-plugin-json
Advanced tools
Comparing version 0.2.0 to 0.3.0
// lessc style.less --plugin=index.js | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const less = require('less'); | ||
const FileManager = function() {}; | ||
module.exports = function(less) { | ||
const FileManager = function() {}; | ||
FileManager.prototype = new less.FileManager(); | ||
FileManager.prototype.stringify = function(obj) { | ||
if (!obj) { | ||
throw new Error('Undefined variable'); | ||
} | ||
// LESS does not support recursive maps | ||
if (obj instanceof Array) { | ||
return obj.join(','); | ||
} | ||
return `{${Object.keys(obj).map((key) => `${key}:${obj[key]}`).join(';')}}`; | ||
}; | ||
FileManager.prototype.transformVariables = function(obj) { | ||
const result = []; | ||
const transform = (innerObj, prefix = []) => { | ||
if (prefix[0] && prefix[0].substr(0, 1) === '$') { | ||
result.push(`@${prefix[0].substr(1)}:${this.stringify(innerObj)};`); | ||
return; | ||
FileManager.prototype = new less.FileManager(); | ||
FileManager.prototype.stringify = function(obj) { | ||
if (!obj) { | ||
throw new Error('Undefined variable'); | ||
} | ||
if (innerObj && typeof innerObj === 'object') { | ||
for (let i = 0, keys = Object.keys(innerObj); i < keys.length; i++) { | ||
const key = keys[i]; | ||
transform(innerObj[key], prefix.concat(key)) | ||
// LESS does not support recursive maps | ||
if (obj instanceof Array) { | ||
return obj.join(','); | ||
} | ||
return `{${Object.keys(obj).map((key) => `${key}:${obj[key]}`).join(';')}}`; | ||
}; | ||
FileManager.prototype.transformVariables = function(obj) { | ||
const result = []; | ||
const transform = (innerObj, prefix = []) => { | ||
if (prefix[0] && prefix[0].substr(0, 1) === '$') { | ||
result.push(`@${prefix[0].substr(1)}:${this.stringify(innerObj)};`); | ||
return; | ||
} | ||
if (innerObj && typeof innerObj === 'object') { | ||
for (let i = 0, keys = Object.keys(innerObj); i < keys.length; i++) { | ||
const key = keys[i]; | ||
transform(innerObj[key], prefix.concat(key)) | ||
} | ||
return; | ||
} | ||
result.push(`@${prefix.join('--')}:${innerObj};`); | ||
return; | ||
} | ||
result.push(`@${prefix.join('--')}:${innerObj};`); | ||
return; | ||
transform(obj); | ||
return result; | ||
}; | ||
FileManager.prototype.getPath = function(filename, currentDirectory) { | ||
if (path.isAbsolute(filename)) { | ||
return filename; | ||
} | ||
return currentDirectory ? path.join(currentDirectory, filename) : filename; | ||
} | ||
transform(obj); | ||
return result; | ||
}; | ||
FileManager.prototype.getPath = function(filename, currentDirectory) { | ||
if (path.isAbsolute(filename)) { | ||
return filename; | ||
} | ||
return currentDirectory ? path.join(currentDirectory, filename) : filename; | ||
} | ||
FileManager.prototype.loadFile = function(filename, currentDirectory, options, environment) { | ||
// Check filename again, some environments (ie. Gulp) skip `supports` method. | ||
if (!filename.match(/\.json?$/)) { | ||
return false; | ||
} | ||
return new Promise((resolve, reject) => { | ||
let contents; | ||
FileManager.prototype.loadFile = function(filename, currentDirectory, options, environment) { | ||
// Check filename again, some environments (ie. Gulp) skip `supports` method. | ||
if (!filename.match(/\.json?$/)) { | ||
return false; | ||
} | ||
return new Promise((resolve, reject) => { | ||
let contents; | ||
try { | ||
const fileContent = require(this.getPath(filename, currentDirectory)); | ||
contents = this.transformVariables(fileContent).join(''); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
if (contents) { | ||
resolve({ contents, filename }); | ||
} else { | ||
reject(new Error('Can\'t generate JSON variables file.')) | ||
} | ||
}); | ||
}; | ||
FileManager.prototype.supports = function(filename, currentDirectory, options, environment) { | ||
if (!filename.match(/\.json?$/)) { | ||
return false; | ||
} | ||
try { | ||
const fileContent = require(this.getPath(filename, currentDirectory)); | ||
contents = this.transformVariables(fileContent).join(''); | ||
const stat = fs.statSync(this.getPath(filename, currentDirectory)); | ||
return stat.isFile(); | ||
} catch (err) { | ||
reject(err); | ||
console.log(err); | ||
} | ||
if (contents) { | ||
resolve({ contents, filename }); | ||
} else { | ||
reject(new Error('Can\'t generate JSON variables file.')) | ||
} | ||
}); | ||
}; | ||
FileManager.prototype.supports = function(filename, currentDirectory, options, environment) { | ||
if (!filename.match(/\.json?$/)) { | ||
return false; | ||
} | ||
}; | ||
FileManager.prototype.tryAppendExtension = function(filename, ext) { | ||
return filename; | ||
}; | ||
try { | ||
const stat = fs.statSync(this.getPath(filename, currentDirectory)); | ||
return stat.isFile(); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
return false; | ||
return FileManager; | ||
}; | ||
FileManager.prototype.tryAppendExtension = function(filename, ext) { | ||
return filename; | ||
}; | ||
module.exports = FileManager; |
// lessc style.less --plugin=index.js | ||
const FileManager = require('./FileManager.js'); | ||
const getFileManager = require('./FileManager.js'); | ||
@@ -7,4 +7,5 @@ module.exports = { | ||
install: function(less, pluginManager) { | ||
const FileManager = getFileManager(less); | ||
pluginManager.addFileManager(new FileManager()); | ||
}, | ||
}; |
{ | ||
"name": "@solidpixels/less-plugin-json", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Allow LESS to import JSON files", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4503
88