next-porto-cli
Version:
A cli for next.js to scaffold your application using porto architecture
276 lines (275 loc) • 9.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStylesPath = exports.getActionsPath = exports.getHooksPath = exports.getHelpersPath = exports.getComponentsPath = exports.getConfigsPath = exports.getAssetsPath = exports.getApiPath = exports.getPagesPath = exports.getContainerPath = exports.getSectionPath = exports.getShipPath = exports.getAppContainersPath = exports.getSrcPath = exports.getBasePath = exports.getPath = exports.shipScaffolding = exports.containerScafffolding = exports.APP_ROOT = exports.PKG_ROOT = void 0;
/* eslint-disable node/no-extraneous-import */
/* eslint-disable @typescript-eslint/no-this-alias */
/* eslint-disable unicorn/no-this-assignment */
/* eslint-disable no-prototype-builtins */
const zod_1 = require("zod");
const path = require("node:path");
const str = require("./string");
const _ = require("lodash");
/**
* ----------------------------------------------------------------------
*/
const distPath = path.dirname(__filename);
exports.PKG_ROOT = path.join(distPath, '../');
// * context of parent this
const _this = this;
// * scaffold containers paths object definition
const containerPathsObj = zod_1.z.object({
section: zod_1.z.string(),
container: zod_1.z.string(),
// * paths
srcPath: zod_1.z.string(),
appContainersPath: zod_1.z.string().optional(),
sectionPath: zod_1.z.string(),
containerPath: zod_1.z.string(),
pagesPath: zod_1.z.string(),
pagesApiPath: zod_1.z.string(),
assetsPath: zod_1.z.string(),
configsPath: zod_1.z.string(),
componentsPath: zod_1.z.string(),
helpersPath: zod_1.z.string(),
hooksPath: zod_1.z.string(),
actionsPath: zod_1.z.string(),
stylesPath: zod_1.z.string(),
});
const pagePathObj = containerPathsObj
.partial()
.pick({ section: true, container: true, pagesPath: true, pagesApiPath: true });
// * scaffold ship paths object definition
const shipPatshObj = zod_1.z.object({
hooks: zod_1.z.string(),
config: zod_1.z.string(),
layouts: zod_1.z.string(),
styles: zod_1.z.string(),
helpers: zod_1.z.string(),
components: zod_1.z.string(),
});
// * path object
const PathObj = zod_1.z.object({
path: zod_1.z.string(),
section: zod_1.z.string().optional(),
container: zod_1.z.string().optional(),
});
// * extract params object from path object
const ParamsObj = PathObj.pick({ section: true, container: true });
// * -----------------------------------------------------------------------------
/**
* Scaffold the container paths
*
* @param section section name
* @param container container name
* @returns ContainerPathsType
*/
const containerScafffolding = (section, container) => {
return {
section,
container,
srcPath: getPath('src'),
sectionPath: getPath('section', { section }),
containerPath: getPath('container', { section, container }),
assetsPath: getPath('assets', { section, container }),
configsPath: getPath('configs', { section, container }),
componentsPath: getPath('components', { section, container }),
helpersPath: getPath('helpers', { section, container }),
hooksPath: getPath('hooks', { section, container }),
actionsPath: getPath('actions', { section, container }),
stylesPath: getPath('styles', { section, container }),
pagesPath: path.resolve(getPath('pages', { section, container }), container.toLowerCase()),
pagesApiPath: path.resolve(getPath('api', { section, container }), container.toLowerCase()),
};
};
exports.containerScafffolding = containerScafffolding;
/**
* Scaffold the ship paths
* @param _projectDir string value for project directory [optional]
* @returns ShipPathsType
*/
const shipScaffolding = (_projectDir) => {
return {
hooks: path.resolve(getShipPath(_projectDir), 'Hooks'),
config: path.resolve(getShipPath(_projectDir), 'Config'),
layouts: path.resolve(getShipPath(_projectDir), 'Layouts'),
styles: path.resolve(getShipPath(_projectDir), 'Styles'),
helpers: path.resolve(getShipPath(_projectDir), 'Helpers'),
components: path.resolve(getShipPath(_projectDir), 'Components'),
};
};
exports.shipScaffolding = shipScaffolding;
/**
* Get the path base on the section & container
* @param _funcName function path short name
* @param _params optional parameter for {section, container}
* @returns string
*/
const getPath = (_funcName, _params) => {
// * format the function path string to proper case
const capitalizeFunc = _.capitalize(_funcName);
// * for path short names
const callFuncString = `get${capitalizeFunc}Path`;
if (!_this.hasOwnProperty(callFuncString) &&
!(_this[callFuncString] instanceof Function)) {
// * empty string
throw new Error(`Invalid function call "${callFuncString}".`);
}
// * if container params exists
if (_params) {
// * destruct optional params
const { section, container } = _params;
// * return path with params
return ['', null, undefined].includes(container)
? _this[callFuncString](section)
: _this[callFuncString](section, container);
}
// * return function without params
return _this[callFuncString]();
};
exports.getPath = getPath;
/**
* Get base application path, includes relative path if parameter exists
* @param _path relative path from the root directory
* @returns string
*/
const getBasePath = (_path) => {
return _path
? path.join(process.cwd(), path.sep, str.trimSlashes(_path))
: process.cwd();
};
exports.getBasePath = getBasePath;
/**
* Get src path
* @returns string
*/
const getSrcPath = () => {
return getBasePath('src');
};
exports.getSrcPath = getSrcPath;
/**
* Get base container path
* @returns string
*/
const getAppContainersPath = () => {
return path.join(getSrcPath(), `${path.sep}Containers`);
};
exports.getAppContainersPath = getAppContainersPath;
/**
* Get ship path
* @param _projectDir string value of the project directory [optional]
* @returns string
*/
const getShipPath = (_projectDir) => {
return path.join(getBasePath(_projectDir), 'src', `${path.sep}Ship`);
};
exports.getShipPath = getShipPath;
/**
* Get section path by name
* @param _section section name
* @returns string
*/
const getSectionPath = (_section) => {
return path.join(getAppContainersPath(), path.sep, str.trimSlashes(_section));
};
exports.getSectionPath = getSectionPath;
/**
* Get specific container path
* @param _section section name
* @param _container container name
* @returns string
*/
const getContainerPath = (_section, _container) => {
return path.join(getSectionPath(_section), path.sep, str.trimSlashes(_container));
};
exports.getContainerPath = getContainerPath;
/**
* Get pages path
* @param _section section name
* @param _container container name
* @returns string
*/
const getPagesPath = (_section, _container) => {
return path.join(getContainerPath(_section, _container), `${path.sep}pages`);
};
exports.getPagesPath = getPagesPath;
/**
* Get page api path
* @param _section section name
* @param _container container name
* @returns string
*/
const getApiPath = (_section, _container) => {
return path.join(getPagesPath(_section, _container), `${path.sep}api`);
};
exports.getApiPath = getApiPath;
/**
* Get asset path of the container
* @param _section section name
* @param _container container name
* @returns string
*/
const getAssetsPath = (_section, _container) => {
return path.join(getContainerPath(_section, _container), `${path.sep}assets`);
};
exports.getAssetsPath = getAssetsPath;
/**
* Get config path of the container
* @param _section section name
* @param _container container name
* @returns string
*/
const getConfigsPath = (_section, _container) => {
return path.join(getContainerPath(_section, _container), `${path.sep}configs`);
};
exports.getConfigsPath = getConfigsPath;
/**
* Get components path of the container
* @param _section section name
* @param _container container name
* @returns string
*/
const getComponentsPath = (_section, _container) => {
return path.join(getContainerPath(_section, _container), `${path.sep}components`);
};
exports.getComponentsPath = getComponentsPath;
/**
* Get helpers path of the container
* @param _section section name
* @param _container container name
* @returns string
*/
const getHelpersPath = (_section, _container) => {
return path.join(getContainerPath(_section, _container), `${path.sep}helpers`);
};
exports.getHelpersPath = getHelpersPath;
/**
* Get hooks path of the container
* @param _section section name
* @param _container container name
* @returns string
*/
const getHooksPath = (_section, _container) => {
return path.join(getContainerPath(_section, _container), `${path.sep}hooks`);
};
exports.getHooksPath = getHooksPath;
/**
* Get actions path of the container
* @param _section section name
* @param _container container name
* @returns string
*/
const getActionsPath = (_section, _container) => {
return path.join(getContainerPath(_section, _container), `${path.sep}actions`);
};
exports.getActionsPath = getActionsPath;
/**
* Get styles path of the container
* @param _section section name
* @param _container container name
* @returns string
*/
const getStylesPath = (_section, _container) => {
return path.join(getContainerPath(_section, _container), `${path.sep}styles`);
};
exports.getStylesPath = getStylesPath;
exports.APP_ROOT = path.resolve(getBasePath());