Skip to content

Commit e789065

Browse files
authored
feat(vscode): add CLI version compatibility functions (#747)
1 parent f1aaece commit e789065

File tree

4 files changed

+136
-45
lines changed

4 files changed

+136
-45
lines changed

extensions/vscode/package-lock.json

Lines changed: 42 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extensions/vscode/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,8 @@
8585
"proxyquire": "^2.1.3",
8686
"sinon": "^15.1.2",
8787
"typescript": "^5.1.3"
88+
},
89+
"dependencies": {
90+
"semver": "^7.5.4"
8891
}
8992
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const sinon = require("sinon");
2+
var proxyquire = require("proxyquire");
3+
4+
import { afterEach, beforeEach } from "mocha";
5+
import * as assert from "assert";
6+
7+
suite("readDartFrogVersion", () => {
8+
let cpStub: any;
9+
let cliVersion: any;
10+
11+
beforeEach(() => {
12+
cpStub = {
13+
execSync: sinon.stub(),
14+
};
15+
16+
cliVersion = proxyquire("../../../utils/cli-version", {
17+
// eslint-disable-next-line @typescript-eslint/naming-convention
18+
child_process: cpStub,
19+
});
20+
});
21+
22+
afterEach(() => {
23+
sinon.restore();
24+
});
25+
26+
test("returns the version of Dart Frog CLI installed in the user's system", () => {
27+
cpStub.execSync.returns("0.3.7");
28+
29+
assert.strictEqual(cliVersion.readDartFrogVersion(), "0.3.7");
30+
});
31+
32+
test("returns undefined if Dart Frog CLI is not installed", () => {
33+
cpStub.execSync.throws();
34+
35+
assert.strictEqual(cliVersion.readDartFrogVersion(), undefined);
36+
});
37+
});
38+
39+
suite("isCompatibleCLIVersion", () => {
40+
let cliVersion: any;
41+
42+
beforeEach(() => {
43+
cliVersion = proxyquire("../../../utils/cli-version", {});
44+
});
45+
46+
test("returns true if the version of Dart Frog CLI installed in the user's system is compatible with this extension", () => {
47+
assert.strictEqual(cliVersion.isCompatibleCLIVersion("0.3.8"), true);
48+
assert.strictEqual(cliVersion.isCompatibleCLIVersion("0.3.7"), true);
49+
});
50+
51+
test("returns false if the version of Dart Frog CLI installed in the user's system is not compatible with this extension", () => {
52+
assert.strictEqual(cliVersion.isCompatibleCLIVersion("1.0.0"), false);
53+
assert.strictEqual(cliVersion.isCompatibleCLIVersion("0.3.6"), false);
54+
});
55+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const cp = require("child_process");
2+
const semver = require("semver");
3+
4+
/**
5+
* The semantic version constraints for Dart Frog CLI to be compatible with this
6+
* extension.
7+
*/
8+
const compatibleCLIVersion = ">=0.3.7 <1.0.0";
9+
10+
/**
11+
* Collects the version of Dart Frog CLI installed in the user's system.
12+
*
13+
* @returns {String | undefined} The semantic version of Dart Frog CLI installed
14+
* in the user's system, or null if Dart Frog CLI is not installed.
15+
*/
16+
export function readDartFrogVersion(): String | undefined {
17+
try {
18+
return cp.execSync(`dart_frog --version`);
19+
} catch (error) {
20+
return undefined;
21+
}
22+
}
23+
24+
/**
25+
* Checks if the version of Dart Frog CLI installed in the user's system is
26+
* compatible with this extension.
27+
*
28+
* @param {String} version The semantic version of Dart Frog CLI installed in
29+
* the user's system.
30+
* @returns {Boolean} True if the version of Dart Frog CLI installed in the
31+
* user's system is compatible with this extension, false otherwise.
32+
* @see {@link readDartFrogVersion}, to collect the version of Dart Frog CLI.
33+
*/
34+
export function isCompatibleCLIVersion(version: String): Boolean {
35+
return semver.satisfies(version, compatibleCLIVersion);
36+
}

0 commit comments

Comments
 (0)