Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Adding multi file contract functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sashman committed Apr 13, 2017
1 parent c600a5e commit 3f5488e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
36 changes: 28 additions & 8 deletions client/read-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,41 @@
const fs = require('fs')
const yaml = require('js-yaml')

const exitOnMissingContract = (contractsPath, skipMissingContractFlag) => skipMissingContractFlag && !fs.existsSync(contractsPath)
const exitOnMissingContract = (contractsPath, skipMissingContractFlag) => {
return skipMissingContractFlag && !fs.existsSync(contractsPath)
}

const contractExists = contractsPath => !fs.existsSync(contractsPath)

const readContents = contractsPath => {
const fileBuffer = fs.readFileSync(contractsPath)
const readFile = filePath => {
const fileBuffer = fs.readFileSync(filePath)

if (contractsPath.endsWith('json')) {
return fileBuffer
if (filePath.endsWith('json')) {
return JSON.parse(fileBuffer.toString())
}

const contracts = yaml.safeLoad(fileBuffer.toString())
const json = JSON.stringify(contracts)
return Buffer.from(json)
return yaml.safeLoad(fileBuffer.toString())
}

const readContents = contractsPath => {
if(fs.lstatSync(contractsPath).isDirectory()){
return fs.readdirSync(contractsPath)
.reduce((acc, file) => {
const contractObject = readFile(`${contractsPath}/${file}`)

if (!Object.keys(acc).length) {
return contractObject
}

const consumer = Object.keys(acc)[0]
const newAcc = {}
newAcc[consumer] = Object.assign(acc[consumer], contractObject[consumer])

return newAcc
}, {})
}

return readFile(contractsPath)
}

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions client/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = (jackalUrl, contractsPath, options, done) => {
return done(`Missing contract file ${contractsPath}`)
}

const bodyBuffer = readContract.readContents(contractsPath)
const jsonContents = readContract.readContents(contractsPath)

const req = {
url: jackal,
Expand All @@ -31,7 +31,7 @@ module.exports = (jackalUrl, contractsPath, options, done) => {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: bodyBuffer
body: JSON.stringify(jsonContents)
}

request(req, handleResponse(done))
Expand Down
2 changes: 1 addition & 1 deletion test/integration/client/send-dir.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Client.Send Integration Test', function () {
after(function (done) { providerOne.stop(done) })
after(function (done) { providerTwo.stop(done) })

context.skip('with valid, passing contracts', function () {
context('with valid, passing contracts', function () {
let port, dbPath, options

before(function (done) {
Expand Down

0 comments on commit 3f5488e

Please sign in to comment.