Skip to content

Commit 32a39d7

Browse files
author
Alice
committed
separated yaml loading logic out into reusable functions
1 parent 0ae1793 commit 32a39d7

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/index.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,19 @@ export default class StepFunctionsOfflinePlugin implements Plugin {
203203
});
204204
}
205205

206+
private parseYaml<T>(filename: string): Promise<T> {
207+
return this.serverless.yamlParser.parse(filename);
208+
}
209+
210+
private serverlessFileExists(filename) {
211+
const serverlessPath = this.serverless.config.servicePath;
212+
if (!serverlessPath) {
213+
throw new this.serverless.classes.Error('Could not find serverless manifest');
214+
}
215+
const fullPath = path.join(serverlessPath, filename);
216+
return this.serverless.utils.fileExistsSync(fullPath);
217+
}
218+
206219
async getRawConfig(): Promise<ServerlessWithError['service']> {
207220
const serverlessPath = this.serverless.config.servicePath;
208221
if (!serverlessPath) {
@@ -211,27 +224,24 @@ export default class StepFunctionsOfflinePlugin implements Plugin {
211224

212225
const manifestFilenames = ['serverless.yaml', 'serverless.yml', 'serverless.json', 'serverless.js'];
213226

214-
const manifestFilename = manifestFilenames
215-
.map(filename => path.join(serverlessPath, filename))
216-
.find(filename => this.serverless.utils.fileExistsSync(filename));
217-
227+
const manifestFilename = manifestFilenames.find(name => this.serverlessFileExists(name));
218228
if (!manifestFilename) {
219229
throw new this.serverless.classes.Error(
220230
`Could not find serverless manifest at path ${serverlessPath}. If this path is incorreect you should adjust the 'servicePath' variable`
221231
);
222232
}
233+
const manifestPath = path.join(serverlessPath, manifestFilename);
223234
let fromFile: ServerlessWithError['service'];
224-
if (/\.json|\.js$/.test(manifestFilename)) {
235+
if (/\.json|\.js$/.test(manifestPath)) {
225236
try {
226-
fromFile = await import(manifestFilename);
237+
fromFile = await import(manifestPath);
227238
return fromFile;
228239
} catch (err) {
229240
console.error(err);
230-
throw new Error(`Unable to import manifest at: ${manifestFilename}`);
241+
throw new Error(`Unable to import manifest at: ${manifestPath}`);
231242
}
232243
}
233-
234-
return this.serverless.yamlParser.parse(manifestFilename);
244+
return this.parseYaml<ServerlessWithError['service']>(manifestPath);
235245
}
236246

237247
parseConfig(): Promise<void> {

0 commit comments

Comments
 (0)