From 2be58135d26e15ad39cda99edd8d2a0b29cce4d9 Mon Sep 17 00:00:00 2001 From: Horiuchi_H Date: Fri, 17 Apr 2015 16:06:20 +0900 Subject: [PATCH] add `node-cron` type definition file --- cron/cron-tests.ts | 66 ++++++++++++++++++++++++++++++++++++++++++++++ cron/cron.d.ts | 27 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 cron/cron-tests.ts create mode 100644 cron/cron.d.ts diff --git a/cron/cron-tests.ts b/cron/cron-tests.ts new file mode 100644 index 0000000000..cea98cdab6 --- /dev/null +++ b/cron/cron-tests.ts @@ -0,0 +1,66 @@ +/// + +import cron = require('cron'); +var CronJob = cron.CronJob; +var CronTime = cron.CronTime; + +var timeZone = 'America/Los_Angeles'; + +// Usage (basic cron usage): +new CronJob('* * * * * *', () => { + console.log('You will see this message every second'); +}, null, true, 'America/Los_Angeles'); + +// Another cron example +var job = new CronJob('00 30 11 * * 1-5', () => { + /* + * Runs every weekday (Monday through Friday) + * at 11:30:00 AM. It does not run on Saturday + * or Sunday. + */ + }, () => { + /* This function is executed when the job stops */ + }, + true, /* Start the job right now */ + timeZone /* Time zone of this job. */ +); + +// Another example with Date +var job = new CronJob(new Date(), () => { + /* runs once at the specified date. */ + }, () => { + /* This function is executed when the job stops */ + }, + true, /* Start the job right now */ + timeZone /* Time zone of this job. */ +); + +// For good measure +var job = new CronJob({ + cronTime: '00 30 11 * * 1-5', + onTick: () => { + /* + * Runs every weekday (Monday through Friday) + * at 11:30:00 AM. It does not run on Saturday + * or Sunday. + */ + }, + start: false, + timeZone: 'America/Los_Angeles' +}); +job.start(); + +// How to check if a cron pattern is valid: +try { + new CronJob('invalid cron pattern', () => { + console.log('this should not be printed'); + }) +} catch(ex) { + console.log("cron pattern not valid"); +} + + +// Check cronTime fomat +new CronTime('* * * * * *'); +new CronTime(new Date()); + diff --git a/cron/cron.d.ts b/cron/cron.d.ts new file mode 100644 index 0000000000..c956a26fb9 --- /dev/null +++ b/cron/cron.d.ts @@ -0,0 +1,27 @@ +// Type definitions for cron 1.0.9 +// Project: https://www.npmjs.com/package/cron +// Definitions by: Hiroki Horiuchi +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "cron" { + + interface CronJobStatic { + new(cronTime: string|Date, onTick: () => void, onComplete?: () => void, start?: boolean, timezone?: string, context?: any): CronJob; + new(options: { + cronTime: string|Date; onTick: () => void; onComplete?: () => void; start?: boolean; timezone?: string; context?: any + }): CronJob; + } + interface CronJob { + start(): void; + stop(): void; + } + export var CronJob: CronJobStatic; + + interface CronTimeStatic { + new(time: string|Date): CronTime; + } + interface CronTime {} + export var CronTime: CronTimeStatic; + +} +