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

Fetch metafield definitions on start-up #597

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/rare-coins-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'theme-check-vscode': minor
---

Fetch metafield definitions on start-up
36 changes: 36 additions & 0 deletions packages/vscode-extension/src/node/metafieldDefinitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as child_process from 'child_process';
import { promisify } from 'node:util';

const exec = promisify(child_process.exec);

const isWin = process.platform === 'win32';

export async function fetchMetafieldDefinitions() {
const path = getShopifyCliPath();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current CLI does not have the ability to pull metafields, so tophat this build the CLI PR here and hardcode this value to the path.

E.g.

const path = '<where-ever-you-cloned-it>/cli/packages/cli/bin/dev.js'


if (!path) {
return;
}

try {
await exec(`${path} theme metafields pull`, { timeout: 10_000 });
} catch (_) {
// CLI command can break because of incorrect version or not being logged in
// If this fails, the user must fetch their own metafield definitions
}
}

// eslint-disable-next-line no-unused-vars
async function getShopifyCliPath() {
if (isWin) {
const { stdout } = await exec(`where.exe shopify`);
const executables = stdout
.replace(/\r/g, '')
.split('\n')
.filter((exe) => exe.endsWith('bat'));
return executables.length > 0 ? executables[0] : '';
} else {
const { stdout } = await exec(`which shopify`);
return stdout.split('\n')[0].replace('\r', '');
}
}
2 changes: 2 additions & 0 deletions packages/vscode-extension/src/node/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AbstractFileSystem } from '@shopify/theme-check-common';
import { getConnection, NodeFileSystem, startServer } from '@shopify/theme-language-server-node';
import { VsCodeFileSystem } from '../common/VsCodeFileSystem';
import { fetchMetafieldDefinitions } from './metafieldDefinitions';

const connection = getConnection();

Expand All @@ -10,6 +11,7 @@ const fileSystems: Record<string, AbstractFileSystem> = {
};

startServer(connection, new VsCodeFileSystem(connection, fileSystems));
fetchMetafieldDefinitions();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should somehow be injected in startServer's fetchMetafieldsForURI injection.

IIRC the metafields should be loaded per theme root, not per workspace.

I don't 100% know how the shopify cli works and if there's an auth per theme, but I don't think someone running a monorepo will necessarily have the same metafield definitions for shop A & shop B


process.on('uncaughtException', (e) => {
console.error(e);
Expand Down
Loading