btrz-pact-s3
Advanced tools
Comparing version 0.1.0 to 0.2.0
212
index.js
"use strict"; | ||
const AWS = require("aws-sdk"), | ||
fs = require("fs"), | ||
path = require('path'); | ||
fs = require("fs"), | ||
path = require('path'); | ||
@@ -22,45 +22,49 @@ class BtrzPactS3 { | ||
} | ||
if (!logger || !logger.error) { | ||
throw new Error("BtrzPactS3 logger is mandatory.") | ||
if (logger && !logger.error) { | ||
throw new Error("BtrzPactS3 logger is not valid.") | ||
} | ||
this.accessKeyId = options.accessKeyId; | ||
this.secretAccessKey = options.secretAccessKey; | ||
this.bucket = options.bucket; | ||
this.logger = logger; | ||
} | ||
this.accessKeyId = options.accessKeyId; | ||
this.secretAccessKey = options.secretAccessKey; | ||
this.bucket = options.bucket; | ||
this.logger = logger; | ||
} | ||
publishSinglePact(pact, _s3Client_) { | ||
return this.getFileKey(pact) | ||
.then((key) => { | ||
publishSinglePact(pact, _s3Client_) { | ||
return this.getFileKey(pact) | ||
.then((key) => { | ||
if (this.logger) { | ||
this.logger.info(`BtrzPactS3::publishPacts() - Uploading ${key}`); | ||
} | ||
let s3Client = _s3Client_ || new AWS.S3({ | ||
accessKeyId: this.accessKeyId, | ||
secretAccessKey: this.secretAccessKey | ||
}); | ||
let self = this; | ||
function resolver(resolve, reject) { | ||
s3Client.putObject( | ||
{ | ||
Bucket: self.bucket, | ||
Key: key, | ||
ACL: "public-read", | ||
Body: fs.createReadStream(pact) | ||
}, | ||
function (err, result) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(); | ||
} | ||
); | ||
} | ||
return new Promise(resolver); | ||
}) | ||
.catch((err) => { | ||
this.logger.error("Error on BtrzPactS3::publishSinglePact()", err); | ||
throw err; | ||
}); | ||
let s3Client = _s3Client_ || new AWS.S3({ | ||
accessKeyId: this.accessKeyId, | ||
secretAccessKey: this.secretAccessKey | ||
}); | ||
let self = this; | ||
function resolver(resolve, reject) { | ||
s3Client.putObject( | ||
{ | ||
Bucket: self.bucket, | ||
Key: key, | ||
ACL: "public-read", | ||
Body: fs.createReadStream(pact) | ||
}, | ||
function (err, result) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(); | ||
} | ||
); | ||
} | ||
return new Promise(resolver); | ||
}) | ||
.catch((err) => { | ||
if (this.logger) { | ||
this.logger.error("Error on BtrzPactS3::publishSinglePact()", err); | ||
} | ||
throw err; | ||
}); | ||
} | ||
@@ -72,39 +76,39 @@ | ||
var walk = function(dir, cb) { | ||
var results = []; | ||
fs.stat(dir, (err, stat) => { | ||
if (stat && stat.isFile()) { | ||
results.push(dir); | ||
return cb(null, results); | ||
} else { | ||
fs.readdir(dir, (err, list) => { | ||
if (err) { | ||
return cb(err); | ||
} | ||
var results = []; | ||
fs.stat(dir, (err, stat) => { | ||
if (stat && stat.isFile()) { | ||
results.push(dir); | ||
return cb(null, results); | ||
} else { | ||
fs.readdir(dir, (err, list) => { | ||
if (err) { | ||
return cb(err); | ||
} | ||
var pending = list.length; | ||
if (!pending) { | ||
return cb(null, results); | ||
} | ||
list.forEach(function(file) { | ||
file = path.resolve(dir, file); | ||
fs.stat(file, (err, stat) => { | ||
if (stat && stat.isDirectory()) { | ||
walk(file, (err, res) => { | ||
results = results.concat(res); | ||
if (!--pending) { | ||
return cb(null, results); | ||
} | ||
}); | ||
} else { | ||
results.push(file); | ||
if (!--pending) { | ||
return cb(null, results); | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
var pending = list.length; | ||
if (!pending) { | ||
return cb(null, results); | ||
} | ||
list.forEach(function(file) { | ||
file = path.resolve(dir, file); | ||
fs.stat(file, (err, stat) => { | ||
if (stat && stat.isDirectory()) { | ||
walk(file, (err, res) => { | ||
results = results.concat(res); | ||
if (!--pending) { | ||
return cb(null, results); | ||
} | ||
}); | ||
} else { | ||
results.push(file); | ||
if (!--pending) { | ||
return cb(null, results); | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
}; | ||
@@ -115,3 +119,5 @@ | ||
if (err && !results) { | ||
self.logger.error("Error on BtrzPactS3::getFilesFromPath()", err); | ||
if (self.logger) { | ||
self.logger.error("Error on BtrzPactS3::getFilesFromPath()", err); | ||
} | ||
return reject(err); | ||
@@ -126,4 +132,4 @@ } | ||
publishPacts(pacts, _s3Client_) { | ||
let self = this; | ||
publishPacts(pacts, _s3Client_) { | ||
let self = this; | ||
@@ -135,3 +141,3 @@ if (!pacts) { | ||
//pacts.forEach(function(pact) { | ||
return this.getFilesFromPath(pacts[0]) | ||
return this.getFilesFromPath(pacts[0]) | ||
.then((files) => { | ||
@@ -145,28 +151,28 @@ return Promise.all(files.map(function (file) { | ||
}); | ||
} | ||
} | ||
getFileKey(filePath) { | ||
getFileKey(filePath) { | ||
function resolver(resolve, reject) { | ||
return fs.readFile(filePath, "utf8", function (err, data) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
let fileName = path.basename(filePath); | ||
let pact = JSON.parse(data); | ||
function resolver(resolve, reject) { | ||
return fs.readFile(filePath, "utf8", function (err, data) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
let fileName = path.basename(filePath); | ||
let pact = JSON.parse(data); | ||
if (!pact.consumer.name) { | ||
return reject(new Error(`The consumer name was not especified in the ${fileName} pact file`)); | ||
} | ||
if (!pact.provider.name) { | ||
return reject(new Error(`The provider name was not especified in the ${fileName} pact file`)); | ||
} | ||
return resolve(`${pact.consumer.name}/${pact.provider.name}/${fileName}`); | ||
}); | ||
if (!pact.consumer.name) { | ||
return reject(new Error(`The consumer name was not especified in the ${fileName} pact file`)); | ||
} | ||
if (!pact.provider.name) { | ||
return reject(new Error(`The provider name was not especified in the ${fileName} pact file`)); | ||
} | ||
return resolve(`${pact.consumer.name}/${pact.provider.name}/${fileName}`); | ||
}); | ||
} | ||
return new Promise(resolver); | ||
} | ||
return new Promise(resolver); | ||
} | ||
} | ||
} | ||
exports.BtrzPactS3 = BtrzPactS3; | ||
exports.BtrzPactS3 = BtrzPactS3; |
{ | ||
"name": "btrz-pact-s3", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "The S3 pact lib for Betterez", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -62,9 +62,2 @@ "use strict"; | ||
it("should throw if logger is not passed", () => { | ||
function sut() { | ||
new BtrzPactS3({accessKeyId:chance.hash(), secretAccessKey: chance.hash(), bucket: chance.word()}); | ||
} | ||
expect(sut).to.throw("BtrzPactS3 logger is mandatory."); | ||
}); | ||
it("should throw if logger is not valid", () => { | ||
@@ -74,3 +67,3 @@ function sut() { | ||
} | ||
expect(sut).to.throw("BtrzPactS3 logger is mandatory."); | ||
expect(sut).to.throw("BtrzPactS3 logger is not valid."); | ||
}); | ||
@@ -77,0 +70,0 @@ |
Sorry, the diff of this file is not supported yet
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
14345