|
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 |
@@ -130,3 +131,114 @@ 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 | + * @returns {Object} The function run result |
| 140 | + */ |
| 141 | +export async function runFunctionWithRunnerDirectly( |
| 142 | + exportName: string, |
| 143 | + input: Record<string, any>, |
| 144 | + functionPath?: string |
| 145 | +): Promise<RunFunctionResult> { |
| 146 | + try { |
| 147 | + const inputJson = JSON.stringify(input); |
| 148 | + |
| 149 | + let functionDir; |
| 150 | + |
| 151 | + if (functionPath !== undefined && functionPath !== null) { |
| 152 | + // Use provided function path |
| 153 | + functionDir = path.resolve(functionPath); |
| 154 | + } else { |
| 155 | + // Calculate paths correctly for when used as a dependency |
| 156 | + functionDir = path.dirname(path.dirname(path.dirname(path.dirname(path.dirname(__dirname))))); |
| 157 | + } |
| 158 | + |
| 159 | + // Find the WASM file in target directory |
| 160 | + const wasmPath = path.join(functionDir, 'target', 'wasm32-wasip1', 'release', `${path.basename(functionDir)}.wasm`); |
| 161 | + const schemaPath = path.join(functionDir, 'schema.graphql'); |
| 162 | + const queryPath = path.join(functionDir, 'src', `${exportName}.graphql`); |
| 163 | + |
| 164 | + // Verify files exist |
| 165 | + if (!fs.existsSync(wasmPath)) { |
| 166 | + return { |
| 167 | + result: null, |
| 168 | + error: `WASM file not found at ${wasmPath}. Make sure the function is built.` |
| 169 | + }; |
| 170 | + } |
| 171 | + |
| 172 | + return new Promise((resolve) => { |
| 173 | + const runnerProcess = spawn('function-runner', [ |
| 174 | + '-f', wasmPath, |
| 175 | + '-e', exportName, |
| 176 | + '--json', |
| 177 | + '-s', schemaPath, |
| 178 | + '-q', queryPath |
| 179 | + ], { |
| 180 | + stdio: ['pipe', 'pipe', 'pipe'] |
| 181 | + }); |
| 182 | + |
| 183 | + let stdout = ''; |
| 184 | + let stderr = ''; |
| 185 | + |
| 186 | + runnerProcess.stdout.on('data', (data) => { |
| 187 | + stdout += data.toString(); |
| 188 | + }); |
| 189 | + |
| 190 | + runnerProcess.stderr.on('data', (data) => { |
| 191 | + stderr += data.toString(); |
| 192 | + }); |
| 193 | + |
| 194 | + runnerProcess.on('close', (code) => { |
| 195 | + if (code !== 0) { |
| 196 | + resolve({ |
| 197 | + result: null, |
| 198 | + error: `function-runner failed with exit code ${code}: ${stderr}` |
| 199 | + }); |
| 200 | + return; |
| 201 | + } |
| 202 | + |
| 203 | + try { |
| 204 | + const result = JSON.parse(stdout); |
| 205 | + |
| 206 | + // function-runner output format: { output: {...} } |
| 207 | + resolve({ |
| 208 | + result: { output: result.output || result }, |
| 209 | + error: null |
| 210 | + }); |
| 211 | + } catch (parseError) { |
| 212 | + resolve({ |
| 213 | + result: null, |
| 214 | + error: `Failed to parse function-runner output: ${parseError instanceof Error ? parseError.message : 'Unknown error'}` |
| 215 | + }); |
| 216 | + } |
| 217 | + }); |
| 218 | + |
| 219 | + runnerProcess.on('error', (error) => { |
| 220 | + resolve({ |
| 221 | + result: null, |
| 222 | + error: `Failed to start function-runner: ${error.message}` |
| 223 | + }); |
| 224 | + }); |
| 225 | + |
| 226 | + runnerProcess.stdin.write(inputJson); |
| 227 | + runnerProcess.stdin.end(); |
| 228 | + }); |
| 229 | + |
| 230 | + } catch (error) { |
| 231 | + if (error instanceof Error) { |
| 232 | + return { |
| 233 | + result: null, |
| 234 | + error: error.message |
| 235 | + }; |
| 236 | + } else { |
| 237 | + return { |
| 238 | + result: null, |
| 239 | + error: 'Unknown error occurred' |
| 240 | + }; |
| 241 | + } |
| 242 | + } |
| 243 | +} |
| 244 | + |
0 commit comments