-
Notifications
You must be signed in to change notification settings - Fork 815
/
index.ts
138 lines (114 loc) · 4.65 KB
/
index.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
import * as path from 'path';
import * as fs from 'fs-extra';
import * as rimraf from 'rimraf';
import { config } from 'dotenv';
import execa from 'execa';
import { getLayerDirectoryName, LayerDirectoryType } from '..';
import { v4 as uuid } from 'uuid';
export * from './add-circleci-tags';
export * from './api';
export * from './appsync';
export * from './envVars';
export * from './getAppId';
export * from './headless';
export * from './nexpect';
export * from './pinpoint';
export * from './projectMeta';
export * from './readJsonFile';
export * from './request';
export * from './retrier';
export * from './sdk-calls';
export * from './selectors';
export * from './sleep';
export * from './transformConfig';
export * from './admin-ui';
export * from './hooks';
// run dotenv config to update env variable
config();
export function deleteProjectDir(root: string) {
rimraf.sync(root);
}
export function deleteAmplifyDir(root: string) {
rimraf.sync(path.join(root, 'amplify'));
}
export function loadFunctionTestFile(fileName: string) {
const functionPath = getTestFileNamePath(fileName);
return fs.readFileSync(functionPath, 'utf-8').toString();
}
export function addNodeDependencies(root: string, functionName: string, dependencies: string[]) {
let indexPath = path.join(getPathToFunction(root, functionName), 'src');
execa.commandSync(`yarn add ${dependencies.join(' ')}`, { cwd: indexPath });
}
export function overrideFunctionCodeNode(root: string, functionName: string, sourceFileName: string, targetFileName: string = 'index.js') {
const sourcePath = getTestFileNamePath(sourceFileName);
const targetPath = path.join(getPathToFunction(root, functionName), 'src', targetFileName);
fs.copySync(sourcePath, targetPath);
}
export function overrideFunctionCodePython(
root: string,
functionName: string,
sourceFileName: string,
targetFileName: string = 'index.py',
) {
const sourcePath = getTestFileNamePath(sourceFileName);
const targetPath = path.join(getPathToFunction(root, functionName), 'lib', 'python', targetFileName);
fs.copySync(sourcePath, targetPath);
}
export function overrideFunctionSrcNode(root: string, functionName: string, content: string, targetFileName: string = 'index.js') {
const dirPath = path.join(getPathToFunction(root, functionName), 'src');
const targetPath = path.join(dirPath, targetFileName);
fs.ensureDirSync(dirPath);
fs.writeFileSync(targetPath, content);
}
export function overrideFunctionSrcPython(root: string, functionName: string, content: string, targetFileName: string = 'index.py') {
const dirPath = path.join(getPathToFunction(root, functionName), 'src');
const targetPath = path.join(dirPath, targetFileName);
fs.ensureDirSync(dirPath);
fs.writeFileSync(targetPath, content);
}
export function overrideLayerCodeNode(
root: string,
projName: string,
layerName: string,
content: string,
targetFileName: string = 'index.js',
) {
const dirPath = path.join(getPathToLayer(root, { projName, layerName }), 'lib', 'nodejs');
const targetPath = path.join(dirPath, targetFileName);
fs.ensureDirSync(dirPath);
fs.writeFileSync(targetPath, content);
}
export function overrideLayerCodePython(
root: string,
projName: string,
layerName: string,
content: string,
targetFileName: string = 'index.py',
) {
const dirPath = path.join(getPathToLayer(root, { projName, layerName }), 'lib', 'python');
const targetPath = path.join(dirPath, targetFileName);
fs.ensureDirSync(dirPath);
fs.writeFileSync(targetPath, content);
}
export function addOptFile(root: string, projName: string, layerName: string, content: string, targetFileName: string): void {
const dirPath = path.join(getPathToLayer(root, { projName, layerName }), 'opt');
const targetPath = path.join(dirPath, targetFileName);
fs.ensureDirSync(dirPath);
fs.writeFileSync(targetPath, content);
}
export function getFunctionSrcNode(root: string, functionName: string, fileName: string = 'index.js'): string {
const indexPath = path.join(getPathToFunction(root, functionName), 'src', fileName);
return fs.readFileSync(indexPath).toString();
}
const getTestFileNamePath = (fileName: string): string =>
path.join(__dirname, '..', '..', '..', 'amplify-e2e-tests', 'functions', fileName);
const getPathToFunction = (root: string, funcName: string): string => path.join(root, 'amplify', 'backend', 'function', funcName);
const getPathToLayer = (root: string, layerProjName: LayerDirectoryType): string =>
path.join(root, 'amplify', 'backend', 'function', getLayerDirectoryName(layerProjName));
/**
* Generate random resource name
* @returns generated resource name
*/
export function generateRandomShortId(): string {
return uuid().split('-')[0];
}