-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a4d2e5
commit bb39a03
Showing
6 changed files
with
185 additions
and
84 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 |
---|---|---|
|
@@ -1749,4 +1749,4 @@ | |
} | ||
}, | ||
"version": "9.0.2" | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import {describe, it, expect} from 'vitest'; | ||
import { | ||
inTemporaryDirectory, | ||
writeFile, | ||
fileExists, | ||
} from '@shopify/cli-kit/node/fs'; | ||
import {createRequire} from 'node:module'; | ||
import {execa} from 'execa'; | ||
import {getHydrogenVersion} from './get-version.js'; | ||
import {PackageJson} from 'type-fest'; | ||
import {joinPath} from '@shopify/cli-kit/node/path'; | ||
import {getSkeletonSourceDir} from './build.js'; | ||
|
||
/** | ||
* Creates a temporary directory with a git repo and a package.json | ||
*/ | ||
async function inTemporaryHydrogenRepo( | ||
cb: (tmpDir: string) => Promise<void>, | ||
{ | ||
cleanGitRepo, | ||
packageJson, | ||
}: { | ||
cleanGitRepo?: boolean; | ||
packageJson?: PackageJson; | ||
} = {cleanGitRepo: true}, | ||
) { | ||
return inTemporaryDirectory(async (tmpDir) => { | ||
// init the git repo | ||
await execa('git', ['init'], {cwd: tmpDir}); | ||
|
||
if (packageJson) { | ||
const packageJsonPath = joinPath(tmpDir, 'package.json'); | ||
await writeFile(packageJsonPath, JSON.stringify(packageJson)); | ||
expect(await fileExists(packageJsonPath)).toBeTruthy(); | ||
} | ||
|
||
// expect to be a git repo | ||
expect(await fileExists(joinPath(tmpDir, '/.git/config'))).toBeTruthy(); | ||
|
||
if (cleanGitRepo) { | ||
await execa('git', ['add', 'package.json'], {cwd: tmpDir}); | ||
|
||
if (process.env.NODE_ENV === 'test' && process.env.CI) { | ||
await execa('git', ['config', 'user.email', '[email protected]'], { | ||
cwd: tmpDir, | ||
}); | ||
await execa('git', ['config', 'user.name', 'Hydrogen Test'], { | ||
cwd: tmpDir, | ||
}); | ||
} | ||
await execa('git', ['commit', '-m', 'initial commit'], {cwd: tmpDir}); | ||
} | ||
|
||
await cb(tmpDir); | ||
}); | ||
} | ||
|
||
async function createOutdatedSkeletonPackageJson() { | ||
const require = createRequire(import.meta.url); | ||
const packageJson: PackageJson = require(joinPath( | ||
getSkeletonSourceDir(), | ||
'package.json', | ||
)); | ||
|
||
if (!packageJson) throw new Error('Could not parse package.json'); | ||
if (!packageJson?.dependencies) | ||
throw new Error('Could not parse package.json dependencies'); | ||
if (!packageJson?.devDependencies) | ||
throw new Error('Could not parse package.json devDependencies'); | ||
|
||
// bump the versions to be outdated | ||
packageJson.dependencies['@shopify/hydrogen'] = '^2023.1.6'; | ||
packageJson.dependencies['@remix-run/react'] = '1.12.0'; | ||
packageJson.devDependencies['@shopify/cli-hydrogen'] = '^4.0.8'; | ||
packageJson.devDependencies['@shopify/remix-oxygen'] = '^1.0.3'; | ||
packageJson.devDependencies['@remix-run/dev'] = '1.12.0'; | ||
packageJson.devDependencies['typescript'] = '^4.9.5'; | ||
|
||
return packageJson; | ||
} | ||
|
||
describe('getHydrogenVersion', async () => { | ||
const OUTDATED_HYDROGEN_PACKAGE_JSON = | ||
await createOutdatedSkeletonPackageJson(); | ||
|
||
it('throws if no package.json is found', async () => { | ||
await inTemporaryHydrogenRepo( | ||
async (appPath) => { | ||
await expect(getHydrogenVersion({appPath})).rejects.toThrowError( | ||
'valid package.json', | ||
); | ||
}, | ||
{packageJson: undefined}, | ||
); | ||
}); | ||
|
||
it('throws if no hydrogen version is found in package.json', async () => { | ||
await inTemporaryHydrogenRepo( | ||
async (appPath) => { | ||
await expect(getHydrogenVersion({appPath})).rejects.toThrowError( | ||
'version in package.json', | ||
); | ||
}, | ||
{ | ||
cleanGitRepo: true, | ||
packageJson: { | ||
name: 'some-name', | ||
dependencies: {}, | ||
}, | ||
}, | ||
); | ||
}); | ||
|
||
it('returns the current hydrogen version from the package.json', async () => { | ||
await inTemporaryHydrogenRepo( | ||
async (appPath) => { | ||
const hydrogen = await getHydrogenVersion({appPath}); | ||
|
||
expect(hydrogen).toBeDefined(); | ||
expect(hydrogen.currentVersion).toMatch('^2023.1.6'); | ||
expect(hydrogen.currentDependencies).toMatchObject({ | ||
...OUTDATED_HYDROGEN_PACKAGE_JSON.dependencies, | ||
...OUTDATED_HYDROGEN_PACKAGE_JSON.devDependencies, | ||
}); | ||
}, | ||
{ | ||
cleanGitRepo: true, | ||
packageJson: OUTDATED_HYDROGEN_PACKAGE_JSON, | ||
}, | ||
); | ||
}); | ||
}); |
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,49 @@ | ||
import { AbortError } from "@shopify/cli-kit/node/error"; | ||
import { readFile } from "@shopify/cli-kit/node/fs"; | ||
import { joinPath } from "@shopify/cli-kit/node/path"; | ||
import { PackageJson } from "type-fest"; | ||
import { getProjectPaths } from "./remix-config.js"; | ||
|
||
interface Return { | ||
currentVersion: string; | ||
currentDependencies: Record<string, string>; | ||
} | ||
|
||
/** | ||
* Gets the current @shopify/hydrogen version from the app's package.json | ||
*/ | ||
export async function getHydrogenVersion({appPath}: {appPath: string}): Promise<Return> { | ||
const {root} = getProjectPaths(appPath); | ||
const packageJsonPath = joinPath(root, 'package.json'); | ||
|
||
let packageJson: PackageJson | undefined; | ||
|
||
try { | ||
packageJson = JSON.parse(await readFile(packageJsonPath)); | ||
} catch { | ||
throw new AbortError( | ||
'Could not find a valid package.json', | ||
'Please make sure you are running the command in a npm project', | ||
); | ||
} | ||
|
||
const currentDependenciesWithEmptyValues = { | ||
...packageJson?.dependencies, | ||
...packageJson?.devDependencies, | ||
}; | ||
|
||
const currentDependencies = Object.fromEntries( | ||
Object.entries(currentDependenciesWithEmptyValues).filter(([_, value]) => value) | ||
) as Record<string, string>; | ||
|
||
const currentVersion = currentDependencies['@shopify/hydrogen']; | ||
|
||
if (!currentVersion) { | ||
throw new AbortError( | ||
'Could not find a valid Hydrogen version in package.json', | ||
'Please make sure you are running the command in a Hydrogen project', | ||
); | ||
} | ||
|
||
return {currentVersion, currentDependencies}; | ||
} |