-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bundle create-hydrogen package to enhance installation speed (#2184)
* Bundle create-app * Support checking create-app versioning * Refactor check cli version * Remove remaining references to LOCAL_DEV * Fix bundled code and copy assets * Changesets * Use CLI source instead of dist * Add integration test * Ensure create-hydrogen tests run after build * Remove cli-kit usage from create-app * Higher timeout for integration test * Avoid snapshot mismatches on CI * Avoid snapshot mismatches on CI * Changesets
- Loading branch information
Showing
17 changed files
with
375 additions
and
285 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@shopify/cli-hydrogen': patch | ||
--- | ||
|
||
Fix CLI upgrade notification when running from a globla process. |
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,5 @@ | ||
--- | ||
'@shopify/create-hydrogen': major | ||
--- | ||
|
||
The code is now bundled to enhance installation speed. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,125 @@ | ||
import { | ||
checkCurrentCLIVersion, | ||
UPGRADABLE_CLI_NAMES, | ||
} from './check-cli-version.js'; | ||
import {afterEach, beforeEach, describe, it, expect, vi} from 'vitest'; | ||
import {mockAndCaptureOutput} from '@shopify/cli-kit/node/testing/output'; | ||
import { | ||
checkForNewVersion, | ||
findUpAndReadPackageJson, | ||
} from '@shopify/cli-kit/node/node-package-manager'; | ||
|
||
vi.mock('@shopify/cli-kit/node/node-package-manager', () => { | ||
return { | ||
checkForNewVersion: vi.fn(), | ||
packageManagerFromUserAgent: vi.fn(() => 'npm'), | ||
findUpAndReadPackageJson: vi.fn(() => | ||
Promise.resolve({ | ||
path: '', | ||
content: { | ||
name: UPGRADABLE_CLI_NAMES.cliHydrogen, | ||
version: '8.0.0', | ||
}, | ||
}), | ||
), | ||
}; | ||
}); | ||
|
||
describe('checkHydrogenVersion()', () => { | ||
const outputMock = mockAndCaptureOutput(); | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
outputMock.clear(); | ||
}); | ||
|
||
describe('when a current version is available', () => { | ||
it('calls checkForNewVersion', async () => { | ||
await checkCurrentCLIVersion(); | ||
|
||
expect(checkForNewVersion).toHaveBeenCalledWith( | ||
UPGRADABLE_CLI_NAMES.cliHydrogen, | ||
expect.stringMatching(/\d{1,2}\.\d{1,2}\.\d{1,2}/), | ||
); | ||
}); | ||
|
||
describe('and it is up to date', () => { | ||
beforeEach(() => { | ||
vi.mocked(checkForNewVersion).mockResolvedValue(undefined); | ||
}); | ||
|
||
it('returns undefined', async () => { | ||
expect(await checkCurrentCLIVersion()).toBe(undefined); | ||
}); | ||
}); | ||
|
||
describe('and it is using @next or @exprimental', () => { | ||
it('returns undefined', async () => { | ||
vi.mocked(checkForNewVersion).mockResolvedValue('8.0.0'); | ||
vi.mocked(findUpAndReadPackageJson).mockResolvedValueOnce({ | ||
path: '', | ||
content: { | ||
name: UPGRADABLE_CLI_NAMES.cliHydrogen, | ||
version: '0.0.0-next-a188915-20230713115118', | ||
}, | ||
}); | ||
|
||
expect(await checkCurrentCLIVersion()).toBe(undefined); | ||
|
||
vi.mocked(findUpAndReadPackageJson).mockResolvedValueOnce({ | ||
path: '', | ||
content: { | ||
name: UPGRADABLE_CLI_NAMES.cliHydrogen, | ||
version: '0.0.0-experimental-a188915-20230713115118', | ||
}, | ||
}); | ||
|
||
expect(await checkCurrentCLIVersion()).toBe(undefined); | ||
}); | ||
}); | ||
|
||
describe('and a new version is available', () => { | ||
beforeEach(() => { | ||
vi.mocked(checkForNewVersion).mockResolvedValue('9.0.0'); | ||
}); | ||
|
||
it('returns a function that prints the upgrade', async () => { | ||
const showUpgrade = await checkCurrentCLIVersion(); | ||
expect(showUpgrade).toBeInstanceOf(Function); | ||
|
||
showUpgrade!(); | ||
|
||
expect(outputMock.info()).toMatch( | ||
/ info .+ Upgrade available .+ Version 9.0.0.+ running v8.0.0.+`npm create @shopify\/hydrogen@latest`/is, | ||
); | ||
}); | ||
|
||
it('outputs a message to the user with the new version', async () => { | ||
const showUpgrade = await checkCurrentCLIVersion(); | ||
const {currentVersion, newVersion} = showUpgrade!(); | ||
|
||
expect(outputMock.info()).toMatch( | ||
new RegExp( | ||
` info .+ Upgrade available .+ Version ${newVersion.replaceAll( | ||
'.', | ||
'\\.', | ||
)}.+ running v${currentVersion.replaceAll('.', '\\.')}`, | ||
'is', | ||
), | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when no current version can be found', () => { | ||
beforeEach(() => { | ||
vi.mocked(findUpAndReadPackageJson).mockRejectedValue(new Error()); | ||
}); | ||
|
||
it('returns undefined and does not call checkForNewVersion', async () => { | ||
expect(await checkCurrentCLIVersion()).toBe(undefined); | ||
|
||
expect(checkForNewVersion).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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,94 @@ | ||
import {fileURLToPath} from 'node:url'; | ||
import { | ||
type PackageManager, | ||
checkForNewVersion, | ||
findUpAndReadPackageJson, | ||
packageManagerFromUserAgent, | ||
} from '@shopify/cli-kit/node/node-package-manager'; | ||
import {renderInfo} from '@shopify/cli-kit/node/ui'; | ||
import {} from '@shopify/cli-kit/node/path'; | ||
import {isHydrogenMonorepo} from './build.js'; | ||
import {inferPackageManagerForGlobalCLI} from '@shopify/cli-kit/node/is-global'; | ||
|
||
export const UPGRADABLE_CLI_NAMES = { | ||
cli: '@shopify/cli', | ||
cliHydrogen: '@shopify/cli-hydrogen', | ||
createApp: '@shopify/create-hydrogen', | ||
} as const; | ||
|
||
/** | ||
* Checks if a new version of the current package is available. | ||
* @returns A function to show the update information if any update is available. | ||
*/ | ||
export async function checkCurrentCLIVersion() { | ||
if (isHydrogenMonorepo && !process.env.SHOPIFY_UNIT_TEST) return; | ||
|
||
const {content: pkgJson} = await findUpAndReadPackageJson( | ||
fileURLToPath(import.meta.url), | ||
).catch(() => ({content: undefined})); | ||
|
||
const pkgName = pkgJson?.name; | ||
const currentVersion = pkgJson?.version; | ||
|
||
if ( | ||
!pkgName || | ||
!currentVersion || | ||
!Object.values(UPGRADABLE_CLI_NAMES).some((name) => name === pkgName) || | ||
currentVersion.includes('next') || | ||
currentVersion.includes('experimental') | ||
) { | ||
return; | ||
} | ||
|
||
const newVersionAvailable = await checkForNewVersion(pkgName, currentVersion); | ||
|
||
if (!newVersionAvailable) return; | ||
|
||
const reference = [ | ||
{ | ||
link: { | ||
label: 'Hydrogen releases', | ||
url: 'https://github.com/Shopify/hydrogen/releases', | ||
}, | ||
}, | ||
]; | ||
|
||
if (pkgName === UPGRADABLE_CLI_NAMES.cli) { | ||
reference.push({ | ||
link: { | ||
label: 'Global CLI reference', | ||
url: 'https://shopify.dev/docs/api/shopify-cli/', | ||
}, | ||
}); | ||
} | ||
|
||
return (packageManager?: PackageManager) => { | ||
packageManager ??= packageManagerFromUserAgent(); | ||
if (packageManager === 'unknown' || !packageManager) { | ||
packageManager = inferPackageManagerForGlobalCLI(); | ||
} | ||
if (packageManager === 'unknown') { | ||
packageManager = 'npm'; | ||
} | ||
|
||
const installMessage = | ||
pkgName === UPGRADABLE_CLI_NAMES.cli | ||
? `Please install the latest Shopify CLI version with \`${ | ||
packageManager === 'yarn' | ||
? `yarn global add ${UPGRADABLE_CLI_NAMES.cli}` | ||
: `${packageManager} install -g ${UPGRADABLE_CLI_NAMES.cli}` | ||
}\` and try again.` | ||
: `Please use the latest version with \`${packageManager} create @shopify/hydrogen@latest\``; | ||
|
||
renderInfo({ | ||
headline: 'Upgrade available', | ||
body: | ||
`Version ${newVersionAvailable} of ${pkgName} is now available.\n` + | ||
`You are currently running v${currentVersion}.\n\n` + | ||
installMessage, | ||
reference, | ||
}); | ||
|
||
return {currentVersion, newVersion: newVersionAvailable}; | ||
}; | ||
} |
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
Oops, something went wrong.