UNPKG

@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.

156 lines (155 loc) 5.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteFile = exports.renameSync = exports.removeDir = exports.isEmptyDir = exports.writeJsonFile = exports.copyDir = exports.createDir = exports.dirFileExists = exports.writeFile = void 0; const tslib_1 = require("tslib"); const os_1 = tslib_1.__importDefault(require("os")); const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); const path_1 = tslib_1.__importDefault(require("path")); const chalk_1 = tslib_1.__importDefault(require("chalk")); const helpers_1 = require("./helpers"); /** * @description : method to check file exists or not * @param {string} p : path of file * @returns {boolean} true if file exists else false */ function dirFileExists(p) { return fs_extra_1.default.existsSync(p); } exports.dirFileExists = dirFileExists; /** * @description : method to check if given path is directory or not * @param {string} path : path of file or directory * @returns {boolean} true if directory else false */ function isDirectory(path) { return fs_extra_1.default.lstatSync(path).isDirectory(); } /** * @description : method to rename directory or filename. * @param {string} currPath : current directory or filename * @param {string} newPath : new directory or filename */ function renameSync(currPath, newPath) { try { fs_extra_1.default.renameSync(currPath, newPath); } catch (e) { console.error(chalk_1.default.red(`${e}: Failed renmaing process.`)); throw e; } } exports.renameSync = renameSync; /** * @description : method to remove folder * @param {string} dirPath : folder name */ function removeDir(dirPath) { try { fs_extra_1.default.rmSync(dirPath, { recursive: true, force: true }); } catch (e) { console.error(chalk_1.default.red('error removing directory')); throw e; } } exports.removeDir = removeDir; /** * @description : method to remove file * @param {string} filePath : file path */ function deleteFile(filePath) { try { fs_extra_1.default.unlinkSync(filePath); } catch (e) { console.error(chalk_1.default.red('error removing file')); throw e; } } exports.deleteFile = deleteFile; /** * @description : method to create folder name * @param {string} dir : folder name */ function createDir(dir) { try { fs_extra_1.default.mkdirSync(dir); } catch (e) { console.error(chalk_1.default.red('error creating directory')); throw e; } } exports.createDir = createDir; /** * @description : method to copy a directory from source to destination * @param {string} sourcePath : source path of directory to be copied * @param {string} destPath : destination path * @param {(string | RegExp)[]} exclusions : list of files and folders name need to be excluded during copy * @param {(string | RegExp)[]} inclusions : list of files and folders name need to be included during copy */ function copyDir(sourcePath, destPath, exclusions = [], inclusions = []) { if (!isDirectory(sourcePath)) { throw new Error('Source path is not a directory.'); } if (exclusions.length > 0 && inclusions.length > 0) { throw new Error('You cannot provide both exclusions and inclusions.'); } const items = fs_extra_1.default.readdirSync(sourcePath); items.forEach(item => { const sourceItem = path_1.default.join(sourcePath, item); const destItem = path_1.default.join(destPath, item); if (exclusions.some(pattern => pattern instanceof RegExp ? pattern.test(item) : item === pattern) || (inclusions.length > 0 && !inclusions.some(pattern => pattern instanceof RegExp ? pattern.test(item) : item === pattern))) { return; } if (isDirectory(sourceItem)) { fs_extra_1.default.ensureDirSync(destItem); copyDir(sourceItem, destItem, exclusions, inclusions); } else { fs_extra_1.default.copySync(sourceItem, destItem); } }); } exports.copyDir = copyDir; /** * @description : method to create file at given location * @param {string} filePath : location where file need to be placed * @param {string | NodeJS.ArrayBufferView} data : data need to be written in file */ function writeFile(filePath, data) { fs_extra_1.default.writeFileSync(filePath, data); } exports.writeFile = writeFile; /** * @description : method to write json file at given locaion * @param {string} jsonFilePath : location where json file need to be placed * @param {Json | BasePackageJson} json : json data need to be written in json file */ function writeJsonFile(jsonFilePath, json) { try { writeFile(jsonFilePath, JSON.stringify(json, null, 2) + os_1.default.EOL); } catch (e) { console.error(chalk_1.default.red(`[${(0, helpers_1.currentDateTime)()}] - Error copying file ${jsonFilePath}. ${e}`)); throw e; } } exports.writeJsonFile = writeJsonFile; /** * @description : method to check if directory is empty or not * @param {string} directoryPath : location of directory * @returns {boolean} true if empty else false */ function isEmptyDir(directoryPath) { try { const files = fs_extra_1.default.readdirSync(directoryPath); return files.length === 0; } catch (e) { return false; } } exports.isEmptyDir = isEmptyDir;