Skip to content

Commit

Permalink
Use TypeScript and compile to JS on release (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbstjn authored Aug 2, 2017
1 parent a2a472c commit 2eeb202
Show file tree
Hide file tree
Showing 22 changed files with 803 additions and 1,047 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
coverage
coverage
dist
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!dist
1 change: 1 addition & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ test:
- yarn lint
override:
- yarn test:cover
- yarn build
post:
- yarn coveralls

Expand Down
35 changes: 20 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"name": "serverless-dynamodb-autoscaling",
"description": "Serverless Plugin for Amazon DynamoDB Auto Scaling.",
"version": "0.0.0",
"main": "src/plugin.js",
"main": "dist/plugin.js",
"scripts": {
"fix": "standard --fix",
"test": "jest test",
"test:cover": "jest test --coverage",
"test": "jest",
"test:cover": "jest --coverage",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"lint": "standard | snazzy"
"lint": "tslint {src,test}/**/*.ts",
"build": "tsc"
},
"keywords": [
"serverless",
Expand All @@ -32,24 +32,29 @@
},
"homepage": "https://github.com/sbstjn/serverless-dynamodb-autoscaling",
"devDependencies": {
"@types/jest": "^20.0.5",
"@types/node": "^8.0.19",
"coveralls": "^2.13.1",
"dot-json": "^1.0.3",
"jest": "^20.0.4",
"snazzy": "^7.0.0",
"standard": "^10.0.2"
"ts-jest": "^20.0.7",
"tslint": "^5.5.0",
"@types/md5": "^2.1.32",
"@types/lodash": "^4.14.71",
"typescript": "^2.4.2"
},
"dependencies": {
"lodash": "^4.17.4",
"md5": "^2.2.1"
},
"standard": {
"envs": [
"node",
"jest"
],
"ignore": [
"node_modules/",
"coverage"
"jest": {
"transform": {
".*": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts)$",
"moduleFileExtensions": [
"ts",
"js"
]
}
}
38 changes: 14 additions & 24 deletions src/aws/names.js → src/aws/names.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const util = require('util')
const md5 = require('md5')
import * as md5 from 'md5'
import * as util from 'util'

function clean (input) {
export function clean(input: string): string {
return truncate(input.replace(/[^a-z0-9+]+/gi, ''))
}

function truncate (input) {
export function truncate(input: string): string {
return input.length <= 64 ? input : input.substr(0, 32) + md5(input)
}

function policyScale (service, table, read, index, stage) {
export function policyScale(service: string, table: string, read: boolean, index?: string, stage?: string): string {
return clean(
util.format(
'%sTable%sScalingPolicy-%s%s%s',
service || '',
service,
read ? 'Read' : 'Write',
table,
index || '',
Expand All @@ -22,31 +22,31 @@ function policyScale (service, table, read, index, stage) {
)
}

function policyRole (service, table, index, stage) {
export function policyRole(service: string, table: string, index?: string, stage?: string): string {
return clean(
util.format(
'%sDynamoDBAutoscalePolicy-%s%s%s',
service || '',
service,
table,
index || '',
stage || ''
)
)
}

function dimension (read, index) {
export function dimension(read: boolean, index: boolean): string {
return util.format(
'dynamodb:%s:%sCapacityUnits',
index ? 'index' : 'table',
read ? 'Read' : 'Write'
)
}

function target (service, table, read, index, stage) {
export function target(service: string, table: string, read: boolean, index?: string, stage?: string): string {
return clean(
util.format(
'%sAutoScalingTarget%s-%s%s%s',
service || '',
service,
read ? 'Read' : 'Write',
table,
index || '',
Expand All @@ -55,7 +55,7 @@ function target (service, table, read, index, stage) {
)
}

function metric (read) {
export function metric(read: boolean): string {
return clean(
util.format(
'DynamoDB%sCapacityUtilization',
Expand All @@ -64,24 +64,14 @@ function metric (read) {
)
}

function role (service, table, index, stage) {
export function role(service: string, table: string, index?: string, stage?: string): string {
return clean(
util.format(
'%sDynamoDBAutoscaleRole-%s%s%s',
service || '',
service,
table,
index || '',
stage || ''
)
)
}

module.exports = {
clean,
dimension,
metric,
policyRole,
policyScale,
role,
target
}
45 changes: 0 additions & 45 deletions src/aws/policy.js

This file was deleted.

51 changes: 51 additions & 0 deletions src/aws/policy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as names from './names'

export default class Policy {
private dependencies: string[] = []
private type: string = 'AWS::ApplicationAutoScaling::ScalingPolicy'

constructor (
private service: string,
private table: string,
private value: number,
private read: boolean,
private scaleIn: number,
private scaleOut: number,
private index: string,
private stage: string
) { }

public setDependencies(list: string[]): Policy {
this.dependencies = list

return this
}

public toJSON(): any {
const nameMetric = names.metric(this.read)
const nameScalePolicy = names.policyScale(this.service, this.table, this.read, this.index, this.stage)
const nameTarget = names.target(this.service, this.table, this.read, this.index, this.stage)

const dependencies = [this.table, nameTarget ].concat(this.dependencies)

return {
[nameScalePolicy]: {
DependsOn: dependencies,
Properties: {
PolicyName: nameScalePolicy,
PolicyType: 'TargetTrackingScaling',
ScalingTargetId: { Ref: nameTarget },
TargetTrackingScalingPolicyConfiguration: {
PredefinedMetricSpecification: {
PredefinedMetricType: nameMetric
},
ScaleInCooldown: this.scaleIn,
ScaleOutCooldown: this.scaleOut,
TargetValue: this.value
}
},
Type: this.type
}
}
}
}
72 changes: 0 additions & 72 deletions src/aws/role.js

This file was deleted.

Loading

0 comments on commit 2eeb202

Please sign in to comment.