mirror of
https://github.com/serverless/serverless.git
synced 2025-12-08 19:46:03 +00:00
Fix #5242 Do this by first supplying the `servicePath` in `~/lib/plugins/lib/validate.js` and `~/lib/plugins/print.js` in a consistent manner with the code in `~/lib/classes/PluginManager.js`. Then, provide a default of `process.cwd()` for `servicePath` in `getCacheFilePath` and `getServerlessConfigFile`.
45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const _ = require('lodash');
|
|
const BbPromise = require('bluebird');
|
|
const path = require('path');
|
|
const fileExists = require('./fs/fileExists');
|
|
const readFile = require('./fs/readFile');
|
|
|
|
const getServerlessConfigFile = _.memoize((srvcPath) => {
|
|
const servicePath = srvcPath || process.cwd();
|
|
const jsonPath = path.join(servicePath, 'serverless.json');
|
|
const ymlPath = path.join(servicePath, 'serverless.yml');
|
|
const yamlPath = path.join(servicePath, 'serverless.yaml');
|
|
const jsPath = path.join(servicePath, 'serverless.js');
|
|
|
|
return BbPromise.props({
|
|
json: fileExists(jsonPath),
|
|
yml: fileExists(ymlPath),
|
|
yaml: fileExists(yamlPath),
|
|
js: fileExists(jsPath),
|
|
}).then((exists) => {
|
|
if (exists.json) {
|
|
return readFile(jsonPath);
|
|
} else if (exists.yml) {
|
|
return readFile(ymlPath);
|
|
} else if (exists.yaml) {
|
|
return readFile(yamlPath);
|
|
} else if (exists.js) {
|
|
return BbPromise.try(() => {
|
|
// use require to load serverless.js
|
|
// eslint-disable-next-line global-require
|
|
const config = require(jsPath);
|
|
|
|
if (_.isPlainObject(config)) {
|
|
return config;
|
|
}
|
|
throw new Error('serverless.js must export plain object');
|
|
});
|
|
}
|
|
return '';
|
|
});
|
|
});
|
|
|
|
module.exports = getServerlessConfigFile;
|