diff --git a/src/commands/cms/crud.ts b/src/commands/cms/crud.ts new file mode 100644 index 0000000..72911bb --- /dev/null +++ b/src/commands/cms/crud.ts @@ -0,0 +1,166 @@ +import {Command, Flags} from '@oclif/core' +import Questions from '../../libraries/Questions' +const inquirer = require('inquirer') +import Generator from '../../libraries/Generator' +import Functions from '../../libraries/Functions' + +let fs = require('fs'); +let ora = require('ora'); +const execa = require('execa'); +const Listr = require('listr'); +var shell = require('shelljs'); +const { exec } = require('child_process'); +let fsSync = require('fs-sync'); +const fsPromises = fs.promises; + +const chalk = require('chalk'); + +export default class CmsCrud extends Command { + + args: {[k: string]: any} = {}; + flags: {[k: string]: any} = {}; + primary: {[k: string]: any} = {}; + inputs: {[k: string]: any} = {}; + spinner: {[k: string]: any} = {}; + repo: string = 'https://github.com/webreinvent/vaahcms-ready'; + target_dir: string = './'; + source_dir: string = ''; + + static description = 'Generate CRUD operations for VaahCMS' + + /* + *--------------------------------------------------- + * Command Flags/Options + *--------------------------------------------------- + */ + static flags = { + help: Flags.boolean({ + description: 'Generate CRUD operation for VaahCMS', + default: false, + }), + }; + + /* + *--------------------------------------------------- + * Command Arguments + *--------------------------------------------------- + */ + static args = {}; + + /* + *--------------------------------------------------- + * Command Execution + *--------------------------------------------------- + */ + async run() { + + let functions = new Functions(); + let is_updates_available = await functions.isUpdatesAvailable(); + if(is_updates_available) + { + return true; + } + + + const {args, flags} = await this.parse(CmsCrud) + + let questions = new Questions(); + + this.primary = await inquirer.prompt(questions.getCrudQuestionsPrimary()); + + let get_questions = questions.getCrudQuestions(this.primary.for); + + this.inputs = await inquirer.prompt(get_questions); + + this.inputs.for = this.primary.for; + + let target = ""; + let source = '\\skeletons\\vaahcms\\crud\\'; + this.inputs['namespace_controller'] = ''; + + if(this.inputs.for == 'Module - Vue3 & PrimeVue') + { + source = '\\skeletons\\vaahcms\\crud-vue3\\'; + this.inputs['namespace'] = 'VaahCms\\Modules\\'+this.inputs.folder_name; + target = "./VaahCms/Modules/"+this.inputs.folder_name; + + } else if(this.inputs.for == 'Module - Vue2 & Buefy'){ + + source = '\\skeletons\\vaahcms\\crud\\'; + this.inputs['namespace'] = 'VaahCms\\Modules\\'+this.inputs.folder_name; + target = "./VaahCms/Modules/"+this.inputs.folder_name; + + } else if(this.inputs.for == 'Theme') + { + this.inputs['namespace'] = 'VaahCms\\Themes\\'+this.inputs.folder_name; + target = "./VaahCms/Themes/"+this.inputs.folder_name; + } else{ + source = '\\skeletons\\vaahcms\\crud-vue3\\'; + this.inputs['namespace_controller'] = this.inputs['namespace']+'\\Http\\Controllers'; + target = this.inputs.path; + } + + + let generator = new Generator(args, flags, this.inputs, source, target); + + this.log(chalk.green('=======================================')); + this.log('Generating CRUD Files'); + this.log(chalk.green('---------------------------------------')); + + const tasks = new Listr([ + { + title: 'Files Generated for CRUD operations', + task: function () { + generator.generateCrudFiles(); + } + } + ]); + + let self = this; + + tasks.run().then((ctx: any) => { + self.successMessage(); + }).catch((err: any) => { + console.error(err); + }); + + } + + //--------------------------------------------------- + successMessage() + { + + this.log(chalk.white.bgGreen.bold(" Files Generated! ")); + this.log(chalk.green("==================================================================")); + this.log(chalk.green("Now, follow following steps:")); + + if(this.inputs['generate_migration'] === 'true') + { + this.log("0.1) Update the migration file with the table name and columns"); + this.log("0.2) Re-activate module to run migrations"); + } + + this.log("2) Include the crud router file in the module's backend route file"); + this.log("3) Include the vue router file"); + + if(this.inputs.for == 'Module - Vue2 & Buefy') + { + this.log("4) Include the vue store file"); + this.log("5) Add vue router link to you menu"); + } + + if(this.inputs.for == 'Module - Vue3 & PrimeVue') + { + this.log("6) Add vue router link to you /Vue/Components/Aside.vue file"); + } + + + this.log(chalk.green("==================================================================")); + + } + + //--------------------------------------------------- + //--------------------------------------------------- + + +} diff --git a/src/libraries/Functions.ts b/src/libraries/Functions.ts index 51e523a..63acf8d 100644 --- a/src/libraries/Functions.ts +++ b/src/libraries/Functions.ts @@ -5,7 +5,6 @@ const chalk = require('chalk'); var semver = require('semver'); const package_json = require("./../../package.json"); - const log = console.log; export default class Helpers { diff --git a/src/libraries/Helpers.ts b/src/libraries/Helpers.ts new file mode 100644 index 0000000..57037ae --- /dev/null +++ b/src/libraries/Helpers.ts @@ -0,0 +1,303 @@ +const axios = require('axios'); +var dateFormat = require('dateformat'); + +const log = console.log; + +export default class Helpers { + args: {[k: string]: any} = {}; + flags: {[k: string]: any} = {}; + inputs: {[k: string]: any} = {}; + package_url: string = 'https://api.npms.io/v2/package/vaah'; + + + constructor(args: object, flags: object, inputs: object) { + this.args = args; + this.flags = flags; + this.inputs = inputs; + + + this.setLowerAndUpperCaseValues(); + } + + //------------------------------------------------------- + isPackageUpdated() + { + + axios.get(this.package_url).then(function (response: any) { + console.log(response); + }) + + + } + //------------------------------------------------------- + + setLowerAndUpperCaseValues() + { + + if(Object.keys(this.args).length) + { + for (let key in this.args) { + if (typeof this.inputs[key] === 'string'){ + this.args[key+'_lower'] = this.args[key].toLowerCase(); + this.args[key+'_upper'] = this.args[key].toUpperCase(); + } + + } + } + + if(Object.keys(this.flags).length) + { + for (let key in this.flags) { + if (typeof this.inputs[key] === 'string'){ + this.flags[key+'_lower'] = this.flags[key].toLowerCase(); + this.flags[key+'_upper'] = this.flags[key].toUpperCase(); + } + + } + } + + if(Object.keys(this.inputs).length) + { + for (let key in this.inputs) { + + if (typeof this.inputs[key] === 'string'){ + this.inputs[key+'_lower'] = this.inputs[key].toLowerCase(); + this.inputs[key+'_upper'] = this.inputs[key].toUpperCase(); + } + + } + } + + } + + //------------------------------------------------------- + replaceAll(str:string, find:string, replace:string){ + return str.replace(new RegExp(find, 'g'), replace); + } + //------------------------------------------------------- + titleCase(str:string) + { + let wordsArray = str.toLowerCase().split(/\s+/); + let upperCased = wordsArray.map(function(word) { + return word.charAt(0).toUpperCase() + word.substr(1); + }); + return upperCased.join(" "); + } + //------------------------------------------------------- + getClassName(str:string) + { + let class_name = ''; + + class_name = str; + class_name = this.replaceAll(class_name, "_", " "); + class_name = this.titleCase(class_name); + class_name = this.replaceAll(class_name, " ", ""); + + return class_name; + + } + //------------------------------------------------------- + getMigrationFileName(str:string) + { + var now = new Date(); + let name = dateFormat(now, "yyyy_mm_dd_HHMMss_")+str; + + name = this.replaceAll(name, " ", "_"); + name = name.toLowerCase(); + name = name+'.php'; + + return name; + + } + //------------------------------------------------------- + getMigrationTableName(str:string) + { + let name = ''; + + name = str.toLowerCase(); + name = this.replaceAll(name, " ", '_'); + + return name; + + } + //------------------------------------------------------- + getDerivedVariables() + { + + let params = { + namespace: '', + target_dir: '', + table_name: '', + class_name: '', + file_name: '', + }; + + + let namespace = 'VaahCms'; + let target_dir = './VaahCms/'; + + + switch (this.inputs.for) { + + case 'module': + namespace = namespace+'\\Modules\\'+this.args.module; + target_dir = target_dir+'Modules/'+this.args.module; + break; + + case 'theme': + namespace = namespace+'\\Themes\\'+this.args.theme; + target_dir = target_dir+'Themes/'+this.args.theme; + break; + + } + + + switch (this.args.type) { + + case 'migration': + + target_dir = target_dir+'/Database/Migrations'; + + params.file_name = this.getMigrationFileName(this.args.name); + params.class_name = this.getClassName(this.args.name); + params.table_name = this.getMigrationTableName(this.args.name); + + break; + + case 'seed': + + namespace = namespace+'\\Database\\Seeders'; + target_dir = target_dir+'/Database/Seeders'; + params.file_name = this.args.name+'TableSeeder.php'; + + break; + + case 'model': + + namespace = namespace+'\\Models'; + target_dir = target_dir+'/Models'; + + break; + + case 'controller': + + params.file_name = this.args.name+'Controller.php'; + + namespace = namespace+'\\Http\\Controllers'; + target_dir = target_dir+'/Http/Controllers'; + + if(this.flags.frontend) + { + namespace = namespace+'\\Frontend'; + target_dir = target_dir+'/Frontend'; + } else if(this.flags.backend) + { + namespace = namespace+'\\Backend'; + target_dir = target_dir+'/Backend'; + } + + break; + + case 'middleware': + + namespace = namespace+'\\Http\\Middleware'; + target_dir = target_dir+'/Http/Middleware'; + + break; + + case 'observer': + + params.file_name = this.args.name+'Observer.php'; + + namespace = namespace+'\\Observers'; + target_dir = target_dir+'/Observers'; + + break; + + case 'trait': + + namespace = namespace+'\\Traits'; + target_dir = target_dir+'/Traits'; + + break; + + case 'test': + + namespace = namespace+'\\Tests'; + target_dir = target_dir+'/Tests'; + + break; + + case 'event': + + namespace = namespace+'\\Events'; + target_dir = target_dir+'/Events'; + + break; + + case 'listener': + + params.file_name = this.args.name+'Listener.php'; + + namespace = namespace+'\\Listeners'; + + target_dir = target_dir+'/Listeners'; + + break; + + + case 'mail': + + params.file_name = this.args.name+'Mail.php'; + + namespace = namespace+'\\Mails'; + + target_dir = target_dir+'/Mails'; + + break; + + + case 'notification': + + params.file_name = this.args.name+'Notification.php'; + + namespace = namespace+'\\Notifications'; + + target_dir = target_dir+'/Notifications'; + + break; + + case 'view': + + params.file_name = this.args.name+'.blade.php'; + target_dir = target_dir+'/Resources/views'; + + if(this.flags.frontend) + { + target_dir = target_dir+'/frontend/pages'; + } else if(this.flags.backend) + { + target_dir = target_dir+'/backend/pages'; + } + + break; + + + } + + + params.namespace = namespace; + params.target_dir = target_dir; + + + return params; + + } + //------------------------------------------------------- + + //------------------------------------------------------- + //------------------------------------------------------- + //------------------------------------------------------- + + +}