(homes: string[])
| 36 | } |
| 37 | |
| 38 | export function findSandboxes(homes: string[]): SandboxFileInformation[] { |
| 39 | const sandboxes = []; |
| 40 | |
| 41 | fromDirMultiple(homes, /\.sandbox.ts$/, /.*node_modules.*/, (filename, home) => { |
| 42 | const sandboxPath = filename.replace(home, '.').replace(/.ts$/, '').replace(/\\/g, '/'); |
| 43 | const contents = readFileSync(filename, 'utf8'); |
| 44 | |
| 45 | const matchSandboxOf = /\s?sandboxOf\s*\(\s*([^)]+?)\s*\)/g.exec(contents); |
| 46 | if (matchSandboxOf) { |
| 47 | const typeName = matchSandboxOf[1].split(',')[0].trim(); |
| 48 | const labelText = /label\s*:\s*['"](.+)['"]/g.exec(matchSandboxOf[0]); |
| 49 | const uniqueIdText = /uniqueId\s*:\s*['"](.+)['"]/g.exec(matchSandboxOf[0]); |
| 50 | |
| 51 | const scenarioMenuItems = []; |
| 52 | |
| 53 | // Tested with https://regex101.com/r/mtp2Fy/2 |
| 54 | // First scenario: May follow directly after sandboxOf function ).add |
| 55 | // Other scenarios: .add with possible whitespace before. Ignore outcommented lines. |
| 56 | const scenarioRegex = /^(?!\/\/)(?:\s*|.*\))\.add\s*\(\s*['"](.+)['"]\s*,\s*{/gm; |
| 57 | let scenarioMatches; |
| 58 | let scenarioIndex = 1; |
| 59 | while ((scenarioMatches = scenarioRegex.exec(contents)) !== null) { |
| 60 | scenarioMenuItems.push({key: scenarioIndex, description: scenarioMatches[1]}); |
| 61 | scenarioIndex++; |
| 62 | } |
| 63 | |
| 64 | const label = labelText ? labelText[1] : ''; |
| 65 | sandboxes.push({ |
| 66 | key: sandboxPath, |
| 67 | uniqueId: uniqueIdText ? uniqueIdText[1] : undefined, |
| 68 | srcPath: home, |
| 69 | searchKey: `${typeName}${label}`, |
| 70 | name: typeName, |
| 71 | label, |
| 72 | scenarioMenuItems, |
| 73 | }); |
| 74 | } |
| 75 | }); |
| 76 | |
| 77 | return sandboxes; |
| 78 | } |
| 79 | |
| 80 | async function writeSandboxContent(filePath, fileContent): Promise<string> { |
| 81 | return new Promise((resolve, reject) => { |
no test coverage detected