1
0

build.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // reimplementation of build.sh in JS so its platform independent
  2. const fs = require('fs');
  3. const pfs = require('fs').promises;
  4. const { spawn } = require('child_process');
  5. const path = require('path');
  6. const cwd = process.cwd();
  7. function spawnAsync(...args) {
  8. return new Promise((resolve, reject) => {
  9. const proc = spawn(...args);
  10. proc.on('close', (code) => {
  11. if (code === 0) {
  12. resolve();
  13. } else {
  14. reject(new Error(`${args[0]} exited with code ${code}`));
  15. }
  16. });
  17. });
  18. }
  19. (async()=>{
  20. if (!fs.existsSync("./peerjs")) {
  21. console.log('node-peerjs: unable to find peerjs folder; cloning the repository from github')
  22. await spawnAsync('git', ['clone', 'https://github.com/peers/peerjs.git', 'peerjs'], {stdio: 'inherit', shell: true});
  23. }
  24. if (fs.existsSync("./peerjs")) {
  25. await pfs.rm("./dist/", {recursive: true, force: true}).catch(err => console.error(err));
  26. }
  27. await pfs.mkdir("./dist/");
  28. // build peerjs
  29. console.log('node-peerjs: building peerjs')
  30. await spawnAsync(path.join(cwd, "./node_modules/.bin/parcel"), [
  31. 'build',
  32. '--no-source-maps', 'lib/exports.ts',
  33. '-d', '../dist',
  34. '--out-file', 'peerjs.min.js'
  35. ], {cwd: 'peerjs', stdio: 'inherit', shell: true});
  36. console.log('node-peerjs: patching peerjs');
  37. const peerjs = await pfs.readFile(path.join(cwd, 'peerjs/dist/peerjs.min.js'), 'utf8');
  38. const header_patch = await pfs.readFile(path.join(cwd, "./patch/header_patch.js"), "utf-8");
  39. const footer_patch = await pfs.readFile(path.join(cwd, "./patch/footer_patch.js"), "utf-8");
  40. const patched = `${header_patch}
  41. ${peerjs}
  42. ${footer_patch}`;
  43. await pfs.writeFile(path.join(cwd, 'dist/peerjs-on-node.js'), patched, 'utf8');
  44. console.log('node-peerjs: done; output: dist/peerjs-on-node.js');
  45. })();