-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget-function-info.ts
More file actions
78 lines (69 loc) · 2.26 KB
/
get-function-info.ts
File metadata and controls
78 lines (69 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Retrieve function information from the Shopify CLI
*/
import path from "path";
import { execa } from "execa";
/**
* Information about a Shopify function
*/
export interface FunctionInfo {
schemaPath: string;
functionRunnerPath: string;
wasmPath: string;
targeting: Record<string, any>;
}
/**
* Retrieves function information from the Shopify CLI
* @param {string} functionDir - The directory path of the function
* @returns {Promise<FunctionInfo>} Function information including schemaPath, functionRunnerPath, wasmPath, and targeting
* @throws {Error} If the CLI command is not available or fails
*/
export async function getFunctionInfo(
functionDir: string,
): Promise<FunctionInfo> {
const resolvedFunctionDir = path.resolve(functionDir);
const appRootDir = path.dirname(resolvedFunctionDir);
const functionName = path.basename(resolvedFunctionDir);
try {
const result = await execa(
"shopify",
["app", "function", "info", "--json", "--path", functionName],
{
cwd: appRootDir,
env: {
...process.env,
SHOPIFY_INVOKED_BY: "shopify-function-test-helpers",
},
},
);
const functionInfo = JSON.parse(result.stdout.trim()) as FunctionInfo;
return functionInfo;
} catch (error: any) {
// Handle JSON parsing errors
if (error instanceof SyntaxError) {
throw new Error(`Failed to parse function info JSON: ${error.message}`);
}
// Check if the error is due to the command not being found
const stderr = error?.stderr || "";
if (
stderr.includes("Command app function info not found") ||
stderr.includes("command not found")
) {
throw new Error(
'The "shopify app function info" command is not available in your CLI version.\n' +
"Please upgrade to the latest version:\n" +
" npm install -g @shopify/cli@latest\n\n",
);
}
// Handle command execution errors
if (error?.exitCode !== undefined) {
throw new Error(
`Function info command failed with exit code ${error.exitCode}: ${stderr}`,
);
}
// Generic error fallback (spawn/process errors)
throw new Error(
`Failed to start shopify function info command: ${error?.message || String(error)}`,
);
}
}