-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
application-package-manager.ts
118 lines (103 loc) · 4.29 KB
/
application-package-manager.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
/********************************************************************************
* Copyright (C) 2017 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import * as path from 'path';
import * as fs from 'fs-extra';
import * as cp from 'child_process';
import { ApplicationPackage, ApplicationPackageOptions } from '@theia/application-package';
import { WebpackGenerator, FrontendGenerator, BackendGenerator } from './generator';
import { ApplicationProcess } from './application-process';
export class ApplicationPackageManager {
readonly pck: ApplicationPackage;
/** application process */
readonly process: ApplicationProcess;
/** manager process */
protected readonly __process: ApplicationProcess;
protected readonly webpack: WebpackGenerator;
protected readonly backend: BackendGenerator;
protected readonly frontend: FrontendGenerator;
constructor(options: ApplicationPackageOptions) {
this.pck = new ApplicationPackage(options);
this.process = new ApplicationProcess(this.pck, options.projectPath);
this.__process = new ApplicationProcess(this.pck, path.join(__dirname, '..'));
this.webpack = new WebpackGenerator(this.pck);
this.backend = new BackendGenerator(this.pck);
this.frontend = new FrontendGenerator(this.pck);
}
protected async remove(fsPath: string): Promise<void> {
if (await fs.pathExists(fsPath)) {
await fs.remove(fsPath);
}
}
async clean(): Promise<void> {
await this.remove(this.pck.lib());
await this.remove(this.pck.srcGen());
await this.remove(this.webpack.configPath);
}
async generate(): Promise<void> {
await this.webpack.generate();
await this.backend.generate();
await this.frontend.generate();
}
async copy(): Promise<void> {
await fs.ensureDir(this.pck.lib());
await fs.copy(this.pck.frontend('index.html'), this.pck.lib('index.html'));
}
async build(args: string[] = []): Promise<void> {
await this.generate();
await this.copy();
return this.__process.run('webpack', args);
}
async start(args: string[] = []): Promise<void> {
if (this.pck.isElectron()) {
return this.startElectron(args);
}
return this.startBrowser(args);
}
async startElectron(args: string[]): Promise<void> {
const { mainArgs, options } = this.adjustArgs([this.pck.frontend('electron-main.js'), ...args]);
const electronCli = require.resolve('electron/cli.js', { paths: [this.pck.projectPath] });
this.__process.fork(electronCli, mainArgs, options);
}
async startBrowser(args: string[]): Promise<void> {
const { mainArgs, options } = this.adjustArgs(args);
this.__process.fork(this.pck.backend('main.js'), mainArgs, options);
}
private adjustArgs(args: string[], forkOptions: cp.ForkOptions = {}): Readonly<{ mainArgs: string[]; options: cp.ForkOptions }> {
const options = {
...this.forkOptions,
forkOptions
};
const mainArgs = [...args];
const inspectIndex = mainArgs.findIndex(v => v.startsWith('--inspect'));
if (inspectIndex !== -1) {
const inspectArg = mainArgs.splice(inspectIndex, 1)[0];
options.execArgv = ['--nolazy', inspectArg];
}
return {
mainArgs,
options
};
}
private get forkOptions(): cp.ForkOptions {
return {
stdio: [0, 1, 2, 'ipc'],
env: {
...process.env,
THEIA_PARENT_PID: String(process.pid)
}
};
}
}