|
| 1 | +/** |
| 2 | + * This scripts helps after recording a scenario to be used for replaying |
| 3 | + * with the mock GitHub API server. |
| 4 | + * |
| 5 | + * Once the scenario has been recorded, it's often useful to remove some of |
| 6 | + * the requests to speed up the replay, particularly ones that fetch the |
| 7 | + * variant analysis status. Once some of the requests have manually been |
| 8 | + * removed, this script can be used to update the numbering of the files. |
| 9 | + * |
| 10 | + * Usage: npx ts-node scripts/fix-scenario-file-numbering.ts <scenario-name> |
| 11 | + */ |
| 12 | + |
| 13 | +import * as fs from 'fs-extra'; |
| 14 | +import * as path from 'path'; |
| 15 | + |
| 16 | +if (process.argv.length !== 3) { |
| 17 | + console.error('Expected 1 argument - the scenario name') |
| 18 | +} |
| 19 | + |
| 20 | +const scenarioName = process.argv[2]; |
| 21 | + |
| 22 | +const extensionDirectory = path.resolve(__dirname, '..'); |
| 23 | +const scenariosDirectory = path.resolve(extensionDirectory, 'src/mocks/scenarios'); |
| 24 | +const scenarioDirectory = path.resolve(scenariosDirectory, scenarioName); |
| 25 | + |
| 26 | +async function fixScenarioFiles() { |
| 27 | + console.log(scenarioDirectory); |
| 28 | + if (!(await fs.pathExists(scenarioDirectory))) { |
| 29 | + console.error('Scenario directory does not exist: ' + scenarioDirectory); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + const files = await fs.readdir(scenarioDirectory); |
| 34 | + |
| 35 | + const orderedFiles = files.sort((a, b) => { |
| 36 | + const aNum = parseInt(a.split('-')[0]); |
| 37 | + const bNum = parseInt(b.split('-')[0]); |
| 38 | + return aNum - bNum; |
| 39 | + }); |
| 40 | + |
| 41 | + let index = 0; |
| 42 | + for (let file of orderedFiles) { |
| 43 | + const ext = path.extname(file); |
| 44 | + if (ext === '.json') { |
| 45 | + const fileName = path.basename(file, ext); |
| 46 | + const fileCurrentIndex = parseInt(fileName.split('-')[0]); |
| 47 | + const fileNameWithoutIndex = fileName.split('-')[1]; |
| 48 | + if (fileCurrentIndex !== index) { |
| 49 | + const newFileName = `${index}-${fileNameWithoutIndex}${ext}`; |
| 50 | + const oldFilePath = path.join(scenarioDirectory, file); |
| 51 | + const newFilePath = path.join(scenarioDirectory, newFileName); |
| 52 | + console.log(`Rename: ${oldFilePath} -> ${newFilePath}`); |
| 53 | + await fs.rename(oldFilePath, newFilePath); |
| 54 | + |
| 55 | + if (fileNameWithoutIndex === 'getVariantAnalysisRepoResult') { |
| 56 | + const oldZipFileName = `${fileCurrentIndex}-getVariantAnalysisRepoResult.body.zip`; |
| 57 | + const newZipFileName = `${index}-getVariantAnalysisRepoResult.body.zip`; |
| 58 | + const oldZipFilePath = path.join(scenarioDirectory, oldZipFileName); |
| 59 | + const newZipFilePath = path.join(scenarioDirectory, newZipFileName); |
| 60 | + console.log(`Rename: ${oldZipFilePath} -> ${newZipFilePath}`); |
| 61 | + await fs.rename(oldZipFilePath, newZipFilePath); |
| 62 | + |
| 63 | + const json = await fs.readJson(newFilePath); |
| 64 | + json.response.body = `file:${newZipFileName}`; |
| 65 | + console.log(`Response.body change to ${json.response.body}`); |
| 66 | + await fs.writeJSON(newFilePath, json); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + index++; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +fixScenarioFiles().catch(e => { |
| 76 | + console.error(e); |
| 77 | + process.exit(2); |
| 78 | +}); |
0 commit comments