-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
patch(cli-utils): Add fallbacks for version checks to
doctor
command (
#358)
- Loading branch information
Showing
3 changed files
with
75 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@gql.tada/cli-utils": patch | ||
--- | ||
|
||
Add fallback version checks to `doctor` command. |
59 changes: 59 additions & 0 deletions
59
packages/cli-utils/src/commands/doctor/helpers/versions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { createRequire } from 'node:module'; | ||
import fs from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
|
||
export interface PackageJson { | ||
name?: string; | ||
dependencies?: Record<string, string>; | ||
devDependencies?: Record<string, string>; | ||
} | ||
|
||
export const readPackageJson = async (): Promise<PackageJson> => { | ||
const packageJsonPath = path.resolve(process.cwd(), 'package.json'); | ||
const file = path.resolve(packageJsonPath); | ||
return JSON.parse(await fs.readFile(file, 'utf-8')); | ||
}; | ||
|
||
export const getTypeScriptVersion = async (meta: PackageJson): Promise<string | null> => { | ||
const pkg = 'typescript'; | ||
if (meta.devDependencies?.[pkg]) { | ||
return meta.devDependencies[pkg]; | ||
} else if (meta.dependencies?.[pkg]) { | ||
return meta.dependencies[pkg]; | ||
} | ||
try { | ||
return (await import(pkg)).version || null; | ||
} catch (_error) { | ||
return null; | ||
} | ||
}; | ||
|
||
export const getGraphQLSPVersion = async (meta: PackageJson): Promise<string | null> => { | ||
const pkg = '@0no-co/graphqlsp'; | ||
if (meta.devDependencies?.[pkg]) { | ||
return meta.devDependencies[pkg]; | ||
} else if (meta.dependencies?.[pkg]) { | ||
return meta.dependencies[pkg]; | ||
} | ||
try { | ||
// NOTE: Resolved from current folder, since it's a child dependency | ||
return createRequire(__dirname)(`${pkg}/package.json`)?.version || null; | ||
} catch (_error) { | ||
return null; | ||
} | ||
}; | ||
|
||
export const getGqlTadaVersion = async (meta: PackageJson): Promise<string | null> => { | ||
const pkg = 'gql.tada'; | ||
if (meta.devDependencies?.[pkg]) { | ||
return meta.devDependencies[pkg]; | ||
} else if (meta.dependencies?.[pkg]) { | ||
return meta.dependencies[pkg]; | ||
} | ||
try { | ||
// NOTE: Resolved from working directory, since it's a parent dependency | ||
return createRequire(process.cwd())(`${pkg}/package.json`)?.version || null; | ||
} catch (_error) { | ||
return null; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters