UNPKG

minecraft-utils-shared

Version:

Shared utils for Minecraft Bedrock / Forge development related utilities.

126 lines (117 loc) 5.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _chalk = _interopRequireDefault(require("chalk")); var _execa = _interopRequireDefault(require("execa")); var _fsExtra = _interopRequireDefault(require("fs-extra")); var _path = _interopRequireDefault(require("path")); var _default_path = _interopRequireDefault(require("./default_path.cjs")); var _files = _interopRequireDefault(require("./files.cjs")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * @file Minecraft Utils Shared - Init * @license Apache-2.0 * @author Markus@Bordihn.de (Markus Bordihn) */ /** * @param {string} npmPackage * @param {string} targetPath (optional) * @returns {boolean} */ const createWorkspace = (npmPackage, targetPath = _default_path.default.project.path) => { if (targetPath == null) { console.log(_chalk.default.red('Invalid target path for creating workspace!')); return false; } else if (!targetPath) { console.log(_chalk.default.red('⚠️ Preparing workspace in current directory!')); targetPath = './'; } else { console.log(_chalk.default.green('⚠️ Preparing workspace at', targetPath)); } // Check if we got a valid targetPath if (_fsExtra.default.existsSync(targetPath)) { console.log(_chalk.default.green('✔️ Found workspace path ...')); } else { console.log(_chalk.default.yellow('❌ Found no workspace path will create one ...')); _fsExtra.default.ensureDirSync(targetPath); } // Make sure we have a existing package.json file if (_fsExtra.default.existsSync(_path.default.join(targetPath, 'package.json'))) { console.log(_chalk.default.green('✔️ Found existing package.json ...')); } else { console.log(_chalk.default.yellow('❌ Found no package.json will create a basic one ...')); try { _execa.default.commandSync('npm init -y private', { cwd: targetPath }); } catch (error) { console.error(_chalk.default.red('⚠️ Unable to create package.json file:', error)); return false; } } // Confirm the file exists before going to the next steps. if (!_fsExtra.default.existsSync(_path.default.join(targetPath, 'package.json'))) { console.error(_chalk.default.red('⚠️ Unable to find package.json file, give up!')); return false; } // Installing a local copy of the npm package, if provided. if (npmPackage) { console.log(_chalk.default.green('Installing package', npmPackage, ' ...')); try { _execa.default.commandSync(`npm install ${npmPackage}`, { cwd: targetPath }); } catch (error) { console.error(_chalk.default.red(`⚠️ Unable to install a local copy of ${npmPackage}:`), error); return false; } } // Add git related files if not exists. const gitAttributesFile = _path.default.join(_default_path.default.assets.init, '.gitattributes'); if (_fsExtra.default.existsSync(gitAttributesFile)) { const targetFile = _path.default.join(targetPath, '.gitattributes'); if (_fsExtra.default.existsSync(targetFile)) { console.log(_chalk.default.yellow('Skipping existing .gitattributes at', targetFile)); } else { console.log(_chalk.default.green('Copying .gitattributes files from', gitAttributesFile)); _files.default.copyFileIfNotExists(_path.default.join(gitAttributesFile), targetFile); } } const gitIgnoreFile = _path.default.join(_default_path.default.assets.init, '.gitignore'); if (_fsExtra.default.existsSync(gitIgnoreFile)) { const targetFile = _path.default.join(targetPath, '.gitignore'); if (_fsExtra.default.existsSync(targetFile)) { console.log(_chalk.default.yellow('Skipping existing .gitignore at', targetFile)); } else { console.log(_chalk.default.green('Copying .gitignore file from', gitIgnoreFile)); _files.default.copyFileIfNotExists(_path.default.join(gitIgnoreFile), targetFile); } } // Add npm ignore file, if needed. const npmIgnoreFile = _path.default.join(_default_path.default.assets.init, '.npmignore'); if (_fsExtra.default.existsSync(npmIgnoreFile)) { const targetFile = _path.default.join(targetPath, '.npmignore'); if (_fsExtra.default.existsSync(targetFile)) { console.log(_chalk.default.yellow('Skipping existing .npmignore at', targetFile)); } else { console.log(_chalk.default.green('Copying .npmignore file from', gitIgnoreFile)); _files.default.copyFileIfNotExists(_path.default.join(npmIgnoreFile), targetFile); } } // Install pre-defined package, if defined! if (npmPackage) { if (targetPath != _default_path.default.project.path) { console.info(`\nUse "${_chalk.default.green('npx ' + npmPackage + ' new')}" inside ${_chalk.default.grey(targetPath)} to create a new project!.\n`); } else { console.info(`\nUse "${_chalk.default.green('npx ' + npmPackage + ' new')}" to create a new project!.\n`); } } console.log(_chalk.default.green('Done. ✔️')); return true; }; var _default = { createWorkspace }; exports.default = _default;