Skip to content

Commit

Permalink
Prompt user to install CLI or show error message on configuration cha…
Browse files Browse the repository at this point in the history
…nge (#141)
  • Loading branch information
vcheung-stripe authored Jan 28, 2021
1 parent f18a4a2 commit 5c39d3d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/stripeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ const fs = require('fs');

export class StripeClient {
telemetry: Telemetry;
isInstalled: boolean;
cliPath: string | null;

constructor(telemetry:Telemetry) {
this.telemetry = telemetry;
this.isInstalled = false;
this.cliPath = null;
vscode.workspace.onDidChangeConfiguration(this.handleDidChangeConfiguration, this);
}
Expand All @@ -24,7 +22,6 @@ export class StripeClient {
const isInstalled = await this.detectInstalled();

if (!isInstalled) {
this.promptInstall();
return;
}

Expand Down Expand Up @@ -135,6 +132,8 @@ export class StripeClient {
`You set a custom installation path for the Stripe CLI, but we couldn't find the executable in '${customInstallPath}'`,
...['Ok']
);
} else {
this.promptInstall();
}
this.cliPath = null;
this.telemetry.sendEvent('cli.notInstalled');
Expand All @@ -154,9 +153,12 @@ export class StripeClient {
private async handleDidChangeConfiguration(e: vscode.ConfigurationChangeEvent) {
const shouldHandleConfigurationChange = e.affectsConfiguration('stripe');
if (shouldHandleConfigurationChange) {
const isAuthenticated = await this.isAuthenticated();
if (!isAuthenticated) {
await this.promptLogin();
const isInstalled = await this.detectInstalled();
if (isInstalled) {
const isAuthenticated = await this.isAuthenticated();
if (!isAuthenticated) {
await this.promptLogin();
}
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/suite/stripeClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ suite('stripeClient', () => {
});
});
});

test('prompts install when CLI is not installed', async () => {
sandbox.stub(fs.promises, 'stat').returns(Promise.resolve({isFile: () => false}));
const showErrorMessageSpy = sandbox.stub(vscode.window, 'showErrorMessage');
const stripeClient = new StripeClient(new NoOpTelemetry());
const isInstalled = await stripeClient.detectInstalled();
assert.strictEqual(isInstalled, false);
assert.deepStrictEqual(
showErrorMessageSpy.args[0],
[
'Welcome! Stripe is using the Stripe CLI behind the scenes, and requires it to be installed on your machine',
{},
'Read instructions on how to install Stripe CLI',
]
);
});
});

suite('with custom CLI install path', () => {
Expand Down Expand Up @@ -110,6 +126,18 @@ suite('stripeClient', () => {
});
});
});

test('shows error when CLI is not at that path', async () => {
statStub.returns(Promise.resolve({isFile: () => false}));
const showErrorMessageSpy = sandbox.stub(vscode.window, 'showErrorMessage');
const stripeClient = new StripeClient(new NoOpTelemetry());
const isInstalled = await stripeClient.detectInstalled();
assert.strictEqual(isInstalled, false);
assert.deepStrictEqual(
showErrorMessageSpy.args[0],
["You set a custom installation path for the Stripe CLI, but we couldn't find the executable in '/foo/bar/baz'", 'Ok'],
);
});
});
});
});

0 comments on commit 5c39d3d

Please sign in to comment.