Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ custom:
Based on the configuration above the plugin will search for a file `scripts/output.js` with the following content:

```js
function handler (data, serverless) {
function handler (data, serverless, options) {
console.log('Received Stack Output', data)
}

Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
"description": "Serverless plugin to process AWS CloudFormation Stack Output",
"license": "MIT",
"author": "Sebastian Müller <mail@sbstjn.com>",
"main": "dist/plugin.js",
"main": "dist",
"scripts": {
"clean": "rimraf dist",
"test": "jest",
"test:cover": "jest --coverage",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"lint": "tslint {src,test}/**/*.ts",
"prebuild": "yarn clean",
"build": "tsc"
},
"repository": {
Expand All @@ -32,13 +34,14 @@
"yamljs": "^0.3.0"
},
"devDependencies": {
"sinon": "^2.3.6",
"jasmine-data-provider": "^2.2.0",
"@types/jest": "^20.0.5",
"@types/node": "^8.0.19",
"coveralls": "^2.13.1",
"dot-json": "^1.0.3",
"jasmine-data-provider": "^2.2.0",
"jest": "^20.0.4",
"rimraf": "^2.6.2",
"sinon": "^2.3.6",
"ts-jest": "^20.0.7",
"tslint": "^5.5.0",
"typescript": "^2.4.2"
Expand Down
10 changes: 7 additions & 3 deletions src/file.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as fs from 'fs'

export default class StackOutputFile {
constructor (private path: string) { }
constructor (
public path: string
) { }

public format (data: {}) {
public format (data: object) {
const ext = this.path.split('.').pop() || ''

switch (ext.toUpperCase()) {
Expand All @@ -19,13 +21,15 @@ export default class StackOutputFile {
}
}

public save (data: {}) {
public save (data: object) {
const content = this.format(data)

try {
fs.writeFileSync(this.path, content)
} catch (e) {
throw new Error('Cannot write to file: ' + this.path)
}

return Promise.resolve()
}
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Plugin from './plugin'

module.exports = Plugin
109 changes: 61 additions & 48 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import * as assert from 'assert'
import * as util from 'util'
import * as assert from 'assert'
import * as util from 'util'

import StackOutputFile from './file'

class StackOutputPlugin {
export default class StackOutputPlugin {
public hooks: {}
private output: OutputConfig

constructor (private serverless: Serverless, private options: Serverless.Options) {
constructor (
private serverless: Serverless,
private options: Serverless.Options
) {
this.hooks = {
'after:deploy:deploy': this.process.bind(this)
}
Expand All @@ -24,7 +27,10 @@ class StackOutputPlugin {
}

get stackName () {
return this.serverless.service.getServiceName() + '-' + this.serverless.getProvider('aws').getStage()
return util.format('%s-%s',
this.serverless.service.getServiceName(),
this.serverless.getProvider('aws').getStage()
)
}

private hasConfig (key: string) {
Expand All @@ -40,76 +46,84 @@ class StackOutputPlugin {
}

private getConfig (key: string) {
return this.serverless.config.servicePath + '/' + this.output[key]
return util.format('%s/%s',
this.serverless.config.servicePath,
this.output[key]
)
}

private callHandler (data: {}) {
private callHandler (data: object) {
const splits = this.handler.split('.')
const func = splits.pop() || ''
const file = splits.join('.')

return new Promise((resolve) => {
require(splits.join('.'))[func](data, this.serverless)
require(file)[func](
data,
this.serverless,
this.options
)

resolve()
})
return Promise.resolve()
}

private saveFile (data: {}) {
private saveFile (data: object) {
const f = new StackOutputFile(this.file)

return new Promise((resolve) => {
f.save(data)

resolve()
})
return f.save(data)
}

private fetch (): Promise<StackDescriptionList> {
return this.serverless.getProvider('aws').request(
'CloudFormation',
'describeStacks',
{
StackName: this.stackName
},
{ StackName: this.stackName },
this.serverless.getProvider('aws').getStage(),
this.serverless.getProvider('aws').getRegion()
)
}

private beautify (data: {Stacks: Array<{ Outputs: Array<{}> }>}) {
private beautify (data: {Stacks: Array<{ Outputs: StackOutputPair[] }>}) {
const stack = data.Stacks.pop() || { Outputs: [] }
const output = stack.Outputs || []

return output.reduce(
(obj: {}, item: StackOutputPair) => Object.assign(obj, {[item.OutputKey]: item.OutputValue}),
{}
(obj, item: StackOutputPair) => (
Object.assign(obj, { [item.OutputKey]: item.OutputValue })
), {}
)
}

private handle (data: {}) {
const promises = []
private handle (data: object) {
return Promise.all(
[
this.handleHandler(data),
this.handleFile(data)
]
)
}

if (this.hasHandler()) {
promises.push(
this.callHandler(data).then(
() => this.serverless.cli.log(
util.format('Stack Output processed with handler: %s', this.output.handler)
)
private handleHandler(data: object) {
return this.hasHandler() ? (
this.callHandler(
data
).then(
() => this.serverless.cli.log(
util.format('Stack Output processed with handler: %s', this.output.handler)
)
)
}
) : Promise.resolve()
}

if (this.hasFile()) {
promises.push(
this.saveFile(data).then(
() => this.serverless.cli.log(
util.format('Stack Output saved to file: %s', this.output.file)
)
private handleFile(data: object) {
return this.hasFile() ? (
this.saveFile(
data
).then(
() => this.serverless.cli.log(
util.format('Stack Output saved to file: %s', this.output.file)
)
)
}

return Promise.all(promises)
) : Promise.resolve()
}

private validate () {
Expand All @@ -123,20 +137,19 @@ class StackOutputPlugin {
}

private process () {
console.log('running stack-output-plugin')

return Promise.resolve().then(
Promise.resolve()
.then(
() => this.validate()
).then(
() => this.fetch()
).then(
(res: StackDescriptionList) => this.beautify(res)
(res) => this.beautify(res)
).then(
(res) => this.handle(res)
).catch(
(err) => this.serverless.cli.log(util.format('Cannot process Stack Output: %s!', err.message))
(err) => this.serverless.cli.log(
util.format('Cannot process Stack Output: %s!', err.message)
)
)
}
}

module.exports = StackOutputPlugin
17 changes: 12 additions & 5 deletions test/file.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'
import using from 'jasmine-data-provider'
import util from 'util'

import * as using from 'jasmine-data-provider'
import File from '../src/file'

describe('File', () => {
Expand All @@ -21,13 +21,20 @@ describe('File', () => {
{file: 'test.zip', valid: false}
],
(data) => {
it('detects' + (data.valid ? ' valid ' : ' invalid ') + data.file, () => {
const name = util.format(
'detects %s %s',
data.valid ? 'valid' : 'invalid',
data.file
)

it(name, () => {
const f = new File(data.file)
const output = { foo: 'bar' }

if (data.valid) {
expect(f.format({ foo: 'bar' })).toBe(data.data)
expect(f.format(output)).toBe(data.data)
} else {
expect(() => f.format()).toThrow()
expect(() => f.format(output)).toThrow()
}
})
}
Expand Down
7 changes: 3 additions & 4 deletions test/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'
import sinon from 'sinon'

import * as sinon from 'sinon'
import * as Plugin from '../src/plugin'
import Plugin from '../src/plugin'

describe('Plugin', () => {
let providerMock = null
Expand Down Expand Up @@ -43,7 +42,7 @@ describe('Plugin', () => {
}
}

const test = new Plugin(config)
const test = new Plugin(config, { serverless: true }, { options: true })

expect(test.hasHandler()).toBe(true)
expect(test.hasFile()).toBe(false)
Expand Down
28 changes: 18 additions & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"target": "es5",
"sourceMap": false,
"allowJs": false,
"moduleResolution": "node",
"noImplicitAny": true,
"rootDir": "./src",
"outDir": "./dist",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"outDir": "dist",
"sourceMap": true,
"strictNullChecks": true,
"noImplicitAny": true,
"strict": true,
"suppressImplicitAnyIndexErrors": true,
"target": "esnext"
"noUnusedLocals": true,
"allowSyntheticDefaultImports": true,
"lib": [
"es2015",
"es2016"
],
"declaration": false
},
"include": [
"src/**/*",
"vendor/**/*"
],
"exclude": [
"node_modules/**/*"
"node_modules/**/*",
"src/**/*.spec.ts"
]
}
}
10 changes: 8 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1989,6 +1989,12 @@ rimraf@^2.6.1:
dependencies:
glob "^7.0.5"

rimraf@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.2.tgz#2ed8150d24a16ea8651e6d6ef0f47c4158ce7a36"
dependencies:
glob "^7.0.5"

safe-buffer@^5.0.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853"
Expand Down Expand Up @@ -2291,8 +2297,8 @@ type-detect@^4.0.0:
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.3.tgz#0e3f2670b44099b0b46c284d136a7ef49c74c2ea"

typescript@^2.4.2:
version "2.4.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.4.2.tgz#f8395f85d459276067c988aa41837a8f82870844"
version "2.6.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.6.1.tgz#ef39cdea27abac0b500242d6726ab90e0c846631"

uglify-js@^2.6:
version "2.8.29"
Expand Down