-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
43 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
packages/theme-language-server-node/src/metafieldDefinitions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', ''); | ||
} | ||
} |