1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | const program = require('commander');
|
10 | const AWS = require('aws-sdk');
|
11 | const apigateway = new AWS.APIGateway();
|
12 |
|
13 | program.option('--prefix <name>', 'stack prefix to remove the stages from');
|
14 | program.option(
|
15 | '--stage <name>',
|
16 | 'name of stage to delete from your apiGateway',
|
17 | 'dev'
|
18 | );
|
19 |
|
20 | program.option(
|
21 | '--doit',
|
22 | ('EXECUTE the commands to delete the stage from your prefixed stacks. Without'
|
23 | + ' this flag, the script only describes the actions that would be taken.'),
|
24 | false
|
25 | );
|
26 |
|
27 | program.parse(process.argv);
|
28 |
|
29 | const thisPrefix = (obj) => obj.name.startsWith(`${program.prefix}-`);
|
30 |
|
31 | const filterResponse = (response) => response.items.filter(thisPrefix);
|
32 |
|
33 |
|
34 | const removeStage = (restApi) => {
|
35 | const param = {
|
36 | restApiId: restApi.id,
|
37 | stageName: program.stage
|
38 | };
|
39 |
|
40 | if (program.doit) {
|
41 | console.log(
|
42 | `DELETING stage: ${param.stageName} from ${restApi.name}(${
|
43 | param.restApiId
|
44 | }).`
|
45 | );
|
46 |
|
47 | return apigateway.deleteStage(param).promise();
|
48 | }
|
49 | console.log(
|
50 | `will delete stage: ${param.stageName} from ${restApi.name}(${
|
51 | param.restApiId
|
52 | }).`
|
53 | );
|
54 | return Promise.resolve();
|
55 | };
|
56 |
|
57 | const removeEachStage = async (restApiList) => {
|
58 | await Promise.all(restApiList.map(removeStage));
|
59 | };
|
60 |
|
61 | apigateway
|
62 | .getRestApis({ limit: 500 })
|
63 | .promise()
|
64 | .then(filterResponse)
|
65 | .then(removeEachStage);
|