Skip to content

Commit 428aef2

Browse files
authored
refactor(vscode): defined isDartFrogCliInstalled (#748)
1 parent e789065 commit 428aef2

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

extensions/vscode/src/commands/install-cli.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const cp = require("child_process");
22

33
import { window, ProgressOptions } from "vscode";
4+
import { isDartFrogCliInstalled } from "../utils/utils";
45

56
/**
67
* Installs Dart Frog CLI in the user's system if not already installed.
78
*/
89
export const installCLI = async (): Promise<void> => {
9-
if (!hasDartFrogCliInstalled()) {
10+
if (!isDartFrogCliInstalled()) {
1011
const options: ProgressOptions = {
1112
location: 15,
1213
title: "Installing Dart Frog CLI...",
@@ -15,21 +16,6 @@ export const installCLI = async (): Promise<void> => {
1516
}
1617
};
1718

18-
/**
19-
* Whether the user has Dart Frog CLI installed in their system.
20-
*
21-
* @returns {boolean} True if the user has Dart Frog CLI installed in their
22-
* system, false otherwise.
23-
*/
24-
function hasDartFrogCliInstalled(): boolean {
25-
try {
26-
cp.execSync(`dart_frog --version`);
27-
return true;
28-
} catch (error) {
29-
return false;
30-
}
31-
}
32-
3319
/**
3420
* Installs Dart Frog CLI from pub.dev.
3521
*

extensions/vscode/src/test/suite/commands/install-cli.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ suite("install-cli command", () => {
1010
let vscodeStub: any;
1111
let childProcessStub: any;
1212
let command: any;
13+
let utilsStub: any;
1314

1415
beforeEach(() => {
1516
vscodeStub = {
@@ -22,11 +23,16 @@ suite("install-cli command", () => {
2223
exec: sinon.stub(),
2324
execSync: sinon.stub(),
2425
};
26+
utilsStub = {
27+
isDartFrogCliInstalled: sinon.stub(),
28+
};
2529

2630
command = proxyquire("../../../commands/install-cli", {
2731
vscode: vscodeStub,
2832
// eslint-disable-next-line @typescript-eslint/naming-convention
2933
child_process: childProcessStub,
34+
// eslint-disable-next-line @typescript-eslint/naming-convention
35+
"../utils/utils": utilsStub,
3036
});
3137
});
3238

@@ -35,7 +41,7 @@ suite("install-cli command", () => {
3541
});
3642

3743
test("does not install if Dart Frog CLI is already installed", async () => {
38-
childProcessStub.execSync.withArgs("dart_frog --version").returns("0.0.0");
44+
utilsStub.isDartFrogCliInstalled.returns(true);
3945

4046
await command.installCLI();
4147

@@ -47,9 +53,7 @@ suite("install-cli command", () => {
4753

4854
suite("installs Dart Frog CLI", () => {
4955
beforeEach(() => {
50-
childProcessStub.execSync
51-
.withArgs("dart_frog --version")
52-
.throws("Command failed");
56+
utilsStub.isDartFrogCliInstalled.returns(false);
5357
});
5458

5559
test("when not already installed", async () => {

extensions/vscode/src/utils/cli-version.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,13 @@ export function readDartFrogVersion(): String | undefined {
3434
export function isCompatibleCLIVersion(version: String): Boolean {
3535
return semver.satisfies(version, compatibleCLIVersion);
3636
}
37+
38+
/**
39+
* Whether the user has Dart Frog CLI installed in their system.
40+
*
41+
* @returns {boolean} True if the user has Dart Frog CLI installed in their
42+
* system, false otherwise.
43+
*/
44+
export function isDartFrogCliInstalled(): boolean {
45+
return readDartFrogVersion() !== undefined;
46+
}

extensions/vscode/src/utils/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./cli-version";

0 commit comments

Comments
 (0)