This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathindex.js
134 lines (119 loc) · 4.32 KB
/
index.js
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
const category = 'video';
const path = require('path');
const fs = require('fs-extra');
const { pushTemplates } = require('./provider-utils/awscloudformation/utils/video-staging');
const { createCDNEnvVars } = require('./provider-utils/awscloudformation/service-walkthroughs/vod-push');
async function add(context, providerName, service) {
const options = {
service,
providerPlugin: providerName,
};
const providerController = require(`./provider-utils/${providerName}/index`);
if (!providerController) {
context.print.error('Provider not configured for this category');
return;
}
return providerController.addResource(context, category, service, options);
}
async function console(context) {
context.print.info(`to be implemented: ${category} console`);
}
async function onAmplifyCategoryOutputChange(context) {
// Hard coded to CF. Find a better way to handle this.
const infoController = require('./provider-utils/awscloudformation/utils/video-getinfo');
await infoController.getInfoVideoAll(context);
}
async function createNewEnv(context, resourceName) {
const { amplify } = context;
const amplifyMeta = amplify.getProjectMeta();
const { teamProviderInfo, localEnvInfo } = context.exeInfo;
const { envName } = localEnvInfo;
if (teamProviderInfo
&& teamProviderInfo[envName]
&& teamProviderInfo[envName].categories
&& teamProviderInfo[envName].categories[category]
&& teamProviderInfo[envName].categories[category][resourceName]
&& teamProviderInfo[envName].categories[category][resourceName].secretPem) {
return;
}
const targetDir = amplify.pathManager.getBackendDirPath();
const props = JSON.parse(fs.readFileSync(`${targetDir}/video/${resourceName}/props.json`));
const options = amplifyMeta.video[resourceName];
if (options.serviceType === 'video-on-demand') {
if (props.contentDeliveryNetwork && props.contentDeliveryNetwork.signedKey) {
await createCDNEnvVars(context, options, resourceName);
}
}
}
async function initEnv(context) {
const { amplify } = context;
const amplifyMeta = amplify.getProjectMeta();
const projectEnvCreate = [];
if (!(category in amplifyMeta) || Object.keys(amplifyMeta[category]).length === 0) {
return;
}
Object.keys(amplifyMeta[category]).forEach((resourceName) => {
projectEnvCreate.push(createNewEnv(context, resourceName));
});
await Promise.all(projectEnvCreate);
await pushTemplates(context);
}
async function migrate(context) {
const { projectPath, amplifyMeta } = context.migrationInfo;
const migrateResourcePromises = [];
Object.keys(amplifyMeta).forEach((categoryName) => {
if (categoryName === category) {
Object.keys(amplifyMeta[category]).forEach((resourceName) => {
try {
const providerController = require(`./provider-utils/${amplifyMeta[category][resourceName].providerPlugin}/index`);
if (providerController) {
migrateResourcePromises.push(providerController.migrateResource(
context,
projectPath,
amplifyMeta[category][resourceName].service,
resourceName,
));
} else {
context.print.error(`Provider not configured for ${category}: ${resourceName}`);
}
} catch (e) {
context.print.warning(`Could not run migration for ${category}: ${resourceName}`);
throw e;
}
});
}
});
await Promise.all(migrateResourcePromises);
}
async function executeAmplifyCommand(context) {
let commandPath = path.normalize(path.join(__dirname, 'commands'));
if (context.input.command === 'help') {
commandPath = path.join(commandPath, category);
} else {
commandPath = path.join(commandPath, category, context.input.command);
}
const commandModule = require(commandPath);
await commandModule.run(context);
}
async function handleAmplifyEvent(context, args) {
if (args.event === 'PrePush') {
await handlePrePush(context);
}
}
async function handlePrePush(context) {
const { amplify } = context;
const amplifyMeta = amplify.getProjectMeta();
if (!(category in amplifyMeta) || Object.keys(amplifyMeta[category]).length === 0) {
return;
}
await pushTemplates(context);
}
module.exports = {
add,
console,
migrate,
onAmplifyCategoryOutputChange,
executeAmplifyCommand,
handleAmplifyEvent,
initEnv,
};