Skip to content

Commit

Permalink
Reduce usage of new URL(...) for local paths (#2169)
Browse files Browse the repository at this point in the history
* Reduce usage of new URL for local paths

* Avoid manipulating strings

* Simplify types in upgrade command

* Reduce usage of new URL for local paths
  • Loading branch information
frandiox committed May 30, 2024
1 parent ebc8a26 commit 91c7698
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 132 deletions.
3 changes: 2 additions & 1 deletion packages/cli/src/commands/hydrogen/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
currentProcessIsGlobal,
inferPackageManagerForGlobalCLI,
} from '@shopify/cli-kit/node/is-global';
import {getPkgJsonPath} from '../../lib/build.js';

const FLAG_MAP = {f: 'force'} as Record<string, string>;

Expand Down Expand Up @@ -132,7 +133,7 @@ export async function runInit(
// Resolving the CLI package from a local directory might fail because
// this code could be run from a global dependency (e.g. on `npm create`).
// Therefore, pass the known path to the package.json directly from here:
fileURLToPath(new URL('../../../package.json', import.meta.url)),
await getPkgJsonPath(),
isGlobal ? 'cli' : 'cliHydrogen',
);

Expand Down
61 changes: 25 additions & 36 deletions packages/cli/src/commands/hydrogen/upgrade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
renderTasks,
} from '@shopify/cli-kit/node/ui';
import {mockAndCaptureOutput} from '@shopify/cli-kit/node/testing/output';
import {type PackageJson} from '@shopify/cli-kit/node/node-package-manager';
import {
buildUpgradeCommandArgs,
displayConfirmation,
Expand All @@ -32,13 +33,12 @@ import {
getSelectedRelease,
runUpgrade,
type CumulativeRelease,
type Dependencies,
type Release,
upgradeNodeModules,
getChangelog,
displayDevUpgradeNotice,
} from './upgrade.js';
import {type PackageJson} from 'type-fest';
import {getSkeletonSourceDir} from '../../lib/build.js';

vi.mock('@shopify/cli-kit/node/session');

Expand Down Expand Up @@ -66,19 +66,12 @@ beforeEach(() => {
outputMock.clear();
});

beforeAll(() => {
process.env.FORCE_CHANGELOG_SOURCE = 'local';
});

afterAll(() => {
delete process.env.FORCE_CHANGELOG_SOURCE;
});

function createOutdatedSkeletonPackageJson() {
async function createOutdatedSkeletonPackageJson() {
const require = createRequire(import.meta.url);
const packageJson = require(fileURLToPath(
new URL('../../../../../templates/skeleton/package.json', import.meta.url),
)) as PackageJson;
const packageJson: PackageJson = require(joinPath(
getSkeletonSourceDir(),
'package.json',
));

if (!packageJson) throw new Error('Could not parse package.json');
if (!packageJson?.dependencies)
Expand Down Expand Up @@ -107,11 +100,8 @@ async function inTemporaryHydrogenRepo(
packageJson,
}: {
cleanGitRepo?: boolean;
packageJson?: null | Record<string, unknown>;
} = {
cleanGitRepo: true,
packageJson: null,
},
packageJson?: PackageJson;
} = {cleanGitRepo: true},
) {
return inTemporaryDirectory(async (tmpDir) => {
// init the git repo
Expand Down Expand Up @@ -162,7 +152,8 @@ function increasePatchVersion(depName: string, deps: Record<string, string>) {

describe('upgrade', async () => {
// Create an outdated skeleton package.json for all tests
const OUTDATED_HYDROGEN_PACKAGE_JSON = createOutdatedSkeletonPackageJson();
const OUTDATED_HYDROGEN_PACKAGE_JSON =
await createOutdatedSkeletonPackageJson();

describe('checkIsGitRepo', () => {
it('renders an error message when not in a git repo', async () => {
Expand Down Expand Up @@ -193,7 +184,7 @@ describe('upgrade', async () => {
'valid package.json',
);
},
{packageJson: null},
{packageJson: undefined},
);
});

Expand Down Expand Up @@ -656,7 +647,7 @@ describe('upgrade', async () => {
const currentDependencies = {
...OUTDATED_HYDROGEN_PACKAGE_JSON.dependencies,
...OUTDATED_HYDROGEN_PACKAGE_JSON.devDependencies,
} as Dependencies;
};

await upgradeNodeModules({
appPath,
Expand All @@ -683,7 +674,7 @@ describe('upgrade', async () => {
const currentDependencies = {
...OUTDATED_HYDROGEN_PACKAGE_JSON.dependencies,
...OUTDATED_HYDROGEN_PACKAGE_JSON.devDependencies,
} as Dependencies;
};

const result: string[] = [
'@shopify/hydrogen@2023.10.0',
Expand Down Expand Up @@ -715,7 +706,7 @@ describe('upgrade', async () => {
...OUTDATED_HYDROGEN_PACKAGE_JSON.devDependencies,
'@remix-run/dev': '1.2.0',
'@remix-run/css-bundle': '1.7.0',
} as Dependencies;
};

const result: string[] = [
'@shopify/cli-hydrogen@6.0.0',
Expand Down Expand Up @@ -750,7 +741,7 @@ describe('upgrade', async () => {
...OUTDATED_HYDROGEN_PACKAGE_JSON.devDependencies,
'@remix-run/dev': '1.8.0',
'@remix-run/css-bundle': '1.8.0',
} as Dependencies;
};

const result: string[] = [
'@shopify/cli-hydrogen@6.0.0',
Expand Down Expand Up @@ -784,7 +775,7 @@ describe('upgrade', async () => {
'@remix-run/react': '2.2.0',
...OUTDATED_HYDROGEN_PACKAGE_JSON.devDependencies,
'@remix-run/dev': '2.2.0',
} as Dependencies;
};

const result: string[] = [
'@shopify/cli-hydrogen@6.0.0',
Expand Down Expand Up @@ -821,10 +812,9 @@ describe('upgrade', async () => {
'@remix-run/react': '2.1.0',
...OUTDATED_HYDROGEN_PACKAGE_JSON.devDependencies,
'@remix-run/dev': '2.1.0',
} as Dependencies;

// simulate a missing required dependency
delete currentDependencies['typescript'];
// simulate a missing required dependency
typescript: '',
};

const result: string[] = [
'@shopify/cli-hydrogen@6.0.0',
Expand Down Expand Up @@ -853,10 +843,9 @@ describe('upgrade', async () => {
'@remix-run/react': '2.1.0',
...OUTDATED_HYDROGEN_PACKAGE_JSON.devDependencies,
'@remix-run/dev': '2.1.0',
} as Dependencies;

// simulate a missing required dependency
delete currentDependencies['typescript'];
// simulate a missing required dependency
typescript: '',
};

const result: string[] = [
'@shopify/cli-hydrogen@6.0.0',
Expand Down Expand Up @@ -889,7 +878,7 @@ describe('upgrade', async () => {
...OUTDATED_HYDROGEN_PACKAGE_JSON.devDependencies,
'@remix-run/dev': '2.1.0',
typescript: '5.3.0', // more up-to-date than that of 2023.10.0
} as Dependencies;
};

const result: string[] = [
'@shopify/cli-hydrogen@6.0.0',
Expand Down Expand Up @@ -918,7 +907,7 @@ describe('upgrade', async () => {
...OUTDATED_HYDROGEN_PACKAGE_JSON.devDependencies,
'@remix-run/dev': '2.1.0',
'@shopify/hydrogen': 'next',
} as Dependencies;
};

const result: string[] = [
'@shopify/cli-hydrogen@6.0.0',
Expand Down
Loading

0 comments on commit 91c7698

Please sign in to comment.