forked from serverless/serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add init command for easy onboarding
- Loading branch information
1 parent
662edbd
commit af35ee3
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"plugins": [ | ||
"./init/index.js", | ||
"./create/create.js", | ||
"./install/install.js", | ||
"./package/index.js", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
'use strict'; | ||
|
||
const BbPromise = require('bluebird'); | ||
const path = require('path'); | ||
const os = require('os'); | ||
|
||
class Init { | ||
constructor(serverless, options) { | ||
this.serverless = serverless; | ||
this.options = options; | ||
|
||
this.commands = { | ||
init: { | ||
usage: 'Initializes a provider with the Serverless Framework', | ||
lifecycleEvents: [ | ||
'init', | ||
], | ||
options: { | ||
provider: { | ||
usage: 'Name of the provider', | ||
required: true, | ||
shortcut: 'p', | ||
}, | ||
key: { | ||
usage: 'Access key for the provider', | ||
shortcut: 'k', | ||
}, | ||
secret: { | ||
usage: 'Secret key for the provider', | ||
shortcut: 's', | ||
} | ||
}, | ||
}, | ||
}; | ||
|
||
this.hooks = { | ||
'init:init': () => BbPromise.bind(this) | ||
.then(this.init), | ||
}; | ||
} | ||
|
||
init() { | ||
|
||
// Sanitize | ||
this.options.provider = this.options.provider.toLowerCase(); | ||
|
||
// Validate | ||
if (['aws'].indexOf(this.options.provider) < 0) { | ||
throw new this.serverless.classes.Error(`Only 'aws' is supported at this time.`); | ||
} | ||
|
||
// Init AWS | ||
return this.initAws(); | ||
}; | ||
|
||
/** | ||
* Init AWS | ||
* - Saves AWS API Keys to a profile on the file system | ||
*/ | ||
|
||
initAws() { | ||
|
||
// Validate | ||
if (!this.options.key || !this.options.secret) { | ||
throw new this.serverless.classes.Error(`Please include --key and --secret options for AWS.`); | ||
} | ||
|
||
// Inform | ||
this.serverless.cli.log('Initializing AWS...'); | ||
this.serverless.cli.log(`Storing an AWS profile on your local file system in '~/.aws/credentials'...`); | ||
|
||
// Locate home directory on user's machine | ||
let env = process.env; | ||
let home = env.HOME || | ||
env.USERPROFILE || | ||
(env.HOMEPATH ? ((env.HOMEDRIVE || 'C:/') + env.HOMEPATH) : null); | ||
|
||
if (!home) { | ||
throw new this.serverless.classes.Error(`Can't find home directory on your local file system.`); | ||
} | ||
|
||
// Check if ~/.aws/credentials exists | ||
let configDir = path.join(home, '.aws'), | ||
credsPath = path.join(configDir, 'credentials'); | ||
|
||
if (this.serverless.utils.fileExistsSync(credsPath)) { | ||
|
||
// Check if credentials files contains anything | ||
let credsFile = this.serverless.utils.readFileSync(credsPath); | ||
|
||
// If credentials file is malformed, throw error and give guidance | ||
if ((credsFile.length && credsFile.indexOf('aws_access_key_id=') < 0) || | ||
(credsFile.length && credsFile.indexOf('aws_secret_access_key=') < 0)) { | ||
// TODO: Add guidance in error message when the serverless error parser stops poorly reformatting everything. | ||
throw new this.serverless.classes.Error('~/.aws/credentials exists, but it\'s formatted incorrectly. Open it and make sure it\'s correctly formatted.'); | ||
} | ||
|
||
// If credentials file exists and is fine, say so, then exit | ||
if (credsFile.length) { | ||
this.serverless.cli.log('Exiting... ~/.aws/credentials exists and is formatted correctly. Make sure you\'re using the correct profile in your serverless.yml'); | ||
return BbPromise.resolve(); | ||
} | ||
} | ||
|
||
// Write credentials file with 'default' profile | ||
this.serverless.utils.writeFileSync( | ||
credsPath, | ||
'[default]' + os.EOL + | ||
'aws_access_key_id=' + this.options.key + os.EOL + | ||
'aws_secret_access_key=' + this.options.secret + os.EOL); | ||
|
||
// Inform | ||
this.serverless.cli.log('Success! ~/.aws/credentials was created and your access keys were stored under the \'default\' profile.'); | ||
|
||
return BbPromise.resolve(); | ||
} | ||
} | ||
|
||
module.exports = Init; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
const expect = require('chai').expect; | ||
const Init = require('../index.js'); | ||
const Serverless = require('../../../Serverless'); | ||
|
||
describe('Init', () => { | ||
let info; | ||
let serverless; | ||
|
||
beforeEach(() => { | ||
serverless = new Serverless(); | ||
init = new Init(serverless); | ||
}); | ||
|
||
describe('#constructor()', () => { | ||
it('should have commands', () => expect(info.commands).to.be.not.empty); | ||
}); | ||
}); |