From 46d5f8ff279dd7e18fa817eeb04206e08122fced Mon Sep 17 00:00:00 2001 From: Fran Dios Date: Mon, 21 Aug 2023 22:41:37 +0900 Subject: [PATCH] Fix Codegen on Windows (#1253) * Fix path for Windows in Oclif manifest generate script * Delay importing ast-grep to fix Oclif manifest on Windows * Revert "Maybe fix not found queries issue from monorepo" This reverts commit 8926b2e6f39e12ea7df13fff6ae319d1293164c3. * Changesets --- .changeset/warm-onions-smile.md | 5 +++++ packages/cli/scripts/generate-manifest.mjs | 7 ++++--- packages/cli/src/lib/ast.ts | 12 ++++++++++++ packages/cli/src/lib/codegen.ts | 7 ++++--- packages/cli/src/lib/setups/css/replacers.ts | 10 +++++----- packages/cli/src/lib/setups/i18n/replacers.ts | 12 ++++++------ 6 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 .changeset/warm-onions-smile.md create mode 100644 packages/cli/src/lib/ast.ts diff --git a/.changeset/warm-onions-smile.md b/.changeset/warm-onions-smile.md new file mode 100644 index 0000000000..041a0ee80f --- /dev/null +++ b/.changeset/warm-onions-smile.md @@ -0,0 +1,5 @@ +--- +'@shopify/cli-hydrogen': patch +--- + +Fix GraphQL Codegen throwing error related to Git on Windows. diff --git a/packages/cli/scripts/generate-manifest.mjs b/packages/cli/scripts/generate-manifest.mjs index ab5389b1f5..8c727f24fb 100644 --- a/packages/cli/scripts/generate-manifest.mjs +++ b/packages/cli/scripts/generate-manifest.mjs @@ -4,13 +4,14 @@ * https://github.com/oclif/oclif/blob/main/src/commands/manifest.ts#L76 */ -import path from 'path'; -import {Plugin} from '@oclif/core'; +import path from 'node:path'; +import {fileURLToPath} from 'node:url'; import {writeFile, access} from 'node:fs/promises'; +import {Plugin} from '@oclif/core'; console.log('\n', 'Generating Oclif manifest...'); -const cwd = new URL('..', import.meta.url).pathname; +const cwd = fileURLToPath(new URL('..', import.meta.url)); if ( !(await access(path.join(cwd, 'dist', 'commands', 'hydrogen')) diff --git a/packages/cli/src/lib/ast.ts b/packages/cli/src/lib/ast.ts new file mode 100644 index 0000000000..3732f77457 --- /dev/null +++ b/packages/cli/src/lib/ast.ts @@ -0,0 +1,12 @@ +export type {SgNode} from '@ast-grep/napi'; + +export async function importLangAstGrep(lang: 'ts' | 'tsx' | 'js' | 'jsx') { + // Delay CJS dependency import and binary import + const astGrep = await import('@ast-grep/napi'); + + if (!(lang in astGrep)) { + throw new Error(`Wrong language for AST: ${lang}`); + } + + return astGrep[lang]; +} diff --git a/packages/cli/src/lib/codegen.ts b/packages/cli/src/lib/codegen.ts index 6ba8a22e00..6c5aff3f9d 100644 --- a/packages/cli/src/lib/codegen.ts +++ b/packages/cli/src/lib/codegen.ts @@ -11,7 +11,7 @@ import { } from '@shopify/hydrogen-codegen'; import {formatCode, getCodeFormatOptions} from './format-code.js'; import {renderFatalError, renderWarning} from '@shopify/cli-kit/node/ui'; -import {joinPath} from '@shopify/cli-kit/node/path'; +import {joinPath, relativePath} from '@shopify/cli-kit/node/path'; import {AbortError} from '@shopify/cli-kit/node/error'; import {spawn} from 'node:child_process'; import {fileURLToPath} from 'node:url'; @@ -161,6 +161,7 @@ function generateDefaultConfig( forceSfapiVersion?: string, ): LoadCodegenConfigResult { const tsDefaultGlob = '*!(*.d).{ts,tsx}'; // No d.ts files + const appDirRelative = relativePath(rootDirectory, appDirectory); return { filepath: 'virtual:codegen', @@ -172,8 +173,8 @@ function generateDefaultConfig( preset, schema, documents: [ - joinPath(rootDirectory, tsDefaultGlob), // E.g. ./server.ts - joinPath(appDirectory, '**', tsDefaultGlob), // E.g. app/routes/_index.tsx + tsDefaultGlob, // E.g. ./server.ts + joinPath(appDirRelative, '**', tsDefaultGlob), // E.g. app/routes/_index.tsx ], ...(!!forceSfapiVersion && { diff --git a/packages/cli/src/lib/setups/css/replacers.ts b/packages/cli/src/lib/setups/css/replacers.ts index c05ab97a53..ec30b595bb 100644 --- a/packages/cli/src/lib/setups/css/replacers.ts +++ b/packages/cli/src/lib/setups/css/replacers.ts @@ -1,9 +1,7 @@ import {AbortError} from '@shopify/cli-kit/node/error'; -import {ts, tsx, js, jsx, type SgNode} from '@ast-grep/napi'; import {type FormatOptions} from '../../format-code.js'; import {findFileWithExtension, replaceFileContent} from '../../file.js'; - -const astGrep = {ts, tsx, js, jsx}; +import {importLangAstGrep, type SgNode} from '../../ast.js'; /** * Adds new properties to the Remix config file. @@ -28,7 +26,8 @@ export async function replaceRemixConfig( } await replaceFileContent(filepath, formatConfig, async (content) => { - const root = astGrep[astType].parse(content).root(); + const astGrep = await importLangAstGrep(astType); + const root = astGrep.parse(content).root(); const remixConfigNode = root.find({ rule: { @@ -135,7 +134,8 @@ export async function replaceRootLinks( return; // Already installed } - const root = astGrep[astType].parse(content).root(); + const astGrep = await importLangAstGrep(astType); + const root = astGrep.parse(content).root(); const lastImportNode = root .findAll({rule: {kind: 'import_statement'}}) diff --git a/packages/cli/src/lib/setups/i18n/replacers.ts b/packages/cli/src/lib/setups/i18n/replacers.ts index de351548fd..3e4bd1de08 100644 --- a/packages/cli/src/lib/setups/i18n/replacers.ts +++ b/packages/cli/src/lib/setups/i18n/replacers.ts @@ -1,12 +1,10 @@ import {AbortError} from '@shopify/cli-kit/node/error'; import {joinPath, relativePath} from '@shopify/cli-kit/node/path'; import {fileExists} from '@shopify/cli-kit/node/fs'; -import {ts, tsx, js, jsx} from '@ast-grep/napi'; import {findFileWithExtension, replaceFileContent} from '../../file.js'; import type {FormatOptions} from '../../format-code.js'; import type {I18nSetupConfig} from './index.js'; - -const astGrep = {ts, tsx, js, jsx}; +import {importLangAstGrep} from '../../ast.js'; /** * Adds the `getLocaleFromRequest` function to the server entrypoint and calls it. @@ -22,7 +20,8 @@ export async function replaceServerI18n( }); await replaceFileContent(filepath, formatConfig, async (content) => { - const root = astGrep[astType].parse(content).root(); + const astGrep = await importLangAstGrep(astType); + const root = astGrep.parse(content).root(); // First parameter of the `fetch` function. // Normally it's called `request`, but it could be renamed. @@ -221,10 +220,11 @@ export async function replaceRemixEnv( entryFilepath, ).replace(/.[tj]sx?$/, ''); - await replaceFileContent(remixEnvPath, formatConfig, (content) => { + await replaceFileContent(remixEnvPath, formatConfig, async (content) => { if (content.includes(`Storefront<`)) return; // Already set up - const root = astGrep.ts.parse(content).root(); + const astGrep = await importLangAstGrep('ts'); + const root = astGrep.parse(content).root(); const storefrontTypeNode = root.find({ rule: {