Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix config file resolution memoization #8231

Merged
merged 1 commit into from Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/plugins/interactiveCli/initializeService.js
Expand Up @@ -80,7 +80,7 @@ module.exports = {

process.chdir(projectDir);
serverless.config.servicePath = projectDir;
getServerlessConfigFile.cache.delete(serverless);
getServerlessConfigFile.delete(serverless);
getServerlessConfigFile(serverless);
})
.then(serverlessConfigFile => {
Expand Down
35 changes: 17 additions & 18 deletions lib/utils/getServerlessConfigFile.js
Expand Up @@ -4,6 +4,7 @@ const _ = require('lodash');
const BbPromise = require('bluebird');
const path = require('path');
const resolveModulePath = require('ncjsm/resolve');
const memoizee = require('memoizee');
const spawn = require('child-process-ext/spawn');
const fileExists = require('./fs/fileExists');
const readFile = require('./fs/readFile');
Expand Down Expand Up @@ -107,25 +108,23 @@ const handleJsOrTsConfigFile = configFile =>
}
});

const getServerlessConfigFile = _.memoize(
serverless =>
getServerlessConfigFilePath(serverless).then(configFilePath => {
if (!configFilePath) return null;
const fileExtension = path.extname(configFilePath);
const isJSOrTsConfigFile = fileExtension === '.js' || fileExtension === '.ts';
const getServerlessConfigFile = memoizee(serverless =>
getServerlessConfigFilePath(serverless).then(configFilePath => {
if (!configFilePath) return null;
const fileExtension = path.extname(configFilePath);
const isJSOrTsConfigFile = fileExtension === '.js' || fileExtension === '.ts';

return (isJSOrTsConfigFile
? handleJsOrTsConfigFile(configFilePath)
: readFile(configFilePath)
).then(config => {
if (_.isPlainObject(config)) return config;
throw new ServerlessError(
`${path.basename(configFilePath)} must export plain object`,
'INVALID_CONFIG_OBJECT_TYPE'
);
});
}),
serverless => `${serverless.processedInput.options.config} - ${serverless.config.servicePath}`
return (isJSOrTsConfigFile
? handleJsOrTsConfigFile(configFilePath)
: readFile(configFilePath)
).then(config => {
if (_.isPlainObject(config)) return config;
throw new ServerlessError(
`${path.basename(configFilePath)} must export plain object`,
'INVALID_CONFIG_OBJECT_TYPE'
);
});
})
);

module.exports = { getConfigFilePath, getServerlessConfigFile, getServerlessConfigFilePath };