Skip to content

Commit 6edb59d

Browse files
authored
Use TypeScript and compile to JS on release (#4)
* Use TypeScript and compile to JS on release * clean up * Update package.json * Make list of hooks public
1 parent a89115a commit 6edb59d

File tree

13 files changed

+595
-1689
lines changed

13 files changed

+595
-1689
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
coverage
2-
node_modules
2+
node_modules
3+
dist

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!dist

circle.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ test:
1010
pre:
1111
- yarn lint
1212
override:
13+
- yarn build
1314
- yarn test:cover
1415
post:
1516
- yarn coveralls
17+
cache_directories:
18+
- ~/.cache/yarn
1619

1720
deployment:
1821
release:

package.json

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
"description": "Serverless plugin to process AWS CloudFormation Stack Output",
44
"license": "MIT",
55
"author": "Sebastian Müller <[email protected]>",
6-
"main": "src/plugin.js",
6+
"main": "dist/plugin.js",
77
"scripts": {
8-
"test": "yarn lint && jest test",
9-
"test:cover": "yarn lint && jest test --coverage",
8+
"test": "jest",
9+
"test:cover": "jest --coverage",
1010
"coveralls": "cat ./coverage/lcov.info | coveralls",
11-
"lint": "standard | snazzy"
11+
"lint": "tslint {src,test}/**/*.ts",
12+
"build": "tsc"
1213
},
1314
"repository": {
1415
"type": "git",
@@ -27,27 +28,29 @@
2728
"output"
2829
],
2930
"dependencies": {
30-
"jasmine-data-provider": "^2.2.0",
31-
"nativefier": "^7.4.0",
32-
"sinon": "^2.3.6",
3331
"tomlify-j0.4": "^2.0.0",
3432
"yamljs": "^0.3.0"
3533
},
3634
"devDependencies": {
35+
"sinon": "^2.3.6",
36+
"jasmine-data-provider": "^2.2.0",
37+
"@types/jest": "^20.0.5",
38+
"@types/node": "^8.0.19",
3739
"coveralls": "^2.13.1",
3840
"dot-json": "^1.0.3",
3941
"jest": "^20.0.4",
40-
"snazzy": "^7.0.0",
41-
"standard": "^10.0.2"
42+
"ts-jest": "^20.0.7",
43+
"tslint": "^5.5.0",
44+
"typescript": "^2.4.2"
4245
},
43-
"standard": {
44-
"envs": [
45-
"node",
46-
"jest"
47-
],
48-
"ignore": [
49-
"node_modules/",
50-
"coverage"
46+
"jest": {
47+
"transform": {
48+
".*": "<rootDir>/node_modules/ts-jest/preprocessor.js"
49+
},
50+
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts)$",
51+
"moduleFileExtensions": [
52+
"ts",
53+
"js"
5154
]
5255
}
5356
}
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
const fs = require('fs')
1+
import * as fs from 'fs'
22

3-
class File {
4-
constructor (path) {
5-
this.path = path
6-
}
3+
export default class StackOutputFile {
4+
constructor (private path: string) { }
75

8-
format (data) {
9-
const ext = this.path.split('.').pop()
6+
public format (data: {}) {
7+
const ext = this.path.split('.').pop() || ''
108

119
switch (ext.toUpperCase()) {
1210
case 'JSON':
@@ -21,7 +19,7 @@ class File {
2119
}
2220
}
2321

24-
save (data) {
22+
public save (data: {}) {
2523
const content = this.format(data)
2624

2725
try {
@@ -31,5 +29,3 @@ class File {
3129
}
3230
}
3331
}
34-
35-
module.exports = File
Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
'use strict'
1+
import * as assert from 'assert'
2+
import * as util from 'util'
23

3-
const util = require('util')
4-
const assert = require('assert')
4+
import StackOutputFile from './file'
55

6-
const File = require('./file.js')
7-
8-
class Plugin {
9-
constructor (serverless, options) {
10-
this.options = options
11-
this.serverless = serverless
6+
class StackOutputPlugin {
7+
public hooks: {}
8+
private output: OutputConfig
129

10+
constructor (private serverless: Serverless, private options: Serverless.Options) {
1311
this.hooks = {
14-
'after:deploy:deploy': () => this.process()
12+
'after:deploy:deploy': this.process.bind(this)
1513
}
14+
15+
this.output = this.serverless.service.custom.output
1616
}
1717

1818
get file () {
@@ -24,47 +24,47 @@ class Plugin {
2424
}
2525

2626
get stackName () {
27-
return this.serverless.service.service + '-' + this.serverless.getProvider('aws').getStage()
27+
return this.serverless.service.getServiceName() + '-' + this.serverless.getProvider('aws').getStage()
2828
}
2929

30-
hasConfig (key) {
31-
return !!this.serverless.service.custom.output && !!this.serverless.service.custom.output[key]
30+
private hasConfig (key: string) {
31+
return !!this.output && !!this.output[key]
3232
}
3333

34-
hasHandler () {
34+
private hasHandler () {
3535
return this.hasConfig('handler')
3636
}
3737

38-
hasFile () {
38+
private hasFile () {
3939
return this.hasConfig('file')
4040
}
4141

42-
getConfig (key) {
43-
return this.serverless.config.servicePath + '/' + this.serverless.service.custom.output[key]
42+
private getConfig (key: string) {
43+
return this.serverless.config.servicePath + '/' + this.output[key]
4444
}
4545

46-
callHandler (data) {
46+
private callHandler (data: {}) {
4747
const splits = this.handler.split('.')
48-
const func = splits.pop()
48+
const func = splits.pop() || ''
4949

50-
return new Promise(resolve => {
50+
return new Promise((resolve) => {
5151
require(splits.join('.'))[func](data)
5252

5353
resolve()
5454
})
5555
}
5656

57-
saveFile (data) {
58-
const f = new File(this.file)
57+
private saveFile (data: {}) {
58+
const f = new StackOutputFile(this.file)
5959

60-
return new Promise(resolve => {
60+
return new Promise((resolve) => {
6161
f.save(data)
6262

6363
resolve()
6464
})
6565
}
6666

67-
fetch () {
67+
private fetch (): Promise<StackDescriptionList> {
6868
return this.serverless.getProvider('aws').request(
6969
'CloudFormation',
7070
'describeStacks',
@@ -76,21 +76,24 @@ class Plugin {
7676
)
7777
}
7878

79-
beautify (data) {
80-
return data.Stacks.pop().Outputs.reduce(
81-
(obj, item) => Object.assign(obj, {[item.OutputKey]: item.OutputValue}),
79+
private beautify (data: {Stacks: Array<{ Outputs: Array<{}> }>}) {
80+
const stack = data.Stacks.pop() || { Outputs: [] }
81+
const output = stack.Outputs || []
82+
83+
return output.reduce(
84+
(obj: {}, item: StackOutputPair) => Object.assign(obj, {[item.OutputKey]: item.OutputValue}),
8285
{}
8386
)
8487
}
8588

86-
handle (data) {
87-
let promises = []
89+
private handle (data: {}) {
90+
const promises = []
8891

8992
if (this.hasHandler()) {
9093
promises.push(
9194
this.callHandler(data).then(
9295
() => this.serverless.cli.log(
93-
util.format('Stack Output processed with handler: %s', this.serverless.service.custom.output.handler)
96+
util.format('Stack Output processed with handler: %s', this.output.handler)
9497
)
9598
)
9699
)
@@ -100,7 +103,7 @@ class Plugin {
100103
promises.push(
101104
this.saveFile(data).then(
102105
() => this.serverless.cli.log(
103-
util.format('Stack Output saved to file: %s', this.serverless.service.custom.output.file)
106+
util.format('Stack Output saved to file: %s', this.output.file)
104107
)
105108
)
106109
)
@@ -109,7 +112,7 @@ class Plugin {
109112
return Promise.all(promises)
110113
}
111114

112-
validate () {
115+
private validate () {
113116
assert(this.serverless, 'Invalid serverless configuration')
114117
assert(this.serverless.service, 'Invalid serverless configuration')
115118
assert(this.serverless.service.provider, 'Invalid serverless configuration')
@@ -119,19 +122,21 @@ class Plugin {
119122
assert(this.options && !this.options.noDeploy, 'Skipping deployment with --noDeploy flag')
120123
}
121124

122-
process () {
125+
private process () {
126+
console.log('running stack-output-plugin')
127+
123128
return Promise.resolve().then(
124129
() => this.validate()
125130
).then(
126131
() => this.fetch()
127132
).then(
128-
res => this.beautify(res)
133+
(res: StackDescriptionList) => this.beautify(res)
129134
).then(
130-
res => this.handle(res)
135+
(res) => this.handle(res)
131136
).catch(
132-
err => this.serverless.cli.log(util.format('Cannot process Stack Output: %s!', err.message))
137+
(err) => this.serverless.cli.log(util.format('Cannot process Stack Output: %s!', err.message))
133138
)
134139
}
135140
}
136141

137-
module.exports = Plugin
142+
module.exports = StackOutputPlugin
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

3-
const using = require('jasmine-data-provider')
4-
const File = require('../src/file.js')
3+
import * as using from 'jasmine-data-provider'
4+
import File from '../src/file'
55

66
describe('File', () => {
77
describe('Constructor', () => {
@@ -20,7 +20,7 @@ describe('File', () => {
2020
{file: 'test.toml', valid: true, type: 'toml', data: 'foo = "bar"'},
2121
{file: 'test.zip', valid: false}
2222
],
23-
data => {
23+
(data) => {
2424
it('detects' + (data.valid ? ' valid ' : ' invalid ') + data.file, () => {
2525
const f = new File(data.file)
2626

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict'
22

3-
const sinon = require('sinon')
4-
const Plugin = require('../')
3+
import * as sinon from 'sinon'
4+
import * as Plugin from '../src/plugin'
55

66
describe('Plugin', () => {
77
let providerMock = null
88
let getProvider = null
9-
let provider = {
9+
const provider = {
1010
request: () => true,
1111
sdk: {
1212
VERSION: '2.21.0'
@@ -25,22 +25,22 @@ describe('Plugin', () => {
2525
describe('Configuration', () => {
2626
it('hasHandler', () => {
2727
const config = {
28+
cli: { log: () => null },
2829
config: {
2930
servicePath: ''
3031
},
31-
cli: { log: () => {} },
32+
getProvider,
3233
region: 'us-east-1',
3334
service: {
34-
provider: {
35-
name: 'aws'
36-
},
3735
custom: {
3836
output: {
3937
handler: 'foo/bar.baz'
4038
}
39+
},
40+
provider: {
41+
name: 'aws'
4142
}
42-
},
43-
getProvider
43+
}
4444
}
4545

4646
const test = new Plugin(config)
@@ -55,22 +55,22 @@ describe('Plugin', () => {
5555
describe('Configuration', () => {
5656
it('hasFile', () => {
5757
const config = {
58+
cli: { log: () => null },
5859
config: {
5960
servicePath: ''
6061
},
61-
cli: { log: () => {} },
62+
getProvider,
6263
region: 'us-east-1',
6364
service: {
64-
provider: {
65-
name: 'aws'
66-
},
6765
custom: {
6866
output: {
6967
file: 'foo/bar.toml'
7068
}
69+
},
70+
provider: {
71+
name: 'aws'
7172
}
72-
},
73-
getProvider
73+
}
7474
}
7575

7676
const test = new Plugin(config)

0 commit comments

Comments
 (0)