-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathfile.js
More file actions
191 lines (185 loc) · 6.25 KB
/
file.js
File metadata and controls
191 lines (185 loc) · 6.25 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
'use strict';
const ensureString = require('type/string/ensure');
const isPlainFunction = require('type/plain-function/is');
const path = require('path');
const fsp = require('fs').promises;
const yaml = require('js-yaml');
const cloudformationSchema = require('../../../utils/serverless-utils/cloudformation-schema');
const ServerlessError = require('../../../serverless-error');
const readFile = async (filePath, serviceDir) => {
try {
return await fsp.readFile(filePath, 'utf8');
} catch (error) {
if (error.code === 'ENOENT') return null;
throw new ServerlessError(
`Cannot parse "${filePath.slice(serviceDir.length + 1)}": ${error.message}`,
'FILE_NOT_ACCESSIBLE'
);
}
};
module.exports = {
resolve: async ({
serviceDir,
params,
address,
resolveConfigurationProperty,
resolveVariable,
resolveVariablesInString,
options,
}) => {
if (!params || !params[0]) {
throw new ServerlessError(
'Missing path argument in variable "file" source',
'MISSING_FILE_SOURCE_PATH'
);
}
const filePath = path.resolve(
serviceDir,
ensureString(params[0], {
Error: ServerlessError,
errorMessage: 'Non-string path argument in variable "file" source: %v',
errorCode: 'INVALID_FILE_SOURCE_PATH_ARGUMENT',
})
);
if (address != null) {
address = ensureString(address, {
Error: ServerlessError,
errorMessage: 'Non-string address argument for variable "file" source: %v',
errorCode: 'INVALID_FILE_SOURCE_ADDRESS',
});
}
let isResolvedByFunction = false;
const content = await (async () => {
switch (path.extname(filePath)) {
case '.yml':
case '.yaml': {
const yamlContent = await readFile(filePath, serviceDir);
if (yamlContent == null) return null;
try {
return yaml.load(yamlContent, {
filename: filePath,
schema: cloudformationSchema,
});
} catch (error) {
throw new ServerlessError(
`Cannot parse "${filePath.slice(serviceDir.length + 1)}": ${error.message}`,
'FILE_PARSE_ERROR'
);
}
}
case '.tfstate':
// fallthrough
case '.json': {
const jsonContent = await readFile(filePath, serviceDir);
if (jsonContent == null) return null;
try {
return JSON.parse(jsonContent);
} catch (error) {
throw new ServerlessError(
`Cannot parse "${filePath.slice(serviceDir.length + 1)}": JSON parse error: ${
error.message
}`,
'FILE_PARSE_ERROR'
);
}
}
case '.js':
case '.cjs': {
try {
require.resolve(filePath);
} catch (error) {
return null;
}
let result;
try {
result = require(filePath);
} catch (error) {
throw new ServerlessError(
`Cannot load "${filePath.slice(serviceDir.length + 1)}": Initialization error: ${
error && error.stack ? error.stack : error
}`,
'FILE_CONTENT_RESOLUTION_ERROR'
);
}
if (isPlainFunction(result)) {
try {
isResolvedByFunction = true;
return await result({ options, resolveConfigurationProperty, resolveVariable });
} catch (error) {
if (error.code === 'MISSING_VARIABLE_DEPENDENCY') throw error;
if (
error.constructor.name === 'ServerlessError' &&
error.message.startsWith('Cannot resolve variable at ')
) {
throw error;
}
throw new ServerlessError(
`Cannot resolve "${path.basename(filePath)}": Returned JS function errored with: ${
error && error.stack ? error.stack : error
}`,
'JS_FILE_FUNCTION_RESOLUTION_ERROR'
);
}
}
try {
return await result;
} catch (error) {
throw new ServerlessError(
`Cannot resolve "${path.basename(filePath)}": Received rejection: ${
error && error.stack ? error.stack : error
}`,
'JS_FILE_RESOLUTION_ERROR'
);
}
}
default:
// Anything else support as plain text
return readFile(filePath, serviceDir);
}
})();
if (!address) return { value: content };
if (content == null) return { value: null };
const propertyKeys = address.split('.');
let result = content;
for (const propertyKey of propertyKeys) {
if (typeof result === 'string') result = await resolveVariablesInString(result);
result = result[propertyKey];
if (result == null) return { value: null };
if (!isResolvedByFunction) {
if (isPlainFunction(result)) {
isResolvedByFunction = true;
try {
result = await result({ options, resolveConfigurationProperty, resolveVariable });
} catch (error) {
if (error.code === 'MISSING_VARIABLE_DEPENDENCY') throw error;
if (
error.constructor.name === 'ServerlessError' &&
error.message.startsWith('Cannot resolve variable at ')
) {
throw error;
}
throw new ServerlessError(
`Cannot resolve "${address}" out of "${path.basename(
filePath
)}": Received rejection: ${error && error.stack ? error.stack : error}`,
'JS_FILE_PROPERTY_FUNCTION_RESOLUTION_ERROR'
);
}
} else {
try {
result = await result;
} catch (error) {
throw new ServerlessError(
`Cannot resolve "${address}" out of "${path.basename(
filePath
)}": Received rejection: ${error && error.stack ? error.stack : error}`,
'JS_FILE_PROPERTY_PROMISE_RESOLUTION_ERROR'
);
}
if (result == null) return { value: null };
}
}
}
return { value: result };
},
};