-
Notifications
You must be signed in to change notification settings - Fork 41
/
sandbox_command.ts
211 lines (196 loc) · 6.49 KB
/
sandbox_command.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
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
import { Argv, CommandModule } from 'yargs';
import fsp from 'fs/promises';
import { AmplifyPrompter } from '@aws-amplify/cli-core';
import { SandboxSingletonFactory } from '@aws-amplify/sandbox';
import {
ClientConfigFormat,
getClientConfigPath,
} from '@aws-amplify/client-config';
import { ArgumentsKebabCase } from '../../kebab_case.js';
import { handleCommandFailure } from '../../command_failure_handler.js';
import { ClientConfigLifecycleHandler } from '../../client-config/client_config_lifecycle_handler.js';
import { ClientConfigGeneratorAdapter } from '../../client-config/client_config_generator_adapter.js';
import { CommandMiddleware } from '../../command_middleware.js';
export type SandboxCommandOptions =
ArgumentsKebabCase<SandboxCommandOptionsCamelCase>;
type SandboxCommandOptionsCamelCase = {
dirToWatch: string | undefined;
exclude: string[] | undefined;
name: string | undefined;
configFormat: ClientConfigFormat | undefined;
configOutDir: string | undefined;
profile: string | undefined;
};
export type EventHandler = () => void;
export type SandboxEventHandlers = {
successfulDeployment: EventHandler[];
successfulDeletion: EventHandler[];
};
export type SandboxEventHandlerParams = {
sandboxName?: string;
clientConfigLifecycleHandler: ClientConfigLifecycleHandler;
};
export type SandboxEventHandlerCreator = (
params: SandboxEventHandlerParams
) => SandboxEventHandlers;
/**
* Command that starts sandbox.
*/
export class SandboxCommand
implements CommandModule<object, SandboxCommandOptions>
{
/**
* @inheritDoc
*/
readonly command: string;
/**
* @inheritDoc
*/
readonly describe: string;
private sandboxName?: string;
/**
* Creates sandbox command.
*/
constructor(
private readonly sandboxFactory: SandboxSingletonFactory,
private readonly sandboxSubCommands: CommandModule[],
private clientConfigGeneratorAdapter: ClientConfigGeneratorAdapter,
private commandMiddleware: CommandMiddleware,
private readonly sandboxEventHandlerCreator?: SandboxEventHandlerCreator
) {
this.command = 'sandbox';
this.describe = 'Starts sandbox, watch mode for amplify deployments';
}
/**
* @inheritDoc
*/
handler = async (args: SandboxCommandOptions): Promise<void> => {
const sandbox = await this.sandboxFactory.getInstance();
this.sandboxName = args.name;
// attaching event handlers
const clientConfigLifecycleHandler = new ClientConfigLifecycleHandler(
this.clientConfigGeneratorAdapter,
args['config-out-dir'],
args['config-format']
);
const eventHandlers = this.sandboxEventHandlerCreator?.({
sandboxName: this.sandboxName,
clientConfigLifecycleHandler,
});
if (eventHandlers) {
Object.entries(eventHandlers).forEach(([event, handlers]) => {
handlers.forEach((handler) => sandbox.on(event, handler));
});
}
const watchExclusions = args.exclude ?? [];
const clientConfigWritePath = await getClientConfigPath(
args['config-out-dir'],
args['config-format']
);
watchExclusions.push(clientConfigWritePath);
await sandbox.start({
dir: args['dir-to-watch'],
exclude: watchExclusions,
name: args.name,
profile: args.profile,
});
process.once('SIGINT', () => void this.sigIntHandler());
};
/**
* @inheritDoc
*/
builder = (yargs: Argv): Argv<SandboxCommandOptions> => {
return (
yargs
// Cast to erase options types used in internal sub command implementation. Otherwise, compiler fails here.
.command(this.sandboxSubCommands)
.option('dir-to-watch', {
describe:
'Directory to watch for file changes. All subdirectories and files will be included. defaults to the current directory.',
type: 'string',
array: false,
global: false,
})
.option('exclude', {
describe:
'An array of paths or glob patterns to ignore. Paths can be relative or absolute and can either be files or directories',
type: 'string',
array: true,
global: false,
})
.option('name', {
describe:
'An optional name to distinguish between different sandbox environments. Default is the name in your package.json',
type: 'string',
array: false,
global: false,
})
.option('config-format', {
describe: 'Client config output format',
type: 'string',
array: false,
choices: Object.values(ClientConfigFormat),
global: false,
})
.option('config-out-dir', {
describe:
'A path to directory where config is written. If not provided defaults to current process working directory.',
type: 'string',
array: false,
global: false,
})
.option('profile', {
describe: 'An AWS profile name.',
type: 'string',
array: false,
})
.check(async (argv) => {
if (argv['dir-to-watch']) {
await this.validateDirectory('dir-to-watch', argv['dir-to-watch']);
}
if (argv['config-out-dir']) {
await this.validateDirectory(
'config-out-dir',
argv['config-out-dir']
);
}
if (argv.name) {
const projectNameRegex = /^[a-zA-Z0-9-]{1,15}$/;
if (!argv.name.match(projectNameRegex)) {
throw new Error(
`--name should match [a-zA-Z0-9-] and less than 15 characters.`
);
}
}
return true;
})
.middleware([this.commandMiddleware.ensureAwsCredentialAndRegion])
.fail((msg, err) => {
handleCommandFailure(msg, err, yargs);
yargs.exit(1, err);
})
);
};
sigIntHandler = async () => {
const answer = await AmplifyPrompter.yesOrNo({
message:
'Would you like to delete all the resources in your sandbox environment (This cannot be undone)?',
defaultValue: false,
});
if (answer)
await (
await this.sandboxFactory.getInstance()
).delete({ name: this.sandboxName });
};
private validateDirectory = async (option: string, dir: string) => {
let stats;
try {
stats = await fsp.stat(dir, {});
} catch (e) {
throw new Error(`--${option} ${dir} does not exist`);
}
if (!stats.isDirectory()) {
throw new Error(`--${option} ${dir} is not a valid directory`);
}
};
}