Runner.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { join } from 'path';
  2. import { spawn } from 'child_process';
  3. import { copyAsync, readJsonAsync, chmodAsync } from 'fs-extra-promise';
  4. const debug = require('debug')('build:runner');
  5. import { Downloader } from './Downloader';
  6. import { FFmpegDownloader } from './FFmpegDownloader';
  7. import { BuildConfig } from './config';
  8. import { mergeOptions, findExecutable, findFFmpeg, tmpDir, spawnAsync, extractGeneric } from './util';
  9. interface IRunnerOptions {
  10. x86?: boolean;
  11. x64?: boolean;
  12. chromeApp?: boolean;
  13. mirror?: string;
  14. detached?: boolean;
  15. mute?: boolean;
  16. }
  17. export class Runner {
  18. public static DEFAULT_OPTIONS: IRunnerOptions = {
  19. x86: false,
  20. x64: false,
  21. chromeApp: false,
  22. mirror: Downloader.DEFAULT_OPTIONS.mirror,
  23. detached: false,
  24. mute: true,
  25. };
  26. public options: IRunnerOptions;
  27. constructor(options: IRunnerOptions = {}, public args: string[]) {
  28. this.options = mergeOptions(Runner.DEFAULT_OPTIONS, options);
  29. debug('in constructor', 'args', args);
  30. debug('in constructor', 'options', this.options);
  31. }
  32. public async run(): Promise<number> {
  33. const platform = process.platform;
  34. const arch = this.options.x86 || this.options.x64
  35. ? (this.options.x86 ? 'ia32' : 'x64')
  36. : process.arch;
  37. const pkg: any = await readJsonAsync(join(this.args[0], this.options.chromeApp ? 'manifest.json' : 'package.json'));
  38. const config = new BuildConfig(pkg);
  39. debug('in run', 'config', config);
  40. const downloader = new Downloader({
  41. platform, arch,
  42. version: config.nwVersion,
  43. flavor: 'sdk',
  44. mirror: this.options.mirror,
  45. useCaches: true,
  46. showProgress: this.options.mute ? false : true,
  47. });
  48. if(!this.options.mute) {
  49. console.info('Fetching NW.js binary...', {
  50. platform: downloader.options.platform,
  51. arch: downloader.options.arch,
  52. version: downloader.options.version,
  53. flavor: downloader.options.flavor,
  54. });
  55. }
  56. const runtimeDir = await downloader.fetchAndExtract();
  57. if(config.ffmpegIntegration) {
  58. // FIXME: Integrate without overwriting extracted files.
  59. //await this.integrateFFmpeg(platform, arch, runtimeDir, pkg, config);
  60. if(!this.options.mute) {
  61. console.warn('Running with FFmpeg integration is not supported.');
  62. }
  63. }
  64. const executable = await findExecutable(platform, runtimeDir);
  65. await chmodAsync(executable, 0o555);
  66. if(!this.options.mute) {
  67. console.info('Launching NW.js app...');
  68. }
  69. const { code, signal } = await spawnAsync(executable, this.args, {
  70. detached: this.options.detached,
  71. });
  72. if(!this.options.mute) {
  73. if(this.options.detached) {
  74. console.info('NW.js app detached.');
  75. await new Promise((resolve, reject) => {
  76. setTimeout(resolve, 3000);
  77. });
  78. }
  79. else {
  80. console.info(`NW.js app exited with ${ code }.`);
  81. }
  82. }
  83. return code;
  84. }
  85. protected async integrateFFmpeg(platform: string, arch: string, runtimeDir: string, pkg: any, config: BuildConfig) {
  86. const downloader = new FFmpegDownloader({
  87. platform, arch,
  88. version: config.nwVersion,
  89. useCaches: true,
  90. showProgress: this.options.mute ? false : true,
  91. });
  92. if(!this.options.mute) {
  93. console.info('Fetching FFmpeg prebuilt...', {
  94. platform: downloader.options.platform,
  95. arch: downloader.options.arch,
  96. version: downloader.options.version,
  97. });
  98. }
  99. const ffmpegDir = await downloader.fetchAndExtract();
  100. const src = await findFFmpeg(platform, ffmpegDir);
  101. const dest = await findFFmpeg(platform, runtimeDir);
  102. await copyAsync(src, dest);
  103. }
  104. }