gulp-json-transform
Advanced tools
Comparing version 0.4.8 to 0.5.0
49
index.js
@@ -1,11 +0,10 @@ | ||
var Promise = require('promise'); | ||
var through = require('through2'); | ||
var log = require('fancy-log'); | ||
var PluginError = require('plugin-error'); | ||
var colors = require('ansi-colors'); | ||
var through = require("through2"); | ||
var log = require("fancy-log"); | ||
var PluginError = require("plugin-error"); | ||
var colors = require("ansi-colors"); | ||
const PLUGIN_NAME = 'gulp-json-transform'; | ||
const PLUGIN_NAME = "gulp-json-transform"; | ||
function jsonPromiseParse(rawStr) { | ||
return new Promise(function(resolve, reject) { | ||
return new Promise(function (resolve, reject) { | ||
var json; | ||
@@ -15,3 +14,3 @@ try { | ||
} catch (e) { | ||
return reject(new Error('Invalid JSON: ' + e.message)); | ||
return reject(new Error("Invalid JSON: " + e.message)); | ||
} | ||
@@ -22,12 +21,15 @@ resolve(json); | ||
module.exports = function(transformFn, jsonSpace) { | ||
module.exports = function (transformFn, jsonSpace) { | ||
if (!transformFn) { | ||
throw new PluginError(PLUGIN_NAME, 'Missing transform function!'); | ||
throw new PluginError(PLUGIN_NAME, "Missing transform function!"); | ||
} | ||
return through.obj(function(file, enc, cb) { | ||
return through.obj(function (file, enc, cb) { | ||
var self = this; | ||
if (file.isStream()) { | ||
return self.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported')); | ||
return self.emit( | ||
"error", | ||
new PluginError(PLUGIN_NAME, "Streaming not supported") | ||
); | ||
} | ||
@@ -39,25 +41,24 @@ | ||
jsonPromiseParse(fileContent) | ||
.then(function(data){ | ||
.then(function (data) { | ||
return transformFn(data, { | ||
path: file.path, | ||
relative: file.relative, | ||
base: file.base | ||
base: file.base, | ||
}); | ||
}) | ||
.then(function(output) { | ||
var isString = (typeof output === 'string'); | ||
file.contents = new Buffer(isString ? output : JSON.stringify(output, null, jsonSpace)); | ||
.then(function (output) { | ||
var isString = typeof output === "string"; | ||
file.contents = Buffer.from( | ||
isString ? output : JSON.stringify(output, null, jsonSpace) | ||
); | ||
self.push(file); | ||
cb(); | ||
}) | ||
.catch(function(e) { | ||
log(PLUGIN_NAME + ':', colors.red(e.message)); | ||
self.emit('error', new PluginError(PLUGIN_NAME, e)); | ||
self.emit('end'); | ||
.catch(function (e) { | ||
log(PLUGIN_NAME + ":", colors.red(e.message)); | ||
self.emit("error", new PluginError(PLUGIN_NAME, e)); | ||
self.emit("end"); | ||
}); | ||
} | ||
}); | ||
}; |
{ | ||
"name": "gulp-json-transform", | ||
"version": "0.4.8", | ||
"version": "0.5.0", | ||
"description": "A gulp plugin to transform json files, pipe json files through it and transform them to other json files or other formats.", | ||
@@ -28,3 +28,3 @@ "main": "./index.js", | ||
"jshint": "^2.9.6", | ||
"mocha": "^9.2.0", | ||
"mocha": "^10.3.0", | ||
"should": "^13.1.2" | ||
@@ -36,3 +36,2 @@ }, | ||
"plugin-error": "^1.0.1", | ||
"promise": "^8.0.1", | ||
"through2": "^2.0.3", | ||
@@ -39,0 +38,0 @@ "vinyl": "^2.1.0" |
156
test/main.js
@@ -1,77 +0,109 @@ | ||
'use strict'; | ||
"use strict"; | ||
var jsonTransform = require('../'); | ||
var Promise = require('promise'); | ||
var jsonTransform = require("../"); | ||
var should = require('should'); | ||
require('mocha'); | ||
var should = require("should"); | ||
require("mocha"); | ||
var Vinyl = require('vinyl'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var Vinyl = require("vinyl"); | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var jshint = require('jshint'); | ||
var jshint = require("jshint"); | ||
describe('gulp-json-transform', function () { | ||
describe("gulp-json-transform", function () { | ||
var testTransform = function (inputFile, transformFn, expected) { | ||
var inputJson = new Vinyl({ | ||
path: "test/fixtures/" + inputFile, | ||
cwd: "test/", | ||
base: "test/fixtures", | ||
contents: fs.readFileSync("test/fixtures/" + inputFile), | ||
}); | ||
var testTransform = function (inputFile, transformFn, expected) { | ||
var inputJson = new Vinyl({ | ||
path: 'test/fixtures/' + inputFile, | ||
cwd: 'test/', | ||
base: 'test/fixtures', | ||
contents: fs.readFileSync('test/fixtures/' + inputFile) | ||
}); | ||
return function (done) { | ||
var stream = jsonTransform(transformFn); | ||
return function (done) { | ||
var stream = jsonTransform(transformFn); | ||
stream.on("error", function (err) { | ||
should.exist(err); | ||
done(err); | ||
}); | ||
stream.on('error', function(err) { | ||
should.exist(err); | ||
done(err); | ||
}); | ||
stream.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
var newFilePath = path.resolve(newFile.path); | ||
var expectedFilePath = path.resolve("test/fixtures/" + inputFile); | ||
newFilePath.should.equal(expectedFilePath); | ||
newFile.relative.should.equal(inputFile); | ||
String(newFile.contents).should.equal(expected); | ||
Buffer.isBuffer(newFile.contents).should.equal(true); | ||
done(); | ||
}); | ||
stream.on('data', function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
var newFilePath = path.resolve(newFile.path); | ||
var expectedFilePath = path.resolve('test/fixtures/' + inputFile); | ||
newFilePath.should.equal(expectedFilePath); | ||
newFile.relative.should.equal(inputFile); | ||
String(newFile.contents).should.equal(expected); | ||
Buffer.isBuffer(newFile.contents).should.equal(true); | ||
done(); | ||
}); | ||
stream.write(inputJson); | ||
stream.end(); | ||
}; | ||
}; | ||
stream.write(inputJson); | ||
stream.end(); | ||
}; | ||
}; | ||
it( | ||
"should transform a json file to a json file", | ||
testTransform( | ||
"input.json", | ||
function (data) { | ||
return { foobar: data.foo + data.bar }; | ||
}, | ||
'{"foobar":"[foo][bar]"}' | ||
) | ||
); | ||
it('should transform a json file to a json file', testTransform('input.json', function(data) { | ||
return {foobar: data.foo + data.bar}; | ||
}, '{"foobar":"[foo][bar]"}')); | ||
it( | ||
"should transform a json file to a text file", | ||
testTransform( | ||
"input.json", | ||
function (data) { | ||
return data.foo + data.bar; | ||
}, | ||
"[foo][bar]" | ||
) | ||
); | ||
it('should transform a json file to a text file', testTransform('input.json', function(data) { | ||
return data.foo + data.bar; | ||
}, '[foo][bar]')); | ||
it( | ||
"should accept the file as a parameter to the transform function", | ||
testTransform( | ||
"input.json", | ||
function (data, file) { | ||
var fileProps = ["path", "relative", "base"]; | ||
for (var fileProp in file) { | ||
if (file.hasOwnProperty(fileProp)) { | ||
var index = fileProps.indexOf(fileProp); | ||
should.ok( | ||
index >= 0, | ||
"file object has illegal property: " + fileProp | ||
); | ||
if (index >= 0) { | ||
fileProps.splice(index, 1); | ||
} | ||
} | ||
} | ||
should.ok( | ||
fileProps.length === 0, | ||
"file object is missing properties: " + JSON.stringify(fileProps) | ||
); | ||
it('should accept the file as a parameter to the transform function', testTransform('input.json', function(data, file) { | ||
var fileProps = ['path', 'relative', 'base']; | ||
for (var fileProp in file) { | ||
if (file.hasOwnProperty(fileProp)) { | ||
var index = fileProps.indexOf(fileProp); | ||
should.ok(index >= 0, 'file object has illegal property: ' + fileProp); | ||
if (index >= 0) { | ||
fileProps.splice(index, 1); | ||
} | ||
} | ||
} | ||
should.ok(fileProps.length === 0, 'file object is missing properties: ' + JSON.stringify(fileProps)); | ||
return file.relative + " - " + data.foo + data.bar; | ||
}, | ||
"input.json - [foo][bar]" | ||
) | ||
); | ||
return file.relative + ' - ' + data.foo + data.bar; | ||
}, 'input.json - [foo][bar]')); | ||
it('should accept promises', testTransform('input.json', function(data) { | ||
return Promise.resolve(data.foo + data.bar); | ||
}, '[foo][bar]')); | ||
it( | ||
"should accept promises", | ||
testTransform( | ||
"input.json", | ||
function (data) { | ||
return Promise.resolve(data.foo + data.bar); | ||
}, | ||
"[foo][bar]" | ||
) | ||
); | ||
}); |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
11837
5
152
- Removedpromise@^8.0.1
- Removedasap@2.0.6(transitive)
- Removedpromise@8.3.0(transitive)