-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): show prompt if local version doesn't match remote
- Loading branch information
Showing
4 changed files
with
140 additions
and
28 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
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
93 changes: 93 additions & 0 deletions
93
packages/sanity/src/_internal/cli/util/compareStudioDependencyVersions.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,93 @@ | ||
import path from 'node:path' | ||
|
||
import resolveFrom from 'resolve-from' | ||
import semver from 'semver' | ||
|
||
import {readPackageJson} from './readPackageJson' | ||
|
||
// TODO: support custom hostname | ||
const AUTO_UPDATE_PACKAGES = { | ||
'react': { | ||
url: 'https://api.sanity.work/v1/modules/react/^18', | ||
shouldAddPathPrefix: true, | ||
}, | ||
'react-dom': {url: 'https://api.sanity.work/v1/modules/react-dom/^18', shouldAddPathPrefix: true}, | ||
'styled-components': { | ||
url: 'https://api.sanity.work/v1/modules/styled-components/^6', | ||
shouldAddPathPrefix: false, | ||
}, | ||
'sanity': {url: 'https://api.sanity.work/v1/modules/sanity/^3', shouldAddPathPrefix: true}, | ||
'@sanity/vision': { | ||
url: 'https://api.sanity.work/v1/modules/@sanity__vision/^3', | ||
shouldAddPathPrefix: false, | ||
}, | ||
} | ||
|
||
// TODO: replace this with a manifest somewhere | ||
export const AUTO_UPDATES_IMPORTMAP = { | ||
imports: Object.keys(AUTO_UPDATE_PACKAGES).reduce<Record<string, string>>((acc, curr) => { | ||
const key = curr as keyof typeof AUTO_UPDATE_PACKAGES | ||
const pkg = AUTO_UPDATE_PACKAGES[key] | ||
|
||
acc[key] = pkg.url | ||
if (pkg.shouldAddPathPrefix) { | ||
acc[`${key}/`] = `${pkg.url}/` | ||
} | ||
|
||
return acc | ||
}, {}), | ||
} | ||
|
||
async function getRemoteResolvedVersion(url: string) { | ||
try { | ||
const res = await fetch(url, {method: 'HEAD', redirect: 'manual'}) | ||
return res.headers.get('x-resolved-version') | ||
} catch (err) { | ||
throw new Error(`Failed to fetch remote version for ${url}: ${err.message}`) | ||
} | ||
} | ||
|
||
export async function compareStudioDependencyVersions(workDir: string): Promise< | ||
| { | ||
error?: { | ||
pkg: string | ||
installed: string | ||
remote: string | ||
} | ||
} | ||
| undefined | ||
> { | ||
const manifest = readPackageJson(path.join(workDir, 'package.json')) | ||
const dependencies = {...manifest.dependencies, ...manifest.devDependencies} | ||
|
||
for (const [pkg, value] of Object.entries(AUTO_UPDATE_PACKAGES)) { | ||
const resolvedVersion = await getRemoteResolvedVersion(value.url) | ||
|
||
if (!resolvedVersion) { | ||
throw new Error(`Failed to fetch remote version for ${value.url}`) | ||
} | ||
|
||
const dependency = dependencies[pkg] | ||
const manifestPath = resolveFrom.silent(workDir, path.join(pkg, 'package.json')) | ||
|
||
const installed = semver.coerce( | ||
manifestPath ? readPackageJson(manifestPath).version : dependency.replace(/[\D.]/g, ''), | ||
) | ||
|
||
if (!installed) { | ||
throw new Error(`Failed to parse installed version for ${pkg}`) | ||
} | ||
|
||
if (!semver.eq(resolvedVersion, installed.version)) { | ||
return { | ||
error: { | ||
pkg, | ||
installed: installed.version, | ||
remote: resolvedVersion, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
return undefined | ||
} |
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,18 @@ | ||
import fs from 'node:fs' | ||
|
||
import {type PackageJson} from '@sanity/cli' | ||
|
||
/** | ||
* Read the `package.json` file at the given path | ||
* | ||
* @param filePath - Path to package.json to read | ||
* @returns The parsed package.json | ||
*/ | ||
export function readPackageJson(filePath: string): PackageJson { | ||
try { | ||
// eslint-disable-next-line no-sync | ||
return JSON.parse(fs.readFileSync(filePath, 'utf8')) | ||
} catch (err) { | ||
throw new Error(`Failed to read "${filePath}": ${err.message}`) | ||
} | ||
} |