-
Notifications
You must be signed in to change notification settings - Fork 710
Expand file tree
/
Copy pathServerlessStackPlugin.ts
More file actions
230 lines (198 loc) · 8.32 KB
/
Copy pathServerlessStackPlugin.ts
File metadata and controls
230 lines (198 loc) · 8.32 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'node:path';
import * as process from 'node:process';
import * as child_process from 'node:child_process';
import type {
CommandLineFlagParameter,
CommandLineStringParameter,
HeftConfiguration,
IHeftTaskSession,
IHeftTaskPlugin,
IHeftTaskRunHookOptions,
IScopedLogger
} from '@rushstack/heft';
import { FileSystem, Import, SubprocessTerminator } from '@rushstack/node-core-library';
import type {
PluginName as Webpack4PluginName,
IWebpackPluginAccessor as IWebpack4PluginAccessor
} from '@rushstack/heft-webpack4-plugin';
import type {
PluginName as Webpack5PluginName,
IWebpackPluginAccessor as IWebpack5PluginAccessor
} from '@rushstack/heft-webpack5-plugin';
const PLUGIN_NAME: 'serverless-stack-plugin' = 'serverless-stack-plugin';
const WEBPACK4_PLUGIN_NAME: typeof Webpack4PluginName = 'webpack4-plugin';
const WEBPACK5_PLUGIN_NAME: typeof Webpack5PluginName = 'webpack5-plugin';
const SST_CLI_PACKAGE_NAME: string = '@serverless-stack/cli';
export default class ServerlessStackPlugin implements IHeftTaskPlugin {
private _logger!: IScopedLogger;
public apply(taskSession: IHeftTaskSession, heftConfiguration: HeftConfiguration): void {
this._logger = taskSession.logger;
// Once https://github.com/serverless-stack/serverless-stack/issues/1537 is fixed, we may be
// eliminate the need for this parameter.
const sstParameter: CommandLineFlagParameter = taskSession.parameters.getFlagParameter('--sst');
const sstStageParameter: CommandLineStringParameter =
taskSession.parameters.getStringParameter('--sst-stage');
// Only tap if the --sst flag is set.
if (sstParameter.value) {
const configureWebpackTap: () => Promise<false> = async () => {
this._logger.terminal.writeLine(
'The command line includes "--sst", redirecting Webpack to Serverless Stack'
);
return false;
};
taskSession.requestAccessToPluginByName(
'@rushstack/heft-webpack4-plugin',
WEBPACK4_PLUGIN_NAME,
async (accessor: IWebpack4PluginAccessor) =>
accessor.hooks.onLoadConfiguration.tapPromise(PLUGIN_NAME, configureWebpackTap)
);
taskSession.requestAccessToPluginByName(
'@rushstack/heft-webpack5-plugin',
WEBPACK5_PLUGIN_NAME,
async (accessor: IWebpack5PluginAccessor) =>
accessor.hooks.onLoadConfiguration.tapPromise(PLUGIN_NAME, configureWebpackTap)
);
taskSession.hooks.run.tapPromise(PLUGIN_NAME, async (runOptions: IHeftTaskRunHookOptions) => {
// TODO: Handle watch / serve mode
await this._runServerlessStackAsync({
taskSession,
heftConfiguration,
sstStage: sstStageParameter.value
});
});
}
}
private async _runServerlessStackAsync(options: {
taskSession: IHeftTaskSession;
heftConfiguration: HeftConfiguration;
sstStage?: string;
serveMode?: boolean;
}): Promise<void> {
let sstCliPackagePath: string;
try {
sstCliPackagePath = Import.resolvePackage({
packageName: SST_CLI_PACKAGE_NAME,
baseFolderPath: options.heftConfiguration.buildFolderPath,
useNodeJSResolver: true
});
} catch (e) {
this._logger.emitError(
new Error(
`The ${options.taskSession.taskName} task cannot start because your project does not seem to have ` +
`a dependency on the "${SST_CLI_PACKAGE_NAME}" package: ` +
e.message
)
);
return;
}
const sstCliEntryPoint: string = this._getSstCliEntryPoint(sstCliPackagePath);
this._logger.terminal.writeVerboseLine('Found SST package in' + sstCliPackagePath);
const sstCommandArgs: string[] = [];
sstCommandArgs.push(sstCliEntryPoint);
if (options.serveMode) {
sstCommandArgs.push('start');
} else {
sstCommandArgs.push('build');
}
if (options.taskSession.parameters.debug || options.taskSession.parameters.verbose) {
sstCommandArgs.push('--verbose');
}
if (options.sstStage) {
sstCommandArgs.push('--stage');
sstCommandArgs.push(options.sstStage);
}
this._logger.terminal.writeVerboseLine('Launching child process: ' + JSON.stringify(sstCommandArgs));
const sstCommandEnv: NodeJS.ProcessEnv = this._getWorkaroundEnvironment(sstCliPackagePath);
const sstCommandResult: child_process.ChildProcess = child_process.spawn(
process.execPath,
sstCommandArgs,
{
cwd: options.heftConfiguration.buildFolderPath,
stdio: ['inherit', 'pipe', 'pipe'],
env: sstCommandEnv,
...SubprocessTerminator.RECOMMENDED_OPTIONS
}
);
SubprocessTerminator.killProcessTreeOnExit(sstCommandResult, SubprocessTerminator.RECOMMENDED_OPTIONS);
let completionResolve: () => void;
let completionReject: (reason: Error) => void;
const completionPromise: Promise<void> = new Promise((resolve, reject) => {
completionResolve = resolve;
completionReject = reject;
});
sstCommandResult.stdout?.on('data', (chunk: Buffer) => {
this._writeOutput(chunk.toString(), (x) => this._logger.terminal.write(x));
});
sstCommandResult.stderr?.on('data', (chunk: Buffer) => {
this._writeOutput(chunk.toString(), (x) => this._logger.terminal.writeError(x));
});
sstCommandResult.on('exit', (code: number | null) => {
if (options.serveMode) {
// The child process is not supposed to terminate in watch mode
this._logger.terminal.writeErrorLine(`SST process terminated with exit code ${code}`);
// TODO: Provide a Heft facility for this
process.exit(1);
} else {
this._logger.terminal.writeVerboseLine(`SST process terminated with exit code ${code}`);
if (!code) {
completionResolve();
} else {
completionReject(new Error(`SST process terminated with exit code ${code}`));
}
}
});
return completionPromise;
}
private _writeOutput(chunk: string, write: (message: string) => void): void {
const lines: string[] = chunk.split('\n');
const lastLine: string = lines.pop() || '';
lines.map((x) => write(x + '\n'));
if (lastLine !== '') {
write(lastLine);
}
}
private _getSstCliEntryPoint(sstCliPackagePath: string): string {
// Entry point for SST prior to v1.2.0
let sstCliEntryPoint: string = path.join(sstCliPackagePath, 'bin/scripts.js');
if (FileSystem.exists(sstCliEntryPoint)) {
return sstCliEntryPoint;
}
// Entry point for SST v1.2.0 and later
sstCliEntryPoint = path.join(sstCliPackagePath, 'bin/scripts.mjs');
if (FileSystem.exists(sstCliEntryPoint)) {
return sstCliEntryPoint;
}
throw new Error(
`${PLUGIN_NAME} task cannot start because the entry point was not found: ${sstCliEntryPoint}`
);
}
// The SST CLI emits a script "<project folder>/.build/run.js" with a bunch of phantom dependencies
// on packages that NPM would shamefully hoist into the project's node_modules folder when
// "@serverless-stack/cli" is being installed. The only reason this works with PNPM is that
// (to improve compatibility) its bin script sets up NODE_PATH to include "common/temp/node_modules/.pnpm/"
// where those dependencies can be found, and this environment variable gets passed along to the run.js
// child process. SST is not following best practices -- regardless of which package manager you're using, there
// is no guarantee that the versions found in this way will correspond to @serverless-stack/cli/package.json.
//
// Since we're invoking the "@serverless-stack/cli/bin/scripts.js" entry point directly, we need to
// reproduce this workaround.
private _getWorkaroundEnvironment(sstCliPackagePath: string): NodeJS.ProcessEnv {
const sstCommandEnv: NodeJS.ProcessEnv = {
...process.env
};
const phantomPaths: string[] = [];
let current: string = path.join(sstCliPackagePath);
while (current) {
phantomPaths.push(path.join(current, 'node_modules'));
const parent: string = path.dirname(current);
if (parent === current) {
break;
}
current = parent;
}
sstCommandEnv.NODE_PATH = phantomPaths.join(path.delimiter);
return sstCommandEnv;
}
}