Skip to content

Commit e997182

Browse files
authored
Use TypeScript and build JS on release (#3)
* Use TypeScript and build JS on release * Build plugin after running tests * Update serverless plugin declaration * Update serverless declaration * Update declarations * Rename plugin class
1 parent b104263 commit e997182

10 files changed

+420
-39
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
node_modules
21
coverage
2+
dist
3+
node_modules

circle.yml

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ test:
1111
- yarn lint
1212
override:
1313
- yarn test
14+
post:
15+
- yarn build
1416

1517
deployment:
1618
release:

package.json

+24-15
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
"name": "serverless-s3bucket-sync",
33
"description": "Serverless Plugin to sync local folders with an S3 bucket",
44
"version": "0.0.0",
5-
"main": "src/plugin.js",
5+
"main": "dist/plugin.js",
66
"scripts": {
7-
"test": "jest test",
8-
"test:cover": "jest test --coverage",
7+
"test": "jest",
8+
"test:cover": "jest --coverage",
99
"coveralls": "cat ./coverage/lcov.info | coveralls",
10-
"lint": "standard | snazzy"
10+
"lint": "tslint {src,test}/**/*.ts",
11+
"build": "tsc"
1112
},
1213
"keywords": [
1314
"serverless",
@@ -34,20 +35,28 @@
3435
"s3": "^4.4.0"
3536
},
3637
"devDependencies": {
38+
"@types/jest": "^20.0.5",
39+
"@types/node": "^8.0.17",
40+
"ts-jest": "^20.0.7",
41+
"tslint": "^5.5.0",
42+
"typescript": "^2.4.2",
3743
"coveralls": "^2.13.1",
3844
"dot-json": "^1.0.3",
39-
"jest": "^20.0.4",
40-
"snazzy": "^7.0.0",
41-
"standard": "^10.0.2"
45+
"jest": "^20.0.4"
4246
},
43-
"standard": {
44-
"envs": [
45-
"node",
46-
"jest"
47+
"jest": {
48+
"transform": {
49+
".*": "<rootDir>/node_modules/ts-jest/preprocessor.js"
50+
},
51+
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts)$",
52+
"moduleFileExtensions": [
53+
"ts",
54+
"js"
4755
],
48-
"ignore": [
49-
"node_modules/",
50-
"coverage"
51-
]
56+
"globals": {
57+
"__TS_CONFIG__": {
58+
"module": "commonjs"
59+
}
60+
}
5261
}
5362
}

src/main.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare interface BucketConfig {
2+
bucket: string
3+
folder: string
4+
}
+17-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const s3 = require('s3')
2-
const util = require('util')
1+
import * as s3 from 's3'
2+
import * as util from 'util'
33

4-
class Plugin {
5-
constructor (serverless) {
6-
this.serverless = serverless
4+
class S3BucketPlugin {
5+
private commands: {}
6+
private hooks: {}
77

8+
constructor (private serverless: Serverless) {
89
this.commands = {
910
sync: { lifecycleEvents: [ 'buckets' ] }
1011
}
@@ -15,31 +16,31 @@ class Plugin {
1516
}
1617
}
1718

18-
options () {
19+
private options () {
1920
return {
2021
maxAsyncS3: 20,
21-
s3RetryCount: 3,
22-
s3RetryDelay: 1000,
23-
multipartUploadThreshold: 20971520,
2422
multipartUploadSize: 15728640,
23+
multipartUploadThreshold: 20971520,
2524
s3Options: {
2625
region: this.serverless.getProvider('aws').getRegion()
27-
}
26+
},
27+
s3RetryCount: 3,
28+
s3RetryDelay: 1000
2829
}
2930
}
3031

31-
client () {
32+
private client () {
3233
return s3.createClient(this.options())
3334
}
3435

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

3940
const uploader = this.client().uploadDir(
4041
{
41-
localDir: this.serverless.config.servicePath + '/' + config.folder,
4242
deleteRemoved: true,
43+
localDir: this.serverless.config.servicePath + '/' + config.folder,
4344
s3Params: {
4445
Bucket: config.bucket
4546
}
@@ -51,11 +52,11 @@ class Plugin {
5152
})
5253
}
5354

54-
sync () {
55+
private sync () {
5556
return Promise.all(
5657
this.serverless.service.custom['s3-sync'].map(this.upload.bind(this))
5758
)
5859
}
5960
}
6061

61-
module.exports = Plugin
62+
module.exports = S3BucketPlugin

tsconfig.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilerOptions": {
3+
"experimentalDecorators": true,
4+
"forceConsistentCasingInFileNames": true,
5+
"module": "commonjs",
6+
"moduleResolution": "node",
7+
"noImplicitAny": true,
8+
"noImplicitReturns": true,
9+
"noImplicitThis": true,
10+
"noUnusedLocals": true,
11+
"outDir": "dist",
12+
"sourceMap": true,
13+
"strictNullChecks": true,
14+
"suppressImplicitAnyIndexErrors": true,
15+
"target": "esnext"
16+
},
17+
"include": [
18+
"src/**/*",
19+
"vendor/**/*"
20+
],
21+
"exclude": [
22+
"node_modules/**/*"
23+
]
24+
}

tslint.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"defaultSeverity": "error",
3+
"extends": [
4+
"tslint:recommended"
5+
],
6+
"jsRules": {},
7+
"rules": {
8+
"no-console": [ false, "error" ],
9+
"quotemark": [ true, "single", "avoid-escape" ],
10+
"semicolon": [ true, "never" ],
11+
"trailing-comma": [ true, { "multiline": "never", "singleline": "never" } ],
12+
"space-before-function-paren": [ true, { "anonymous": "always", "named": "never", "asyncArrow": "always" } ],
13+
"indent": [ true, "spaces", 2 ],
14+
"interface-name": [ true, "never-prefix" ]
15+
},
16+
"rulesDirectory": []
17+
}

vendor/s3.d.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
declare module 's3' {
2+
namespace s3 {
3+
interface ClientConfig {
4+
maxAsyncS3: number,
5+
multipartUploadSize: number,
6+
multipartUploadThreshold: number,
7+
s3Options: {
8+
region: string
9+
},
10+
s3RetryCount: number,
11+
s3RetryDelay: number
12+
}
13+
14+
interface UploadConfig {
15+
deleteRemoved: boolean,
16+
localDir: string,
17+
s3Params: {
18+
Bucket: string
19+
}
20+
}
21+
22+
interface Upload {
23+
on(event: string, handler: () => any): null
24+
}
25+
26+
interface Client {
27+
uploadDir(config: s3.UploadConfig): s3.Upload
28+
}
29+
}
30+
31+
export function createClient(config: s3.ClientConfig): s3.Client
32+
}

vendor/serverless.d.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
declare interface Serverless {
2+
cli: {
3+
log(message: string): null
4+
}
5+
6+
config: {
7+
servicePath: string
8+
}
9+
10+
service: {
11+
custom: {}
12+
}
13+
14+
getProvider(name: string): {
15+
getRegion: () => string
16+
}
17+
}

0 commit comments

Comments
 (0)