Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"dependencies": {
"core-js": "^3.46.0",
"execa": "^7.2.0",
"graphql": "^16.11.0"
},
"devDependencies": {
Expand Down
76 changes: 76 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 48 additions & 77 deletions src/methods/build-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* Build a function run payload from a cart and options
*/

import { spawn } from "child_process";
import path from "path";
import { fileURLToPath } from "url";

import { execa } from "execa";

/**
* Interface for the build function result
*/
Expand All @@ -26,86 +27,56 @@ const __dirname = path.dirname(__filename);
export async function buildFunction(
functionPath?: string,
): Promise<BuildFunctionResult> {
try {
let functionDir;
let appRootDir;
let functionName;
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);
}
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);
}

return new Promise((resolve, reject) => {
const shopifyProcess = spawn(
"shopify",
["app", "function", "build", "--path", functionName],
{
cwd: appRootDir,
stdio: ["pipe", "pipe", "pipe"],
env: {
...process.env,
SHOPIFY_INVOKED_BY: "shopify-function-test-helpers",
},
try {
const result = await execa(
"shopify",
["app", "function", "build", "--path", functionName],
{
cwd: appRootDir,
env: {
...process.env,
SHOPIFY_INVOKED_BY: "shopify-function-test-helpers",
},
);
},
);

let stdout = "";
let stderr = "";

shopifyProcess.stdout.on("data", (data) => {
stdout += data.toString();
});

shopifyProcess.stderr.on("data", (data) => {
stderr += data.toString();
});

shopifyProcess.on("close", (code) => {
if (code !== 0) {
reject(
new Error(`Build command failed with exit code ${code}: ${stderr}`),
);
return;
}

resolve({
success: true,
output: stdout.trim(),
error: null,
});
});

shopifyProcess.on("error", (error) => {
reject(
new Error(`Failed to start shopify build command: ${error.message}`),
);
});
});
} catch (error) {
if (error instanceof Error) {
return {
success: false,
output: null,
error: error.message,
};
} else {
return {
success: false,
output: null,
error: "Unknown error occurred during build",
};
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)}`,
);
}
}
Loading