MCPcopy Create free account
hub / github.com/Dart-Code/Dart-Code / getSdkVersion

Function getSdkVersion

src/shared/utils/fs.ts:378–434  ·  view source on GitHub ↗
(logger: Logger, { sdkRoot }: { sdkRoot?: string })

Source from the content-addressed store, hash-verified

376}
377
378export function getSdkVersion(logger: Logger, { sdkRoot }: { sdkRoot?: string }): string | undefined {
379 if (!sdkRoot)
380 return undefined;
381
382 // Try to read the new JSON file for Flutter. Don't use exist checks as it races,
383 // just try to read and see if we get contents.
384 const jsonVersionFile = path.join(sdkRoot, "bin", "cache", "flutter.version.json");
385 let jsonVersionFileContent: string | undefined;
386 try {
387 jsonVersionFileContent = fs.readFileSync(jsonVersionFile, "utf8").trim();
388 } catch {
389 }
390
391 if (jsonVersionFileContent) {
392 let versionData: any;
393 try {
394 versionData = JSON.parse(jsonVersionFileContent);
395 } catch (e) {
396 logger.error(`${jsonVersionFile} existed, but could not be parsed as JSON (${e}): ${jsonVersionFileContent}, falling back to legacy file`);
397 }
398
399 if (versionData) {
400 const flutterVersion = versionData.flutterVersion;
401 if (typeof flutterVersion === "string") {
402 const validVersion = nullToUndefined(semver.valid(flutterVersion));
403 if (validVersion) {
404 return validVersion;
405 } else {
406 logger.error(`${jsonVersionFile} did not contain a valid "flutterVersion": ${jsonVersionFileContent}, falling back to legacy file`);
407 }
408 } else {
409 logger.error(`${jsonVersionFile} did not contain a "flutterVersion": ${jsonVersionFileContent}, falling back to legacy file`);
410 }
411 }
412 }
413
414 const versionFile = path.join(sdkRoot, "version");
415 if (!fs.existsSync(versionFile))
416 return undefined;
417 try {
418 return nullToUndefined(
419 semver.valid(
420 fs
421 .readFileSync(versionFile, "utf8")
422 .trim()
423 .split("\n")
424 .filter((l) => l)
425 .filter((l) => l.trim().substr(0, 1) !== "#")
426 .join("\n")
427 .trim()
428 )
429 );
430 } catch (e) {
431 logger.error(e);
432 return undefined;
433 }
434}
435

Callers 2

promptForSdkFunction · 0.90
scanWorkspaceMethod · 0.90

Calls 2

nullToUndefinedFunction · 0.90
errorMethod · 0.65

Tested by

no test coverage detected