Skip to content

Commit 92dffa2

Browse files
authored
feat(vscode): suggest installing Dart Frog CLI instead of silently installing (#755)
1 parent 6fa5ccf commit 92dffa2

File tree

2 files changed

+128
-12
lines changed

2 files changed

+128
-12
lines changed

extensions/vscode/src/extension.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { installCLI, newRoute, newMiddleware, updateCLI } from "./commands";
33
import {
44
readDartFrogCLIVersion,
55
isCompatibleDartFrogCLIVersion,
6+
isDartFrogCLIInstalled,
67
} from "./utils";
78

89
/**
@@ -14,10 +15,15 @@ import {
1415
* @returns The same instance of the extension context passed in.
1516
*/
1617
export function activate(
17-
context: vscode.ExtensionContext
18+
context: vscode.ExtensionContext,
19+
suggestInstallingCLI: () => Promise<void> = suggestInstallingDartFrogCLI,
20+
ensureCompatibleCLI: () => Promise<void> = ensureCompatibleDartFrogCLI
1821
): vscode.ExtensionContext {
19-
installCLI();
20-
ensureCompatibleDartFrogCLI();
22+
if (!isDartFrogCLIInstalled()) {
23+
suggestInstallingCLI();
24+
} else {
25+
ensureCompatibleCLI();
26+
}
2127

2228
context.subscriptions.push(
2329
vscode.commands.registerCommand("extension.install-cli", installCLI),
@@ -28,6 +34,35 @@ export function activate(
2834
return context;
2935
}
3036

37+
/**
38+
* Suggests the user to install Dart Frog CLI.
39+
*
40+
* This method should be called upon activation of the extension whenever
41+
* Dart Frog CLI is not installed in the user's system.
42+
*
43+
* It prompts the user to install Dart Frog CLI. This is optional, the user
44+
* can choose to install Dart Frog CLI at a later time but the extension may
45+
* not work as intended until Dart Frog CLI is installed.
46+
*
47+
* @see {@link isDartFrogCLIInstalled}, to check if Dart Frog CLI is installed
48+
*/
49+
export async function suggestInstallingDartFrogCLI(): Promise<void> {
50+
const selection = await vscode.window.showWarningMessage(
51+
"Dart Frog CLI is not installed. Install Dart Frog CLI to use this extension.",
52+
"Install Dart Frog CLI",
53+
"Ignore"
54+
);
55+
switch (selection) {
56+
case "Install Dart Frog CLI":
57+
await installCLI();
58+
break;
59+
case "Ignore":
60+
break;
61+
default:
62+
break;
63+
}
64+
}
65+
3166
/**
3267
* Checks if the version of Dart Frog CLI installed in the user's system is
3368
* compatible with this extension, suggesting to install if it is not.

extensions/vscode/src/test/suite/extension.test.ts

Lines changed: 90 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,39 +95,120 @@ suite("activate", () => {
9595
});
9696
});
9797

98-
test("calls installCLI", () => {
98+
test("calls suggestInstallingDartFrogCLI when Dart Frog CLI is not installed", () => {
99+
const vscodeStub = {
100+
commands: {
101+
registerCommand: sinon.stub(),
102+
},
103+
};
104+
105+
const utilsStub = {
106+
isDartFrogCLIInstalled: sinon.stub(),
107+
};
108+
utilsStub.isDartFrogCLIInstalled.returns(false);
109+
110+
const extension = proxyquire("../../extension", {
111+
vscode: vscodeStub,
112+
// eslint-disable-next-line @typescript-eslint/naming-convention
113+
"./utils": utilsStub,
114+
});
115+
116+
const context = { subscriptions: [] };
117+
const suggestInstallingCLI = sinon.stub();
118+
const ensureCompatibleCLI = sinon.stub();
119+
extension.activate(context, suggestInstallingCLI, ensureCompatibleCLI);
120+
121+
sinon.assert.calledOnce(suggestInstallingCLI);
122+
});
123+
124+
test("calls ensureCompatibleDartFrogCLI when Dart Frog CLI is installed", () => {
99125
const vscodeStub = {
100126
commands: {
101127
registerCommand: sinon.stub(),
102128
},
103129
};
104130

105131
const utilsStub = {
106-
readDartFrogCLIVersion: sinon.stub(),
107-
isCompatibleDartFrogCLIVersion: sinon.stub(),
108132
isDartFrogCLIInstalled: sinon.stub(),
109133
};
110-
utilsStub.readDartFrogCLIVersion.returns("0.0.0");
111-
utilsStub.isCompatibleDartFrogCLIVersion.returns(true);
112134
utilsStub.isDartFrogCLIInstalled.returns(true);
113135

114-
const commandsStub = {
136+
const extension = proxyquire("../../extension", {
137+
vscode: vscodeStub,
138+
// eslint-disable-next-line @typescript-eslint/naming-convention
139+
"./utils": utilsStub,
140+
});
141+
142+
const context = { subscriptions: [] };
143+
const suggestInstallingCLI = sinon.stub();
144+
const ensureCompatibleCLI = sinon.stub();
145+
extension.activate(context, suggestInstallingCLI, ensureCompatibleCLI);
146+
147+
sinon.assert.calledOnce(ensureCompatibleCLI);
148+
});
149+
});
150+
151+
suite("suggestInstallingDartFrogCLI", () => {
152+
let vscodeStub: any;
153+
let utilsStub: any;
154+
let commandsStub: any;
155+
let extension: any;
156+
157+
beforeEach(() => {
158+
vscodeStub = {
159+
window: {
160+
showWarningMessage: sinon.stub(),
161+
},
162+
};
163+
164+
utilsStub = {
165+
isDartFrogCLIInstalled: sinon.stub(),
166+
};
167+
utilsStub.isDartFrogCLIInstalled.returns(false);
168+
169+
commandsStub = {
115170
installCLI: sinon.stub(),
116171
};
117172

118-
const extension = proxyquire("../../extension", {
173+
extension = proxyquire("../../extension", {
119174
vscode: vscodeStub,
120175
// eslint-disable-next-line @typescript-eslint/naming-convention
121176
"./utils": utilsStub,
122177
// eslint-disable-next-line @typescript-eslint/naming-convention
123178
"./commands": commandsStub,
124179
});
180+
});
125181

126-
const context = { subscriptions: [] };
127-
extension.activate(context);
182+
afterEach(() => {
183+
sinon.restore();
184+
});
185+
186+
test("shows warning suggesting to install Dart Frog CLI", async () => {
187+
await extension.suggestInstallingDartFrogCLI();
188+
189+
sinon.assert.calledOnceWithExactly(
190+
vscodeStub.window.showWarningMessage,
191+
"Dart Frog CLI is not installed. Install Dart Frog CLI to use this extension.",
192+
"Install Dart Frog CLI",
193+
"Ignore"
194+
);
195+
});
196+
197+
test("installs Dart Frog CLI when selected", async () => {
198+
vscodeStub.window.showWarningMessage.returns("Install Dart Frog CLI");
199+
200+
await extension.suggestInstallingDartFrogCLI();
128201

129202
sinon.assert.calledOnce(commandsStub.installCLI);
130203
});
204+
205+
test("does not install Dart Frog CLI when ignored", async () => {
206+
vscodeStub.window.showWarningMessage.returns("Ignore");
207+
208+
await extension.suggestInstallingDartFrogCLI();
209+
210+
sinon.assert.notCalled(commandsStub.installCLI);
211+
});
131212
});
132213

133214
suite("ensureCompatibleDartFrogCLI", () => {

0 commit comments

Comments
 (0)