@wocker/cron-plugin
Version:
Cron plugin for wocker
154 lines (153 loc) • 4.78 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CrontabExpression = void 0;
class CrontabExpression {
constructor(params) {
this._second = params.second;
this._minute = params.minute;
this._hour = params.hour;
this._dayOfMonth = params.dayOfMonth;
this._month = params.month;
this._dayOfWeek = params.dayOfWeek;
if (params.alias) {
this._alias = params.alias;
}
this.command = params.command;
}
get alias() {
return this._alias;
}
set alias(alias) {
const params = CrontabExpression.parseExpression(`${CrontabExpression.Aliases[alias]} ${this.command}`, "extended");
this._alias = alias;
this._second = params.second;
this._minute = params.minute;
this._hour = params.hour;
this._dayOfMonth = params.dayOfMonth;
this._month = params.month;
this._dayOfWeek = params.dayOfWeek;
}
get second() {
return this._second;
}
set second(second) {
if (this._second === second) {
return;
}
this._second = second;
this.resetAlias();
}
get minute() {
return this._minute;
}
set minute(minute) {
if (this._minute === minute) {
return;
}
this._minute = minute;
this.resetAlias();
}
get hour() {
return this._hour;
}
set hour(hour) {
if (this._hour === hour) {
return;
}
this._hour = hour;
this.resetAlias();
}
get dayOfMonth() {
return this._dayOfMonth;
}
set dayOfMonth(dayOfMonth) {
if (this._dayOfMonth === dayOfMonth) {
return;
}
this._dayOfMonth = dayOfMonth;
this.resetAlias();
}
get month() {
return this._month;
}
set month(month) {
if (this._month === month) {
return;
}
this._month = month;
this.resetAlias();
}
get dayOfWeek() {
return this._dayOfWeek;
}
set dayOfWeek(dayOfWeek) {
if (this._dayOfWeek === dayOfWeek) {
return;
}
this._dayOfWeek = dayOfWeek;
this.resetAlias();
}
resetAlias() {
if (this._alias) {
delete this._alias;
}
}
toString(format) {
if (this._alias) {
return `@${this._alias} ${this.command}`;
}
const standard = `${this._minute} ${this._hour} ${this._dayOfMonth} ${this._month} ${this._dayOfWeek}`;
if (format === "extended" || this._second !== "0") {
return `${this._second} ${standard} ${this.command}`;
}
return `${standard} ${this.command}`;
}
static parseExpression(expression, type = "standard") {
const trimmed = expression.trim();
if (trimmed.startsWith("@")) {
const spaceIndex = trimmed.search(/\s/), aliasStr = spaceIndex === -1 ? trimmed.slice(1) : trimmed.slice(1, spaceIndex), command = spaceIndex === -1 ? "" : trimmed.slice(spaceIndex + 1);
if (!(aliasStr in CrontabExpression.Aliases)) {
throw new Error(`Unknown alias: @${aliasStr}`);
}
const alias = aliasStr;
return Object.assign(Object.assign({}, this.parseExpression(`${CrontabExpression.Aliases[alias]} ${command}`, "extended")), { alias });
}
const tokens = type === "extended" ? [] : ["0"];
let command = expression.trim();
while (tokens.length < 6) {
const index = command.search(/\s/);
if (index === -1) {
throw new Error("Arguments missing");
}
tokens.push(command.slice(0, index));
command = command.slice(index + 1).trim();
}
return {
second: tokens[0],
minute: tokens[1],
hour: tokens[2],
dayOfMonth: tokens[3],
month: tokens[4],
dayOfWeek: tokens[5],
command
};
}
static parse(expression, type = "standard") {
return new CrontabExpression(this.parseExpression(expression, type));
}
}
exports.CrontabExpression = CrontabExpression;
(function (CrontabExpression) {
CrontabExpression.Aliases = {
yearly: "0 0 0 1 1 *",
annually: "0 0 0 1 1 *",
monthly: "0 0 0 1 * *",
weekly: "0 0 0 * * 0",
daily: "0 0 0 * * *",
hourly: "0 0 * * * *",
minutely: "0 * * * * *",
secondly: "* * * * * *",
weekdays: "0 0 0 * * 1-5",
weekends: "0 0 0 * * 0,6"
};
})(CrontabExpression || (exports.CrontabExpression = CrontabExpression = {}));