From dedbbb8d5c4e7b4a18385e97722164305b485cab Mon Sep 17 00:00:00 2001 From: Alok Swamy Date: Fri, 15 Nov 2024 15:28:26 -0500 Subject: [PATCH] Fetch metafield definitions on start-up --- .changeset/rare-coins-sit.md | 5 +++ .../src/node/metafieldDefinitions.ts | 36 +++++++++++++++++++ packages/vscode-extension/src/node/server.ts | 2 ++ 3 files changed, 43 insertions(+) create mode 100644 .changeset/rare-coins-sit.md create mode 100644 packages/vscode-extension/src/node/metafieldDefinitions.ts diff --git a/.changeset/rare-coins-sit.md b/.changeset/rare-coins-sit.md new file mode 100644 index 00000000..2d11c0ef --- /dev/null +++ b/.changeset/rare-coins-sit.md @@ -0,0 +1,5 @@ +--- +'theme-check-vscode': minor +--- + +Fetch metafield definitions on start-up diff --git a/packages/vscode-extension/src/node/metafieldDefinitions.ts b/packages/vscode-extension/src/node/metafieldDefinitions.ts new file mode 100644 index 00000000..710d051f --- /dev/null +++ b/packages/vscode-extension/src/node/metafieldDefinitions.ts @@ -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(); + + 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', ''); + } +} diff --git a/packages/vscode-extension/src/node/server.ts b/packages/vscode-extension/src/node/server.ts index 15c239a8..3d1c2b12 100644 --- a/packages/vscode-extension/src/node/server.ts +++ b/packages/vscode-extension/src/node/server.ts @@ -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(); @@ -10,6 +11,7 @@ const fileSystems: Record = { }; startServer(connection, new VsCodeFileSystem(connection, fileSystems)); +fetchMetafieldDefinitions(); process.on('uncaughtException', (e) => { console.error(e);