UNPKG

minecraft-utils-shared

Version:

Shared utils for Minecraft Bedrock / Forge development related utilities.

194 lines (182 loc) 5.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _chalk = _interopRequireDefault(require("chalk")); var _fsExtra = _interopRequireDefault(require("fs-extra")); var _glob = _interopRequireDefault(require("glob")); var _path = _interopRequireDefault(require("path")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * @file Minecraft Utils Shared - Files * @license Apache-2.0 * @author Markus@Bordihn.de (Markus Bordihn) */ /** * @param {string} source * @param {string} target */ const copyFileIfNotExists = (source, target) => { if (_fsExtra.default.existsSync(source) && !_fsExtra.default.existsSync(target)) { const parentDir = _path.default.dirname(target); if (parentDir && !_fsExtra.default.existsSync(parentDir)) { _fsExtra.default.ensureDirSync(parentDir); } _fsExtra.default.copyFileSync(source, target); } }; /** * @param {string} source * @param {string} target */ const copyFolderIfNotExists = (source, target) => { if (_fsExtra.default.existsSync(source) && !_fsExtra.default.existsSync(target)) { _fsExtra.default.copySync(source, target); } }; /** * @param {string} source * @param {string} target */ const createBackupFile = (source, target) => { if (_fsExtra.default.existsSync(source)) { if (!target || source == target) { _fsExtra.default.copyFileSync(source, `${source}.bak`); } else { _fsExtra.default.copyFileSync(source, target); } } }; /** * @param {string} folderPath * @param {string} name * @param {string} content */ const createFileIfNotExists = (folderPath, name, content = '') => { const pathName = name ? _path.default.join(folderPath, name) : folderPath; if (!_fsExtra.default.existsSync(pathName)) { _fsExtra.default.writeFileSync(pathName, content, error => { if (error) { return console.error(_chalk.default.red('Error creating new file', pathName, ':', error)); } }); } }; /** * @param {string} folderPath * @param {string} name */ const createFolderIfNotExists = (folderPath, name) => { const pathName = name ? _path.default.join(folderPath, name) : folderPath; if (!_fsExtra.default.existsSync(pathName)) { _fsExtra.default.mkdirSync(pathName, { recursive: true }, error => { if (error) { return console.error(_chalk.default.red('Error creating new folder', pathName, ':', error)); } }); } }; /** * @param {string} name * @returns {string} */ const normalizeFileName = (name = '') => { return name.replace(/\s+/g, '_').replace(':', '__').replace(/[^a-zA-Z0-9_.-]/g, ''); }; /** * @param {string} oldPath * @param {string} newPath * @param {boolean} overwrite */ const renameFile = (oldPath, newPath, overwrite = false) => { _fsExtra.default.copySync(oldPath, newPath, { overwrite: overwrite }); if (_fsExtra.default.existsSync(newPath)) { _fsExtra.default.removeSync(oldPath); } }; /** * @param {string} oldPath * @param {string} newPath * @param {boolean} overwrite */ const renameFileIfExists = (oldPath, newPath, overwrite = false) => { if (_fsExtra.default.existsSync(oldPath)) { renameFile(oldPath, newPath, overwrite); } }; /** * @param {string} search_path * @param {string} pattern * @param {string} from * @param {string} to * @returns {Array<string>} */ const replaceInFiles = (search_path = process.cwd(), pattern, from, to) => { const searchPath = _path.default.resolve(search_path); const relevantFiles = []; const result = []; _glob.default.sync(pattern, { cwd: searchPath, nodir: true }).map(file => { relevantFiles.push(_path.default.resolve(searchPath, file)); }); if (relevantFiles.length <= 0) { return result; } for (const file of relevantFiles || []) { const content = _fsExtra.default.readFileSync(file, 'utf-8'); if (content && content.includes(from)) { // Use split and join to replace all findings. _fsExtra.default.writeFileSync(file, content.split(from).join(to), error => { if (error) { return console.error(_chalk.default.red('Error replacing content for', file, ':', error)); } }); result.push(file); } } return result; }; /** * @param {string} filePath * @param {string} file * @returns {string} */ const returnIfFileExists = (filePath, file) => { if (file) { if (!filePath) { return ''; } filePath = _path.default.join(filePath, file); } return _fsExtra.default.existsSync(filePath) ? filePath : ''; }; /** * @param {string} search_path * @param {string} pattern * @param {string} name * @param {string} to */ const setPlaceholder = (search_path = process.cwd(), pattern, name, to) => { replaceInFiles(search_path, pattern, `[[ --${name}-- ]]`, to); }; var _default = { copyFileIfNotExists, copyFolderIfNotExists, createBackupFile, createFileIfNotExists, createFolderIfNotExists, normalizeFileName, renameFile, renameFileIfExists, replaceInFiles, returnIfFileExists, setPlaceholder }; exports.default = _default;