Skip to content

Commit

Permalink
Use TypeScript and build JS on release (#3)
Browse files Browse the repository at this point in the history
* Use TypeScript and build JS on release

* Build plugin after running tests

* Update serverless plugin declaration

* Update serverless declaration

* Update declarations

* Rename plugin class
  • Loading branch information
sbstjn committed Jul 31, 2017
1 parent b104263 commit e997182
Show file tree
Hide file tree
Showing 10 changed files with 420 additions and 39 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
dist
node_modules
2 changes: 2 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ test:
- yarn lint
override:
- yarn test
post:
- yarn build

deployment:
release:
Expand Down
39 changes: 24 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"name": "serverless-s3bucket-sync",
"description": "Serverless Plugin to sync local folders with an S3 bucket",
"version": "0.0.0",
"main": "src/plugin.js",
"main": "dist/plugin.js",
"scripts": {
"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 @@ -34,20 +35,28 @@
"s3": "^4.4.0"
},
"devDependencies": {
"@types/jest": "^20.0.5",
"@types/node": "^8.0.17",
"ts-jest": "^20.0.7",
"tslint": "^5.5.0",
"typescript": "^2.4.2",
"coveralls": "^2.13.1",
"dot-json": "^1.0.3",
"jest": "^20.0.4",
"snazzy": "^7.0.0",
"standard": "^10.0.2"
"jest": "^20.0.4"
},
"standard": {
"envs": [
"node",
"jest"
"jest": {
"transform": {
".*": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts)$",
"moduleFileExtensions": [
"ts",
"js"
],
"ignore": [
"node_modules/",
"coverage"
]
"globals": {
"__TS_CONFIG__": {
"module": "commonjs"
}
}
}
}
4 changes: 4 additions & 0 deletions src/main.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare interface BucketConfig {
bucket: string
folder: string
}
33 changes: 17 additions & 16 deletions src/plugin.js → src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const s3 = require('s3')
const util = require('util')
import * as s3 from 's3'
import * as util from 'util'

class Plugin {
constructor (serverless) {
this.serverless = serverless
class S3BucketPlugin {
private commands: {}
private hooks: {}

constructor (private serverless: Serverless) {
this.commands = {
sync: { lifecycleEvents: [ 'buckets' ] }
}
Expand All @@ -15,31 +16,31 @@ class Plugin {
}
}

options () {
private options () {
return {
maxAsyncS3: 20,
s3RetryCount: 3,
s3RetryDelay: 1000,
multipartUploadThreshold: 20971520,
multipartUploadSize: 15728640,
multipartUploadThreshold: 20971520,
s3Options: {
region: this.serverless.getProvider('aws').getRegion()
}
},
s3RetryCount: 3,
s3RetryDelay: 1000
}
}

client () {
private client () {
return s3.createClient(this.options())
}

upload (config) {
return new Promise(resolve => {
private upload (config: BucketConfig) {
return new Promise((resolve) => {
this.serverless.cli.log(util.format('Syncing folder "%s" to S3 bucket "%s"', config.folder, config.bucket))

const uploader = this.client().uploadDir(
{
localDir: this.serverless.config.servicePath + '/' + config.folder,
deleteRemoved: true,
localDir: this.serverless.config.servicePath + '/' + config.folder,
s3Params: {
Bucket: config.bucket
}
Expand All @@ -51,11 +52,11 @@ class Plugin {
})
}

sync () {
private sync () {
return Promise.all(
this.serverless.service.custom['s3-sync'].map(this.upload.bind(this))
)
}
}

module.exports = Plugin
module.exports = S3BucketPlugin
24 changes: 24 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"outDir": "dist",
"sourceMap": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"target": "esnext"
},
"include": [
"src/**/*",
"vendor/**/*"
],
"exclude": [
"node_modules/**/*"
]
}
17 changes: 17 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"jsRules": {},
"rules": {
"no-console": [ false, "error" ],
"quotemark": [ true, "single", "avoid-escape" ],
"semicolon": [ true, "never" ],
"trailing-comma": [ true, { "multiline": "never", "singleline": "never" } ],
"space-before-function-paren": [ true, { "anonymous": "always", "named": "never", "asyncArrow": "always" } ],
"indent": [ true, "spaces", 2 ],
"interface-name": [ true, "never-prefix" ]
},
"rulesDirectory": []
}
32 changes: 32 additions & 0 deletions vendor/s3.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
declare module 's3' {
namespace s3 {
interface ClientConfig {
maxAsyncS3: number,
multipartUploadSize: number,
multipartUploadThreshold: number,
s3Options: {
region: string
},
s3RetryCount: number,
s3RetryDelay: number
}

interface UploadConfig {
deleteRemoved: boolean,
localDir: string,
s3Params: {
Bucket: string
}
}

interface Upload {
on(event: string, handler: () => any): null
}

interface Client {
uploadDir(config: s3.UploadConfig): s3.Upload
}
}

export function createClient(config: s3.ClientConfig): s3.Client
}
17 changes: 17 additions & 0 deletions vendor/serverless.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
declare interface Serverless {
cli: {
log(message: string): null
}

config: {
servicePath: string
}

service: {
custom: {}
}

getProvider(name: string): {
getRegion: () => string
}
}
Loading

0 comments on commit e997182

Please sign in to comment.