-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetch metafield definitions on start-up using CLI (#597)
- Loading branch information
Showing
5 changed files
with
87 additions
and
1 deletion.
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,6 @@ | ||
--- | ||
'@shopify/theme-language-server-common': minor | ||
'@shopify/theme-language-server-node': minor | ||
--- | ||
|
||
Fetch metafield definitions on start-up using CLI |
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
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
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
41 changes: 41 additions & 0 deletions
41
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
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(); | ||
|
||
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, | ||
}); | ||
} 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', ''); | ||
} | ||
} |