|
| 1 | +#!/usr/bin/env node |
| 2 | +function argv (argv) { |
| 3 | + const args = {} |
| 4 | + const rest = [] |
| 5 | + const max = argv.length |
| 6 | + const optionReg = /^--?/ |
| 7 | + const isOption = key => optionReg.test(key) |
| 8 | + for (let i = 0; i < max; i++) { |
| 9 | + let key = argv[i] |
| 10 | + let val = true |
| 11 | + const parts = key.split('=') |
| 12 | + if (parts.length > 1) { |
| 13 | + key = parts.shift() |
| 14 | + val = parts.join('=') |
| 15 | + } else if (i < max - 1) { |
| 16 | + const next = argv[i + 1] |
| 17 | + if (next === '=' && (i += 2) < max) { |
| 18 | + val = argv[i] |
| 19 | + } else if (isOption(key) && !isOption(next)) { |
| 20 | + val = next |
| 21 | + i += 1 |
| 22 | + } |
| 23 | + } |
| 24 | + if (!isOption(key)) { |
| 25 | + rest.push(key) |
| 26 | + } else { |
| 27 | + args[key.replace(optionReg, '')] = val |
| 28 | + } |
| 29 | + } |
| 30 | + let watch = args.w || args.watch || false |
| 31 | + const file = rest[0] || null |
| 32 | + let help = args.h || args.help || !file || false |
| 33 | + let output = args.o || args.output || null |
| 34 | + if (args.wo) { |
| 35 | + output = args.wo |
| 36 | + watch = true |
| 37 | + } |
| 38 | + |
| 39 | + if (typeof output !== 'string') { |
| 40 | + // No output file defined |
| 41 | + help = true |
| 42 | + } |
| 43 | + return { help, watch, output, file } |
| 44 | +} |
| 45 | + |
| 46 | +function rethrow (msg, e) { |
| 47 | + e.message = `${msg}:\n\n${e.stack.split('\n').map(line => ` ${line}`).join('\n')}` |
| 48 | + throw e |
| 49 | +} |
| 50 | + |
| 51 | +const controlZ = new Promise(resolve => { |
| 52 | + process.once('SIGINT', () => resolve(true)) |
| 53 | +}) |
| 54 | + |
| 55 | +async function start ({ help, watch, output, file } = {}) { |
| 56 | + if (help) { |
| 57 | + console.log(` |
| 58 | + Usage: protocol-buffers-dts [schema-file.proto] [options] |
| 59 | + |
| 60 | + --output, -o [output-file.d.ts] |
| 61 | + --watch, -w (recompile on schema change) |
| 62 | + `) |
| 63 | + return 1 |
| 64 | + } |
| 65 | + const { parse } = require('protocol-buffers-schema') |
| 66 | + const { fromSchema } = require('..') |
| 67 | + const { readFile, writeFile } = require('fs').promises |
| 68 | + let watcher |
| 69 | + while (true) { |
| 70 | + let rawSchema |
| 71 | + try { |
| 72 | + try { |
| 73 | + rawSchema = await readFile(file, 'utf-8') |
| 74 | + } catch (e) { |
| 75 | + rethrow(`Error while reading ${file}`, e) |
| 76 | + } |
| 77 | + let schema |
| 78 | + try { |
| 79 | + schema = parse(rawSchema) |
| 80 | + } catch (e) { |
| 81 | + rethrow(`Error while parsing ${file}`, e) |
| 82 | + } |
| 83 | + let dts |
| 84 | + try { |
| 85 | + dts = fromSchema(schema) |
| 86 | + } catch (e) { |
| 87 | + rethrow(`Error while creating dts from ${file}`, e) |
| 88 | + } |
| 89 | + try { |
| 90 | + await writeFile(output, dts) |
| 91 | + } catch (e) { |
| 92 | + rethrow(`Error while writing dts to ${output}`, e) |
| 93 | + } |
| 94 | + console.log(`Successfully wrote ${output}.`) |
| 95 | + } catch (err) { |
| 96 | + if (!watch) throw err |
| 97 | + console.error(err) |
| 98 | + } |
| 99 | + if (!watch) { |
| 100 | + break |
| 101 | + } |
| 102 | + if (!watcher) { |
| 103 | + watcher = require('fs').watch(file, { interval: 100 }) |
| 104 | + } |
| 105 | + console.log('Watching for changes.') |
| 106 | + if (await Promise.race([ |
| 107 | + controlZ, |
| 108 | + new Promise(resolve => watcher.once('change', () => resolve(false))) |
| 109 | + ])) { |
| 110 | + break |
| 111 | + } |
| 112 | + } |
| 113 | + if (watcher) { |
| 114 | + console.log('\nClosing watcher.') |
| 115 | + watcher.close() |
| 116 | + } |
| 117 | + return 0 |
| 118 | +} |
| 119 | + |
| 120 | +if (require.main === module) { |
| 121 | + start(argv(process.argv.slice(2))).then( |
| 122 | + code => process.exit(code), |
| 123 | + e => { |
| 124 | + console.error(e.stack) |
| 125 | + process.exit(1) |
| 126 | + } |
| 127 | + ) |
| 128 | +} else { |
| 129 | + module.exports = { |
| 130 | + argv, |
| 131 | + start |
| 132 | + } |
| 133 | +} |
0 commit comments