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