@shagital/adonisjs-crud-generator
Version:
Adonisjs Admin Panel Generator is a package that helps you quickly scaffold your typical CRUD admin interfaces. It generates the admin panel code based on the existing (migrated) table in the database
91 lines (90 loc) • 4.44 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const standalone_1 = require("@adonisjs/core/build/standalone");
const helpers_1 = require("../common/helpers");
const fs_1 = __importDefault(require("fs"));
const util_1 = __importDefault(require("util"));
const execSync = util_1.default.promisify(require('child_process').execSync);
class PermissionMigrationGenerator extends standalone_1.BaseCommand {
async run() {
const dbInstance = this.application.container.use('Adonis/Lucid/Database');
if (this.connection) {
(0, helpers_1.validateConnection)(dbInstance, this.application.config, this.connection);
}
const tableColumns = (0, helpers_1.getTableColumnsAndTypes)(dbInstance, this.application.config);
let tableName = this.table.toLowerCase();
await tableColumns(tableName, this.connection);
let migrationFile = `${__dirname}/../templates/migration.txt`;
this.logger.info('Add migration file');
let data = String(fs_1.default.readFileSync(migrationFile));
let migrationPath = this.application.migrationsPath(`admin_permissions_for_${tableName}.ts`);
let pascalName = (0, helpers_1.pascalCase)(tableName);
let adminUserModelNamespace = this.application.config.get('CrudGenerator.admin_user', 'App/Models/User');
let userModelName = String((0, helpers_1.resolveModelName)(adminUserModelNamespace));
data = data
.replace(new RegExp('{{model}}', 'g'), tableName)
.replace(new RegExp('{{pascalCase}}', 'g'), pascalName)
.replace('{{userModel}}', userModelName)
.replace('{{userModelNamespace}}', adminUserModelNamespace);
fs_1.default.writeFileSync(migrationPath, data);
if (this.migrate) {
await this.runMigration();
}
process.exit();
}
async runMigration() {
this.logger.info('Run migration');
let vm = this;
execSync(`node ace migration:run`, { stdio: 'inherit' }, (_e, stdout, _stderr) => {
if (!stdout.includes('Database migrated successfully')) {
return vm.error(`Error: ${stdout}`);
}
vm.logger.info('Permission seeding complete');
});
}
}
/**
* Command name is used to run the command
*/
PermissionMigrationGenerator.commandName = 'crud:permission';
/**
* Command description is displayed in the "help" output
*/
PermissionMigrationGenerator.description = 'Generate CRUD for a table';
PermissionMigrationGenerator.settings = {
/**
* Set the following value to true, if you want to load the application
* before running the command
*/
loadApp: true,
/**
* Set the following value to true, if you want this command to keep running until
* you manually decide to exit the process
*/
stayAlive: false,
};
__decorate([
standalone_1.args.string({ description: 'Table to generate model and relationships for' }),
__metadata("design:type", String)
], PermissionMigrationGenerator.prototype, "table", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Run new migration' }),
__metadata("design:type", Boolean)
], PermissionMigrationGenerator.prototype, "migrate", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Specify custom DB connection to use' }),
__metadata("design:type", String)
], PermissionMigrationGenerator.prototype, "connection", void 0);
exports.default = PermissionMigrationGenerator;