Skip to content

Commit

Permalink
Check remix versions from project root (#2157)
Browse files Browse the repository at this point in the history
* Check remix versions from project root

* Fix tests

* fix format

* Hardcode required remix version

* Hardcode required remix version

* fix tests

* fix tests

* Export variable
  • Loading branch information
isaacroldan committed May 30, 2024
1 parent 2cfe725 commit 699f0d8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/lib/classic-compiler/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export async function runClassicCompilerDev({
});
}

checkRemixVersions();
checkRemixVersions(root);

if (!disableVersionCheck) {
displayDevUpgradeNotice({targetPath: appPath});
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/lib/remix-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {AbortError} from '@shopify/cli-kit/node/error';
import {outputWarn} from '@shopify/cli-kit/node/output';
import {fileExists} from '@shopify/cli-kit/node/fs';
import {muteRemixLogs} from './log.js';
import {getRequiredRemixVersion} from './remix-version-check.js';
import {REQUIRED_REMIX_VERSION} from './remix-version-check.js';
import {findFileWithExtension} from './file.js';
import {getViteConfig} from './vite-config.js';
import {importLocal} from './import-utils.js';
Expand Down Expand Up @@ -45,7 +45,7 @@ export function getProjectPaths(appPath?: string) {
}

export function handleRemixImportFail(): never {
const remixVersion = getRequiredRemixVersion();
const remixVersion = REQUIRED_REMIX_VERSION;
throw new AbortError(
'Could not load Remix packages.',
`Please make sure you have \`@remix-run/dev@${remixVersion}\` installed` +
Expand Down
9 changes: 3 additions & 6 deletions packages/cli/src/lib/remix-version-check.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {describe, it, expect, vi} from 'vitest';
import {mockAndCaptureOutput} from '@shopify/cli-kit/node/testing/output';
import {checkRemixVersions} from './remix-version-check.js';
import {cwd} from '@shopify/cli-kit/node/path';

const requireMock = vi.fn();
vi.mock('node:module', async () => {
Expand All @@ -23,20 +24,16 @@ vi.mock('node:module', async () => {
describe('remix-version-check', () => {
it('does nothing when versions are in sync', () => {
const outputMock = mockAndCaptureOutput();
checkRemixVersions();
checkRemixVersions(cwd());

expect(outputMock.warn()).toBe('');
});

it('warns when versions are out of sync', () => {
const expectedVersion = '42.0.0-test';
vi.mocked(requireMock).mockReturnValueOnce({
// Hydrogen expected version
peerDependencies: {'@remix-run/dev': expectedVersion},
});

const outputMock = mockAndCaptureOutput();
checkRemixVersions();
checkRemixVersions(cwd(), expectedVersion);

const output = outputMock.warn();
expect(output).toMatch(`Hydrogen requires Remix @${expectedVersion}`);
Expand Down
29 changes: 15 additions & 14 deletions packages/cli/src/lib/remix-version-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ import {createRequire} from 'node:module';
import {fileURLToPath} from 'node:url';
import {renderWarning} from '@shopify/cli-kit/node/ui';

export function getRequiredRemixVersion(
require = createRequire(import.meta.url),
) {
const hydrogenPkgJson = require(fileURLToPath(
new URL('../../package.json', import.meta.url),
));

return hydrogenPkgJson.peerDependencies['@remix-run/dev'] as string;
}
export const REQUIRED_REMIX_VERSION = '^2.1.0';

export function checkRemixVersions() {
export function checkRemixVersions(
projectPath: string,
requiredVersionInHydrogen = REQUIRED_REMIX_VERSION,
) {
const require = createRequire(import.meta.url);
const requiredVersionInHydrogen = getRequiredRemixVersion(require);

// Require this after requiring the hydrogen
// package version to avoid breaking test mocks.
Expand All @@ -29,7 +23,7 @@ export function checkRemixVersions() {
'node',
'express',
'eslint-config',
].map((name) => getRemixPackageVersion(require, name));
].map((name) => getRemixPackageVersion(require, name, projectPath));

const outOfSyncPkgs = pkgs.filter(
(pkg) =>
Expand All @@ -55,12 +49,19 @@ export function checkRemixVersions() {
});
}

function getRemixPackageVersion(require: NodeRequire, name: string) {
// When using the global CLI, remix packages are loaded from the project root.
function getRemixPackageVersion(
require: NodeRequire,
name: string,
root: string,
) {
const pkgName = '@remix-run/' + name;
const result = {name: pkgName, version: ''};

try {
const pkgJsonPath = require.resolve(`${pkgName}/package.json`);
const pkgJsonPath = require.resolve(`${pkgName}/package.json`, {
paths: [root],
});
const pkgJson = require(pkgJsonPath);
result.version = pkgJson.version as string;
} catch {
Expand Down

0 comments on commit 699f0d8

Please sign in to comment.