|
| 1 | +// Copy script to clipboard |
| 2 | +// Cuts code above `// ------ Everything above this line will get cut when running copy script` |
| 3 | + |
| 4 | +const { promises: fsp } = require('fs') |
| 5 | +const { resolve: r } = require('path') |
| 6 | +const { createHmac } = require('crypto') |
| 7 | +const clipboardy = require('clipboardy') |
| 8 | +const typescript = require('typescript') |
| 9 | + |
| 10 | +const file = r(__dirname, 'index.ts') |
| 11 | +const separator = '// ------ Everything above this line will get cut when running copy script' |
| 12 | + |
| 13 | +const sha256 = str => createHmac('sha256', 'shitty-salt').update(str).digest('hex') |
| 14 | + |
| 15 | +const run = async () => { |
| 16 | + const content = await fsp.readFile(file, { encoding: 'utf8' }) |
| 17 | + |
| 18 | + let script = '' |
| 19 | + if (content.includes(separator)) script = content.split(separator)[1].trim() |
| 20 | + else script = content.trim() |
| 21 | + |
| 22 | + // Wrap in function |
| 23 | + script = `;(() => {\n\n${script}\n\n})()` |
| 24 | + |
| 25 | + // Remove top-level TypeScript return ignore |
| 26 | + script = script.replace(/\/\/ \@ts-ignore\n\s*return/g, 'return') |
| 27 | + |
| 28 | + if (process.argv.find(x => x === '--js')) { |
| 29 | + console.log('Compile TypeScript') |
| 30 | + script = typescript.transpileModule(script, require('./tsconfig.json')).outputText |
| 31 | + } |
| 32 | + if (process.argv.find(x => x === '--copy')) { |
| 33 | + await clipboardy.write(script) |
| 34 | + console.log('Copied script to clipboard.') |
| 35 | + } |
| 36 | + if (process.argv.find(x => x === '--dist')) { |
| 37 | + // Output file (for CodinGame IDE sync) |
| 38 | + if ( |
| 39 | + !(await fsp.access(r(__dirname, 'dist')).then( |
| 40 | + () => true, |
| 41 | + () => false |
| 42 | + )) |
| 43 | + ) |
| 44 | + await fsp.mkdir(r(__dirname, 'dist')) |
| 45 | + await fsp.writeFile(r(__dirname, 'dist', 'index.js'), script) |
| 46 | + } |
| 47 | + |
| 48 | + console.log(`Script SHA256: ${sha256(script)}.`) |
| 49 | +} |
| 50 | + |
| 51 | +run() |
0 commit comments