-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild-function.ts
More file actions
82 lines (73 loc) · 2.22 KB
/
build-function.ts
File metadata and controls
82 lines (73 loc) · 2.22 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
79
80
81
82
/**
* Build a function run payload from a cart and options
*/
import path from "path";
import { fileURLToPath } from "url";
import { execa } from "execa";
/**
* Interface for the build function result
*/
export interface BuildFunctionResult {
success: boolean;
output: string | null;
error: string | null;
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* Build a function run payload from a cart and options
* @param {string} functionPath - Optional path to the function directory
* @returns {Object} A function run payload
*/
export async function buildFunction(
functionPath?: string,
): Promise<BuildFunctionResult> {
let functionDir;
let appRootDir;
let functionName;
if (functionPath) {
// Use provided function path
functionDir = path.resolve(functionPath);
appRootDir = path.dirname(functionDir);
functionName = path.basename(functionDir);
} else {
// Calculate paths correctly for when used as a dependency:
// __dirname = /path/to/function/tests/node_modules/function-testing-helpers/src/methods
// Go up 5 levels to get to function directory: ../../../../../ = /path/to/function
functionDir = path.dirname(
path.dirname(path.dirname(path.dirname(path.dirname(__dirname)))),
);
appRootDir = path.dirname(functionDir);
functionName = path.basename(functionDir);
}
try {
const result = await execa(
"shopify",
["app", "function", "build", "--path", functionName],
{
cwd: appRootDir,
env: {
...process.env,
SHOPIFY_INVOKED_BY: "shopify-function-test-helpers",
},
},
);
return {
success: true,
output: result.stdout.trim(),
error: null,
};
} catch (error: any) {
// Handle command execution errors (non-zero exit code)
if (error?.exitCode !== undefined) {
const stderr = error?.stderr || "";
throw new Error(
`Build command failed with exit code ${error.exitCode}${stderr ? `: ${stderr}` : ""}`,
);
}
// Handle spawn/process errors (command not found, etc.)
throw new Error(
`Failed to start shopify build command: ${error?.message || String(error)}`,
);
}
}