serverless/lib/utils/getServerlessConfigFile.js
Daniel Schep 2b81303370 --config option.
closes #5589
closes #4485
closes #4473
closes #4124
closes #5822
2019-06-10 11:19:14 -04:00

71 lines
2.0 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 getServerlessConfigFilePath = (configFileName, srvcPath) => {
const servicePath = srvcPath || process.cwd();
if (configFileName) {
const customPath = path.join(servicePath, configFileName);
return fileExists(customPath).then(exists => (exists ? customPath : null));
}
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.yaml) {
return yamlPath;
} else if (exists.yml) {
return ymlPath;
} else if (exists.json) {
return jsonPath;
} else if (exists.js) {
return jsPath;
}
return null;
});
};
const handleJsConfigFile = (jsConfigFile) => BbPromise.try(() => {
// use require to load serverless.js
// eslint-disable-next-line global-require
const configExport = require(jsConfigFile);
// In case of a promise result, first resolve it.
return configExport;
}).then(config => {
if (_.isPlainObject(config)) {
return config;
}
throw new Error('serverless.js must export plain object');
});
const getServerlessConfigFile = _.memoize((configFileName, srvcPath) => getServerlessConfigFilePath(configFileName, srvcPath)
.then(configFilePath => {
if (configFilePath !== null) {
const isJSConfigFile = _.last(_.split(configFilePath, '.')) === 'js';
if (isJSConfigFile) {
return handleJsConfigFile(configFilePath);
}
return readFile(configFilePath);
}
return '';
}), (configFileName, srvcPath) => srvcPath);
module.exports = { getServerlessConfigFile, getServerlessConfigFilePath };