generated from surplex/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuppeteer.js
78 lines (65 loc) · 2.39 KB
/
puppeteer.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import puppeteer from "puppeteer"
import * as fs from "fs"
import prettier from "prettier"
import ext2mui from "./ext2mui.js"
let customColumnMapper = (col, res) => {}
if (fs.existsSync('./customColumnMapper.js')) {
const module = await import('./customColumnMapper.js')
customColumnMapper = module.default
console.log('Using customColumnMapper')
}
let customContentReplacer = (a) => a
if (fs.existsSync('./customContentReplacer.js')) {
const module = await import('./customContentReplacer.js')
customContentReplacer = module.default
console.log('Using customContentReplacer')
}
const run = async() => {
const url = process.env.E2M_URL
const namespaces = process.env.E2M_NS.split(',')
fs.rmdirSync("./output", { recursive: true })
fs.mkdirSync("./output")
const browser = await puppeteer.launch({
headless: true,
args: [
"--no-sandbox",
"--disable-web-security",
"--disable-features=IsolateOrigins",
"--disable-site-isolation-trials"
],
ignoreHTTPSErrors: true
})
let page = await browser.newPage()
page.on("console", (msg) => console.log(msg.text()))
await page.goto(url)
page.on("pageerror", function (err) {
console.debug("!Page error: ", err)
})
page.on("error", function (err) {
console.debug("!Error: ", err)
})
// we cannot pass 'customColumnMapper' as a function, so we pass it as a string
const results = JSON.parse(await page.evaluate(ext2mui, namespaces, customColumnMapper.toString()))
console.log("Found " + Object.keys(results).length + " grids.")
for (const fullName in results) {
const pieces = fullName.split(":")
const viewName = pieces[0]
const fileName = pieces[1] === "unknown" ? "columns.ts" : `${pieces[1]}.columns.ts`
const path = `./output/${viewName}`
if (!fs.existsSync(path)) {
fs.mkdirSync(path)
}
let content = JSON.stringify(results[fullName])
content = customContentReplacer(content)
// replace renderCell stringyfied function with actual function
content = content.replace(/"renderCell":"(function\([^^)]*\){)(.*?)}"/g, '"renderCell":$1/*$2*/}')
content = "import { GridColDef } from '@mui/x-data-grid'\n\nexport const columns: GridColDef[] = " + content
fs.writeFileSync(`${path}/${fileName}`, prettier.format(content, {
semi: false,
parser: "typescript",
trailingComma: "none"
}))
}
await browser.close()
}
await run()