@crowdin/app-project-module
Version:
Module that generates for you all common endpoints for serving standalone Crowdin App
98 lines (97 loc) • 3.29 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeCron = void 0;
exports.initialize = initialize;
exports.getCron = getCron;
const logger_1 = require("./logger");
/**
* Default Node.js cron handler that uses node-cron
*/
class NodeCron {
constructor() {
this.pendingSchedules = [];
this.initialized = false;
// Dynamically import node-cron
Promise.resolve().then(() => __importStar(require('node-cron'))).then((nodeCron) => {
this.cron = nodeCron.default || nodeCron;
this.initialized = true;
// Schedule all pending tasks
this.pendingSchedules.forEach(({ expression, task }) => {
this.cron.schedule(expression, task);
});
this.pendingSchedules = [];
})
.catch((error) => {
console.error('Failed to load node-cron:', error);
throw new Error('node-cron is required for the default cron. Please install it or provide a custom cron in config.');
});
}
schedule(expression, task) {
if (this.initialized && this.cron) {
this.cron.schedule(expression, task);
}
else {
// Queue the task until node-cron is loaded
this.pendingSchedules.push({ expression, task });
}
}
}
exports.NodeCron = NodeCron;
let cron;
/**
* Initialize the cron instance
* @param config - Client configuration
*/
function initialize(config) {
if (config.cron) {
(0, logger_1.log)('Using custom cron implementation');
cron = config.cron;
}
else {
(0, logger_1.log)('Using default NodeCron implementation');
cron = new NodeCron();
}
}
/**
* Get the initialized cron instance
* @returns The cron instance
*/
function getCron() {
if (!cron) {
throw new Error('Cron not initialized');
}
return cron;
}