Skip to content

Commit

Permalink
Merge pull request #11 from bhoudu/develop
Browse files Browse the repository at this point in the history
Upgrade deps
  • Loading branch information
bhoudu authored Jun 5, 2022
2 parents cb9bde3 + 0a3d5d6 commit 9196c3f
Show file tree
Hide file tree
Showing 12 changed files with 2,833 additions and 1,427 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ test-folder
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# AWS
.aws
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# Zambda: zipping things for AWS Lambda
[![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)

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

Installation
## Usage


Install zambda with yarn or npm

yarn install -g zambda


You then write a JSON zambda configuration file `lambda_handler.zambda.json` for instance.
Write a JSON zambda configuration file `lambda_handler.zambda.json` for instance.

{
"workDir": "zambda-dist",
Expand All @@ -34,7 +36,7 @@ You then write a JSON zambda configuration file `lambda_handler.zambda.json` for
]
}

You run zambda with your conf file as parameter.
Run zambda with your conf file as parameter.

zambda lambda_handler.zambda.json

Expand Down
43 changes: 15 additions & 28 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
module.exports = {
roots: [
"<rootDir>/src"
],
roots: ['<rootDir>/src'],
transform: {
"^.+\\.tsx?$": "ts-jest"
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: "(//.*|(\\.|/)(test|spec|steps))\\.tsx?$",
moduleFileExtensions: [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
modulePaths: [
'<rootDir>/lib'
],
testRegex: '(//.*|(\\.|/)(test|spec|steps))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
modulePaths: ['<rootDir>/lib'],
collectCoverage: true,
coverageDirectory: 'reports',
coverageReporters: [
'lcov',
],
coverageReporters: ['lcov'],
reporters: [
"default",
"./node_modules/jest-sonarcloud-reporter",
'default',
'./node_modules/jest-sonarcloud-reporter',
[
"./node_modules/jest-html-reporter",
'./node_modules/jest-html-reporter',
{
"pageTitle": "Test Report",
"includeFailureMsg": true,
"includeConsoleLog": true
}
]
]
pageTitle: 'Test Report',
includeFailureMsg: true,
includeConsoleLog: true,
},
],
],
};
29 changes: 16 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,36 @@
"LICENSE"
],
"dependencies": {
"archiver": "^5.3.0"
"@aws-sdk/client-s3": "^3.100.0",
"@aws-sdk/lib-storage": "^3.100.0",
"archiver": "^5.3.1"
},
"devDependencies": {
"@types/node": "^12.12.36",
"@semantic-release/git": "^10.0.1",
"@semantic-release/github": "^8.0.4",
"@semantic-release/npm": "^9.0.1",
"@types/jest": "27.4.0",
"@types/node": "^12.12.36",
"@typescript-eslint/eslint-plugin": "^5.10.0",
"@typescript-eslint/parser": "^5.10.0",
"conventional-changelog-conventionalcommits": "^4.6.3",
"cross-env-default": "^5.1.3-1",
"eslint": "^8.7.0",
"eslint-config-prettier": "^8.3.0",
"eslint": "^8.17.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-import": "^2.25.4",
"jest": "^27.4.7",
"jest-html-reporter": "^3.4.2",
"jest": "^28.1.0",
"jest-html-reporter": "^3.5.0",
"jest-sonarcloud-reporter": "^1.0.1",
"mkdirp": "^1.0.4",
"npm-run-all": "^4.1.5",
"prettier": "^2.5.1",
"prettier": "^2.6.2",
"rimraf": "^3.0.2",
"semantic-release": "^19.0.2",
"semantic-release-gitmoji": "^1.4.2",
"ts-jest": "^28.0.4",
"typescript": "^4.5.4",
"ts-jest": "^27.1.3",
"semantic-release": "^19.0.2",
"@semantic-release/npm": "^9.0.0",
"@semantic-release/git": "^10.0.1",
"@semantic-release/github": "^8.0.2"
"yarn-audit-fix": "^9.3.1"
},
"jestSonar": {
"withRelativePaths": true,
Expand Down
61 changes: 20 additions & 41 deletions release.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
module.exports = {
branches: [
{
name: 'develop',
prerelease: true
},
{
name: 'release',
prerelease: 'rc'
},
{
name: 'main'
name: 'main',
},
],
plugins: [
Expand All @@ -18,18 +10,10 @@ module.exports = {
{
releaseRules: {
major: {
include: [
':boom:',
]
include: [':boom:'],
},
minor: {
include: [
':sparkles:',
':zap:',
':fire:',
':alembic:',
':rocket:',
],
include: [':sparkles:', ':zap:', ':fire:', ':alembic:', ':rocket:'],
},
patch: {
include: [
Expand All @@ -44,35 +28,30 @@ module.exports = {
':abc:',
':pencil:',
':recycle:',
":bento:",
":arrow_up:",
':bento:',
':arrow_up:',
':wrench:',
':robot:'
]
}
}
}
':robot:',
],
},
},
},
],
[
"@semantic-release/github",
'@semantic-release/github',
{
"assets": [
"lib/**",
"package.json",
"README.md"
]
}
assets: ['lib/**', 'package.json', 'README.md'],
},
],
[
'@semantic-release/git',
{
assets: [
'README.md',
'package.json'
],
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 %>'
}
assets: ['README.md', 'package.json'],
message:
// eslint-disable-next-line max-len
":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 %>",
},
],
'@semantic-release/npm',
]
}
],
};
55 changes: 55 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import fs from 'fs';

export interface ZambdaFile {
source: string;
destination?: string;
}

export type ZambdaFolder = string | ZambdaFile;

export interface ZambdaZip {
name: string;
folders: ZambdaFolder[];
files: ZambdaFile[];
}

export interface S3Destination {
path: string;
prefix: string;
}

export interface ZambdaS3 {
bucketName: string;
destination?: string;
}

export interface ZambdaConfig {
workDir: string;
zip: ZambdaZip;
s3?: ZambdaS3;
}

/**
* Reads zambda config file from file system and return parsed config object
*
* @param configFilePath to read conf from on file system
*/
export function parseZambdaConfig(configFilePath: string): ZambdaConfig {
const configJson: string = fs.readFileSync(configFilePath, {
encoding: 'UTF-8',
});
if (!configJson) {
throw new Error('JSON file: ' + configFilePath + ' cannot be read!');
}
// Parse configuration
return JSON.parse(configJson) as ZambdaConfig;
}

/**
* Returns workPath ending with trailing '/'
*
* @param config for zambda
*/
export function getWorkPath(config: ZambdaConfig): string {
return config.workDir.endsWith('/') ? config.workDir : config.workDir + '/';
}
48 changes: 48 additions & 0 deletions src/s3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Upload } from '@aws-sdk/lib-storage';
import { S3, Tag } from '@aws-sdk/client-s3';
import { PutObjectCommandInput } from '@aws-sdk/client-s3/dist-types/commands/PutObjectCommand';
import { PutObjectRequest } from '@aws-sdk/client-s3/dist-types/models/models_0';

/**
* Push zambda generated zip to S3 bucket
*
* @param s3 client to use
* @param bucket to push zip in
* @param key in S3 bucket
* @param body zip file
* @param tags to put along file in S3
* @param queueSize to use with multiple part upload client
* @param partSize to use with multiple part upload client
*/
export async function pushS3(
s3: S3,
bucket: string,
key: string,
body: PutObjectRequest['Body'] | string | Uint8Array | Buffer,
tags: Tag[] = [],
queueSize = 2,
partSize = 1,
): Promise<boolean> {
try {
const params: PutObjectCommandInput = {
Bucket: bucket,
Key: key,
Body: body,
};
const parallelUploads3 = new Upload({
client: s3,
tags: [...tags], // optional tags
queueSize, // optional concurrency configuration
partSize, // optional size of each part in MB
leavePartsOnError: false, // optional manually handle dropped parts
params,
});
parallelUploads3.on('httpUploadProgress', (progress) => {
console.log(progress);
});
return parallelUploads3.done().then(() => true);
} catch (e) {
console.log(e);
return false;
}
}
27 changes: 20 additions & 7 deletions src/zambda.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env node

import { zip } from "./zip";
import fs from 'fs';
import { getWorkPath, parseZambdaConfig } from './config';
import { zipWithConf } from './zip';
import { pushS3 } from './s3';
import { S3 } from '@aws-sdk/client-s3';

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

// Zip!
const confFile = process.argv[2];
zip(confFile)
.then(function handler(): void {
console.log('Zambda archive for conf: ' + confFile + ' has been generated!');
const confFilePath = process.argv[2];
const S3Suffix = process.argv.length > 3 ? '-' + process.argv[3] : '';
const zambdaConfig = parseZambdaConfig(confFilePath);
zipWithConf(zambdaConfig)
.then(async function handler(): Promise<void> {
console.log('Zambda archive for conf: ' + confFilePath + ' has been generated!');
// EXPERIMENTAL, NOT READY YET
if (zambdaConfig.s3) {
const s3 = new S3({});
const destinationPath = getWorkPath(zambdaConfig) + zambdaConfig.zip.name;
const readStream = fs.createReadStream(destinationPath, 'utf8');
const s3key = zambdaConfig.s3.destination + S3Suffix;
await pushS3(s3, zambdaConfig.s3.bucketName, s3key, readStream);
}
process.exit(0);
})
.catch(e => {
.catch((e) => {
console.error(e + '');
process.exit(1);
});
Loading

0 comments on commit 9196c3f

Please sign in to comment.