nmdk
Version:
CLI tool for downloading and setting up Minecraft mod development kits (MDK) for Forge, Fabric, and NeoForge
98 lines (75 loc) • 3.72 kB
JavaScript
const axios = require('axios');
const path = require('path');
const fs = require('fs-extra');
const { downloadFileFromUrl } = require('../utils/download');
const { extractProjectArchive, reorganizeProjectStructure } = require('../utils/extract');
const { sanitizeGradleConfigurationFiles, configureModMetadata, executeGradleBuildCommands } = require('../utils/gradle');
const ora = require('ora');
const chalk = require('chalk');
async function resolveNeoForgeMDKDownloadUrl(mcVersion, neoforgeVersion) {
const spinner = ora('Resolving NeoForge MDK download URL').start();
try {
const baseUrl = 'https://maven.neoforged.net/releases/net/neoforged/neoforge';
const mdkUrl = `${baseUrl}/${neoforgeVersion}/neoforge-${neoforgeVersion}-mdk.zip`;
const response = await axios.head(mdkUrl);
if (response.status === 200) {
spinner.succeed(chalk.green('NeoForge MDK URL resolved'));
return mdkUrl;
} else {
throw new Error('NeoForge MDK URL not found');
}
} catch (error) {
spinner.fail('Failed to get NeoForge MDK URL');
throw new Error(`Failed to get NeoForge MDK URL: ${error.message}`);
}
}
async function initializeNeoForgeProject(modName, mcVersion, neoforgeVersion, projectPath, modConfig) {
const spinner = ora('Initializing NeoForge project').start();
try {
const mdkUrl = await resolveNeoForgeMDKDownloadUrl(mcVersion, neoforgeVersion);
const archivePath = path.join(projectPath, 'neoforge-mdk.zip');
await downloadFileFromUrl(mdkUrl, archivePath);
const tempExtractPath = path.join(projectPath, 'temp-extract');
await extractProjectArchive(archivePath, tempExtractPath);
await reorganizeProjectStructure(tempExtractPath, projectPath);
await fs.remove(tempExtractPath);
await fs.remove(archivePath);
await sanitizeGradleConfigurationFiles(projectPath);
await configureModMetadata(projectPath, modConfig);
await configureModsTomlFile(projectPath, modConfig);
spinner.succeed(chalk.green('NeoForge project initialized'));
} catch (error) {
spinner.fail('Failed to setup NeoForge project');
throw new Error(`Failed to setup NeoForge project: ${error.message}`);
}
}
async function configureModsTomlFile(projectPath, modConfig) {
const modsTomlPath = path.join(projectPath, 'src/main/resources/META-INF/mods.toml');
if (await fs.pathExists(modsTomlPath)) {
let content = await fs.readFile(modsTomlPath, 'utf8');
content = content.replace(/modLoader="[^"]*"/g, `modLoader="neoforge"`);
content = content.replace(/loaderVersion="[^"]*"/g, `loaderVersion="[20.1,)"`);
content = content.replace(/license="[^"]*"/g, `license="MIT"`);
content = content.replace(/displayName="[^"]*"/g, `displayName="${modConfig.modName}"`);
content = content.replace(/description="[^"]*"/g, `description="${modConfig.description}"`);
content = content.replace(/authors="[^"]*"/g, `authors="${modConfig.author}"`);
content = content.replace(/version="[^"]*"/g, `version="${modConfig.version}"`);
await fs.writeFile(modsTomlPath, content);
}
}
async function executeNeoForgeGradleCommands(projectPath, options = []) {
const commands = [];
if (options.includes('setupDecompWorkspace')) {
commands.push('./gradlew setupDecompWorkspace');
}
if (options.includes('build')) {
commands.push('./gradlew build');
}
if (commands.length > 0) {
await executeGradleBuildCommands(projectPath, commands);
}
}
module.exports = {
initializeNeoForgeProject,
executeNeoForgeGradleCommands
};