Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@solidpixels/less-plugin-json

Package Overview
Dependencies
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@solidpixels/less-plugin-json - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

159

FileManager.js
// 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",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc