This repository has been archived by the owner on Feb 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Runner.ts
145 lines (104 loc) · 4.07 KB
/
Runner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { resolve } from 'path';
import { spawn } from 'child_process';
import { copyAsync, readJsonAsync, chmodAsync } from 'fs-extra-promise';
const debug = require('debug')('build:runner');
import { Downloader } from './Downloader';
import { FFmpegDownloader } from './FFmpegDownloader';
import { BuildConfig } from './config';
import { mergeOptions, findExecutable, findFFmpeg, tmpDir, spawnAsync, extractGeneric } from './util';
interface IRunnerOptions {
x86?: boolean;
x64?: boolean;
chromeApp?: boolean;
mirror?: string;
detached?: boolean;
mute?: boolean;
}
export class Runner {
public static DEFAULT_OPTIONS: IRunnerOptions = {
x86: false,
x64: false,
chromeApp: false,
mirror: Downloader.DEFAULT_OPTIONS.mirror,
detached: false,
mute: true,
};
public options: IRunnerOptions;
constructor(options: IRunnerOptions = {}, public args: string[]) {
this.options = mergeOptions(Runner.DEFAULT_OPTIONS, options);
debug('in constructor', 'args', args);
debug('in constructor', 'options', this.options);
}
public async run(): Promise<number> {
const platform = process.platform;
const arch = this.options.x86 || this.options.x64
? (this.options.x86 ? 'ia32' : 'x64')
: process.arch;
const pkg: any = await readJsonAsync(resolve(this.args[0], this.options.chromeApp ? 'manifest.json' : 'package.json'));
const config = new BuildConfig(pkg);
debug('in run', 'config', config);
const downloader = new Downloader({
platform, arch,
version: config.nwVersion,
flavor: 'sdk',
mirror: this.options.mirror,
useCaches: true,
showProgress: this.options.mute ? false : true,
});
if(!this.options.mute) {
console.info('Fetching NW.js binary...', {
platform: downloader.options.platform,
arch: downloader.options.arch,
version: downloader.options.version,
flavor: downloader.options.flavor,
});
}
const runtimeDir = await downloader.fetchAndExtract();
if(config.ffmpegIntegration) {
// FIXME: Integrate without overwriting extracted files.
//await this.integrateFFmpeg(platform, arch, runtimeDir, pkg, config);
if(!this.options.mute) {
console.warn('Running with FFmpeg integration is not supported.');
}
}
const executable = await findExecutable(platform, runtimeDir);
await chmodAsync(executable, 0o555);
if(!this.options.mute) {
console.info('Launching NW.js app...');
}
const { code, signal } = await spawnAsync(executable, this.args, {
detached: this.options.detached,
});
if(!this.options.mute) {
if(this.options.detached) {
console.info('NW.js app detached.');
await new Promise((resolve, reject) => {
setTimeout(resolve, 3000);
});
}
else {
console.info(`NW.js app exited with ${ code }.`);
}
}
return code;
}
protected async integrateFFmpeg(platform: string, arch: string, runtimeDir: string, pkg: any, config: BuildConfig) {
const downloader = new FFmpegDownloader({
platform, arch,
version: config.nwVersion,
useCaches: true,
showProgress: this.options.mute ? false : true,
});
if(!this.options.mute) {
console.info('Fetching FFmpeg prebuilt...', {
platform: downloader.options.platform,
arch: downloader.options.arch,
version: downloader.options.version,
});
}
const ffmpegDir = await downloader.fetchAndExtract();
const src = await findFFmpeg(platform, ffmpegDir);
const dest = await findFFmpeg(platform, runtimeDir);
await copyAsync(src, dest);
}
}