UNPKG

auto-tasker

Version:

A Node.js scheduling utility that simplifies task scheduling with human-readable syntax. Convert intuitive time formats into cron expressions and schedule tasks effortlessly using node-schedule

1 lines 16.6 kB
(()=>{var t={478:(t,e,r)=>{function o(t){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},o(t)}function n(t,e){for(var r=0;r<e.length;r++){var o=e[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,i(o.key),o)}}function i(t){var e=function(t){if("object"!=o(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=o(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==o(e)?e:e+""}var a=r(635),u=r(77),c=u.validateTimeFormat,l=u.validateWeekDays,f=u.validateTimePeriod,s=r(511).mapNumberToTimeFormat,y=r(296),p=y.DEAFULTS,d=y.validTimeUnits,v=r(17).handleError,b=function(){return t=function t(){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.scheduleData={},this._excutionChain=[],this.scheduleData.callBackTask=null},e=[{key:"every",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:p.every;if("string"!=typeof t)throw new Error("Invalid time unit, every must be set to string.");if(!d.includes(t.toLowerCase()))throw new Error("Invalid time unit, every must be set to 'week' or 'day' or 'hour'");return this.scheduleData.every=t.toLowerCase(),this._excutionChain.push("every"),this.scheduleData.on=p.on,this.scheduleData.at=p.at,this.scheduleData.period=p.period,this}},{key:"on",value:function(t){if(this._excutionChain.includes("every")||this.every(),"week"!==this.scheduleData.every)throw new Error("Invalid time unit, every must be set to week");if(!l(t))throw new Error("Invalid day of week");return this.scheduleData.on=t,this._excutionChain.push("on"),this}},{key:"at",value:function(t,e){try{var r;if("string"!=typeof t||isNaN(Number(t))||(t=s(Number(t))),"number"==typeof t&&(t=s(t)),this._excutionChain.includes("every")||this.every("day"),(this._excutionChain.includes("on")||this._excutionChain.includes("every"))&&(t||(t=p.at),e||(e=p.period)),"week"!==this.scheduleData.every&&"day"!==this.scheduleData.every)throw new Error("Invalid time unit, every must be set to week or day");if(!c(t)||!f(e))throw new Error("Invalid time format or time period");return this.scheduleData.at=t,this.scheduleData.period=null===(r=e)||void 0===r?void 0:r.toLowerCase(),this._excutionChain.push("at"),this}catch(t){throw v(t,t.message)}}},{key:"do",value:function(t){if(!t)throw new Error("callBackTask is required");return this.scheduleData.callBackTask=t,a.createSchedule(this.scheduleData)}}],e&&n(t.prototype,e),Object.defineProperty(t,"prototype",{writable:!1}),t;var t,e}();t.exports=b},635:(t,e,r)=>{function o(t){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},o(t)}function n(t,e){for(var r=0;r<e.length;r++){var o=e[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,i(o.key),o)}}function i(t){var e=function(t){if("object"!=o(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=o(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==o(e)?e:e+""}var a=r(301),u=r(747),c=r(16),l=function(){return t=function t(){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.schedules=[]},e=[{key:"createSchedule",value:function(t){var e,r=t.every,o=t.at,n=t.period,i=t.on,l=t.callBackTask;switch(r){case"week":e=new a(i,o,n);break;case"day":e=new u(o,n);break;case"hour":e=new c(i);break;default:throw new Error("Invalid time unit, every must be set to week or day or hour")}return e.task(l).excute()}}],null&&n(t.prototype,null),e&&n(t,e),Object.defineProperty(t,"prototype",{writable:!1}),t;var t,e}();t.exports=l},983:(t,e,r)=>{function o(t){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},o(t)}function n(t,e){for(var r=0;r<e.length;r++){var o=e[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,i(o.key),o)}}function i(t){var e=function(t){if("object"!=o(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=o(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==o(e)?e:e+""}var a=r(703),u=r(17).handleError,c=function(){return t=function t(){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.cronExcuter=a},e=[{key:"task",value:function(t){try{if(!t)throw new Error("callBackTask is required");return this.callBackTask=t,this}catch(t){throw u(t,"callBackTask is required")}}},{key:"excute",value:function(){var t=this,e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.cronExcuter;try{if(!this.callBackTask)throw new Error("task() method must be called before excute()");e.schedule(this.toCronExp(),(function(){t.callBackTask(),console.log("Cron job created successfully. Cron expression: ".concat(t.toCronExp()))}))}catch(t){throw u(t,t.message)}}}],e&&n(t.prototype,e),Object.defineProperty(t,"prototype",{writable:!1}),t;var t,e}();t.exports=c},954:(t,e,r)=>{var o=r(478);t.exports={ScheduleBuilder:o}},747:(t,e,r)=>{function o(t){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},o(t)}function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,o=Array(e);r<e;r++)o[r]=t[r];return o}function i(t,e){for(var r=0;r<e.length;r++){var o=e[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,a(o.key),o)}}function a(t){var e=function(t){if("object"!=o(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=o(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==o(e)?e:e+""}function u(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(t){}return(u=function(){return!!t})()}function c(t){return c=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},c(t)}function l(t,e){return l=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},l(t,e)}var f=r(983),s=r(77),y=s.validateTimeFormat,p=s.validateTimePeriod,d=function(t){function e(t,r){var n,i;if(function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e),i=function(t,e,r){return e=c(e),function(t,e){if(e&&("object"==o(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}(t,u()?Reflect.construct(e,r||[],c(t).constructor):e.apply(t,r))}(this,e),!t)throw new Error("Invalid schedule data");return i.time=t,i.period=null!==(n=null==r?void 0:r.toLowerCase())&&void 0!==n?n:"am",i}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&l(t,e)}(e,t),r=e,(a=[{key:"toCronExp",value:function(){if(!y(this.time)||!p(this.period))throw new Error("Invalid time format");if(this.time){var t=function(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var o,n,i,a,u=[],c=!0,l=!1;try{if(i=(r=r.call(t)).next,0===e){if(Object(r)!==r)return;c=!1}else for(;!(c=(o=i.call(r)).done)&&(u.push(o.value),u.length!==e);c=!0);}catch(t){l=!0,n=t}finally{try{if(!c&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(l)throw n}}return u}}(t,e)||function(t,e){if(t){if("string"==typeof t)return n(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(t,e):void 0}}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}(this.time.split(":").map(Number),2),e=t[0],r=t[1];"am"===this.period&&12===e&&(e=0),"pm"===this.period&&12!==e&&(e+=12);var o="".concat(r?"".concat(r," "):"0 ").concat(e?"".concat(e):"");return"".concat(o," * * *")}return"* * * * *"}}])&&i(r.prototype,a),Object.defineProperty(r,"prototype",{writable:!1}),r;var r,a}(f);t.exports=d},16:(t,e,r)=>{function o(t){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},o(t)}function n(t,e){for(var r=0;r<e.length;r++){var o=e[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,i(o.key),o)}}function i(t){var e=function(t){if("object"!=o(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=o(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==o(e)?e:e+""}function a(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(t){}return(a=function(){return!!t})()}function u(t){return u=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},u(t)}function c(t,e){return c=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},c(t,e)}var l=r(983),f=r(77).validateWeekDays,s=function(t){function e(t){var r;if(function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e),(r=function(t,e,r){return e=u(e),function(t,e){if(e&&("object"==o(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}(t,a()?Reflect.construct(e,r||[],u(t).constructor):e.apply(t,r))}(this,e)).everyMinute=!t,r.day=f(t),!r.day&&!r.everyMinute)throw new Error("Invalid day");return r}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&c(t,e)}(e,t),r=e,(i=[{key:"toCronExp",value:function(){if(this.day)return"0 * * * ".concat(this.day-1);if(this.everyMinute)return"* * * * *";throw new Error("Invalid Cron Operation")}}])&&n(r.prototype,i),Object.defineProperty(r,"prototype",{writable:!1}),r;var r,i}(l);t.exports=s},301:(t,e,r)=>{function o(t){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},o(t)}function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,o=Array(e);r<e;r++)o[r]=t[r];return o}function i(t,e){for(var r=0;r<e.length;r++){var o=e[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,a(o.key),o)}}function a(t){var e=function(t){if("object"!=o(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=o(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==o(e)?e:e+""}function u(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(t){}return(u=function(){return!!t})()}function c(t){return c=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},c(t)}function l(t,e){return l=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},l(t,e)}var f=r(983),s=r(77),y=s.validateTimeFormat,p=s.validateWeekDays,d=s.validateTimePeriod,v=function(t){function e(t,r,n){var i;if(function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e),i=function(t,e,r){return e=c(e),function(t,e){if(e&&("object"==o(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}(t,u()?Reflect.construct(e,r||[],c(t).constructor):e.apply(t,r))}(this,e),!t||!r||!n)throw new Error("Invalid schedule data");return i.day=t.toLowerCase(),i.time=r,i.period=n.toLowerCase(),i}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&l(t,e)}(e,t),r=e,(a=[{key:"toCronExp",value:function(){if(!1===y(this.time))throw new Error("Invalid time format");if(!1===d(this.period))throw new Error("Invalid schedule data");if(this.time){var t=function(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var o,n,i,a,u=[],c=!0,l=!1;try{if(i=(r=r.call(t)).next,0===e){if(Object(r)!==r)return;c=!1}else for(;!(c=(o=i.call(r)).done)&&(u.push(o.value),u.length!==e);c=!0);}catch(t){l=!0,n=t}finally{try{if(!c&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(l)throw n}}return u}}(t,e)||function(t,e){if(t){if("string"==typeof t)return n(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(t,e):void 0}}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}(this.time.split(":").map(Number),2),e=t[0],r=t[1];"am"===this.period&&12===e&&(e=0),"pm"===this.period&&12!==e&&(e+=12);var o=p(this.day);if(!o)throw new Error("Invalid day of week");return"".concat(r?"".concat(r):0," ").concat(e?"".concat(e):0," * * ").concat(o-1)}throw new Error("Invalid time format")}}])&&i(r.prototype,a),Object.defineProperty(r,"prototype",{writable:!1}),r;var r,a}(f);t.exports=v},511:t=>{t.exports={mapNumberToTimeFormat:function(t){if("string"==typeof t)throw new Error("Invalid time format");if(t>=0&&t<=12)return 1===(t=t.toString()).length&&(t="0".concat(t)),"".concat(t,":00");throw new Error("Invalid time format")}}},296:t=>{t.exports={DEAFULTS:{every:"week",on:"monday",at:"00:00",period:"am"},validTimeUnits:["day","week","hour"],weekDays:["sunday","monday","tuesday","wednesday","thursday","friday","saturday"]}},17:t=>{t.exports={handleError:function(t){return new Error(arguments.length>1&&void 0!==arguments[1]?arguments[1]:"An unexpected error occurred. Please check your configuration.")}}},77:(t,e,r)=>{var o=r(296),n=(o.DEAFULTS,o.weekDays);t.exports={validateWeekDays:function(t){return!!n.includes(null==t?void 0:t.toLowerCase())&&n.indexOf(t.toLowerCase())+1},validateTimeFormat:function(t){return!!t.match(/^(0?[0-9]|1[0-2]):([0-5]\d)$/)},validateTimePeriod:function(t){return!("string"!=typeof t||!["am","pm"].includes(null==t?void 0:t.toLowerCase()))}}},703:t=>{"use strict";t.exports=require("node-cron")}},e={},r=function r(o){var n=e[o];if(void 0!==n)return n.exports;var i=e[o]={exports:{}};return t[o](i,i.exports,r),i.exports}(954);module.exports=r})();