hexabot/api/migrations/config/create.ts

64 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-09-10 09:50:11 +00:00
/*
* Copyright © 2024 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
/* eslint-disable no-console */
import fs from 'fs';
import path from 'path';
2024-09-24 07:02:32 +00:00
import escapeRegExp from 'lodash/escapeRegExp';
2024-09-24 04:21:00 +00:00
2024-09-10 09:50:11 +00:00
// Get the argument passed (e.g., "all-users-fr")
const arg: string | undefined = process.argv[2];
// Check if the argument exists
if (!arg) {
console.error('Please provide a name for a new migration.');
process.exit(1);
}
// Define the path to the migrations directory and the template file
const migrationsDir: string = path.join(__dirname, '../');
const templatePath: string = path.join(__dirname, '../config/template.ts');
// Check if a migration with the same name (excluding timestamp) already exists
const migrationExists: boolean = fs.readdirSync(migrationsDir).some((file) => {
2024-09-24 07:02:32 +00:00
const regex = new RegExp(`^[0-9]+-${escapeRegExp(arg)}\\.ts$`);
2024-09-10 09:50:11 +00:00
return regex.test(file);
});
if (migrationExists) {
console.error(`A migration with the name "${arg}" already exists.`);
process.exit(1);
}
// Generate a timestamp
const timestamp: string = Date.now().toString();
// Create the filename using the timestamp and argument
const filename: string = `${timestamp}-${arg}.ts`;
// Read the template content from the file
let template: string;
try {
template = fs.readFileSync(templatePath, 'utf8');
} catch (err) {
console.error('Error reading template file:', err);
process.exit(1);
}
// Define the full path to save the file
const filePath: string = path.join(migrationsDir, filename);
// Write the template to the file
fs.writeFile(filePath, template, (err) => {
if (err) {
console.error('Error writing file:', err);
} else {
console.log(`File created: ${filename}`);
}
});