Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Hydrogen CLI to use latest cli-kit prompts #733

Merged
merged 25 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
929077e
Update CLI, kit and oclif
frandiox Mar 29, 2023
dd3229e
Update imports for most of the cli-kit changes
frandiox Mar 29, 2023
6475c39
Update prompts and fix tests
frandiox Mar 29, 2023
50e3d11
Change arg names
frandiox Mar 29, 2023
91a3ed1
Fix Oclif version in dev for generating manifest
frandiox Mar 29, 2023
15e554f
Remove unused code
frandiox Mar 29, 2023
01342d7
Upgrade to cli-kit 3.45.0-pre.5 to fix React issues
frandiox Mar 29, 2023
3b92568
Add ansi-colors
frandiox Mar 29, 2023
72ca649
Cleanup
frandiox Mar 29, 2023
332f332
Merge branch '2023-01' into fd-update-cli-kit
frandiox Mar 29, 2023
9b6f0b4
Merge branch '2023-01' into fd-update-cli-kit
frandiox Apr 5, 2023
067e217
Fix new CLI files
frandiox Apr 5, 2023
46b6d2b
Update to latest cli-kit stable
frandiox Apr 5, 2023
6221cc5
Change default value for some confirmation prompts
frandiox Apr 5, 2023
66bd8f2
Restore turbo integrity in package-lock
frandiox Apr 5, 2023
e34b6e8
Changeset
frandiox Apr 5, 2023
76f4c8f
Use AbortError in template-downloader
frandiox Apr 5, 2023
93f274d
Show progress with new task component
frandiox Apr 5, 2023
b6a3fd6
Rename utils to lib
frandiox Apr 5, 2023
537afd5
Adjust changeset type
frandiox Apr 5, 2023
26473cc
Clear output mocks properly
frandiox Apr 6, 2023
cbcdc40
Update .changeset/tender-plums-hang.md
frandiox Apr 7, 2023
d6100c9
Merge branch '2023-01' into fd-update-cli-kit
frandiox Apr 7, 2023
8b8e3eb
Merge branch '2023-01' into fd-update-cli-kit
frandiox Apr 7, 2023
c615344
Bump create-hydrogen
frandiox Apr 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/tender-plums-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@shopify/cli-hydrogen': minor
---

Updated CLI prompts. It's recommended to update your version of `@shopify/cli` to `3.45.0` when updating `@shopify/cli-hydrogen`.

```json
"dependencies: {
"@shopify/cli": "3.45.0",
// ...
}
```
frandiox marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion .changeset/tricky-llamas-call.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
'@shopify/cli-hydrogen': patch
'@shopify/cli-hydrogen': minor
frehner marked this conversation as resolved.
Show resolved Hide resolved
---

Added a new `shortcut` command that creates a global `h2` alias for the Hydrogen CLI:
Expand Down
40,461 changes: 33,049 additions & 7,412 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@
"@types/prettier": "^2.7.2",
"@types/recursive-readdir": "^2.2.1",
"@types/tar-fs": "^2.0.1",
"oclif": "^3.4.2",
"vitest": "^0.28.1",
"tempy": "^3.0.0"
"oclif": "2.1.4",
frandiox marked this conversation as resolved.
Show resolved Hide resolved
"tempy": "^3.0.0",
"vitest": "^0.28.1"
},
"peerDependencies": {
"@remix-run/react": "^1.14.3",
"@shopify/hydrogen-react": "^2023.1.7",
"@shopify/remix-oxygen": "^1.0.4"
},
"dependencies": {
"@oclif/core": "^1.20.4",
"@oclif/core": "2.1.4",
"@remix-run/dev": "^1.14.3",
"@shopify/cli-kit": "3.29.0",
"@shopify/cli-kit": "3.45.0",
"@shopify/mini-oxygen": "^1.3.1",
"ansi-colors": "^4.1.3",
"fast-glob": "^3.2.12",
"fs-extra": "^10.1.0",
"gunzip-maybe": "^1.4.2",
Expand Down
42 changes: 21 additions & 21 deletions packages/cli/src/commands/hydrogen/build.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import path from 'path';
import {output, file} from '@shopify/cli-kit';
import colors from '@shopify/cli-kit/node/colors';
import {getProjectPaths, getRemixConfig} from '../../utils/config.js';
import {
deprecated,
commonFlags,
flagsToCamelObject,
} from '../../utils/flags.js';
outputInfo,
outputWarn,
outputContent,
outputToken,
} from '@shopify/cli-kit/node/output';
import {fileSize, copyFile, rmdir} from '@shopify/cli-kit/node/fs';
import {getProjectPaths, getRemixConfig} from '../../lib/config.js';
import {deprecated, commonFlags, flagsToCamelObject} from '../../lib/flags.js';
import Command from '@shopify/cli-kit/node/base-command';
import Flags from '@oclif/core/lib/flags.js';
import {checkLockfileStatus} from '../../utils/check-lockfile.js';
import {findMissingRoutes} from '../../utils/missing-routes.js';
import {Flags} from '@oclif/core';
import {checkLockfileStatus} from '../../lib/check-lockfile.js';
import {findMissingRoutes} from '../../lib/missing-routes.js';
import {getPackageManager} from '@shopify/cli-kit/node/node-package-manager';
import {colors} from '../../lib/colors.js';

const LOG_WORKER_BUILT = '📦 Worker built';

Expand All @@ -34,7 +36,6 @@ export default class Build extends Command {
};

async run(): Promise<void> {
// @ts-ignore
const {flags} = await this.parse(Build);
const directory = flags.path ? path.resolve(flags.path) : process.cwd();

Expand Down Expand Up @@ -64,10 +65,10 @@ export async function runBuild({

const [remixConfig] = await Promise.all([
getRemixConfig(root),
file.rmdir(buildPath, {force: true}),
rmdir(buildPath, {force: true}),
]);

output.info(`\n🏗️ Building in ${process.env.NODE_ENV} mode...`);
outputInfo(`\n🏗️ Building in ${process.env.NODE_ENV} mode...`);

const {build} = await import('@remix-run/dev/dist/compiler/build.js');
const {logCompileFailure} = await import(
Expand All @@ -89,18 +90,17 @@ export async function runBuild({

if (process.env.NODE_ENV !== 'development') {
console.timeEnd(LOG_WORKER_BUILT);
const sizeMB = (await file.size(buildPathWorkerFile)) / (1024 * 1024);
const sizeMB = (await fileSize(buildPathWorkerFile)) / (1024 * 1024);

output.info(
output.content` ${colors.dim(
outputInfo(
outputContent` ${colors.dim(
path.relative(root, buildPathWorkerFile),
)} ${output.token.yellow(sizeMB.toFixed(2))} MB\n`,
)} ${outputToken.yellow(sizeMB.toFixed(2))} MB\n`,
);

if (sizeMB >= 1) {
output.warn(
outputWarn(
`🚨 Worker bundle exceeds 1 MB! This can delay your worker response.${
// @ts-ignore
remixConfig.serverMinify
? ''
: ' Minify your bundle by adding `serverMinify: true` to remix.config.js.'
Expand All @@ -115,7 +115,7 @@ export async function runBuild({
const packageManager = await getPackageManager(root);
const exec = packageManager === 'npm' ? 'npx' : packageManager;

output.warn(
outputWarn(
`Heads up: Shopify stores have a number of standard routes that aren’t set up yet.\n` +
`Some functionality and backlinks might not work as expected until these are created or redirects are set up.\n` +
`This build is missing ${missingRoutes.length} route${
Expand All @@ -135,5 +135,5 @@ export async function copyPublicFiles(
publicPath: string,
buildPathClient: string,
) {
return file.copy(publicPath, buildPathClient);
return copyFile(publicPath, buildPathClient);
}
24 changes: 11 additions & 13 deletions packages/cli/src/commands/hydrogen/check.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import Command from '@shopify/cli-kit/node/base-command';
import {path} from '@shopify/cli-kit';
import {commonFlags} from '../../utils/flags.js';
import {getRemixConfig} from '../../utils/config.js';
import {
findMissingRoutes,
logMissingRoutes,
} from '../../utils/missing-routes.js';
import {resolvePath} from '@shopify/cli-kit/node/path';
import {commonFlags} from '../../lib/flags.js';
import {getRemixConfig} from '../../lib/config.js';
import {findMissingRoutes, logMissingRoutes} from '../../lib/missing-routes.js';

import {Args} from '@oclif/core';

export default class GenerateRoute extends Command {
static description =
Expand All @@ -15,19 +14,18 @@ export default class GenerateRoute extends Command {
path: commonFlags.path,
};

static args = [
{
static args = {
resource: Args.string({
name: 'resource',
description: `The resource to check. Currently only 'routes' is supported.`,
required: true,
options: ['routes'],
},
];
}),
};

async run(): Promise<void> {
// @ts-ignore
const {flags, args} = await this.parse(GenerateRoute);
const directory = flags.path ? path.resolve(flags.path) : process.cwd();
const directory = flags.path ? resolvePath(flags.path) : process.cwd();

if (args.resource === 'routes') {
await runCheckRoutes({directory});
Expand Down
32 changes: 14 additions & 18 deletions packages/cli/src/commands/hydrogen/dev.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import path from 'path';
import fs from 'fs/promises';
import {output, file} from '@shopify/cli-kit';
import {outputInfo} from '@shopify/cli-kit/node/output';
import {fileExists} from '@shopify/cli-kit/node/fs';
import {copyPublicFiles} from './build.js';
import {getProjectPaths, getRemixConfig} from '../../utils/config.js';
import {muteDevLogs} from '../../utils/log.js';
import {
deprecated,
commonFlags,
flagsToCamelObject,
} from '../../utils/flags.js';
import {getProjectPaths, getRemixConfig} from '../../lib/config.js';
import {muteDevLogs} from '../../lib/log.js';
import {deprecated, commonFlags, flagsToCamelObject} from '../../lib/flags.js';
import Command from '@shopify/cli-kit/node/base-command';
import Flags from '@oclif/core/lib/flags.js';
import {startMiniOxygen} from '../../utils/mini-oxygen.js';
import {checkHydrogenVersion} from '../../utils/check-version.js';
import {addVirtualRoutes} from '../../utils/virtual-routes.js';
import {Flags} from '@oclif/core';
import {startMiniOxygen} from '../../lib/mini-oxygen.js';
import {checkHydrogenVersion} from '../../lib/check-version.js';
import {addVirtualRoutes} from '../../lib/virtual-routes.js';

const LOG_INITIAL_BUILD = '\n🏁 Initial build';
const LOG_REBUILDING = '🧱 Rebuilding...';
Expand All @@ -35,7 +32,6 @@ export default class Dev extends Command {
};

async run(): Promise<void> {
// @ts-ignore
const {flags} = await this.parse(Dev);
const directory = flags.path ? path.resolve(flags.path) : process.cwd();

Expand Down Expand Up @@ -74,7 +70,7 @@ async function runDev({
return [fileRelative, path.resolve(root, fileRelative)] as const;
};

const serverBundleExists = () => file.exists(buildPathWorkerFile);
const serverBundleExists = () => fileExists(buildPathWorkerFile);

let miniOxygenStarted = false;
async function safeStartMiniOxygen() {
Expand Down Expand Up @@ -118,7 +114,7 @@ async function runDev({
},
async onFileCreated(file: string) {
const [relative, absolute] = getFilePaths(file);
output.info(`\n📄 File created: ${relative}`);
outputInfo(`\n📄 File created: ${relative}`);

if (absolute.startsWith(publicPath)) {
await copyPublicFiles(
Expand All @@ -129,7 +125,7 @@ async function runDev({
},
async onFileChanged(file: string) {
const [relative, absolute] = getFilePaths(file);
output.info(`\n📄 File changed: ${relative}`);
outputInfo(`\n📄 File changed: ${relative}`);

if (absolute.startsWith(publicPath)) {
await copyPublicFiles(
Expand All @@ -140,14 +136,14 @@ async function runDev({
},
async onFileDeleted(file: string) {
const [relative, absolute] = getFilePaths(file);
output.info(`\n📄 File deleted: ${relative}`);
outputInfo(`\n📄 File deleted: ${relative}`);

if (absolute.startsWith(publicPath)) {
await fs.unlink(absolute.replace(publicPath, buildPathClient));
}
},
onRebuildStart() {
output.info(LOG_REBUILDING);
outputInfo(LOG_REBUILDING);
console.time(LOG_REBUILT);
},
async onRebuildFinish() {
Expand Down
61 changes: 25 additions & 36 deletions packages/cli/src/commands/hydrogen/generate/route.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import {describe, it, expect, vi, beforeEach} from 'vitest';
import {temporaryDirectoryTask} from 'tempy';
import {runGenerate, GENERATOR_TEMPLATES_DIR} from './route.js';
import {file, path, ui} from '@shopify/cli-kit';
import {renderConfirmationPrompt} from '@shopify/cli-kit/node/ui';
import {readFile, writeFile, mkdir} from '@shopify/cli-kit/node/fs';
import {joinPath, dirname} from '@shopify/cli-kit/node/path';

describe('generate/route', () => {
beforeEach(() => {
vi.resetAllMocks();
vi.mock('@shopify/cli-kit', async () => {
const cliKit: any = await vi.importActual('@shopify/cli-kit');
return {
...cliKit,
output: {
...cliKit.output,
success: vi.fn(),
},
ui: {
prompt: vi.fn(),
},
};
});
vi.mock('@shopify/cli-kit/node/output');
vi.mock('@shopify/cli-kit/node/ui');
});

it('generates a route file', async () => {
Expand All @@ -38,7 +29,7 @@ describe('generate/route', () => {

// Then
expect(
await file.read(path.join(appRoot, 'app/routes', `${route}.jsx`)),
await readFile(joinPath(appRoot, 'app/routes', `${route}.jsx`)),
).toContain(`const str = 'hello world'`);
});
});
Expand All @@ -61,17 +52,17 @@ describe('generate/route', () => {

// Then
expect(
await file.read(path.join(appRoot, 'app/routes', `${route}.tsx`)),
await readFile(joinPath(appRoot, 'app/routes', `${route}.tsx`)),
).toContain(`const str = 'hello typescript'`);
});
});

it('prompts the user if there the file already exists', async () => {
await temporaryDirectoryTask(async (tmpDir) => {
// Given
vi.mocked(ui.prompt).mockImplementationOnce(async () => {
return {value: 'overwrite'};
});
vi.mocked(renderConfirmationPrompt).mockImplementationOnce(
async () => true,
);

const route = 'page/$pageHandle';
const {appRoot, templatesRoot} = await createHydrogen(tmpDir, {
Expand All @@ -86,22 +77,20 @@ describe('generate/route', () => {
});

// Then
expect(ui.prompt).toHaveBeenCalledWith(
expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining('already exists'),
}),
]),
expect(renderConfirmationPrompt).toHaveBeenCalledWith(
expect.objectContaining({
message: expect.stringContaining('already exists'),
}),
);
});
});

it('does not prompt the user if the force property is true', async () => {
await temporaryDirectoryTask(async (tmpDir) => {
// Given
vi.mocked(ui.prompt).mockImplementationOnce(async () => {
return {value: 'overwrite'};
});
vi.mocked(renderConfirmationPrompt).mockImplementationOnce(
async () => true,
);

const route = 'page/$pageHandle';
const {appRoot, templatesRoot} = await createHydrogen(tmpDir, {
Expand All @@ -117,7 +106,7 @@ describe('generate/route', () => {
});

// Then
expect(ui.prompt).not.toHaveBeenCalled();
expect(renderConfirmationPrompt).not.toHaveBeenCalled();
});
});
});
Expand All @@ -134,25 +123,25 @@ async function createHydrogen(
) {
for (const item of files) {
const [filePath, fileContent] = item;
const fullFilePath = path.join(directory, 'app', filePath);
await file.mkdir(path.dirname(fullFilePath));
await file.write(fullFilePath, fileContent);
const fullFilePath = joinPath(directory, 'app', filePath);
await mkdir(dirname(fullFilePath));
await writeFile(fullFilePath, fileContent);
}

for (const item of templates) {
const [filePath, fileContent] = item;
const fullFilePath = path.join(
const fullFilePath = joinPath(
directory,
GENERATOR_TEMPLATES_DIR,
'routes',
`${filePath}.tsx`,
);
await file.mkdir(path.dirname(fullFilePath));
await file.write(fullFilePath, fileContent);
await mkdir(dirname(fullFilePath));
await writeFile(fullFilePath, fileContent);
}

return {
appRoot: path.join(directory, 'app'),
appRoot: joinPath(directory, 'app'),
templatesRoot: directory,
};
}
Loading