sx-cli-tool
Version:
Laravel va Vue frameworklaridagi monolith arxitektura loyihalarni boshqarish uchun mo'ljallangan CLI tool
73 lines (69 loc) • 2.67 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateMigration = generateMigration;
exports.generateMigrationFileName = generateMigrationFileName;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const folder_1 = require("../../utils/folder");
const prettier_1 = require("../../utils/prettier");
// generate migration with fields for laravel
function generateMigration(entityName, fields, projectPath) {
const migrationPath = path_1.default.join(projectPath, "database", "migrations", generateMigrationFileName(entityName.apiIdPlural));
if (!fs_1.default.existsSync(migrationPath)) {
const template = `
use Illuminate\\Database\\Migrations\\Migration;
use Illuminate\\Database\\Schema\\Blueprint;
use Illuminate\\Support\\Facades\\Schema;
return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(
'${entityName.apiIdPlural}',
function (Blueprint $table) {
$table->id();
${fields
.map((field) => ` $table->${field.type}('${field.name}')${field.isNullable ? "->nullable();" : ";"}`)
.join("\n")}
$table->timestamps();
}
);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('${entityName.apiIdPlural}');
}
};
`;
(0, folder_1.ensureDirectoryExists)(migrationPath);
// if the migration file does not exist, create it
fs_1.default.writeFileSync(migrationPath, template, "utf8");
(0, prettier_1.formatPhpFile)(migrationPath);
}
}
function generateMigrationFileName(tableName) {
const now = new Date();
// Format the date as YYYY_MM_DD_HHMMSS
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, "0"); // Months are zero-indexed
const day = String(now.getDate()).padStart(2, "0");
const hours = String(now.getHours()).padStart(2, "0");
const minutes = String(now.getMinutes()).padStart(2, "0");
const seconds = String(now.getSeconds()).padStart(2, "0");
// Create the migration file name
const timestamp = `${year}_${month}_${day}_${hours}${minutes}${seconds}`;
const fileName = `${timestamp}_create_${tableName}_table.php`;
return fileName;
}