Skip to content

Commit

Permalink
fix: Ensure to memoize config file resolution by instance
Browse files Browse the repository at this point in the history
Memoizing by service name doesn't work when test configuration changes in same process
  • Loading branch information
medikoo committed Sep 14, 2020
1 parent 05627d6 commit 3177e40
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
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 };

0 comments on commit 3177e40

Please sign in to comment.