UNPKG

next-porto-cli

Version:

A cli for next.js to scaffold your application using porto architecture

136 lines (135 loc) 4.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); require("module-alias/register"); const path = require("node:path"); const path_1 = require("@helpers/path"); const core_1 = require("@oclif/core"); const container_1 = require("@generators/container"); const filesystem_1 = require("@helpers/filesystem"); class Page extends core_1.Command { /** * Run the command * @returns void */ async run() { var _a, _b; // * let force = false; // * let pagePath = null; // * get user input for flags let { flags } = await this.parse(Page); // * if container is missing in the command flag // * run manual prompt if (!Object.keys(flags).includes('container') || ['', null, undefined].includes(flags.container)) { // * trigger manual prompt // * section const section = await core_1.CliUx.ux.prompt('Enter section name', { default: 'AppSection', required: true, }); // * container const container = await core_1.CliUx.ux.prompt('Enter container name', { required: true, }); // * page const page = await core_1.CliUx.ux.prompt('Enter page path', { required: true, }); // * api const api = await core_1.CliUx.ux.confirm('Would you like to include api page?'); // * override flags k/v flags = { section, container, page, api, force, json: true, }; // * scaffold paths for container page pagePath = { section, container, pagesPath: path.resolve((0, path_1.getPath)('pages', { section, container }), container.toLowerCase(), page), }; // * append api path if (api) { pagePath.pagesApiPath = path.resolve((0, path_1.getPath)('api', { section, container }), container.toLowerCase(), page); } } // * flags complete if (pagePath === null) { // * scaffold paths for container page pagePath = { section: flags.section, container: flags.container, pagesPath: path.resolve((0, path_1.getPath)('pages', { section: flags.section, container: flags.container, }), (_a = flags === null || flags === void 0 ? void 0 : flags.container) === null || _a === void 0 ? void 0 : _a.toLowerCase(), flags === null || flags === void 0 ? void 0 : flags.page), }; // * append api path if (flags.api) { pagePath.pagesApiPath = path.resolve((0, path_1.getPath)('api', { section: flags.section, container: flags.container }), (_b = flags === null || flags === void 0 ? void 0 : flags.container) === null || _b === void 0 ? void 0 : _b.toLowerCase(), flags.page); } } // * override exisitng container if ((0, filesystem_1.fileExist)(pagePath.pagesPath) && flags.force) { const deleteExistingContainer = await core_1.CliUx.ux.confirm('This will delete the exisitng container and all of its content, continue?'); // * delete container if (deleteExistingContainer) { // * container page (0, filesystem_1.forceDelete)(pagePath.pagesPath); if (flags.api) { // * container api page (0, filesystem_1.forceDelete)(pagePath.pagesApiPath); } } else { this.exit(0); } } // * generate page paths (0, container_1.generateFromPaths)(pagePath); } } exports.default = Page; // * disable variable argument validation Page.strict = false; // * Page.description = 'Create a page & page api files for container'; // * command flags Page.flags = { section: core_1.Flags.string({ char: 's', required: true, default: 'AppSection', description: 'Section name', }), container: core_1.Flags.string({ char: 'c', required: false, dependsOn: ['section'], description: 'Container name', }), page: core_1.Flags.string({ char: 'p', required: false, dependsOn: ['container'], description: 'Page path', }), api: core_1.Flags.boolean({ char: 'a', required: false, default: false, description: 'Will include api page files', }), force: core_1.Flags.boolean({ char: 'f', required: false, default: false, description: 'Override the existing page files', }), };