-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.js
56 lines (46 loc) · 1.49 KB
/
new.js
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
const { chromium } = require("playwright");
const fs = require("fs");
const kata = process.argv[2];
if (!kata) {
console.log("Please provide a kata url or kata id as first argument");
process.exit(1);
}
let kataId;
try {
const url = new URL(kata);
const pathSegments = url.pathname.split("/");
kataId = pathSegments[2];
} catch (error) {
// catch Uncaught TypeError [ERR_INVALID_URL]: Invalid URL
if (error.code === "ERR_INVALID_URL" && kata.length === 24) {
kataId = kata;
} else {
console.log("Please provide a valid kata url or kata id as first argument");
process.exit(1);
}
}
(async () => {
const browser = await chromium.launch({ headless: true });
const page = await browser.newPage();
page.on("response", async (response) => {
if (response.url().includes("session")) {
const data = await response.body();
const res = JSON.parse(Buffer.from(data).toString());
const func = res.exampleFixture.match(/assert.*?\((.*?)\(/)[1] || "func";
fs.writeFileSync(
`${__dirname}/tests.spec.js`,
res.exampleFixture
? `const ${func} = require('./work');
const Test = require("@codewars/test-compat");
${res.exampleFixture}`
: "",
);
fs.writeFileSync(`${__dirname}/work.js`, `module.exports = ${res.setup}`);
console.log(`Kata ${kataId} created! :)`);
process.exit(0);
}
});
await page.goto(`https://www.codewars.com/kata/${kataId}/train/javascript`, {
waitUntil: "networkidle",
});
})();