@coderebus/react-archetype
Version:
React-archetype is a cli tool (or a generator) that will generate apps based on the rendering pattern (CSR,SSR, SSG, ISR etc) of your choice.
55 lines (54 loc) • 2.23 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyCommandType = exports.replaceString = exports.mergeJsons = exports.isObject = void 0;
const tslib_1 = require("tslib");
const deepmerge_1 = tslib_1.__importDefault(require("deepmerge"));
/**
* @description : method to check whether an item is object or not
* @param {unknown} item : any data item
* @returns {boolean} true if object, false otherwise
*/
const isObject = (item) => {
return Boolean(item) && typeof item === 'object' && !Array.isArray(item);
};
exports.isObject = isObject;
/**
* @description : method to merge two JSON objects
* @param {Json} masterJson : parent JSON object
* @param {Json} slaveJson : slave JSON object to be merged
* @returns {Json} merged JSON object
*/
const mergeJsons = (masterJson, slaveJson) => {
const json = (0, deepmerge_1.default)(masterJson, slaveJson);
return json;
};
exports.mergeJsons = mergeJsons;
/**
* @description : method to replace a string from an object
* @param {T} input : source object or string
* @param {string | RegExp} regexStr : string to be replaced
* @param {string} replacedStr : string to replace with
* @returns {T} updated source data
*/
function replaceString(input, regexStr, replacedStr = '') {
const regex = new RegExp(regexStr, 'gi');
const str = JSON.stringify(input).replace(regex, replacedStr);
return JSON.parse(str);
}
exports.replaceString = replaceString;
/**
* @description : method to create the correct command type as per user input
* @param {PackageJson} obj : input packagejson in which command type needs to be applied
* @param {TCommandType} commandType : package manager (npm, yarn, pnpm)
* @returns {PackageJson} updated package.json object with the correct command type applied
*/
function applyCommandType(obj, commandType) {
const packageString = JSON.stringify(obj);
const regex = /{commandType}/gi;
let str = packageString.replace(regex, commandType);
if (commandType === 'npm') {
str = packageString.replace(regex, `${commandType} run`);
}
return JSON.parse(str);
}
exports.applyCommandType = applyCommandType;
;