compile.ts 757 B

1234567891011121314151617181920212223242526272829303132
  1. import { main as pbjs } from 'protobufjs/cli/pbjs'
  2. import { main as pbts } from 'protobufjs/cli/pbts'
  3. import path from 'path'
  4. import fs from 'fs'
  5. const OUT_DIR = path.resolve(__dirname, '../compiled')
  6. if (!fs.existsSync(OUT_DIR)) {
  7. fs.mkdirSync(OUT_DIR)
  8. }
  9. pbjs(
  10. ['--target', 'static-module', '-w', 'commonjs', '-o', `${OUT_DIR}/index.js`, '--force-long', 'proto/*.proto'],
  11. function (err) {
  12. if (err) {
  13. throw err
  14. }
  15. console.log(`${OUT_DIR}/index.js updated`)
  16. }
  17. )
  18. pbts([`${OUT_DIR}/*.js`], function (err, output) {
  19. if (err) {
  20. throw err
  21. }
  22. // Fix missing Long import
  23. output = `import { Long } from 'long'\n${output}`
  24. fs.writeFileSync(`${OUT_DIR}/index.d.ts`, output)
  25. console.log(`${OUT_DIR}/index.d.ts updated`)
  26. })