Skip to content

Commit

Permalink
Fix Codegen on Windows (#1253)
Browse files Browse the repository at this point in the history
* 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 8926b2e.

* Changesets
  • Loading branch information
frandiox committed Aug 21, 2023
1 parent ec21cfd commit 46d5f8f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-onions-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-hydrogen': patch
---

Fix GraphQL Codegen throwing error related to Git on Windows.
7 changes: 4 additions & 3 deletions packages/cli/scripts/generate-manifest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
12 changes: 12 additions & 0 deletions packages/cli/src/lib/ast.ts
Original file line number Diff line number Diff line change
@@ -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];
}
7 changes: 4 additions & 3 deletions packages/cli/src/lib/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand All @@ -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 && {
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/lib/setups/css/replacers.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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: {
Expand Down Expand Up @@ -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'}})
Expand Down
12 changes: 6 additions & 6 deletions packages/cli/src/lib/setups/i18n/replacers.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 46d5f8f

Please sign in to comment.