Skip to content

Commit

Permalink
Init project
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrik Fricke committed Mar 22, 2017
0 parents commit 9a8c420
Show file tree
Hide file tree
Showing 8 changed files with 2,825 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# serverless-dotenv
22 changes: 22 additions & 0 deletions circle.yml
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
36 changes: 36 additions & 0 deletions package.json
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/"
]
}
}
75 changes: 75 additions & 0 deletions src/index.js
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
7 changes: 7 additions & 0 deletions test/index.spec.js
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()
})
})
Loading

0 comments on commit 9a8c420

Please sign in to comment.