|
5 | 5 | import { spawn } from 'child_process'; |
6 | 6 | import path from 'path'; |
7 | 7 | import { fileURLToPath } from 'url'; |
| 8 | +import fs from 'fs'; |
8 | 9 |
|
9 | 10 | /** |
10 | 11 | * Interface for the run function result |
@@ -48,7 +49,7 @@ export async function runFunction( |
48 | 49 | functionName = path.basename(functionDir); |
49 | 50 | } |
50 | 51 |
|
51 | | - return new Promise((resolve, reject) => { |
| 52 | + return new Promise((resolve) => { |
52 | 53 | const shopifyProcess = spawn('shopify', [ |
53 | 54 | 'app', 'function', 'run', |
54 | 55 | '--export', exportName, |
@@ -130,3 +131,143 @@ export async function runFunction( |
130 | 131 | } |
131 | 132 | } |
132 | 133 |
|
| 134 | +/** |
| 135 | + * Run a function by calling function-runner directly (bypassing Shopify CLI) |
| 136 | + * @param {String} exportName - The function export name |
| 137 | + * @param {String} input - The input data |
| 138 | + * @param {String} [functionPath] - Optional path to the function directory |
| 139 | + * @param {String} [wasmPath] - Optional direct path to the WASM file |
| 140 | + * @returns {Object} The function run result |
| 141 | + */ |
| 142 | +export async function runFunctionWithRunnerDirectly( |
| 143 | + exportName: string, |
| 144 | + input: Record<string, any>, |
| 145 | + functionPath?: string, |
| 146 | + wasmPath?: string |
| 147 | +): Promise<RunFunctionResult> { |
| 148 | + try { |
| 149 | + const inputJson = JSON.stringify(input); |
| 150 | + |
| 151 | + let functionDir, functionName; |
| 152 | + |
| 153 | + if (functionPath !== undefined && functionPath !== null) { |
| 154 | + // Use provided function path |
| 155 | + functionDir = path.resolve(functionPath); |
| 156 | + functionName = path.basename(functionDir); |
| 157 | + } else { |
| 158 | + // Calculate paths correctly for when used as a dependency: |
| 159 | + // __dirname = /path/to/function/tests/node_modules/function-testing-helpers/src/methods |
| 160 | + // Go up 5 levels to get to function directory: ../../../../../ = /path/to/function |
| 161 | + functionDir = path.dirname(path.dirname(path.dirname(path.dirname(path.dirname(__dirname))))); |
| 162 | + functionName = path.basename(functionDir); |
| 163 | + } |
| 164 | + |
| 165 | + // Find WASM file - use provided path or search common locations |
| 166 | + let resolvedWasmPath: string | null = null; |
| 167 | + |
| 168 | + if (wasmPath) { |
| 169 | + // Use user-provided WASM path |
| 170 | + resolvedWasmPath = path.resolve(wasmPath); |
| 171 | + if (!fs.existsSync(resolvedWasmPath)) { |
| 172 | + return { |
| 173 | + result: null, |
| 174 | + error: `WASM file not found at provided path: ${resolvedWasmPath}` |
| 175 | + }; |
| 176 | + } |
| 177 | + } else { |
| 178 | + // Check Rust build output location |
| 179 | + const rustWasmPath = path.join(functionDir, 'target', 'wasm32-wasip1', 'release', `${functionName}.wasm`); |
| 180 | + if (fs.existsSync(rustWasmPath)) { |
| 181 | + resolvedWasmPath = rustWasmPath; |
| 182 | + } else { |
| 183 | + // Check dist folder (JavaScript functions) |
| 184 | + const distWasmPath = path.join(functionDir, 'dist', 'function.wasm'); |
| 185 | + if (fs.existsSync(distWasmPath)) { |
| 186 | + resolvedWasmPath = distWasmPath; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + if (!resolvedWasmPath) { |
| 191 | + return { |
| 192 | + result: null, |
| 193 | + error: `WASM file not found in function directory: ${functionDir}` |
| 194 | + }; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + const schemaPath = path.join(functionDir, 'schema.graphql'); |
| 199 | + const queryPath = path.join(functionDir, 'src', `${exportName}.graphql`); |
| 200 | + |
| 201 | + return new Promise((resolve) => { |
| 202 | + const runnerProcess = spawn('function-runner', [ |
| 203 | + '-f', resolvedWasmPath, |
| 204 | + '-e', exportName, |
| 205 | + '--json', |
| 206 | + '-s', schemaPath, |
| 207 | + '-q', queryPath |
| 208 | + ], { |
| 209 | + stdio: ['pipe', 'pipe', 'pipe'] |
| 210 | + }); |
| 211 | + |
| 212 | + let stdout = ''; |
| 213 | + let stderr = ''; |
| 214 | + |
| 215 | + runnerProcess.stdout.on('data', (data) => { |
| 216 | + stdout += data.toString(); |
| 217 | + }); |
| 218 | + |
| 219 | + runnerProcess.stderr.on('data', (data) => { |
| 220 | + stderr += data.toString(); |
| 221 | + }); |
| 222 | + |
| 223 | + runnerProcess.on('close', (code) => { |
| 224 | + if (code !== 0) { |
| 225 | + resolve({ |
| 226 | + result: null, |
| 227 | + error: `function-runner failed with exit code ${code}: ${stderr}` |
| 228 | + }); |
| 229 | + return; |
| 230 | + } |
| 231 | + |
| 232 | + try { |
| 233 | + const result = JSON.parse(stdout); |
| 234 | + |
| 235 | + // function-runner output format: { output: {...} } |
| 236 | + resolve({ |
| 237 | + result: { output: result.output || result }, |
| 238 | + error: null |
| 239 | + }); |
| 240 | + } catch (parseError) { |
| 241 | + resolve({ |
| 242 | + result: null, |
| 243 | + error: `Failed to parse function-runner output: ${parseError instanceof Error ? parseError.message : 'Unknown error'}` |
| 244 | + }); |
| 245 | + } |
| 246 | + }); |
| 247 | + |
| 248 | + runnerProcess.on('error', (error) => { |
| 249 | + resolve({ |
| 250 | + result: null, |
| 251 | + error: `Failed to start function-runner: ${error.message}` |
| 252 | + }); |
| 253 | + }); |
| 254 | + |
| 255 | + runnerProcess.stdin.write(inputJson); |
| 256 | + runnerProcess.stdin.end(); |
| 257 | + }); |
| 258 | + |
| 259 | + } catch (error) { |
| 260 | + if (error instanceof Error) { |
| 261 | + return { |
| 262 | + result: null, |
| 263 | + error: error.message |
| 264 | + }; |
| 265 | + } else { |
| 266 | + return { |
| 267 | + result: null, |
| 268 | + error: 'Unknown error occurred' |
| 269 | + }; |
| 270 | + } |
| 271 | + } |
| 272 | +} |
| 273 | + |
0 commit comments