UNPKG

rename-module

Version:

Rename project module, name and text

348 lines (332 loc) 11.4 kB
/** * @file Contains main logic for rename modules * @author Leonardo de Freitas Oliveira <leo_defoliveira@live.com> * @version 2.0.0 */ // rename-module -p 'C:\Users\leonardo.oliveira\Documents\company-type' -o 'company-type' -n 'new-name' -pf '{{' -sf '}}' var fs = require('fs-extra'); var _ = require('lodash'); var obj = { /** * Override all text occurrences and its variations by another pattern, in file content and name and in folder name. * @param {object} cfg - object containing config values */ replace: function(cfg) { obj.log(cfg, arguments.callee); var arr = cfg.oldKeys || cfg.oldKey; if (Array.isArray(arr)) { var arrNew = cfg.newKeys || cfg.newKey; for (var i = 0; i < arr.length; i++) { var cfgP = _.cloneDeep(cfg); cfgP.oldKey = arr[i]; cfgP.newKey = arrNew[i]; var result = obj.replace(cfgP); if (result) { cfgP.data = result; } } } if (cfg.root) { var cfgC = _.cloneDeep(cfg); var stats = fs.statSync(cfgC.root); cfgC.isDir = stats.isDirectory(); if (cfgC.isDir) { return obj.replaceFolder(cfgC); } else { return obj.replaceFile(cfgC); } } else { return obj.replaceData(cfgC); } }, /** * Override all text occurrences and its variations by another pattern, folder name and replaces the same in child file and folders. * @param {object} cfg - object containing config values * @param {string} root - Indicates the full parent folder path. * @param {string} oldKey - Text to be overridden. * @param {string} newKey - Text to override old text. * @param {string} prefix - Prefix to be considered in previous value . * @param {string} suffix - Suffix to be considered in previous value . */ replaceFolder: function(cfg) { var cfgP = _.cloneDeep(cfg); obj.log(cfgP, arguments.callee); var root = cfgP.root; var oldKey = cfgP.oldKey; var newKey = cfgP.newKey; cfgP.isDir = false; root = root.endsWith(obj.getPathSeparator(root)) ? root : root + obj.getPathSeparator(root); var children = fs.readdirSync(root); for (var childIndex in children) { var child = children[childIndex]; var cfgC = _.cloneDeep(cfgP); cfgC.root = root + child obj.replace(cfgC); } var name = obj.replaceName(cfgP); return name; }, /** * Override all text occurrences and its variations by another pattern, in file content and name. * @param {object} config - object containing config values */ replaceFile: function(cfg) { var cfgC = _.cloneDeep(cfg); obj.log(cfgC, arguments.callee); cfgC.isDir = false; var body = obj.replaceContent(cfgC); var name = obj.replaceName(cfgC); return name; }, /** * Override all text occurrences and its variations by another pattern, in data content * @param {object} cfg - object containing config values * @param {string} data - Indicates the data to be modified. * @param {string} key - Indicates the config key do be considered. * @param {string} oldKey - Text to be overridden. * @param {string} newKey - Text to override old text. * @param {string} prefix - Prefix to be considered in previous value . * @param {string} suffix - Suffix to be considered in previous value . * @returns {string} */ replaceData: function(cfg) { var cfgC = _.cloneDeep(cfg); cfgC.key = cfg.key || '__'; var data = (cfgC.data)? cfgC.data.toString(): null; var oldKey = cfgC.oldKey; var newKey = cfgC.newKey; var prefix = cfgC.prefix; var suffix = cfgC.suffix; var config = obj.getConfig(cfgC); var oldVals = obj.getStringFormats(oldKey, config.__ || config); var newVals = obj.getStringFormats(newKey, config.__ || config); var body = data; for (var index in oldVals) { body = body.replaceString( data, oldVals[index], newVals[index], prefix, suffix ); } return body; }, /** * Get the slash used in root path / or \ * @param {string} root - Indicates the full parent folder path. * @returns {string} */ getPathSeparator: function(root) { if (!obj.pathSeparator) { obj.pathSeparator = root.includes('/') ? '/' : '\\'; } return obj.pathSeparator; }, /** * Override all text occurrences and its variations by another pattern, in file content. * @param {object} config - object containing config values * @param {string} root - Indicates the file path * @param {string} oldKey - Text to be overridden. * @param {string} newKey - Text to override old text. * @param {string} prefix - Prefix to be considered in previous value . * @param {string} suffix - Suffix to be considered in previous value . */ replaceContent: function(cfg) { var cfgC = _.cloneDeep(cfg); var root = cfgC.root; var oldKey = cfgC.oldKey; var newKey = cfgC.newKey; var prefix = cfgC.prefix; var suffix = cfgC.suffix; var config = obj.getConfig(root, false); var oldVals = obj.getStringFormats( oldKey, config.body || config.__ || config ); var newVals = obj.getStringFormats( newKey, config.body || config.__ || config ); var body = fs.readFileSync(root).toString(); for (var index in oldVals) { body = obj.replaceString( body, oldVals[index], newVals[index], prefix, suffix ); } fs.writeFileSync(root, body); return body; }, /** * Override all text occurrences and its variations by another pattern, in folder. * @param {object} cfg - object containing config values * @param {string} root - Indicates the folder, it script will work inside. * @param {string} isDir - Indicates if it is about a folder. * @param {string} oldKey - Text to be overridden. * @param {string} newKey - Text to override old text. * @param {string} prefix - Prefix to be considered in previous value . * @param {string} suffix - Suffix to be considered in previous value . * @returns {string} */ replaceName: function(cfg) { var cfgC = _.cloneDeep(cfg); var root = cfgC.root; var oldKey = cfgC.oldKey; var newKey = cfgC.newKey; var prefix = cfgC.prefix; var suffix = cfgC.suffix; var isDir = cfgC.isDir; var separator = obj.getPathSeparator(root); var config = obj.getConfig(cfgC); var oldNames = obj.getStringFormats( oldKey, config.name || config.__ || config ); var newNames = obj.getStringFormats( newKey, config.name || config.__ || config ); var nameS = root.split(separator); var oldName = (newName = nameS[nameS.length - 1]); var folder = nameS.slice(0, nameS.length - 1).join(separator) + separator; for (var index in oldNames) { newName = obj.replaceString( newName, oldNames[index], newNames[index], prefix, suffix ); } if (newName != oldName) { fs.renameSync(folder + oldName, folder + newName); } return newName; }, /** * Override all text occurrences and its variations by another pattern, in folder. * @param {object} cfg - object containing config values * @param {string} value - file / folder name or path. * @param {string} isDir - Indicate if previous parameter is about a folder. * @param {string} key - A custom config key to be considered instead of dir our file config * @returns {object} */ getConfig: function(cfg) { var value = cfg.value; var isDir = cfg.isDir; var key = cfg.key; var config = null; if (key) { config = (obj.cfg) ? obj.cfg[key] || obj.cfg.__ : {}; return config; } var ext = null; if (!obj.cfg) { obj.cfg = require(process.env.RENAME_MODULE_CFG_FILE || '../assets/cfg.json'); } if (!isDir && value) { var splited = value.split('.'); ext = splited[splited.length - 1]; } if (isDir) { config = (obj.cfg) ? obj.cfg.dir || obj.cfg.__ : {}; } else { if (!obj.cfg.file) { config = (obj.cfg) ? obj.cfg.__ : {}; } else { if(obj.cfg){ for (var index in obj.cfg.file) { if (index.replace('__', '') == ext) { config = obj.cfg.file[index]; if (typeof config == 'string') { config = obj.cfg.template[config]; } } } config = config || obj.cfg.file.__; } } } return config; }, /** * Override all text occurrences in string. * @param {string} str - string to be modified. * @param {string} prev - String/ Regex to be overrided. * @param {string} next - String to overrid. * @param {string} prefix - Prefix to be considered in previous value . * @param {string} suffix - Suffix to be considered in previous value . * @returns {object} */ replaceString: function(str, prev, next, prefix, suffix) { var fullPrev = (prefix || '') + prev + (suffix || ''); if (prev instanceof RegExp) { return str.replace(new RegExp(fullPrev), next); } return str.split(fullPrev).join(next); }, /** * Get all string variations. * @param {string} val - Pattern to be extracted in many formats. * @param {string} formats - List of formats used to convert the text. * @param {string} format - Unique format to convert the text . * @returns {array<string>} */ getStringFormats: function(val, formats, format) { if (!format && formats) { var vals = []; for (index in formats) { format = formats[index]; vals.push(obj.getStringFormats(val, null, format)); } return vals; } switch (format) { case 'upperFirst camelCase': return _.upperFirst(_.camelCase(val)); case 'camelCase': return _.camelCase(val); case 'lowerCase': return val.toLowerCase(); case 'upperCase': return val.toUpperCase(); case 'capitalize': return _.capitalize(val); case 'snakeCase': return _.snakeCase(val); case 'snakeCase upperCase': return _.snakeCase(val).toUpperCase(); case 'snakeCase lowerCase': return _.snakeCase(val).toLowerCase(); case 'kebabCase': return _.kebabCase(val); case 'kebabCase upperCase': return _.kebabCase(val).toUpperCase(); case 'kebabCase lowerCase': return _.kebabCase(val).toLowerCase(); case 'upperFirst': return _.upperFirst(val); default: if (format.startsWith('regex')) { return new RegExp(format.replace('regex', '').trim()); } return val; } }, /** * Log method * @param {object} config - config value to be logged * @param {string} method - method it displayed the log */ log: function(config, method){ // console.log(method.name + ' -> '+ JSON.stringify(config)); } }; module.exports = obj;