Skip to content

Commit b73d807

Browse files
jycouetdummdidumm
andauthored
fix: Better detection version (#2801)
* step 1 of version * start managing things * adding semver to test * let's not redo semver! * better fallback * not only this ;) * Update version.ts --------- Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
1 parent 2e2c6c3 commit b73d807

File tree

5 files changed

+132
-12
lines changed

5 files changed

+132
-12
lines changed

packages/svelte-vscode/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,10 @@
741741
"devDependencies": {
742742
"@types/lodash": "^4.14.116",
743743
"@types/node": "^18.0.0",
744+
"@types/semver": "^7.7.0",
744745
"@types/vscode": "^1.67",
745746
"js-yaml": "^3.14.0",
747+
"semver": "^7.7.2",
746748
"tslib": "^2.4.0",
747749
"typescript": "^5.8.2",
748750
"vitest": "^3.2.4",

packages/svelte-vscode/src/sveltekit/utils.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { TextDecoder } from 'util';
22
import * as path from 'path';
33
import { Uri, workspace } from 'vscode';
44
import type { GenerateConfig } from './generateFiles/types';
5+
import { atLeast } from '../version';
56

67
export async function fileExists(file: string) {
78
try {
@@ -31,10 +32,20 @@ export async function checkProjectKind(path: string): Promise<GenerateConfig['ki
3132
const jsconfig = await findFile(path, 'jsconfig.json');
3233

3334
const svelteVersion = await getVersionFromPackageJson('svelte');
34-
const withRunes = svelteVersion ? versionAtLeast(svelteVersion, 5) : true;
35+
const withRunes = atLeast({
36+
packageName: 'svelte',
37+
versionMin: '5',
38+
versionToCheck: svelteVersion ?? '',
39+
fallback: true
40+
});
3541

3642
const svelteKitVersion = await getVersionFromPackageJson('@sveltejs/kit');
37-
let withProps = svelteKitVersion ? versionAtLeast(svelteKitVersion, 2, 16) : true;
43+
let withProps = atLeast({
44+
packageName: '@sveltejs/kit',
45+
versionMin: '2.16',
46+
versionToCheck: svelteKitVersion ?? '',
47+
fallback: true
48+
});
3849

3950
const withTs = !!tsconfig && (!jsconfig || tsconfig.length >= jsconfig.length);
4051
let withSatisfies = false;
@@ -44,7 +55,12 @@ export async function checkProjectKind(path: string): Promise<GenerateConfig['ki
4455
paths: [tsconfig]
4556
});
4657
const { version } = require(packageJSONPath);
47-
withSatisfies = version ? versionAtLeast(version, 4, 9) : true;
58+
withSatisfies = atLeast({
59+
packageName: 'typescript',
60+
versionMin: '4.9',
61+
versionToCheck: version,
62+
fallback: true
63+
});
4864
} catch (e) {
4965
withSatisfies = true;
5066
}
@@ -58,14 +74,6 @@ export async function checkProjectKind(path: string): Promise<GenerateConfig['ki
5874
};
5975
}
6076

61-
function versionAtLeast(version: string, major: number, minor?: number): boolean {
62-
const [majorVersion, minorVersion] = version.split('.');
63-
return (
64-
(Number(majorVersion) === major && Number(minorVersion) >= (minor ?? 0)) ||
65-
Number(majorVersion) > major
66-
);
67-
}
68-
6977
export async function getVersionFromPackageJson(packageName: string): Promise<string | undefined> {
7078
const packageJsonList = await workspace.findFiles('**/package.json', '**/node_modules/**');
7179

packages/svelte-vscode/src/version.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { lt, coerce, gte } from 'semver';
2+
3+
/**
4+
* @example
5+
* const supported = atLeast({
6+
* packageName: 'node',
7+
* versionMin: '18.3',
8+
* versionToCheck: process.versions.node
9+
* fallback: true // optional
10+
* });
11+
*/
12+
export function atLeast(o: {
13+
packageName: string;
14+
versionMin: string;
15+
versionToCheck: string;
16+
fallback: boolean;
17+
}): boolean;
18+
export function atLeast(o: {
19+
packageName: string;
20+
versionMin: string;
21+
versionToCheck: string;
22+
fallback?: undefined;
23+
}): boolean | undefined;
24+
25+
// Implementation
26+
export function atLeast(o: {
27+
packageName: string;
28+
versionMin: string;
29+
versionToCheck: string;
30+
fallback?: boolean;
31+
}): boolean | undefined {
32+
const { packageName, versionMin, versionToCheck, fallback } = o;
33+
if (versionToCheck === undefined || versionToCheck === '') return fallback;
34+
35+
if (
36+
versionToCheck.includes('latest') ||
37+
versionToCheck.includes('catalog:') ||
38+
versionToCheck.includes('http')
39+
) {
40+
console.warn(`Version '${versionToCheck}' for '${packageName}' is not supported`);
41+
return fallback;
42+
}
43+
try {
44+
const vMin = coerce(versionMin);
45+
const vToCheck = coerce(versionToCheck);
46+
if (vMin && vToCheck) {
47+
return gte(vToCheck, vMin);
48+
}
49+
} catch (error) {}
50+
return fallback;
51+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { expect, describe, it } from 'vitest';
2+
import { atLeast } from '../src/version';
3+
4+
describe('atLeast', () => {
5+
const combinationsAtLeast = [
6+
{ min: '5', version: '>=5', supported: true },
7+
{ min: '5', version: '>=5.0.0', supported: true },
8+
{ min: '5', version: '5.0.0', supported: true },
9+
{ min: '5', version: '5', supported: true },
10+
{ min: '5', version: '4', supported: false },
11+
{ min: '5', version: '4.9', supported: false },
12+
{ min: '5', version: '', supported: undefined },
13+
{ min: '5', version: 'catalog:', supported: undefined },
14+
{ min: '5', version: 'latest', supported: undefined },
15+
{ min: '5', version: 'latest', fallback: true, supported: true },
16+
{ min: '5', version: 'latest', fallback: false, supported: false }
17+
];
18+
it.each(combinationsAtLeast)(
19+
'(min $min, $version, $fallback) => $supported',
20+
({ min, version, supported, fallback }) => {
21+
if (fallback !== undefined) {
22+
expect(
23+
atLeast({
24+
packageName: 'myPkg',
25+
versionMin: min,
26+
versionToCheck: version,
27+
fallback
28+
})
29+
).toEqual(supported);
30+
} else {
31+
expect(
32+
atLeast({
33+
packageName: 'myPkg',
34+
versionMin: min,
35+
versionToCheck: version
36+
})
37+
).toEqual(supported);
38+
}
39+
}
40+
);
41+
});

pnpm-lock.yaml

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)