variable population now is aware of default stage and region

This commit is contained in:
Eslam A. Hefnawy 2016-06-20 18:36:55 +02:00
parent 96a3441c1e
commit ff0ed5a200
3 changed files with 42 additions and 42 deletions

View File

@ -42,20 +42,17 @@ class Serverless {
}
init() {
return this.service.load()
// create a new CLI instance
this.cli = new CLI(this, this.config.interactive);
// get an array of commands and options that should be processed
this.processedInput = this.cli.processInput();
// set the options
this.pluginManager.setOptions(this.processedInput.options);
return this.service.load(this.processedInput.options)
.then(() => {
// create a new CLI instance
this.cli = new CLI(
this,
this.config.interactive
);
// get an array of commands and options that should be processed
this.processedInput = this.cli.processInput();
// set the options
this.pluginManager.setOptions(this.processedInput.options);
// load all plugins
this.pluginManager.loadAllPlugins();

View File

@ -76,15 +76,19 @@ class Service {
return BbPromise.resolve(that);
})
.then(() => {
/*
* TODO: Do we need to update default options here?
*/
if (!options.stage) {
options.stage = this.defaults.stage;
}
if (!options.region) {
options.region = this.defaults.region;
}
// Validate: Check stage exists
if (options.stage) this.getStage(options.stage);
this.getStage(options.stage);
// Validate: Check region exists in stage
if (options.region) this.getRegionInStage(options.stage, options.region);
this.getRegionInStage(options.stage, options.region);
let varTemplateSyntax = /\${([\s\S]+?)}/g;
@ -95,7 +99,9 @@ class Service {
this.variableSyntax = true;
}
const variablesObject = this.getVariables(options.stage, options.region);
const commonVars = this.getVariables();
const stageVars = this.getVariables(options.stage);
const regionVars = this.getVariables(options.stage, options.region);
// temporally remove environment obj. Doesn't make sense to
// populate environment (stages, regions, vars)
@ -117,11 +123,21 @@ class Service {
val.match(varTemplateSyntax).forEach((variableSyntax) => {
const variableName = variableSyntax
.replace(varTemplateSyntax, (match, varName) => varName.trim());
let value;
if (variableName in variablesObject) {
value = variablesObject[variableName];
let value;
if (variableName in commonVars) {
value = commonVars[variableName];
}
if (variableName in stageVars) {
value = stageVars[variableName];
}
if (variableName in regionVars) {
value = regionVars[variableName];
}
// Populate
if (!value && !value !== '') {
// SCLI.log('WARNING: This variable is not defined: ' + variableName);

View File

@ -230,18 +230,14 @@ describe('Service', () => {
},
stages: {
dev: {
vars: {
testVar: 'stageVar',
},
vars: {},
regions: {},
},
},
};
serverlessEnvYaml.stages.dev.regions['us-east-1'] = {
vars: {
testVar: 'regionVar',
},
vars: {},
};
SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yaml'),
@ -261,8 +257,8 @@ describe('Service', () => {
expect(loadedService.service).to.be.equal('commonVar');
expect(loadedService.plugins).to.deep.equal(['testPlugin']);
expect(loadedService.environment.vars).to.deep.equal(commonVars);
expect(serviceInstance.environment.stages.dev.regions['us-east-1'].vars.testVar)
.to.be.equal('regionVar');
expect(serviceInstance.environment.stages.dev.regions['us-east-1'].vars)
.to.deep.equal({});
expect(loadedService.resources.aws).to.deep.equal({ resourcesProp: 'value' });
expect(loadedService.resources.azure).to.deep.equal({});
expect(loadedService.resources.google).to.deep.equal({});
@ -292,9 +288,7 @@ describe('Service', () => {
};
serverlessEnvYaml.stages.dev.regions['us-east-1'] = {
vars: {
testVar: 'regionVar',
},
vars: {},
};
SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yaml'),
@ -304,10 +298,7 @@ describe('Service', () => {
const serverless = new Serverless({ servicePath: tmpDirPath });
serviceInstance = new Service(serverless);
const options = {
stage: 'dev',
};
return serviceInstance.load(options).then((loadedService) => {
return serviceInstance.load().then((loadedService) => {
expect(loadedService.service).to.be.equal('stageVar');
});
});
@ -349,11 +340,7 @@ describe('Service', () => {
const serverless = new Serverless({ servicePath: tmpDirPath });
serviceInstance = new Service(serverless);
const options = {
stage: 'dev',
region: 'us-east-1',
};
return serviceInstance.load(options).then((loadedService) => {
return serviceInstance.load().then((loadedService) => {
expect(loadedService.service).to.be.equal('regionVar');
});
});