Skip to content

Commit 99114c8

Browse files
pvdlggr2m
authored andcommitted
fix: ignore invalid JSON in stdout
1 parent 72d0822 commit 99114c8

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ Execute a shell command to prepare the release.
6363

6464
Execute a shell command to publish the release.
6565

66-
| Command property | Description |
67-
|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|
68-
| `exit code` | Any non `0` code is considered as an unexpected error and will stop the `semantic-release` execution with an error. |
69-
| `stdout` | Only the `release` information must be written to `stdout` as parseable JSON (for example `{"name": "Release name", "url": "http://url/release/1.0.0"}`). |
70-
| `stderr` | Can be used for logging. |
66+
| Command property | Description |
67+
|------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
68+
| `exit code` | Any non `0` code is considered as an unexpected error and will stop the `semantic-release` execution with an error. |
69+
| `stdout` | The `release` information can be written to `stdout` as parseable JSON (for example `{"name": "Release name", "url": "http://url/release/1.0.0"}`). If the command write non parseable JSON to `stdout` no `release` information will be returned. |
70+
| `stderr` | Can be used for logging. |
7171

7272
## success
7373

index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const {castArray, isPlainObject} = require('lodash');
22
const parseJson = require('parse-json');
3+
const debug = require('debug')('semantic-release:exec');
34
const SemanticReleaseError = require('@semantic-release/error');
45
const execScript = require('./lib/exec-script');
56
const verifyConfig = require('./lib/verify-config');
@@ -52,7 +53,15 @@ async function prepare(pluginConfig, params) {
5253

5354
async function publish(pluginConfig, params) {
5455
const stdout = await execScript(pluginConfig, params);
55-
return stdout.trim() ? parseJson(stdout) : undefined;
56+
try {
57+
return stdout.trim() ? parseJson(stdout) : undefined;
58+
} catch (err) {
59+
debug(stdout);
60+
debug(err);
61+
params.logger.log(
62+
`The command ${pluginConfig.cmd} wrote invalid JSON to stdout. The stdout content will be ignored.`
63+
);
64+
}
5665
}
5766

5867
async function success(pluginConfig, params) {

test/publish.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@ test.serial('Parse JSON returned by publish script', async t => {
2424
t.deepEqual(result, {name: 'Release name', url: 'https://host.com/release/1.0.0'});
2525
});
2626

27-
test.serial('Return "undefined" if the publish script wrtite nothing to stdout', async t => {
27+
test.serial('Return "undefined" if the publish script wrtite invalid JSON to stdout', async t => {
2828
const pluginConfig = {
29-
cmd: './test/fixtures/echo-args.sh',
29+
cmd: './test/fixtures/echo-args.sh invalid_json',
3030
};
3131
const params = {logger: t.context.logger};
3232

3333
const result = await publish(pluginConfig, params);
3434
t.is(result, undefined);
3535
});
3636

37-
test.serial('Throw JSONError if publish script write invalid JSON to stdout', async t => {
37+
test.serial('Return "undefined" if the publish script wrtite nothing to stdout', async t => {
3838
const pluginConfig = {
39-
cmd: './test/fixtures/echo-args.sh invalid_json',
39+
cmd: './test/fixtures/echo-args.sh',
4040
};
4141
const params = {logger: t.context.logger};
4242

43-
const error = await t.throws(publish(pluginConfig, params));
44-
t.is(error.name, 'JSONError');
43+
const result = await publish(pluginConfig, params);
44+
t.is(result, undefined);
4545
});
4646

4747
test.serial('Throw "Error" if the publish script does not returns 0', async t => {

0 commit comments

Comments
 (0)