UNPKG

next-porto-cli

Version:

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

169 lines (168 loc) 5.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseConfigFile = exports.generateTsConfig = exports.generateJsConfig = exports.generateConfig = exports.checkConfigFile = void 0; /* eslint-disable no-prototype-builtins */ /* eslint-disable @typescript-eslint/no-this-alias */ /* eslint-disable unicorn/no-this-assignment */ /* eslint-disable node/no-extraneous-import */ require("module-alias/register"); const zod_1 = require("zod"); const _ = require("lodash"); const path = require("node:path"); const index_1 = require("@helpers/index"); // * context of parent this const _this = this; // * const DEFAULT_CONFIG = { compilerOptions: { baseUrl: '.', paths: { '@Containers/*': ['./src/Containers/*'], '@Ship/*': ['./src/Ship/*'], }, }, include: ['./pages', './src/**/*'], exclude: ['node_modules'], }; // * Object const configObj = zod_1.z.object({ compilerOptions: zod_1.z.any(), include: zod_1.z.array(zod_1.z.string()), exclude: zod_1.z.array(zod_1.z.string()), }); /** * check for existing config json file on the project root * @param _projectPath path of the project directory * @returns boolean */ const checkConfigFile = async (_projectPath) => { let configFunc = ''; const configs = ['jsconfig.json', 'tsconfig.json']; // * for (const config of configs) { // * if (index_1.filesystem.fileExist(path.resolve(_projectPath, config))) { configFunc = 'generate' + _.capitalize(_.split(config, 'config')[0]) + 'Config'; break; } } return configFunc === '' ? 'generateJsConfig' : configFunc; }; exports.checkConfigFile = checkConfigFile; /** * Run config function for generating config file * @param _projectPath path of the project directory * @return void */ const generateConfig = async (_projectPath) => { const configFunc = await checkConfigFile(_projectPath); if (_this.hasOwnProperty(configFunc) && _this[configFunc] instanceof Function) { await _this[configFunc](_projectPath); } }; exports.generateConfig = generateConfig; /** * Generate jsconfig.json config file * @param _projectPath path to the project directory * @return void */ const generateJsConfig = async (_projectPath) => { /** ** Usually jsconfig does not exist in a newly create project ** so we will need to check the config file exists. */ const configFileExist = index_1.filesystem.fileExist(path.resolve(_projectPath, 'jsconfig.json')); // * if (configFileExist) { // * const parsedConfigFile = await parseConfigFile(path.resolve(_projectPath, 'jsconfig.json')); // * if (parsedConfigFile !== undefined) { // * merge data from default config parsedConfigFile.compilerOptions = { ...DEFAULT_CONFIG.compilerOptions, ...parsedConfigFile.compilerOptions, }; // * parsedConfigFile.include = [ ...new Set([...parsedConfigFile.include, ...DEFAULT_CONFIG.include]), ]; // * parsedConfigFile.exclude = [ ...new Set([...parsedConfigFile.exclude, ...DEFAULT_CONFIG.exclude]), ]; } // * create file await index_1.filesystem.write({ filename: 'jsconfig.json', path: path.resolve(_projectPath), content: JSON.stringify(parsedConfigFile, null, 2), }); } else { // * create new config file await index_1.filesystem.write({ filename: 'jsconfig.json', path: path.resolve(_projectPath), content: JSON.stringify(DEFAULT_CONFIG, null, 2), }); } }; exports.generateJsConfig = generateJsConfig; /** * Generate tsconfig.json config file * @param _projectPath path to the project directory * @return void */ const generateTsConfig = async (_projectPath) => { // * const configFileExist = index_1.filesystem.fileExist(path.resolve(_projectPath, 'tsconfig.json')); // * if (configFileExist) { // * const parsedConfigFile = await parseConfigFile(path.resolve(_projectPath, 'tsconfig.json')); // * if (parsedConfigFile !== undefined) { // * merge data from default config parsedConfigFile.compilerOptions = { ...DEFAULT_CONFIG.compilerOptions, ...parsedConfigFile.compilerOptions, }; // * parsedConfigFile.include = [ ...new Set([...parsedConfigFile.include, ...DEFAULT_CONFIG.include]), ]; // * parsedConfigFile.exclude = [ ...new Set([...parsedConfigFile.exclude, ...DEFAULT_CONFIG.exclude]), ]; } // * create file await index_1.filesystem.write({ filename: 'tsconfig.json', path: path.resolve(_projectPath), content: JSON.stringify(parsedConfigFile, null, 2), }); } else { // * create new config file await index_1.filesystem.write({ filename: 'tsconfig.json', path: path.resolve(_projectPath), content: JSON.stringify(DEFAULT_CONFIG, null, 2), }); } }; exports.generateTsConfig = generateTsConfig; /** * Parse a json config file content into json object * @param _pathToJsonFile path to json config file * @returns any | undefined */ const parseConfigFile = async (_pathToJsonFile) => { const content = await index_1.filesystem.readFile(_pathToJsonFile); return JSON.parse(content); }; exports.parseConfigFile = parseConfigFile;