-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathread.js
More file actions
170 lines (162 loc) · 5.46 KB
/
read.js
File metadata and controls
170 lines (162 loc) · 5.46 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
'use strict';
const ensureString = require('type/string/ensure');
const isPlainObject = require('type/plain-object/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 readConfigurationFile = async (configurationPath) => {
try {
return await fsp.readFile(configurationPath, 'utf8');
} catch (error) {
if (error.code === 'ENOENT') {
throw new ServerlessError(
`Cannot parse "${path.basename(configurationPath)}": File not found`,
'CONFIGURATION_NOT_FOUND'
);
}
throw new ServerlessError(
`Cannot parse "${path.basename(configurationPath)}": ${error.message}`,
'CONFIGURATION_NOT_ACCESSIBLE'
);
}
};
const parseConfigurationFile = async (configurationPath) => {
switch (path.extname(configurationPath)) {
case '.yml':
case '.yaml': {
const content = await readConfigurationFile(configurationPath);
try {
return yaml.load(content, {
filename: configurationPath,
schema: cloudformationSchema,
});
} catch (error) {
throw new ServerlessError(
`Cannot parse "${path.basename(configurationPath)}": ${error.message}`,
'CONFIGURATION_PARSE_ERROR'
);
}
}
case '.json': {
const content = await readConfigurationFile(configurationPath);
try {
return JSON.parse(content);
} catch (error) {
throw new ServerlessError(
`Cannot parse "${path.basename(configurationPath)}": JSON parse error: ${error.message}`,
'CONFIGURATION_PARSE_ERROR'
);
}
}
case '.ts':
case '.mts': {
try {
/**
* Jiti does not support `tsconfig.paths`, so we need to use tsx to load the configuration file.
* @see https://github.com/unjs/jiti/issues/373
* @see https://tsx.is/dev-api/tsx-require
*/
// eslint-disable-next-line import/no-unresolved
const tsx = require('tsx/cjs/api');
const content = tsx.require(configurationPath, __filename);
return content.default || content;
} catch (error) {
throw new ServerlessError(
`Cannot parse "${path.basename(configurationPath)}": Initialization error: ${
error && error.stack ? error.stack : error
}`,
'CONFIGURATION_INITIALIZATION_ERROR'
);
}
}
// fallthrough
case '.cjs':
case '.js': {
const configurationEventuallyDeferred = (() => {
try {
require.resolve(configurationPath);
} catch {
throw new ServerlessError(
`Cannot load "${path.basename(configurationPath)}": File not found`,
'CONFIGURATION_NOT_FOUND'
);
}
try {
return require(configurationPath);
} catch (error) {
throw new ServerlessError(
`Cannot load "${path.basename(configurationPath)}": Initialization error: ${
error && error.stack ? error.stack : error
}`,
'CONFIGURATION_INITIALIZATION_ERROR'
);
}
})();
try {
return await configurationEventuallyDeferred;
} catch (error) {
throw new ServerlessError(
`Cannot load "${path.basename(configurationPath)}": Initialization error: ${
error && error.stack ? error.stack : error
}`,
'CONFIGURATION_INITIALIZATION_ERROR'
);
}
}
case '.mjs': {
try {
require.resolve(configurationPath);
} catch {
throw new ServerlessError(
`Cannot load "${path.basename(configurationPath)}": File not found`,
'CONFIGURATION_NOT_FOUND'
);
}
try {
// dynamic import might not be available depending on the node version of the user, but we assume it is, since
// the user explicitly declared his use of es-module syntax by using the .mjs extension.
return (await require('../utils/import-esm')(configurationPath)).default;
} catch (error) {
throw new ServerlessError(
`Cannot load "${path.basename(configurationPath)}": Initialization error: ${
error && error.stack ? error.stack : error
}`,
'CONFIGURATION_INITIALIZATION_ERROR'
);
}
}
default:
throw new ServerlessError(
`Cannot parse "${path.basename(configurationPath)}": Unsupported file extension`,
'UNSUPPORTED_CONFIGURATION_TYPE'
);
}
};
module.exports = async (configurationPath) => {
configurationPath = path.resolve(
ensureString(configurationPath, {
name: 'configurationPath',
})
);
let configuration = await parseConfigurationFile(configurationPath);
if (!isPlainObject(configuration)) {
throw new ServerlessError(
`Invalid configuration at "${path.basename(configurationPath)}": Plain object expected`,
'INVALID_CONFIGURATION_EXPORT'
);
}
// Ensure no internal complex objects and no circural references
try {
configuration = JSON.parse(JSON.stringify(configuration));
} catch (error) {
throw new ServerlessError(
`Invalid configuration at "${path.basename(
configurationPath
)}": Plain JSON structure expected, when parsing observed error: ${error.message}`,
'INVALID_CONFIGURATION_STRUCTURE'
);
}
return configuration;
};