forked from Jimdo/serverless-dotenv
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Henrik Fricke
committed
Mar 22, 2017
0 parents
commit 9a8c420
Showing
8 changed files
with
2,825 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 2 | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
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 @@ | ||
node_modules |
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 @@ | ||
# serverless-dotenv |
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,22 @@ | ||
machine: | ||
node: | ||
version: 4.5.0 | ||
|
||
dependencies: | ||
pre: | ||
- npm install -g yarn | ||
- yarn global dot-json | ||
|
||
override: | ||
- yarn | ||
|
||
test: | ||
override: | ||
- yarn test | ||
|
||
deployment: | ||
production: | ||
tag: /v[0-9]+(\.[0-9]+)*$/ | ||
commands: | ||
- dot-json package.json version ${CIRCLE_TAG:1} | ||
- yarn publish |
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,36 @@ | ||
{ | ||
"name": "serverless-dotenv", | ||
"description": "serverless plugin to fetch environment variables and write it to a .env file", | ||
"main": "src/index.js", | ||
"author": "Henrik Fricke <[email protected]>", | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "$(yarn bin)/jest test", | ||
"lint": "$(yarn bin)/standard | $(yarn bin)/snazzy" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Jimdo/serverless-dotenv.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/Jimdo/serverless-dotenv/issues" | ||
}, | ||
"homepage": "https://github.com/Jimdo/serverless-dotenv", | ||
"dependencies": { | ||
"mkdirp": "^0.5.1" | ||
}, | ||
"devDependencies": { | ||
"jest": "^19.0.2", | ||
"snazzy": "^6.0.0", | ||
"standard": "^9.0.2" | ||
}, | ||
"standard": { | ||
"envs": [ | ||
"node", | ||
"jest" | ||
], | ||
"ignore": [ | ||
"node_modules/" | ||
] | ||
} | ||
} |
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,75 @@ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
const mkdirp = require('mkdirp') | ||
const util = require('util') | ||
|
||
class DotEnvPlugin { | ||
constructor (serverless) { | ||
this.serverless = serverless | ||
|
||
this.commands = { | ||
dotenv: { | ||
usage: 'Create .env file with serverless environment variables', | ||
lifecycleEvents: [ | ||
'init', | ||
'collectGlobalEnvVariables', | ||
'collectFunctionEnvVariables', | ||
'appendOfflineVariables', | ||
'writeDotEnvFile' | ||
] | ||
} | ||
} | ||
|
||
this.hooks = { | ||
'before:offline:start:init': () => this.serverless.pluginManager.run(['dotenv']), | ||
'dotenv:init': this.init.bind(this), | ||
'dotenv:collectGlobalEnvVariables': this.collectGlobalEnvVariables.bind(this), | ||
'dotenv:collectFunctionEnvVariables': this.collectFunctionEnvVariables.bind(this), | ||
'dotenv:appendOfflineVariables': this.appendOfflineVariables.bind(this), | ||
'dotenv:writeDotEnvFile': this.writeDotEnvFile.bind(this) | ||
} | ||
|
||
this.environmentVariables = {} | ||
} | ||
|
||
init () { | ||
this.serverless.cli.log('Creating .env file...') | ||
} | ||
|
||
collectGlobalEnvVariables () { | ||
this._collectEnvVariables(this.serverless.service.provider.environment) | ||
} | ||
|
||
collectFunctionEnvVariables () { | ||
const functions = this.serverless.service.functions | ||
|
||
Object.keys(functions).forEach(func => this._collectEnvVariables(functions[func].environment)) | ||
} | ||
|
||
appendOfflineVariables () { | ||
this.environmentVariables['IS_OFFLINE'] = true | ||
this.environmentVariables['API_ENDPOINT'] = 'http://localhost:3000' | ||
} | ||
|
||
writeDotEnvFile () { | ||
mkdirp.sync('.serverless') | ||
|
||
const path = util.format('%s/.serverless/.env', this.serverless.config.servicePath) | ||
let dotEnvDocument = '' | ||
|
||
Object.keys(this.environmentVariables).forEach(envVar => { | ||
dotEnvDocument += util.format('%s=%s\r\n', envVar, this.environmentVariables[envVar]) | ||
}) | ||
|
||
fs.writeFileSync(path, dotEnvDocument) | ||
} | ||
|
||
_collectEnvVariables (environment) { | ||
Object.keys(environment || {}).forEach(envVar => { | ||
this.environmentVariables[envVar] = environment[envVar] | ||
}) | ||
} | ||
} | ||
|
||
module.exports = DotEnvPlugin |
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,7 @@ | ||
'use strict' | ||
|
||
describe('Dotenv plugin', () => { | ||
it('should be true', () => { | ||
expect(true).toBeTruthy() | ||
}) | ||
}) |
Oops, something went wrong.