-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
file-server.impl.ts
165 lines (148 loc) · 4.01 KB
/
file-server.impl.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { exec, execSync } from 'child_process';
import { ExecutorContext, joinPathFragments } from '@nrwl/devkit';
import ignore from 'ignore';
import { readFileSync } from 'fs';
import { Schema } from './schema';
import { watch } from 'chokidar';
import { workspaceLayout } from '@nrwl/workspace/src/core/file-utils';
function getHttpServerArgs(options: Schema) {
const args = [] as any[];
if (options.port) {
args.push(`-p ${options.port}`);
}
if (options.host) {
args.push(`-a ${options.host}`);
}
if (options.ssl) {
args.push(`-S`);
}
if (options.sslCert) {
args.push(`-C ${options.sslCert}`);
}
if (options.sslKey) {
args.push(`-K ${options.sslKey}`);
}
if (options.proxyUrl) {
args.push(`-P ${options.proxyUrl}`);
}
if (typeof options.secure === 'boolean') {
args.push(`--proxy-options.secure ${options.secure}`);
}
return args;
}
function getBuildTargetCommand(options: Schema) {
const cmd = [`npx nx run ${options.buildTarget}`];
if (options.withDeps) {
cmd.push(`--with-deps`);
}
if (options.parallel) {
cmd.push(`--parallel`);
}
if (options.maxParallel) {
cmd.push(`--maxParallel=${options.maxParallel}`);
}
return cmd.join(' ');
}
function getBuildTargetOutputPath(options: Schema, context: ExecutorContext) {
let buildOptions;
try {
const [project, target, config] = options.buildTarget.split(':');
const buildTarget = context.workspace.projects[project].targets[target];
buildOptions = config
? { ...buildTarget.options, ...buildTarget.configurations[config] }
: buildTarget.options;
} catch (e) {
throw new Error(`Invalid buildTarget: ${options.buildTarget}`);
}
// TODO: vsavkin we should also check outputs
const outputPath = buildOptions.outputPath;
if (!outputPath) {
throw new Error(
`Invalid buildTarget: ${options.buildTarget}. The target must contain outputPath property.`
);
}
return outputPath;
}
function getIgnoredGlobs(root: string) {
const ig = ignore();
try {
ig.add(readFileSync(`${root}/.gitignore`, 'utf-8'));
} catch {}
try {
ig.add(readFileSync(`${root}/.nxignore`, 'utf-8'));
} catch {}
return ig;
}
function createFileWatcher(root: string, changeHandler: () => void) {
const ignoredGlobs = getIgnoredGlobs(root);
const layout = workspaceLayout();
const watcher = watch(
[
joinPathFragments(layout.appsDir, '**'),
joinPathFragments(layout.libsDir, '**'),
],
{
cwd: root,
ignoreInitial: true,
}
);
watcher.on('all', (_event: string, path: string) => {
if (ignoredGlobs.ignores(path)) return;
changeHandler();
});
return { close: () => watcher.close() };
}
export default async function* fileServerExecutor(
options: Schema,
context: ExecutorContext
) {
let running = false;
const run = () => {
if (!running) {
running = true;
try {
execSync(getBuildTargetCommand(options), {
stdio: [0, 1, 2],
});
} catch {}
running = false;
}
};
const watcher = createFileWatcher(context.root, run);
// perform initial run
run();
const outputPath = getBuildTargetOutputPath(options, context);
const args = getHttpServerArgs(options);
const serve = exec(`npx http-server ${outputPath} ${args.join(' ')}`, {
cwd: context.root,
});
const processExitListener = () => {
serve.kill();
watcher.close();
};
process.on('exit', processExitListener);
process.on('SIGTERM', processExitListener);
serve.stdout.on('data', (chunk) => {
if (chunk.toString().indexOf('GET') === -1) {
process.stdout.write(chunk);
}
});
serve.stderr.on('data', (chunk) => {
process.stderr.write(chunk);
});
yield {
success: true,
baseUrl: `${options.ssl ? 'https' : 'http'}://${options.host}:${
options.port
}`,
};
return new Promise<{ success: boolean }>((res) => {
serve.on('exit', (code) => {
if (code == 0) {
res({ success: true });
} else {
res({ success: false });
}
});
});
}