MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / extractScenarioSource

Function extractScenarioSource

e2e/src/scenario.ts:248–284  ·  view source on GitHub ↗
(filePath: string, name: string)

Source from the content-addressed store, hash-verified

246 * a parsing edge case can never fail a run.
247 */
248const extractScenarioSource = (filePath: string, name: string): string | undefined => {
249 try {
250 const source = readFileSync(filePath, "utf8").replace(/^import[\s\S]*?;[^\S\n]*$/gm, "");
251 const needle = "scenario(";
252 const blocks: Array<{ start: number; end: number; mine: boolean }> = [];
253 let index = 0;
254 while ((index = source.indexOf(needle, index)) !== -1) {
255 let depth = 0;
256 let end = -1;
257 for (let i = index + needle.length - 1; i < source.length; i++) {
258 if (source[i] === "(") depth++;
259 else if (source[i] === ")") {
260 depth--;
261 if (depth === 0) {
262 end = source[i + 1] === ";" ? i + 2 : i + 1;
263 break;
264 }
265 }
266 }
267 if (end === -1) return undefined; // unbalanced — bail to be safe
268 blocks.push({
269 start: index,
270 end,
271 mine: source.slice(index, end).includes(`"${name}"`),
272 });
273 index = end;
274 }
275 if (!blocks.some((b) => b.mine)) return undefined;
276 let out = source;
277 for (const block of [...blocks].reverse()) {
278 if (!block.mine) out = out.slice(0, block.start) + out.slice(block.end);
279 }
280 return `${out.replace(/\n{3,}/g, "\n\n").trim()}\n`;
281 } catch {
282 return undefined;
283 }
284};

Callers 1

scenarioFunction · 0.85

Calls 2

pushMethod · 0.80
replaceMethod · 0.65

Tested by

no test coverage detected