Skip to content

Commit

Permalink
feat(CanDeploy): Set json output as the default for CanDeploy
Browse files Browse the repository at this point in the history
BREAKING CHANGE: CanDeploy now defaults to json output (and returns the parsed object as the result of the promise. If you were using CanDeploy and relied on parsing the logged output, you will need to explicitly set `output: table` in your CanDeploy options.
  • Loading branch information
TimothyJones committed Oct 14, 2019
1 parent 869d397 commit 200abe7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
43 changes: 25 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,30 +302,37 @@ var opts = {
};

pact.canDeploy(opts)
.then(function () {
// Deployment worked
.then(function (result) {
// You can deploy this
// If output is not specified or is json, result describes the result of the check.
// If outout is 'table', it is the human readable string returned by the check
})
.catch(function() {
// Deployment failed
.catch(function(error) {
// You can't deploy this
// if output is not specified, or is json, error will be an object describing
// the result of the check (if the check failed),
// if output is 'table', then the error will be a string describing the output from the binary,

// In both cases, `error` will be an Error object if something went wrong during the check.
});
```

**Options**:

| Parameter | Required? | Type | Description |
| -------------------- | --------- | ----------- | ----------------------------------------------------------------------------------- |
| `participant` | true | string | The participant name. Required. |
| `participantVersion` | true | string | Version of the participant. Must follow after the participant. Required. |
| `latest` | false | string | Use the latest participant version, Must follow after participant. Optional |
| `to` | false | string | Which tag are you deploying to, Must follow after participant. Optional |
| `pactBroker` | true | string | URL of the Pact Broker to publish pacts to. Required. |
| `pactBrokerUsername` | false | string | Username for Pact Broker basic authentication. Optional |
| `pactBrokerPassword` | false | string | Password for Pact Broker basic authentication. Optional |
| `pactBrokerToken` | false | string | Bearer token for Pact Broker authentication. Optional |
| `output` | false | json,table | Specify output to show, json or table. Optional |
| `verbose` | false | flag | Set logging mode to verbose. Optional |
| `retryWhileUnknown` | false | number | The number of times to retry while there is an unknown verification result. Optional|
| `retryInterval` | false | number | The time between retries in seconds, use with retryWhileUnknown. Optional |
| Parameter | Required? | Type | Description |
| -------------------- | --------- | ---------- | ------------------------------------------------------------------------------------ |
| `participant` | true | string | The participant name. Required. |
| `participantVersion` | true | string | Version of the participant. Must follow after the participant. Required. |
| `latest` | false | string | Use the latest participant version, Must follow after participant. Optional |
| `to` | false | string | Which tag are you deploying to, Must follow after participant. Optional |
| `pactBroker` | true | string | URL of the Pact Broker to publish pacts to. Required. |
| `pactBrokerUsername` | false | string | Username for Pact Broker basic authentication. Optional |
| `pactBrokerPassword` | false | string | Password for Pact Broker basic authentication. Optional |
| `pactBrokerToken` | false | string | Bearer token for Pact Broker authentication. Optional |
| `output` | false | json,table | Specify output to show, json or table. Optional. Defaults to json. |
| `verbose` | false | flag | Set logging mode to verbose. Optional |
| `retryWhileUnknown` | false | number | The number of times to retry while there is an unknown verification result. Optional |
| `retryInterval` | false | number | The time between retries in seconds, use with retryWhileUnknown. Optional |

### Stub Servers

Expand Down
30 changes: 19 additions & 11 deletions src/can-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export class CanDeploy {
options = options || {};
// Setting defaults
options.timeout = options.timeout || 60000;
if (!options.output) {
options.output = 'json';
}

checkTypes.assert.nonEmptyString(
options.participant,
Expand Down Expand Up @@ -97,23 +100,28 @@ export class CanDeploy {
instance.stdout.on('data', l => output.push(l));
instance.stderr.on('data', l => output.push(l));
instance.once('close', code => {
let o: any = output.join('\n');
const result: any = output.join('\n');

let success = false;
if (this.options.output === 'json') {
o = JSON.parse(o);
success = o.summary.deployable;
} else {
success = /Computer says yes/gim.exec(o) !== null;
try {
const parsed = JSON.parse(result);
if (code === 0 && parsed.summary.deployable) {
return deferred.resolve(parsed);
}
return deferred.reject(parsed);
} catch (e) {
logger.error(`can-i-deploy produced non-json output:\n${result}`);
return deferred.reject(new Error(result));
}
}

if (code === 0 || success) {
logger.info(o);
return deferred.resolve(o);
if (code === 0) {
logger.info(result);
return deferred.resolve(result);
}

logger.error(`can-i-deploy did not return success message:\n${o}`);
return deferred.reject(o);
logger.error(`can-i-deploy did not return success message:\n${result}`);
return deferred.reject(result);
});

return deferred.promise.timeout(
Expand Down

0 comments on commit 200abe7

Please sign in to comment.