Skip to content

Commit

Permalink
Use task to run listen command (#836)
Browse files Browse the repository at this point in the history
* Use task to run listen command

* Remove unused code

* reinstall deps

* remove extra file
  • Loading branch information
vcheung-stripe authored Nov 14, 2024
1 parent 95b0a35 commit b4238e0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 152 deletions.
34 changes: 0 additions & 34 deletions src/shellEscape.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/stripeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import * as vscode from 'vscode';
import {OSType, getOSType} from './utils';
import {setCliVersion, setStripeAccountId} from './stripeWorkspaceState';
import {Telemetry} from './telemetry';
import compareVersions from 'compare-versions';

const execa = require('execa');
const fs = require('fs');
const compareVersions = require('compare-versions');

// The recommended minimum version of the CLI needed to get the full features of this extension.
const MIN_CLI_VERSION = 'v1.5.13';
Expand Down
58 changes: 17 additions & 41 deletions src/stripeTerminal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as vscode from 'vscode';
import {StripeClient} from './stripeClient';
import {shellEscape} from './shellEscape';

type SupportedStripeCommand = 'events' | 'listen' | 'logs' | 'login' | 'trigger';

Expand Down Expand Up @@ -32,46 +31,23 @@ export class StripeTerminal {

const globalCLIFlags = this.getGlobalCLIFlags();

const commandString = shellEscape([cliPath, command, ...args, ...globalCLIFlags]);

try {
const terminal = await this.createTerminal();
terminal.sendText(commandString);
terminal.show();
} catch (e: any) {
vscode.window.showErrorMessage(e.message);
}
}

private async createNewSplitTerminal(): Promise<vscode.Terminal | undefined> {
const lastTerminal = this.terminals[this.terminals.length - 1];
lastTerminal.show();

// Note that this splits off of the user's currently visible terminal. That's why `.show()` is
// called above.
await vscode.commands.executeCommand<vscode.Terminal>('workbench.action.terminal.split');

// After `workbench.action.terminal.split`, the activeTerminal becomes the newly split terminal.
// Note that there is no API guarantee for this behavior; a prior implementation relied on
// different behavior entirely. Because historically this behavior has been in flux, we should
// move to an official API for creating split terminals once that lands in vscode.
return vscode.window.activeTerminal;
}

private async createTerminal(): Promise<vscode.Terminal> {
if (this.terminals.length > 0) {
const terminal = await this.createNewSplitTerminal();
if (!terminal) {
throw new Error('Failed to create a terminal for this Stripe command. Please try again.');
}

this.terminals.push(terminal);
return terminal;
}

const terminal = vscode.window.createTerminal('Stripe');
this.terminals.push(terminal);
return terminal;
vscode.tasks.executeTask(new vscode.Task(
{type: 'stripe', command},
vscode.TaskScope.Workspace,
command,
'stripe',
new vscode.ShellExecution(cliPath, [
command,
...args.map((arg) => ({
quoting: vscode.ShellQuoting.Strong,
value: arg,
})),
...globalCLIFlags.map((arg) => ({
quoting: vscode.ShellQuoting.Strong,
value: arg,
})),
])
));
}

// The Stripe CLI supports a number of flags for every command. See https://stripe.com/docs/cli/flags
Expand Down
70 changes: 0 additions & 70 deletions test/suite/shellEscape.test.ts

This file was deleted.

30 changes: 24 additions & 6 deletions test/suite/stripeTerminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,39 @@ suite('stripeTerminal', function () {
['/usr/local/bin/stripe', '/custom/path/to/stripe'].forEach((path) => {
suite(`when the Stripe CLI is installed at ${path}`, () => {
test(`runs command with ${path}`, async () => {
const sendTextStub = sandbox.stub(terminalStub, 'sendText');
const createTerminalStub = sandbox
const executeTaskSpy = sandbox.spy(vscode.tasks, 'executeTask');
sandbox.stub(terminalStub, 'sendText');
sandbox
.stub(vscode.window, 'createTerminal')
.returns(terminalStub);
const stripeClientStub = <any>{getCLIPath: () => {}, isAuthenticated: () => true};
const getCLIPathStub = sandbox
sandbox
.stub(stripeClientStub, 'getCLIPath')
.returns(Promise.resolve(path));

const stripeTerminal = new StripeTerminal(stripeClientStub);
await stripeTerminal.execute('listen', ['--forward-to', 'localhost']);

assert.strictEqual(getCLIPathStub.callCount, 1);
assert.strictEqual(createTerminalStub.callCount, 1);
assert.deepStrictEqual(sendTextStub.args[0], [`${path} listen --forward-to localhost`]);
assert.strictEqual(executeTaskSpy.callCount, 1);
assert.deepStrictEqual(executeTaskSpy.args[0], [
new vscode.Task(
{type: 'stripe', command: 'listen'},
vscode.TaskScope.Workspace,
'listen',
'stripe',
new vscode.ShellExecution(path, [
'listen',
{
quoting: vscode.ShellQuoting.Strong,
value: '--forward-to'
},
{
quoting: vscode.ShellQuoting.Strong,
value: 'localhost'
}
])
),
]);
});
});
});
Expand Down

0 comments on commit b4238e0

Please sign in to comment.