Skip to content

Commit

Permalink
added basic plugin hook to trigger on package
Browse files Browse the repository at this point in the history
  • Loading branch information
levi-20 committed Jan 19, 2025
1 parent bfb2186 commit 7bbd614
Show file tree
Hide file tree
Showing 9 changed files with 8,270 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules
src/**.js
dist
8,163 changes: 8,162 additions & 1 deletion package-lock.json

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
{
"name": "lambda-cron",
"version": "0.0.3",
"description": "A simple plugin to schedule cronjobs for lambdas",
"main": "index.js",
"description": "Super light weight Serverless Plugin to simplify adding cron jobs to functions",
"main": "index.ts",
"type": "module",
"module": "commonjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "levi",
"repository": {
"url": "https://github.com/levi-20/lambda-cron"
},
"license": "MIT"
"license": "MIT",
"devDependencies": {
"serverless": "^3.0.0",
"@types/aws-lambda": "^8.10.145",
"@types/serverless": "^3.12.23",
"@types/node": "22.10.7"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Cronify
A simple serverless plugin to schedule lambdas
A simple serverless plugin to schedule lambdas for cron

[![Node.js Package](https://github.com/levi-20/cronify/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/levi-20/cronify/actions/workflows/npm-publish.yml)

Expand Down
9 changes: 9 additions & 0 deletions src/cron-rates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @format */

export const dailyAt = (dailyTime: number) => {
return `cron(0 ${dailyTime} * * ? *)`;
};

// export const every = (time: number, rate: any) {

// }
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @format */

import LambdaCronJobs from './lambda-cron';

module.exports = LambdaCronJobs;
27 changes: 27 additions & 0 deletions src/lambda-cron.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/** @format */

import Serverless from 'serverless';
import { Functions, Hooks, PluginConfig } from './types';

export const CONFIG_ROOT_KEY = 'lambdaCronJobs';
export const BEFORE_PACKAGE_HOOK = 'before:package:initialize';

export default class LambdaCronJobs {
functions: Functions;
provider: any;
log: any;
pluginConfig: PluginConfig;
hooks: Hooks;

constructor(serverless: Serverless, options: Serverless.Options) {
// this.log = log;
this.functions = serverless.service?.functions;
this.provider = serverless.getProvider('aws');
this.pluginConfig = serverless.service?.custom[CONFIG_ROOT_KEY];
this.hooks = { [BEFORE_PACKAGE_HOOK]: () => this.beforePackage() };
}

private beforePackage() {
this.log.info('plugin hook called');
}
}
39 changes: 39 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/** @format */

import Serverless from 'serverless';

export interface Functions {
[key: string]:
| Serverless.FunctionDefinitionHandler
| Serverless.FunctionDefinitionImage;
}
export interface PluginConfigItem {
rate: Rate;
input?: Record<string, unknown>;
}

export interface PluginConfig {
[key: string]: PluginConfigItem[];
}

export interface FunctionCronJobConfig {
functionName: string;
cronJobConfig: PluginConfigItem[];
}

export type SlsLog = Serverless['cli']['log'];

export interface Rate {
time: number;
rate: RateString;
}

export enum RateString {
'minute',
'hour',
'month',
}

export interface Hooks {
[key: string]: () => void;
}
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "ES2019",
"strict": true,
"module": "ESNext",
"esModuleInterop": true,
"moduleResolution": "node",
"outDir": "dist",
"rootDir": "src"
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}

0 comments on commit 7bbd614

Please sign in to comment.