Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/neat-toys-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@slack/cli-hooks": patch
---

fix(cli-hooks): stop app process if the start hook exits

Fixes a CLI [issue](https://github.com/slackapi/slack-cli/issues/128) where daemon app processes were spawned if the CLI was exited without being interrupted.
11 changes: 11 additions & 0 deletions packages/cli-hooks/src/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ export default function start(cwd) {
app.on('close', (code) => {
process.exit(code);
});
process.on('exit', () => {
app.kill();
});
process.on('SIGINT', () => {
app.kill('SIGINT');
process.exit();
});
process.on('SIGTERM', () => {
app.kill('SIGTERM');
process.exit();
});
}

/**
Expand Down
59 changes: 59 additions & 0 deletions packages/cli-hooks/src/start.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import start from './start.js';
* @property {MockStreams} stdout - Output logged to standard output streams.
* @property {MockStreams} stderr - Output logged to standard error streams.
* @property {sinon.SinonStub} on - A fallback event to mock the spawn closure.
* @property {sinon.SinonStub} [kill] - Forced exit code of a spawned process.
*/

describe('start implementation', async () => {
Expand All @@ -41,6 +42,7 @@ describe('start implementation', async () => {
stdout: { on: sinon.stub(), setEncoding: () => {} },
stderr: { on: sinon.stub() },
on: sinon.stub(),
kill: sinon.stub(),
};
spawnStub = sinon.stub(childProcess, 'spawn').returns(/** @type {any} */ (mockSpawnProcess));
process.env.SLACK_CLI_XAPP = 'xapp-example';
Expand Down Expand Up @@ -126,4 +128,61 @@ describe('start implementation', async () => {
});
});
});

describe('stops the app process', () => {
/** @type {sinon.SinonStub} */
let processStub;
/** @type {sinon.SinonStub} */
let exitStub;
/** @type {MockSpawnProcess} */
let mockSpawnProcess;
/** @type {sinon.SinonStub} */
let spawnStub;

beforeEach(() => {
process.env.SLACK_CLI_CUSTOM_FILE_PATH = 'app.js';
processStub = sinon.stub(process, 'on');
exitStub = sinon.stub(process, 'exit');
mockSpawnProcess = {
stdout: { on: sinon.stub(), setEncoding: () => {} },
stderr: { on: sinon.stub() },
on: sinon.stub(),
kill: sinon.stub(),
};
spawnStub = sinon.stub(childProcess, 'spawn').returns(/** @type {any} */ (mockSpawnProcess));
});

afterEach(() => {
sinon.restore();
process.env.SLACK_CLI_CUSTOM_FILE_PATH = undefined;
});

it('stops app process on hook exit', () => {
start('./');
assert.ok(spawnStub.called);
assert.ok(spawnStub.calledWith('node', [path.resolve('app.js')]));
const handler = processStub.getCalls().find((call) => call.args[0] === 'exit')?.args[1];
assert.ok(handler, 'exit handler should be registered');
handler();
assert.ok(mockSpawnProcess.kill?.called);
});

it('stops app process on hook SIGINT', () => {
start('./');
const handler = processStub.getCalls().find((call) => call.args[0] === 'SIGINT')?.args[1];
assert.ok(handler, 'SIGINT handler should be registered');
handler();
assert.ok(mockSpawnProcess.kill?.calledWith('SIGINT'));
assert.ok(exitStub.called);
});

it('stops app process on hook SIGTERM', () => {
start('./');
const handler = processStub.getCalls().find((call) => call.args[0] === 'SIGTERM')?.args[1];
assert.ok(handler, 'SIGTERM handler should be registered');
handler();
assert.ok(mockSpawnProcess.kill?.calledWith('SIGTERM'));
assert.ok(exitStub.called);
});
});
});