|
| 1 | +import * as fs from 'node:fs' |
| 2 | +import * as path from 'node:path' |
| 3 | +import { fileURLToPath } from 'node:url' |
| 4 | +import { parseArgs } from 'node:util' |
| 5 | + |
| 6 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 7 | +const rootDir = path.resolve(__dirname, '../../..') |
| 8 | +const filePath = path.join(rootDir, 'node_modules', 'xvfb-maybe', 'src', 'xvfb-maybe.js') |
| 9 | + |
| 10 | +const args = process.argv.slice(2) |
| 11 | +const optionsDef = { |
| 12 | + width: { |
| 13 | + type: 'string', |
| 14 | + short: 'w', |
| 15 | + }, |
| 16 | + height: { |
| 17 | + type: 'string', |
| 18 | + short: 'h', |
| 19 | + }, |
| 20 | +} as const |
| 21 | + |
| 22 | +const { values: options } = parseArgs({ args, options: optionsDef }) |
| 23 | + |
| 24 | +console.log('Adjust screen resolution') |
| 25 | +console.log(` Width : ${options.width}`) |
| 26 | +console.log(` Height: ${options.height}`) |
| 27 | + |
| 28 | +const insertBefore = "const dblDashPos = args.indexOf('--')," |
| 29 | +const codeToInsert = ` args.unshift('--server-args=-screen 0 ${options.width}x${options.height}x24', '--');` |
| 30 | + |
| 31 | +const sourceCode = fs.readFileSync(filePath, 'utf-8') |
| 32 | + |
| 33 | +if (sourceCode.includes(codeToInsert)) { |
| 34 | + console.log('🔧 xvfb-maybe is already patched') |
| 35 | + process.exit(0) |
| 36 | +} |
| 37 | + |
| 38 | +const lines = sourceCode.split('\n') |
| 39 | +const index = lines.findIndex((line) => line.includes(insertBefore)) |
| 40 | + |
| 41 | +if (index !== -1) { |
| 42 | + lines.splice(index, 0, codeToInsert) |
| 43 | + const newCode = lines.join('\n') |
| 44 | + fs.writeFileSync(filePath, newCode, 'utf-8') |
| 45 | + console.log('\n✅ xvfb-maybe is patched successfully\n\n') |
| 46 | +} else { |
| 47 | + console.log('\n💥 could not find the target line.\n\n') |
| 48 | +} |
0 commit comments