Skip to content

Commit 9196c3f

Browse files
authored
Merge pull request #11 from bhoudu/develop
Upgrade deps
2 parents cb9bde3 + 0a3d5d6 commit 9196c3f

12 files changed

Lines changed: 2833 additions & 1427 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ test-folder
3838
!.vscode/tasks.json
3939
!.vscode/launch.json
4040
!.vscode/extensions.json
41+
42+
# AWS
43+
.aws

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# Zambda: zipping things for AWS Lambda
22
[![npm version](https://badge.fury.io/js/zambda.svg)](https://badge.fury.io/js/zambda) [![Build Status](https://github.com/bhoudu/zambda/actions/workflows/build.yml/badge.svg?branch=develop)](https://github.com/bhoudu/zambda/actions?query=branch%3Adevelop)
33

4-
Zambda is a basic CLI to package files in a zip based on JSON file.
4+
Zambda is a basic CLI to package files in a zip based on a JSON configuration file.
55

6-
Installation
6+
## Usage
77

88

9+
Install zambda with yarn or npm
10+
911
yarn install -g zambda
1012

1113

12-
You then write a JSON zambda configuration file `lambda_handler.zambda.json` for instance.
14+
Write a JSON zambda configuration file `lambda_handler.zambda.json` for instance.
1315

1416
{
1517
"workDir": "zambda-dist",
@@ -34,7 +36,7 @@ You then write a JSON zambda configuration file `lambda_handler.zambda.json` for
3436
]
3537
}
3638

37-
You run zambda with your conf file as parameter.
39+
Run zambda with your conf file as parameter.
3840

3941
zambda lambda_handler.zambda.json
4042

jest.config.js

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,24 @@
11
module.exports = {
2-
roots: [
3-
"<rootDir>/src"
4-
],
2+
roots: ['<rootDir>/src'],
53
transform: {
6-
"^.+\\.tsx?$": "ts-jest"
4+
'^.+\\.tsx?$': 'ts-jest',
75
},
8-
testRegex: "(//.*|(\\.|/)(test|spec|steps))\\.tsx?$",
9-
moduleFileExtensions: [
10-
"ts",
11-
"tsx",
12-
"js",
13-
"jsx",
14-
"json",
15-
"node"
16-
],
17-
modulePaths: [
18-
'<rootDir>/lib'
19-
],
6+
testRegex: '(//.*|(\\.|/)(test|spec|steps))\\.tsx?$',
7+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
8+
modulePaths: ['<rootDir>/lib'],
209
collectCoverage: true,
2110
coverageDirectory: 'reports',
22-
coverageReporters: [
23-
'lcov',
24-
],
11+
coverageReporters: ['lcov'],
2512
reporters: [
26-
"default",
27-
"./node_modules/jest-sonarcloud-reporter",
13+
'default',
14+
'./node_modules/jest-sonarcloud-reporter',
2815
[
29-
"./node_modules/jest-html-reporter",
16+
'./node_modules/jest-html-reporter',
3017
{
31-
"pageTitle": "Test Report",
32-
"includeFailureMsg": true,
33-
"includeConsoleLog": true
34-
}
35-
]
36-
]
18+
pageTitle: 'Test Report',
19+
includeFailureMsg: true,
20+
includeConsoleLog: true,
21+
},
22+
],
23+
],
3724
};

package.json

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,36 @@
4040
"LICENSE"
4141
],
4242
"dependencies": {
43-
"archiver": "^5.3.0"
43+
"@aws-sdk/client-s3": "^3.100.0",
44+
"@aws-sdk/lib-storage": "^3.100.0",
45+
"archiver": "^5.3.1"
4446
},
4547
"devDependencies": {
46-
"@types/node": "^12.12.36",
48+
"@semantic-release/git": "^10.0.1",
49+
"@semantic-release/github": "^8.0.4",
50+
"@semantic-release/npm": "^9.0.1",
4751
"@types/jest": "27.4.0",
52+
"@types/node": "^12.12.36",
4853
"@typescript-eslint/eslint-plugin": "^5.10.0",
4954
"@typescript-eslint/parser": "^5.10.0",
5055
"conventional-changelog-conventionalcommits": "^4.6.3",
5156
"cross-env-default": "^5.1.3-1",
52-
"eslint": "^8.7.0",
53-
"eslint-config-prettier": "^8.3.0",
57+
"eslint": "^8.17.0",
58+
"eslint-config-prettier": "^8.5.0",
59+
"eslint-plugin-import": "^2.26.0",
5460
"eslint-plugin-prettier": "^4.0.0",
55-
"eslint-plugin-import": "^2.25.4",
56-
"jest": "^27.4.7",
57-
"jest-html-reporter": "^3.4.2",
61+
"jest": "^28.1.0",
62+
"jest-html-reporter": "^3.5.0",
5863
"jest-sonarcloud-reporter": "^1.0.1",
5964
"mkdirp": "^1.0.4",
6065
"npm-run-all": "^4.1.5",
61-
"prettier": "^2.5.1",
66+
"prettier": "^2.6.2",
6267
"rimraf": "^3.0.2",
68+
"semantic-release": "^19.0.2",
6369
"semantic-release-gitmoji": "^1.4.2",
70+
"ts-jest": "^28.0.4",
6471
"typescript": "^4.5.4",
65-
"ts-jest": "^27.1.3",
66-
"semantic-release": "^19.0.2",
67-
"@semantic-release/npm": "^9.0.0",
68-
"@semantic-release/git": "^10.0.1",
69-
"@semantic-release/github": "^8.0.2"
72+
"yarn-audit-fix": "^9.3.1"
7073
},
7174
"jestSonar": {
7275
"withRelativePaths": true,

release.config.js

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
module.exports = {
22
branches: [
33
{
4-
name: 'develop',
5-
prerelease: true
6-
},
7-
{
8-
name: 'release',
9-
prerelease: 'rc'
10-
},
11-
{
12-
name: 'main'
4+
name: 'main',
135
},
146
],
157
plugins: [
@@ -18,18 +10,10 @@ module.exports = {
1810
{
1911
releaseRules: {
2012
major: {
21-
include: [
22-
':boom:',
23-
]
13+
include: [':boom:'],
2414
},
2515
minor: {
26-
include: [
27-
':sparkles:',
28-
':zap:',
29-
':fire:',
30-
':alembic:',
31-
':rocket:',
32-
],
16+
include: [':sparkles:', ':zap:', ':fire:', ':alembic:', ':rocket:'],
3317
},
3418
patch: {
3519
include: [
@@ -44,35 +28,30 @@ module.exports = {
4428
':abc:',
4529
':pencil:',
4630
':recycle:',
47-
":bento:",
48-
":arrow_up:",
31+
':bento:',
32+
':arrow_up:',
4933
':wrench:',
50-
':robot:'
51-
]
52-
}
53-
}
54-
}
34+
':robot:',
35+
],
36+
},
37+
},
38+
},
5539
],
5640
[
57-
"@semantic-release/github",
41+
'@semantic-release/github',
5842
{
59-
"assets": [
60-
"lib/**",
61-
"package.json",
62-
"README.md"
63-
]
64-
}
43+
assets: ['lib/**', 'package.json', 'README.md'],
44+
},
6545
],
6646
[
6747
'@semantic-release/git',
6848
{
69-
assets: [
70-
'README.md',
71-
'package.json'
72-
],
73-
message: ':tada: Release <%= nextRelease.version %> - <%= new Date().toLocaleDateString(\'en-US\', {year: \'numeric\', month: \'short\', day: \'numeric\', hour: \'numeric\', minute: \'numeric\' }) %> [skip ci]\\n\\n<%= nextRelease.notes %>'
74-
}
49+
assets: ['README.md', 'package.json'],
50+
message:
51+
// eslint-disable-next-line max-len
52+
":tada: Release <%= nextRelease.version %> - <%= new Date().toLocaleDateString('en-US', {year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric' }) %> [skip ci]\\n\\n<%= nextRelease.notes %>",
53+
},
7554
],
7655
'@semantic-release/npm',
77-
]
78-
}
56+
],
57+
};

src/config.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import fs from 'fs';
2+
3+
export interface ZambdaFile {
4+
source: string;
5+
destination?: string;
6+
}
7+
8+
export type ZambdaFolder = string | ZambdaFile;
9+
10+
export interface ZambdaZip {
11+
name: string;
12+
folders: ZambdaFolder[];
13+
files: ZambdaFile[];
14+
}
15+
16+
export interface S3Destination {
17+
path: string;
18+
prefix: string;
19+
}
20+
21+
export interface ZambdaS3 {
22+
bucketName: string;
23+
destination?: string;
24+
}
25+
26+
export interface ZambdaConfig {
27+
workDir: string;
28+
zip: ZambdaZip;
29+
s3?: ZambdaS3;
30+
}
31+
32+
/**
33+
* Reads zambda config file from file system and return parsed config object
34+
*
35+
* @param configFilePath to read conf from on file system
36+
*/
37+
export function parseZambdaConfig(configFilePath: string): ZambdaConfig {
38+
const configJson: string = fs.readFileSync(configFilePath, {
39+
encoding: 'UTF-8',
40+
});
41+
if (!configJson) {
42+
throw new Error('JSON file: ' + configFilePath + ' cannot be read!');
43+
}
44+
// Parse configuration
45+
return JSON.parse(configJson) as ZambdaConfig;
46+
}
47+
48+
/**
49+
* Returns workPath ending with trailing '/'
50+
*
51+
* @param config for zambda
52+
*/
53+
export function getWorkPath(config: ZambdaConfig): string {
54+
return config.workDir.endsWith('/') ? config.workDir : config.workDir + '/';
55+
}

src/s3.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Upload } from '@aws-sdk/lib-storage';
2+
import { S3, Tag } from '@aws-sdk/client-s3';
3+
import { PutObjectCommandInput } from '@aws-sdk/client-s3/dist-types/commands/PutObjectCommand';
4+
import { PutObjectRequest } from '@aws-sdk/client-s3/dist-types/models/models_0';
5+
6+
/**
7+
* Push zambda generated zip to S3 bucket
8+
*
9+
* @param s3 client to use
10+
* @param bucket to push zip in
11+
* @param key in S3 bucket
12+
* @param body zip file
13+
* @param tags to put along file in S3
14+
* @param queueSize to use with multiple part upload client
15+
* @param partSize to use with multiple part upload client
16+
*/
17+
export async function pushS3(
18+
s3: S3,
19+
bucket: string,
20+
key: string,
21+
body: PutObjectRequest['Body'] | string | Uint8Array | Buffer,
22+
tags: Tag[] = [],
23+
queueSize = 2,
24+
partSize = 1,
25+
): Promise<boolean> {
26+
try {
27+
const params: PutObjectCommandInput = {
28+
Bucket: bucket,
29+
Key: key,
30+
Body: body,
31+
};
32+
const parallelUploads3 = new Upload({
33+
client: s3,
34+
tags: [...tags], // optional tags
35+
queueSize, // optional concurrency configuration
36+
partSize, // optional size of each part in MB
37+
leavePartsOnError: false, // optional manually handle dropped parts
38+
params,
39+
});
40+
parallelUploads3.on('httpUploadProgress', (progress) => {
41+
console.log(progress);
42+
});
43+
return parallelUploads3.done().then(() => true);
44+
} catch (e) {
45+
console.log(e);
46+
return false;
47+
}
48+
}

src/zambda.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env node
2-
3-
import { zip } from "./zip";
2+
import fs from 'fs';
3+
import { getWorkPath, parseZambdaConfig } from './config';
4+
import { zipWithConf } from './zip';
5+
import { pushS3 } from './s3';
6+
import { S3 } from '@aws-sdk/client-s3';
47

58
const isOK = process.argv.length > 2;
69
if (!isOK) {
@@ -9,13 +12,23 @@ if (!isOK) {
912
}
1013

1114
// Zip!
12-
const confFile = process.argv[2];
13-
zip(confFile)
14-
.then(function handler(): void {
15-
console.log('Zambda archive for conf: ' + confFile + ' has been generated!');
15+
const confFilePath = process.argv[2];
16+
const S3Suffix = process.argv.length > 3 ? '-' + process.argv[3] : '';
17+
const zambdaConfig = parseZambdaConfig(confFilePath);
18+
zipWithConf(zambdaConfig)
19+
.then(async function handler(): Promise<void> {
20+
console.log('Zambda archive for conf: ' + confFilePath + ' has been generated!');
21+
// EXPERIMENTAL, NOT READY YET
22+
if (zambdaConfig.s3) {
23+
const s3 = new S3({});
24+
const destinationPath = getWorkPath(zambdaConfig) + zambdaConfig.zip.name;
25+
const readStream = fs.createReadStream(destinationPath, 'utf8');
26+
const s3key = zambdaConfig.s3.destination + S3Suffix;
27+
await pushS3(s3, zambdaConfig.s3.bucketName, s3key, readStream);
28+
}
1629
process.exit(0);
1730
})
18-
.catch(e => {
31+
.catch((e) => {
1932
console.error(e + '');
2033
process.exit(1);
2134
});

0 commit comments

Comments
 (0)