mirror of
https://git.bits.team/Bits/mod-manager.git
synced 2024-11-24 15:08:22 -05:00
Add migrate command
This commit is contained in:
parent
f7758680d1
commit
0071d32eb7
38
src/commands/migrate_command.ts
Normal file
38
src/commands/migrate_command.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import Subcommand from "./subcommand.js";
|
||||||
|
import {Command} from "commander";
|
||||||
|
import ModManager from "../mod-manager.js";
|
||||||
|
import MinecraftUtils from "../util/minecraft_utils.js";
|
||||||
|
import Mods from "../mods/mods.js";
|
||||||
|
import PrintUtils from "../util/print_utils.js";
|
||||||
|
import MigrateError from "../errors/migrate_error.js";
|
||||||
|
|
||||||
|
export default class MigrateCommand implements Subcommand {
|
||||||
|
registerCommand(program: Command): void {
|
||||||
|
program.command("migrate")
|
||||||
|
.description("Migrates all mods to provided Minecraft version")
|
||||||
|
.argument("[version]", "The Minecraft version to try and migrate to")
|
||||||
|
.option("-f, --force", "Migrates all mods, but discards any non essential mods that are not available", false)
|
||||||
|
.action((version, options) => {
|
||||||
|
ModManager.execute(async () => {
|
||||||
|
const force = options.force;
|
||||||
|
|
||||||
|
// If no version is provided, prompt user for one
|
||||||
|
if (version === "" || version == undefined) {
|
||||||
|
version = await MinecraftUtils.getMinecraftVersionFromInput("What Minecraft version would you like to migrate to?");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start the migration
|
||||||
|
try {
|
||||||
|
await Mods.migrate(version, force)
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof MigrateError) {
|
||||||
|
PrintUtils.error(e.message)
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -20,25 +20,10 @@ export default class MigratePossibleCommand implements Subcommand {
|
|||||||
version = await MinecraftUtils.getMinecraftVersionFromInput("What Minecraft version would you like to migrate to?");
|
version = await MinecraftUtils.getMinecraftVersionFromInput("What Minecraft version would you like to migrate to?");
|
||||||
}
|
}
|
||||||
|
|
||||||
// If version is valid, check if migration is possible
|
try {
|
||||||
if (await MinecraftUtils.isValidVersion(version)) {
|
await Mods.isMigratePossible(version, force);
|
||||||
try {
|
} catch (e) {
|
||||||
const possible = await Mods.isMigratePossible(version, force);
|
PrintUtils.error(e.message)
|
||||||
|
|
||||||
if (possible) {
|
|
||||||
PrintUtils.success(`It is possible to migrate to version ${version}`)
|
|
||||||
} else {
|
|
||||||
PrintUtils.error(`It is not possible to migrate to version ${version}`)
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
if (!force) {
|
|
||||||
PrintUtils.error("There are no mods installed, try `mod-manager install -h` to learn more!")
|
|
||||||
} else {
|
|
||||||
PrintUtils.error("There are no mods installed that are marked essential. Try `mod-manager essential -h` to learn more!")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
PrintUtils.error(`${version} is not a valid Minecraft version`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
7
src/errors/migrate_error.ts
Normal file
7
src/errors/migrate_error.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export default class MigrateError extends Error {
|
||||||
|
constructor(message: string | undefined) {
|
||||||
|
super(message);
|
||||||
|
this.name = this.constructor.name;
|
||||||
|
Error.captureStackTrace(this, this.constructor);
|
||||||
|
}
|
||||||
|
}
|
@ -13,6 +13,7 @@ import EssentialCommand from "./commands/essential_command.js";
|
|||||||
import {readFileSync, unlinkSync} from "fs";
|
import {readFileSync, unlinkSync} from "fs";
|
||||||
import UpdateCommand from "./commands/upgrade_command.js";
|
import UpdateCommand from "./commands/upgrade_command.js";
|
||||||
import MigratePossibleCommand from "./commands/migrate_possible.js";
|
import MigratePossibleCommand from "./commands/migrate_possible.js";
|
||||||
|
import MigrateCommand from "./commands/migrate_command.js";
|
||||||
|
|
||||||
|
|
||||||
export default class ModManager {
|
export default class ModManager {
|
||||||
@ -27,7 +28,8 @@ export default class ModManager {
|
|||||||
new UninstallCommand(),
|
new UninstallCommand(),
|
||||||
new EssentialCommand(),
|
new EssentialCommand(),
|
||||||
new UpdateCommand(),
|
new UpdateCommand(),
|
||||||
new MigratePossibleCommand()
|
new MigratePossibleCommand(),
|
||||||
|
new MigrateCommand()
|
||||||
];
|
];
|
||||||
|
|
||||||
static FilePaths = class {
|
static FilePaths = class {
|
||||||
|
@ -7,6 +7,7 @@ import {readFileSync, unlinkSync, writeFileSync} from "fs";
|
|||||||
import Util from "../util/util.js";
|
import Util from "../util/util.js";
|
||||||
import ModManager from "../mod-manager.js";
|
import ModManager from "../mod-manager.js";
|
||||||
import MinecraftUtils from "../util/minecraft_utils.js";
|
import MinecraftUtils from "../util/minecraft_utils.js";
|
||||||
|
import MigrateError from "../errors/migrate_error.js";
|
||||||
|
|
||||||
export default class Mods {
|
export default class Mods {
|
||||||
private static readonly MOD_SOURCES: Array<ModSource> = [
|
private static readonly MOD_SOURCES: Array<ModSource> = [
|
||||||
@ -125,7 +126,7 @@ export default class Mods {
|
|||||||
// Remove mod from list
|
// Remove mod from list
|
||||||
mods = mods.filter(item => !Mods.areModsEqual(item, modToMark));
|
mods = mods.filter(item => !Mods.areModsEqual(item, modToMark));
|
||||||
|
|
||||||
// Toggle essnetial status, and write back to file
|
// Toggle essential status, and write back to file
|
||||||
modToMark.essential = !modToMark.essential;
|
modToMark.essential = !modToMark.essential;
|
||||||
mods.push(modToMark)
|
mods.push(modToMark)
|
||||||
this.writeFile(mods);
|
this.writeFile(mods);
|
||||||
@ -212,7 +213,15 @@ export default class Mods {
|
|||||||
const mods = !force ? this.getTrackedMods() : this.getEssentialMods();
|
const mods = !force ? this.getTrackedMods() : this.getEssentialMods();
|
||||||
|
|
||||||
if (Util.isArrayEmpty(mods)) {
|
if (Util.isArrayEmpty(mods)) {
|
||||||
throw new Error("Mods list is empty")
|
const msg = !force ? "" +
|
||||||
|
"There are no mods installed, try `mod-manager install -h` to learn more!" : "" +
|
||||||
|
"There are no mods installed that are marked essential. Try `mod-manager essential -h` to learn more!";
|
||||||
|
|
||||||
|
throw new Error(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!await MinecraftUtils.isValidVersion(version)) {
|
||||||
|
throw new Error(`${version} is not a valid Minecraft version`)
|
||||||
}
|
}
|
||||||
|
|
||||||
let availableList = [];
|
let availableList = [];
|
||||||
@ -240,10 +249,72 @@ export default class Mods {
|
|||||||
availableList = availableList.filter(available => !available)
|
availableList = availableList.filter(available => !available)
|
||||||
|
|
||||||
// If the array is empty, all the mods reported as available, and a migration is possible
|
// If the array is empty, all the mods reported as available, and a migration is possible
|
||||||
return Util.isArrayEmpty(availableList);
|
const possible = Util.isArrayEmpty(availableList);
|
||||||
|
|
||||||
|
// Report and return whether it is possible
|
||||||
|
if (possible) {
|
||||||
|
PrintUtils.success(`It is possible to migrate to version ${version}`)
|
||||||
|
} else {
|
||||||
|
PrintUtils.error(`It is not possible to migrate to version ${version}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return possible;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static getEssentialMods() {
|
private static getEssentialMods() {
|
||||||
return this.getTrackedMods().filter(mod => mod.essential);
|
return this.getTrackedMods().filter(mod => mod.essential);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Migrates to the provided version of minecraft
|
||||||
|
* @param version
|
||||||
|
* @param force
|
||||||
|
*/
|
||||||
|
static async migrate(version: string, force: boolean) {
|
||||||
|
const mods = this.getTrackedMods();
|
||||||
|
|
||||||
|
if (Util.isArrayEmpty(mods)) {
|
||||||
|
throw new MigrateError("There are no mods installed right now. Try `mod-manager install -h` to learn more!")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!await MinecraftUtils.isValidVersion(version)) {
|
||||||
|
throw new MigrateError(`${version} is not a valid version of Minecraft`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!await Mods.isMigratePossible(version, force)) {
|
||||||
|
throw new MigrateError(`It is not possible to migrate to ${version}.`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// For every tracked mod
|
||||||
|
for (let mod of mods) {
|
||||||
|
const source = this.getSourceFromName(mod.source);
|
||||||
|
|
||||||
|
const spinner = new PrintUtils.Spinner(`Uninstalling ${mod.name} ${mod.version}...`);
|
||||||
|
spinner.start();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Uninstall it
|
||||||
|
this.silentUninstall(mod);
|
||||||
|
|
||||||
|
// Get the latest version
|
||||||
|
const latestVersion = await source.getLatestVersion(mod.id, version)
|
||||||
|
|
||||||
|
// Install the new mod
|
||||||
|
spinner.updateText(`Installing ${mod.name} ${latestVersion.version_number}..`)
|
||||||
|
const newMod = await source.install(latestVersion, mod.essential)
|
||||||
|
this.trackMod(newMod)
|
||||||
|
|
||||||
|
spinner.succeed(`Successfully installed ${newMod.name} ${newMod.version}`)
|
||||||
|
} catch (e) {
|
||||||
|
// If a mod is not available, but is essential, throw error, else, warn user, and continue.
|
||||||
|
if (mod.essential) {
|
||||||
|
throw new Error("Attempted to migrate a mod that is not available for migration")
|
||||||
|
} else {
|
||||||
|
spinner.warn(`${mod.name} is not available. Discarding...`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PrintUtils.success(`Successfully migrated to ${version}`)
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user