Skip to content

Commit

Permalink
Refactor CLI command runner
Browse files Browse the repository at this point in the history
  • Loading branch information
aswamy committed Jan 14, 2025
1 parent 4a429e1 commit 915b4ac
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-geese-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/theme-language-server-node': patch
---

[Internal] Refactor CLI command runner to run any CLI command
36 changes: 36 additions & 0 deletions packages/theme-language-server-node/src/cliCommand.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';

const shopifyCliPathPromise = getShopifyCliPath();

// 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', '');
}
}

export async function runCliCommand(command: string, uri?: string, timeout: number = 10_000) {
const path = await shopifyCliPathPromise;

if (!path) {
throw Error('Shopify CLI not found');
}

return await exec(`${path} ${command}`, {
cwd: uri ? new URL(uri): undefined,
timeout,
});
}
35 changes: 2 additions & 33 deletions packages/theme-language-server-node/src/metafieldDefinitions.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,10 @@
import * as child_process from 'child_process';
import { promisify } from 'node:util';

const exec = promisify(child_process.exec);

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

const shopifyCliPathPromise = getShopifyCliPath();
import { runCliCommand } from "./cliCommand";

export async function fetchMetafieldDefinitionsForURI(uri: string) {
const path = await shopifyCliPathPromise;

if (!path) {
return;
}

try {
await exec(`${path} theme metafields pull`, {
cwd: new URL(uri),
timeout: 10_000,
});
await runCliCommand('theme metafields pull', uri);
} 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', '');
}
}

0 comments on commit 915b4ac

Please sign in to comment.