Skip to content

Commit

Permalink
feat: use cwd and env options passed by core
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Jul 17, 2018
1 parent a4071cc commit cdb248f
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 97 deletions.
36 changes: 18 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const verifyConfig = require('./lib/verify-config');

const PLUGIN_TYPES = ['analyzeCommits', 'verifyRelease', 'generateNotes', 'publish', 'success', 'fail'];

async function verifyConditions(pluginConfig, params) {
for (const [option, value] of Object.entries(params.options || {})) {
async function verifyConditions(pluginConfig, context) {
for (const [option, value] of Object.entries(context.options || {})) {
if (PLUGIN_TYPES.includes(option)) {
for (const plugin of castArray(value)) {
if (
Expand All @@ -24,52 +24,52 @@ async function verifyConditions(pluginConfig, params) {
verifyConfig(pluginConfig);

try {
await execScript(pluginConfig, params);
await execScript(pluginConfig, context);
} catch (err) {
throw new SemanticReleaseError(err.stdout, 'EVERIFYCONDITIONS');
}
}

async function analyzeCommits(pluginConfig, params) {
const stdout = await execScript(pluginConfig, params);
async function analyzeCommits(pluginConfig, context) {
const stdout = await execScript(pluginConfig, context);
return stdout.trim() ? stdout : undefined;
}

async function verifyRelease(pluginConfig, params) {
async function verifyRelease(pluginConfig, context) {
try {
await execScript(pluginConfig, params);
await execScript(pluginConfig, context);
} catch (err) {
throw new SemanticReleaseError(err.stdout, 'EVERIFYRELEASE');
}
}

async function generateNotes(pluginConfig, params) {
return execScript(pluginConfig, params);
async function generateNotes(pluginConfig, context) {
return execScript(pluginConfig, context);
}

async function prepare(pluginConfig, params) {
await execScript(pluginConfig, params);
async function prepare(pluginConfig, context) {
await execScript(pluginConfig, context);
}

async function publish(pluginConfig, params) {
const stdout = await execScript(pluginConfig, params);
async function publish(pluginConfig, context) {
const stdout = await execScript(pluginConfig, context);
try {
return stdout.trim() ? parseJson(stdout) : undefined;
} catch (err) {
debug(stdout);
debug(err);
params.logger.log(
context.logger.log(
`The command ${pluginConfig.cmd} wrote invalid JSON to stdout. The stdout content will be ignored.`
);
}
}

async function success(pluginConfig, params) {
await execScript(pluginConfig, params);
async function success(pluginConfig, context) {
await execScript(pluginConfig, context);
}

async function fail(pluginConfig, params) {
await execScript(pluginConfig, params);
async function fail(pluginConfig, context) {
await execScript(pluginConfig, context);
}

module.exports = {verifyConditions, analyzeCommits, verifyRelease, generateNotes, prepare, publish, success, fail};
6 changes: 3 additions & 3 deletions lib/exec-script.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const {template} = require('lodash');
const execa = require('execa');

module.exports = async ({cmd, ...config}, {logger, ...opts}) => {
const script = template(cmd)({config, ...opts});
module.exports = async ({cmd, ...config}, {cwd, env, logger, ...context}) => {
const script = template(cmd)({config, ...context});

logger.log('Call script %s', script);

const shell = execa.shell(script);
const shell = execa.shell(script, {cwd, env});
shell.stdout.pipe(process.stdout);
shell.stderr.pipe(process.stderr);

Expand Down
18 changes: 9 additions & 9 deletions test/analyze-commits.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@ test.beforeEach(t => {
t.context.logger = {log: t.context.log, error: t.context.error};
});

test.serial('Return the value analyzeCommits script wrote to stdout', async t => {
test('Return the value analyzeCommits script wrote to stdout', async t => {
const pluginConfig = {
cmd: './test/fixtures/echo-args.sh "minor "',
};
const params = {logger: t.context.logger};
const context = {logger: t.context.logger};

const result = await analyzeCommits(pluginConfig, params);
const result = await analyzeCommits(pluginConfig, context);
t.is(result, 'minor');
});

test.serial('Return "undefined" if the analyzeCommits script wrtite nothing to stdout', async t => {
test('Return "undefined" if the analyzeCommits script wrtite nothing to stdout', async t => {
const pluginConfig = {
cmd: './test/fixtures/echo-args.sh " "',
};
const params = {logger: t.context.logger};
const context = {logger: t.context.logger};

const result = await analyzeCommits(pluginConfig, params);
const result = await analyzeCommits(pluginConfig, context);
t.is(result, undefined);
});

test.serial('Throw Error if if the analyzeCommits script does not returns 0', async t => {
test('Throw Error if if the analyzeCommits script does not returns 0', async t => {
const pluginConfig = {cmd: 'exit 1'};
const params = {logger: t.context.logger};
const context = {logger: t.context.logger};

await t.throws(analyzeCommits(pluginConfig, params), Error);
await t.throws(analyzeCommits(pluginConfig, context), Error);
});
8 changes: 4 additions & 4 deletions test/exec-script.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ test.afterEach.always(t => {

test.serial('Pipe script output to stdout and stderr', async t => {
const pluginConfig = {cmd: '>&2 echo "write to stderr" && echo "write to stdout"'};
const params = {logger: t.context.logger, options: {}};
const context = {logger: t.context.logger, options: {}};

const result = await execScript(pluginConfig, params);
const result = await execScript(pluginConfig, context);

t.is(result, 'write to stdout');
t.is(t.context.stdout.args[0][0].toString().trim(), 'write to stdout');
Expand All @@ -30,8 +30,8 @@ test.serial('Pipe script output to stdout and stderr', async t => {

test.serial('Generate command with template', async t => {
const pluginConfig = {cmd: `./test/fixtures/echo-args.sh \${config.conf} \${lastRelease.version}`, conf: 'confValue'};
const params = {lastRelease: {version: '1.0.0'}, logger: t.context.logger};
const context = {lastRelease: {version: '1.0.0'}, logger: t.context.logger};

const result = await execScript(pluginConfig, params);
const result = await execScript(pluginConfig, context);
t.is(result, 'confValue 1.0.0');
});
12 changes: 6 additions & 6 deletions test/fail.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ test.beforeEach(t => {
t.context.logger = {log: t.context.log, error: t.context.error};
});

test.serial('Execute script in fail step', async t => {
test('Execute script in fail step', async t => {
const pluginConfig = {cmd: './test/fixtures/echo-args.sh'};
const params = {logger: t.context.logger};
const context = {logger: t.context.logger};

await t.notThrows(fail(pluginConfig, params));
await t.notThrows(fail(pluginConfig, context));
});

test.serial('Throw "Error" if the fail script does not returns 0', async t => {
test('Throw "Error" if the fail script does not returns 0', async t => {
const pluginConfig = {cmd: 'exit 1'};
const params = {logger: t.context.logger};
const context = {logger: t.context.logger};

await t.throws(fail(pluginConfig, params), Error);
await t.throws(fail(pluginConfig, context), Error);
});
12 changes: 6 additions & 6 deletions test/generate-notes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ test.beforeEach(t => {
t.context.logger = {log: t.context.log, error: t.context.error};
});

test.serial('Return the value generateNotes script wrote to stdout', async t => {
test('Return the value generateNotes script wrote to stdout', async t => {
const pluginConfig = {
cmd: './test/fixtures/echo-args.sh "\nRelease note \n\n"',
};
const params = {logger: t.context.logger};
const context = {logger: t.context.logger};

const result = await generateNotes(pluginConfig, params);
const result = await generateNotes(pluginConfig, context);
t.is(result, 'Release note');
});

test.serial('Throw "Error" if if the generateNotes script does not returns 0', async t => {
test('Throw "Error" if if the generateNotes script does not returns 0', async t => {
const pluginConfig = {cmd: 'exit 1'};
const params = {logger: t.context.logger};
const context = {logger: t.context.logger};

await t.throws(generateNotes(pluginConfig, params), Error);
await t.throws(generateNotes(pluginConfig, context), Error);
});
12 changes: 6 additions & 6 deletions test/prepare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ test.beforeEach(t => {
t.context.logger = {log: t.context.log, error: t.context.error};
});

test.serial('Execute script in prepare step', async t => {
test('Execute script in prepare step', async t => {
const pluginConfig = {cmd: './test/fixtures/echo-args.sh'};
const params = {logger: t.context.logger};
const context = {logger: t.context.logger};

await t.notThrows(prepare(pluginConfig, params));
await t.notThrows(prepare(pluginConfig, context));
});

test.serial('Throw "Error" if the prepare script does not returns 0', async t => {
test('Throw "Error" if the prepare script does not returns 0', async t => {
const pluginConfig = {cmd: 'exit 1'};
const params = {logger: t.context.logger};
const context = {logger: t.context.logger};

await t.throws(prepare(pluginConfig, params), Error);
await t.throws(prepare(pluginConfig, context), Error);
});
24 changes: 12 additions & 12 deletions test/publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,40 @@ test.beforeEach(t => {
t.context.logger = {log: t.context.log, error: t.context.error};
});

test.serial('Parse JSON returned by publish script', async t => {
test('Parse JSON returned by publish script', async t => {
const pluginConfig = {
cmd:
'./test/fixtures/echo-args.sh {\\"name\\": \\"Release name\\", \\"url\\": \\"https://host.com/release/1.0.0\\"}',
};
const params = {logger: t.context.logger};
const context = {logger: t.context.logger};

const result = await publish(pluginConfig, params);
const result = await publish(pluginConfig, context);
t.deepEqual(result, {name: 'Release name', url: 'https://host.com/release/1.0.0'});
});

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

const result = await publish(pluginConfig, params);
const result = await publish(pluginConfig, context);
t.is(result, undefined);
});

test.serial('Return "undefined" if the publish script wrtite nothing to stdout', async t => {
test('Return "undefined" if the publish script wrtite nothing to stdout', async t => {
const pluginConfig = {
cmd: './test/fixtures/echo-args.sh',
};
const params = {logger: t.context.logger};
const context = {logger: t.context.logger};

const result = await publish(pluginConfig, params);
const result = await publish(pluginConfig, context);
t.is(result, undefined);
});

test.serial('Throw "Error" if the publish script does not returns 0', async t => {
test('Throw "Error" if the publish script does not returns 0', async t => {
const pluginConfig = {cmd: 'exit 1'};
const params = {logger: t.context.logger, options: {}};
const context = {logger: t.context.logger, options: {}};

await t.throws(publish(pluginConfig, params), Error);
await t.throws(publish(pluginConfig, context), Error);
});
12 changes: 6 additions & 6 deletions test/success.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ test.beforeEach(t => {
t.context.logger = {log: t.context.log, error: t.context.error};
});

test.serial('Execute script in success step', async t => {
test('Execute script in success step', async t => {
const pluginConfig = {cmd: './test/fixtures/echo-args.sh'};
const params = {logger: t.context.logger};
const context = {logger: t.context.logger};

await t.notThrows(success(pluginConfig, params));
await t.notThrows(success(pluginConfig, context));
});

test.serial('Throw "Error" if the success script does not returns 0', async t => {
test('Throw "Error" if the success script does not returns 0', async t => {
const pluginConfig = {cmd: 'exit 1'};
const params = {logger: t.context.logger};
const context = {logger: t.context.logger};

await t.throws(success(pluginConfig, params), Error);
await t.throws(success(pluginConfig, context), Error);
});
Loading

0 comments on commit cdb248f

Please sign in to comment.