nmdk
Version:
CLI tool for downloading and setting up Minecraft mod development kits (MDK) for Forge, Fabric, and NeoForge
114 lines (95 loc) • 3.71 kB
JavaScript
const fs = require('fs-extra');
const path = require('path');
const extractZip = require('extract-zip');
const tar = require('tar');
const ora = require('ora');
const chalk = require('chalk');
async function extractProjectArchive(archivePath, destPath) {
const spinner = ora('Extracting project archive').start();
try {
await fs.ensureDir(destPath);
const ext = path.extname(archivePath).toLowerCase();
if (ext === '.zip') {
await extractZip(archivePath, { dir: destPath });
} else if (ext === '.tar' || ext === '.tar.gz' || ext === '.tgz') {
await tar.extract({
file: archivePath,
cwd: destPath
});
} else {
throw new Error(`Unsupported archive format: ${ext}`);
}
const items = await fs.readdir(destPath);
if (items.length === 1) {
const itemPath = path.join(destPath, items[0]);
const stat = await fs.stat(itemPath);
if (stat.isDirectory()) {
const subItems = await fs.readdir(itemPath);
for (const subItem of subItems) {
const srcPath = path.join(itemPath, subItem);
const targetPath = path.join(destPath, subItem);
await fs.move(srcPath, targetPath, { overwrite: true });
}
await fs.remove(itemPath);
}
}
const javaSrcPath = path.join(destPath, 'src/main/java');
if (await fs.pathExists(javaSrcPath)) {
} else {
const mainJavaPath = path.join(destPath, 'main/java');
if (await fs.pathExists(mainJavaPath)) {
await fs.ensureDir(javaSrcPath);
const javaFiles = await fs.readdir(mainJavaPath);
for (const javaFile of javaFiles) {
const srcPath = path.join(mainJavaPath, javaFile);
const targetPath = path.join(javaSrcPath, javaFile);
await fs.copy(srcPath, targetPath, { overwrite: true });
}
}
}
spinner.succeed(chalk.green('Project archive extracted'));
} catch (error) {
spinner.fail('Failed to extract archive');
throw new Error(`Failed to extract archive: ${error.message}`);
}
}
async function reorganizeProjectStructure(sourceDir, destDir) {
const spinner = ora('Reorganizing project structure').start();
try {
await fs.ensureDir(destDir);
const items = await fs.readdir(sourceDir);
if (items.length === 1) {
const itemPath = path.join(sourceDir, items[0]);
const stat = await fs.stat(itemPath);
if (stat.isDirectory()) {
const subItems = await fs.readdir(itemPath);
for (const subItem of subItems) {
const srcPath = path.join(itemPath, subItem);
const destPath = path.join(destDir, subItem);
await fs.move(srcPath, destPath, { overwrite: true });
}
await fs.remove(itemPath);
}
} else {
// If multiple items, copy all of them
for (const item of items) {
const srcPath = path.join(sourceDir, item);
const destPath = path.join(destDir, item);
const stat = await fs.stat(srcPath);
if (stat.isDirectory()) {
await fs.copy(srcPath, destPath, { overwrite: true });
} else {
await fs.copy(srcPath, destPath, { overwrite: true });
}
}
}
spinner.succeed(chalk.green('Project structure reorganized'));
} catch (error) {
spinner.fail('Failed to organize project structure');
throw new Error(`Failed to organize project: ${error.message}`);
}
}
module.exports = {
extractProjectArchive,
reorganizeProjectStructure
};