Skip to content

Commit

Permalink
fix(cli): read version in runtime instead of bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Nov 7, 2022
1 parent c2978cd commit d5f5afc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
Expand Up @@ -7,7 +7,7 @@ import decompress from 'decompress'
import resolveFrom from 'resolve-from'
import validateNpmPackageName from 'validate-npm-package-name'
import {absolutify, pathIsEmpty} from '@sanity/util/fs'
import * as pkg from '../../../package.json'
import {getCliVersion} from '../../util/getCliVersion'
import {readJson} from '../../util/readJson'
import {dynamicRequire} from '../../util/dynamicRequire'
import {SanityJson} from '../../types'
Expand Down Expand Up @@ -36,6 +36,7 @@ export async function bootstrapFromTemplate(
dependencies: any
}> {
const {prompt, workDir} = context
const cliVersion = await getCliVersion()
let inProjectContext = false
try {
const projectManifest = await readJson<SanityJson>(path.join(workDir, 'sanity.json'))
Expand Down Expand Up @@ -81,7 +82,7 @@ export async function bootstrapFromTemplate(

if (minimumBaseVersion) {
debug('Template requires Sanity version %s', minimumBaseVersion)
const installed = getSanityVersion(workDir)
const installed = getSanityVersion(workDir, cliVersion)
debug('Installed Sanity version is %s', installed)

if (semver.lt(installed, minimumBaseVersion)) {
Expand All @@ -93,11 +94,11 @@ export async function bootstrapFromTemplate(

if (minimumCliVersion) {
debug('Template requires Sanity CLI version %s', minimumCliVersion)
debug('Installed CLI version is %s', pkg.version)
debug('Installed CLI version is %s', cliVersion)

if (semver.lt(pkg.version, minimumCliVersion)) {
if (semver.lt(cliVersion, minimumCliVersion)) {
throw new Error(
`Template requires @sanity/cli at version ${minimumCliVersion}, installed is ${pkg.version}`
`Template requires @sanity/cli at version ${minimumCliVersion}, installed is ${cliVersion}`
)
}
}
Expand Down Expand Up @@ -186,8 +187,8 @@ function parseJson<T = any>(json: string): T | undefined {
}
}

function getSanityVersion(workDir: string): string {
function getSanityVersion(workDir: string, fallback: string): string {
// This is only used in v2, thus `@sanity/base`
const basePkg = resolveFrom.silent(workDir, '@sanity/base/package.json')
return basePkg ? dynamicRequire(basePkg).version : pkg.version
return basePkg ? dynamicRequire(basePkg).version : fallback
}
Expand Up @@ -3,10 +3,10 @@ import promiseProps from 'promise-props-recursive'
import semver from 'semver'
import semverCompare from 'semver-compare'
import getLatestVersion from 'get-latest-version'
import * as pkg from '../../../package.json'
import type {CliCommandContext, PackageJson} from '../../types'
import {dynamicRequire} from '../../util/dynamicRequire'
import {getLocalVersion} from '../../util/getLocalVersion'
import {getCliVersion} from '../../util/getCliVersion'

/*
* The `sanity upgrade` command should only be responsible for upgrading the
Expand Down Expand Up @@ -62,6 +62,7 @@ export async function findSanityModuleVersions(
): Promise<ModuleVersionResult[]> {
const {spinner} = context.output
const {target, includeCli} = {...defaultOptions, ...options}
const cliVersion = await getCliVersion()

// Declared @sanity modules and their wanted version ranges in package.json
const sanityModules = filterSanityModules(getLocalManifest(context.workDir))
Expand All @@ -70,7 +71,7 @@ export async function findSanityModuleVersions(
const resolveOpts = {includeCli, target}
const spin = spinner('Resolving latest versions').start()
const versions = await promiseProps<ModuleVersionInfo[]>(
buildPackageArray(sanityModules, context.workDir, resolveOpts)
buildPackageArray(sanityModules, context.workDir, resolveOpts, cliVersion)
)

const packages = Object.values(versions)
Expand Down Expand Up @@ -114,18 +115,19 @@ function filterSanityModules(manifest: Partial<PackageJson>): Record<string, str
function buildPackageArray(
packages: Record<string, string>,
workDir: string,
options: FindModuleVersionOptions = {}
options: FindModuleVersionOptions = {},
cliVersion: string
): PromisedModuleVersionInfo[] {
const {includeCli, target} = options

const modules = []
if (includeCli) {
const [cliMajor] = pkg.version.split('.')
const latest = tryFindLatestVersion(pkg.name, target || `^${cliMajor}`)
const [cliMajor] = cliVersion.split('.')
const latest = tryFindLatestVersion('@sanity/cli', target || `^${cliMajor}`)
modules.push({
name: pkg.name,
declared: `^${pkg.version}`,
installed: trimHash(pkg.version),
name: '@sanity/cli',
declared: `^${cliVersion}`,
installed: trimHash(cliVersion),
latest: latest.then((versions) => versions.latest),
latestInRange: latest.then((versions) => versions.latestInRange),
isPinned: false,
Expand Down
6 changes: 4 additions & 2 deletions packages/@sanity/cli/src/run.ts
@@ -1,5 +1,7 @@
import path from 'path'
import {version} from '../package.json'
import {runCli} from './cli'
import {getCliVersion} from './util/getCliVersion'

runCli(path.join(__dirname, '..'), {cliVersion: version})
getCliVersion().then((cliVersion) => {
runCli(path.join(__dirname, '..'), {cliVersion})
})
21 changes: 21 additions & 0 deletions packages/@sanity/cli/src/util/getCliVersion.ts
@@ -0,0 +1,21 @@
import fs from 'fs/promises'
import path from 'path'
import pkgDir from 'pkg-dir'

export async function getCliVersion(): Promise<string> {
const cliPath = pkgDir.sync(__dirname)

if (!cliPath) {
throw new Error('Unable to resolve root of @sanity/cli module')
}

let data: string | undefined
try {
data = await fs.readFile(path.join(cliPath, 'package.json'), 'utf-8')
} catch (err) {
throw new Error(`Unable to read @sanity/cli/package.json: ${err.message}`)
}

const pkg = JSON.parse(data)
return pkg.version
}

0 comments on commit d5f5afc

Please sign in to comment.