| const path = require('path'); |
| const mongoose = require('mongoose'); |
| const { v5: uuidv5 } = require('uuid'); |
| const { Banner } = require('@librechat/data-schemas').createModels(mongoose); |
| require('module-alias')({ base: path.resolve(__dirname, '..', 'api') }); |
| const { askQuestion, askMultiLineQuestion, silentExit } = require('./helpers'); |
| const connect = require('./connect'); |
|
|
| (async () => { |
| await connect(); |
|
|
| |
| |
| |
| console.purple('--------------------------'); |
| console.purple('Update the banner!'); |
| console.purple('--------------------------'); |
| |
| |
| |
| let displayFrom = ''; |
| let displayTo = ''; |
| let message = ''; |
| let isPublic = undefined; |
| |
| if (process.argv.length >= 3) { |
| displayFrom = process.argv[2]; |
| displayTo = process.argv[3]; |
| message = process.argv[4]; |
| isPublic = process.argv[5] === undefined ? undefined : process.argv[5] === 'true'; |
| } else { |
| console.orange( |
| 'Usage: npm run update-banner <displayFrom(Format: yyyy-mm-ddTHH:MM:SSZ)> <displayTo(Format: yyyy-mm-ddTHH:MM:SSZ)> <message> <isPublic(true/false)>', |
| ); |
| console.orange('Note: if you do not pass in the arguments, you will be prompted for them.'); |
| console.purple('--------------------------'); |
| } |
|
|
| |
| |
| |
| if (!displayFrom) { |
| displayFrom = await askQuestion('Display From (Format: yyyy-mm-ddTHH:MM:SSZ, Default: now):'); |
| } |
|
|
| |
| const dateTimeRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/; |
| if (displayFrom && !dateTimeRegex.test(displayFrom)) { |
| console.red('Error: Invalid date format for displayFrom. Please use yyyy-mm-ddTHH:MM:SSZ.'); |
| silentExit(1); |
| } |
|
|
| displayFrom = displayFrom ? new Date(displayFrom) : new Date(); |
|
|
| if (!displayTo) { |
| displayTo = await askQuestion( |
| 'Display To (Format: yyyy-mm-ddTHH:MM:SSZ, Default: not specified):', |
| ); |
| } |
|
|
| if (displayTo && !dateTimeRegex.test(displayTo)) { |
| console.red('Error: Invalid date format for displayTo. Please use yyyy-mm-ddTHH:MM:SSZ.'); |
| silentExit(1); |
| } |
|
|
| displayTo = displayTo ? new Date(displayTo) : null; |
|
|
| if (!message) { |
| message = await askMultiLineQuestion( |
| 'Enter your message ((Enter a single dot "." on a new line to finish)):', |
| ); |
| } |
|
|
| if (message.trim() === '') { |
| console.red('Error: Message cannot be empty!'); |
| silentExit(1); |
| } |
|
|
| if (isPublic === undefined) { |
| const isPublicInput = await askQuestion('Is public (y/N):'); |
| isPublic = isPublicInput.toLowerCase() === 'y' ? true : false; |
| } |
|
|
| |
| |
| const NAMESPACE = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; |
| const bannerId = uuidv5(message, NAMESPACE); |
|
|
| let result; |
| try { |
| |
| |
| |
| const existingBanner = await Banner.findOne(); |
| if (existingBanner) { |
| result = await Banner.findByIdAndUpdate( |
| existingBanner._id, |
| { |
| displayFrom, |
| displayTo, |
| message, |
| bannerId, |
| isPublic, |
| }, |
| { new: true }, |
| ); |
| } else { |
| result = await Banner.create({ |
| displayFrom, |
| displayTo, |
| message, |
| bannerId, |
| isPublic, |
| }); |
| } |
| } catch (error) { |
| console.red('Error: ' + error.message); |
| console.error(error); |
| silentExit(1); |
| } |
|
|
| if (!result) { |
| console.red('Error: Something went wrong while updating the banner!'); |
| console.error(result); |
| silentExit(1); |
| } |
|
|
| console.green('Banner updated successfully!'); |
| console.purple(`bannerId: ${bannerId}`); |
| console.purple(`from: ${displayFrom}`); |
| console.purple(`to: ${displayTo || 'not specified'}`); |
| console.purple(`Banner: ${message}`); |
| console.purple(`isPublic: ${isPublic}`); |
| silentExit(0); |
| })(); |
|
|
| process.on('uncaughtException', (err) => { |
| if (!err.message.includes('fetch failed')) { |
| console.error('There was an uncaught error:'); |
| console.error(err); |
| } |
|
|
| if (err.message.includes('fetch failed')) { |
| return; |
| } else { |
| process.exit(1); |
| } |
| }); |
|
|