Skip to content

Commit

Permalink
fix(#62): a relative mods folder will now be calculated from the conf…
Browse files Browse the repository at this point in the history
…ig file's location and not the current run dir as before
  • Loading branch information
meza committed Dec 21, 2023
1 parent 938bb8e commit f42ceb4
Show file tree
Hide file tree
Showing 24 changed files with 141 additions and 68 deletions.
3 changes: 2 additions & 1 deletion src/actions/add.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { add } from './add.js';
import {
ensureConfiguration,
ensureConfiguration, getModsFolder,
readLockFile,
writeConfigFile,
writeLockFile
Expand Down Expand Up @@ -72,6 +72,7 @@ describe('The add module', async () => {

// the main configuration to work with
vi.mocked(ensureConfiguration).mockResolvedValue(context.randomConfiguration.generated);
vi.mocked(getModsFolder).mockReturnValue(context.randomConfiguration.generated.modsFolder);
vi.mocked(readLockFile).mockResolvedValue([]);

// the mod details returned from the repository
Expand Down
4 changes: 2 additions & 2 deletions src/actions/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path';
import { fetchModDetails } from '../repositories/index.js';
import { Mod, Platform } from '../lib/modlist.types.js';
import {
ensureConfiguration,
ensureConfiguration, getModsFolder,
readLockFile,
writeConfigFile,
writeLockFile
Expand Down Expand Up @@ -73,7 +73,7 @@ export const add = async (platform: Platform, id: string, options: AddOptions, l
options.version
);

await downloadFile(modData.downloadUrl, path.resolve(configuration.modsFolder, modData.fileName));
await downloadFile(modData.downloadUrl, path.resolve(getModsFolder(options.config, configuration), modData.fileName));

const installations = await readLockFile(options, logger);

Expand Down
17 changes: 15 additions & 2 deletions src/actions/change.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import { chance } from 'jest-chance';
import { DefaultOptions } from '../mmm.js';
import { changeGameVersion } from './change.js';
import { generateModsJson } from '../../test/modlistGenerator.js';
import { fileExists, ensureConfiguration, readLockFile, writeConfigFile, writeLockFile } from '../lib/config.js';
import {
fileExists,
ensureConfiguration,
readLockFile,
writeConfigFile,
writeLockFile,
getModsFolder
} from '../lib/config.js';
import { install } from './install.js';
import { testGameVersion } from './testGameVersion.js';
import { generateModInstall } from '../../test/modInstallGenerator.js';
Expand Down Expand Up @@ -48,6 +55,7 @@ describe('The change action', () => {
options.quiet = quietFlag;

vi.mocked(ensureConfiguration).mockResolvedValueOnce(config);
vi.mocked(getModsFolder).mockReturnValue(config.modsFolder);
vi.mocked(readLockFile).mockResolvedValue([]);

await changeGameVersion(version, options, logger);
Expand Down Expand Up @@ -95,8 +103,10 @@ describe('The change action', () => {
generateModConfig({ id: install1.id, type: install1.type }).generated,
generateModConfig({ id: install2.id, type: install2.type }).generated,
generateModConfig({ id: install3.id, type: install3.type }).generated
]
],
modsFolder: '/mods'
}).generated);
vi.mocked(getModsFolder).mockReturnValue('/mods');

await changeGameVersion(version, options, logger);

Expand All @@ -117,13 +127,16 @@ describe('The change action', () => {
const install3 = generateModInstall({ fileName: 'mymod3' }).generated;

vi.mocked(readLockFile).mockResolvedValueOnce([install1, install3]);

vi.mocked(ensureConfiguration).mockResolvedValue(generateModsJson({
modsFolder: '/mods',
mods: [
generateModConfig({ id: install1.id, type: install1.type }).generated,
generateModConfig({ id: install2.id, type: install2.type }).generated,
generateModConfig({ id: install3.id, type: install3.type }).generated
]
}).generated);
vi.mocked(getModsFolder).mockReturnValue('/mods');

await changeGameVersion(version, options, logger);

Expand Down
4 changes: 2 additions & 2 deletions src/actions/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { testGameVersion } from './testGameVersion.js';
import { Mod } from '../lib/modlist.types.js';
import {
ensureConfiguration,
fileExists, readLockFile,
fileExists, getModsFolder, readLockFile,
writeConfigFile,
writeLockFile
} from '../lib/config.js';
Expand All @@ -25,7 +25,7 @@ export const changeGameVersion = async (gameVersion: string, options: VerifyUpgr

if (hasInstallation(mod, installations)) {
const installedModIndex = getInstallation(mod, installedMods);
const oldModPath = path.resolve(configuration.modsFolder, installedMods[installedModIndex].fileName);
const oldModPath = path.resolve(getModsFolder(options.config, configuration), installedMods[installedModIndex].fileName);
if (await fileExists(oldModPath)) {
await fs.rm(oldModPath);
}
Expand Down
19 changes: 18 additions & 1 deletion src/actions/install.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { install } from './install.js';
import { fetchModDetails } from '../repositories/index.js';
import { ensureConfiguration, readLockFile, writeConfigFile, writeLockFile } from '../lib/config.js';
import { ensureConfiguration, getModsFolder, readLockFile, writeConfigFile, writeLockFile } from '../lib/config.js';
import { generateRemoteModDetails } from '../../test/generateRemoteDetails.js';
import { downloadFile } from '../lib/downloader.js';
import { updateMod } from '../lib/updater.js';
Expand Down Expand Up @@ -74,6 +74,7 @@ describe('The install module', () => {

// Prepare the configuration file state
vi.mocked(ensureConfiguration).mockResolvedValueOnce(randomConfiguration);
vi.mocked(getModsFolder).mockReturnValue(randomConfiguration.modsFolder);
vi.mocked(readLockFile).mockResolvedValueOnce(emptyLockFile);

// Prepare the details the mod details fetcher should return
Expand Down Expand Up @@ -116,6 +117,7 @@ describe('The install module', () => {

// Prepare the configuration file state
vi.mocked(ensureConfiguration).mockResolvedValueOnce(randomConfiguration);
vi.mocked(getModsFolder).mockReturnValue(randomConfiguration.modsFolder);
vi.mocked(readLockFile).mockResolvedValueOnce(emptyLockFile);

// Prepare the details the mod details fetcher should return
Expand All @@ -140,6 +142,7 @@ describe('The install module', () => {

// Prepare the configuration file state
vi.mocked(ensureConfiguration).mockResolvedValueOnce(randomConfiguration);
vi.mocked(getModsFolder).mockReturnValue(randomConfiguration.modsFolder);
vi.mocked(readLockFile).mockResolvedValueOnce([
randomInstallation
]);
Expand Down Expand Up @@ -178,6 +181,7 @@ describe('The install module', () => {

// Prepare the configuration file state
vi.mocked(ensureConfiguration).mockResolvedValueOnce(randomConfiguration);
vi.mocked(getModsFolder).mockReturnValue(randomConfiguration.modsFolder);
vi.mocked(readLockFile).mockResolvedValueOnce([
randomInstallation
]);
Expand Down Expand Up @@ -216,6 +220,7 @@ describe('The install module', () => {
const { randomInstalledMod, randomInstallation, randomConfiguration } = setupOneInstalledMod();

vi.mocked(ensureConfiguration).mockResolvedValueOnce(randomConfiguration);
vi.mocked(getModsFolder).mockReturnValue(randomConfiguration.modsFolder);
vi.mocked(readLockFile).mockResolvedValueOnce([randomInstallation]);
vi.mocked(getHash).mockResolvedValueOnce(randomInstallation.hash);

Expand All @@ -233,6 +238,7 @@ describe('The install module', () => {
randomInstalledMod.version = '1.1.0';

vi.mocked(ensureConfiguration).mockResolvedValueOnce(randomConfiguration);
vi.mocked(getModsFolder).mockReturnValue(randomConfiguration.modsFolder);
vi.mocked(readLockFile).mockResolvedValueOnce([randomInstallation]);
vi.mocked(getHash).mockResolvedValueOnce(randomInstallation.hash);

Expand All @@ -248,6 +254,7 @@ describe('The install module', () => {

// Prepare the configuration file state
vi.mocked(ensureConfiguration).mockResolvedValueOnce(randomConfiguration);
vi.mocked(getModsFolder).mockReturnValue(randomConfiguration.modsFolder);
vi.mocked(readLockFile).mockResolvedValueOnce([
randomInstallation
]);
Expand Down Expand Up @@ -281,6 +288,7 @@ describe('The install module', () => {
const emptyConfiguration = generateModsJson().generated;
const emptyInstallations: ModInstall[] = [];
vi.mocked(ensureConfiguration).mockResolvedValueOnce(emptyConfiguration);
vi.mocked(getModsFolder).mockReturnValue(emptyConfiguration.modsFolder);
vi.mocked(readLockFile).mockResolvedValueOnce(emptyInstallations);

const file1 = chance.word();
Expand All @@ -303,6 +311,7 @@ describe('The install module', () => {
const emptyConfiguration = generateModsJson().generated;
const emptyInstallations: ModInstall[] = [];
vi.mocked(ensureConfiguration).mockResolvedValueOnce(emptyConfiguration);
vi.mocked(getModsFolder).mockReturnValue(emptyConfiguration.modsFolder);
vi.mocked(readLockFile).mockResolvedValueOnce(emptyInstallations);

const file1 = chance.word();
Expand Down Expand Up @@ -341,6 +350,7 @@ describe('The install module', () => {
const emptyConfiguration = generateModsJson().generated;
const emptyInstallations: ModInstall[] = [];
vi.mocked(ensureConfiguration).mockResolvedValueOnce(emptyConfiguration);
vi.mocked(getModsFolder).mockReturnValue(emptyConfiguration.modsFolder);
vi.mocked(readLockFile).mockResolvedValueOnce(emptyInstallations);

vi.mocked(hasInstallation).mockReturnValueOnce(false);
Expand Down Expand Up @@ -379,6 +389,7 @@ describe('The install module', () => {

// Prepare the configuration file state
vi.mocked(ensureConfiguration).mockResolvedValueOnce(randomConfiguration);
vi.mocked(getModsFolder).mockReturnValue(randomConfiguration.modsFolder);
vi.mocked(readLockFile).mockResolvedValueOnce([
randomInstallation
]);
Expand All @@ -403,6 +414,7 @@ describe('The install module', () => {

// Prepare the configuration file state
vi.mocked(ensureConfiguration).mockResolvedValueOnce(randomConfiguration);
vi.mocked(getModsFolder).mockReturnValue(randomConfiguration.modsFolder);
vi.mocked(readLockFile).mockResolvedValueOnce([
randomInstallation
]);
Expand Down Expand Up @@ -430,6 +442,8 @@ describe('The install module', () => {

// Prepare the configuration file state
vi.mocked(ensureConfiguration).mockResolvedValueOnce(randomConfiguration);
vi.mocked(getModsFolder).mockReturnValue(randomConfiguration.modsFolder);
vi.mocked(getModsFolder).mockReturnValue(randomConfiguration.modsFolder);
vi.mocked(readLockFile).mockResolvedValueOnce(emptyLockFile);

// Prepare the details the mod details fetcher should return
Expand All @@ -456,6 +470,7 @@ describe('The install module', () => {

// Prepare the configuration file state
vi.mocked(ensureConfiguration).mockResolvedValueOnce(randomConfiguration);
vi.mocked(getModsFolder).mockReturnValue(randomConfiguration.modsFolder);
vi.mocked(readLockFile).mockResolvedValueOnce(emptyLockFile);

const error = new CouldNotFindModException('id', Platform.MODRINTH);
Expand All @@ -478,6 +493,7 @@ describe('The install module', () => {

// Prepare the configuration file state
vi.mocked(ensureConfiguration).mockResolvedValueOnce(randomConfiguration);
vi.mocked(getModsFolder).mockReturnValue(randomConfiguration.modsFolder);
vi.mocked(readLockFile).mockResolvedValueOnce(emptyLockFile);
const error = new NoRemoteFileFound(aModName, Platform.CURSEFORGE);
vi.mocked(fetchModDetails).mockRejectedValueOnce(error);
Expand All @@ -500,6 +516,7 @@ describe('The install module', () => {

// Prepare the configuration file state
vi.mocked(ensureConfiguration).mockResolvedValueOnce(randomConfiguration);
vi.mocked(getModsFolder).mockReturnValue(randomConfiguration.modsFolder);
vi.mocked(readLockFile).mockResolvedValueOnce(emptyLockFile);

vi.mocked(fetchModDetails).mockRejectedValueOnce(error);
Expand Down
18 changes: 13 additions & 5 deletions src/actions/install.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import chalk from 'chalk';
import { ensureConfiguration, fileExists, readLockFile, writeConfigFile, writeLockFile } from '../lib/config.js';
import {
ensureConfiguration,
fileExists,
getModsFolder,
readLockFile,
writeConfigFile,
writeLockFile
} from '../lib/config.js';
import path from 'path';
import { fetchModDetails } from '../repositories/index.js';
import { downloadFile } from '../lib/downloader.js';
Expand All @@ -26,7 +33,7 @@ const getMod = async (moddata: RemoteModDetails, modsFolder: string) => {

const handleUnknownFiles = async (options: DefaultOptions, configuration: ModsJson, installations: ModInstall[], logger: Logger) => {
//const modsFolder = getModsDir(options.config, configuration.modsFolder);
const allFiles = await getModFiles(options.config, configuration.modsFolder);
const allFiles = await getModFiles(options.config, configuration);
const nonManagedFiles = allFiles.filter((filePath) => {
return !fileIsManaged(filePath, installations);
});
Expand All @@ -50,6 +57,7 @@ export const install = async (options: DefaultOptions, logger: Logger) => {
await handleUnknownFiles(options, configuration, installations, logger);
const installedMods = installations;
const mods = configuration.mods;
const modsFolder = getModsFolder(options.config, configuration);

const processMod = async (mod: Mod, index: number) => {
const canonVersion = mod.version || 'latest';
Expand All @@ -59,7 +67,7 @@ export const install = async (options: DefaultOptions, logger: Logger) => {
if (hasInstallation(mod, installations)) {
const installedModIndex = getInstallation(mod, installedMods);

const modPath = path.resolve(configuration.modsFolder, installedMods[installedModIndex].fileName);
const modPath = path.resolve(getModsFolder(options.config, configuration), installedMods[installedModIndex].fileName);

if (!await fileExists(modPath)) {
logger.log(`${mod.name} doesn't exist, downloading from ${installedMods[installedModIndex].type}`);
Expand All @@ -70,7 +78,7 @@ export const install = async (options: DefaultOptions, logger: Logger) => {
const installedHash = await getHash(modPath);
if (installedMods[installedModIndex].hash !== installedHash) {
logger.log(`${mod.name} has hash mismatch, downloading from source`);
await updateMod(installedMods[installedModIndex], modPath, configuration.modsFolder);
await updateMod(installedMods[installedModIndex], modPath, modsFolder);
return;
}
return;
Expand All @@ -90,7 +98,7 @@ export const install = async (options: DefaultOptions, logger: Logger) => {

// no installation exists
logger.log(`${mod.name} doesn't exist, downloading from ${mod.type}`);
const dlData = await getMod(modData, configuration.modsFolder);
const dlData = await getMod(modData, modsFolder);

installedMods.push({
name: modData.name,
Expand Down
3 changes: 2 additions & 1 deletion src/actions/prune.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Logger } from '../lib/Logger.js';
import { prune, PruneOptions } from './prune.js';
import { ModInstall, ModsJson } from '../lib/modlist.types.js';
import { generateModsJson } from '../../test/modlistGenerator.js';
import { ensureConfiguration, readLockFile } from '../lib/config.js';
import { ensureConfiguration, getModsFolder, readLockFile } from '../lib/config.js';
import { chance } from 'jest-chance';
import { fileIsManaged } from '../lib/configurationHelper.js';
import { shouldPruneFiles } from '../interactions/shouldPruneFiles.js';
Expand Down Expand Up @@ -40,6 +40,7 @@ describe('The prune action', () => {

vi.mocked(ensureConfiguration).mockResolvedValueOnce(context.configuration);
vi.mocked(readLockFile).mockResolvedValueOnce(context.installations);
vi.mocked(getModsFolder).mockReturnValue(context.configuration.modsFolder);

});

Expand Down
6 changes: 3 additions & 3 deletions src/actions/prune.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DefaultOptions } from '../mmm.js';
import { Logger } from '../lib/Logger.js';
import { ensureConfiguration, readLockFile } from '../lib/config.js';
import { ensureConfiguration, getModsFolder, readLockFile } from '../lib/config.js';
import path from 'node:path';
import fs from 'fs/promises';
import { fileIsManaged } from '../lib/configurationHelper.js';
Expand All @@ -14,9 +14,9 @@ export interface PruneOptions extends DefaultOptions {
export const prune = async (options: PruneOptions, logger: Logger) => {
const configuration = await ensureConfiguration(options.config, logger);
const installations = await readLockFile(options, logger);
const modsFolder = path.resolve(configuration.modsFolder);
const modsFolder = getModsFolder(options.config, configuration);

const files = await getModFiles(options.config, configuration.modsFolder);
const files = await getModFiles(options.config, configuration);

if (files.length === 0) {
logger.log('You have no files in your mods folder.');
Expand Down
6 changes: 3 additions & 3 deletions src/actions/remove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { Mod, ModInstall, ModsJson } from '../lib/modlist.types.js';
import { generateModsJson } from '../../test/modlistGenerator.js';
import { removeAction, RemoveOptions } from './remove.js';
import { Logger } from '../lib/Logger.js';
import { ensureConfiguration, readLockFile, writeConfigFile, writeLockFile } from '../lib/config.js';
import { ensureConfiguration, getModsFolder, readLockFile, writeConfigFile, writeLockFile } from '../lib/config.js';
import { generateModConfig } from '../../test/modConfigGenerator.js';
import { findLocalMods, getInstallation, getModsDir, hasInstallation } from '../lib/configurationHelper.js';
import { findLocalMods, getInstallation, hasInstallation } from '../lib/configurationHelper.js';
import { chance } from 'jest-chance';
import { generateModInstall } from '../../test/modInstallGenerator.js';
import fs from 'fs/promises';
Expand Down Expand Up @@ -151,7 +151,7 @@ describe('The remove action', () => {

const config = generateModsJson({ mods: [mod1, mod2, mod3] }).generated;

vi.mocked(getModsDir).mockReturnValue(path.resolve('/mods'));
vi.mocked(getModsFolder).mockReturnValue('/mods');
vi.mocked(ensureConfiguration).mockResolvedValueOnce(config);
vi.mocked(readLockFile).mockResolvedValueOnce([
mod1Install,
Expand Down
6 changes: 3 additions & 3 deletions src/actions/remove.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DefaultOptions } from '../mmm.js';
import { Logger } from '../lib/Logger.js';
import { ensureConfiguration, readLockFile, writeConfigFile, writeLockFile } from '../lib/config.js';
import { findLocalMods, getInstallation, getModsDir, hasInstallation } from '../lib/configurationHelper.js';
import { ensureConfiguration, getModsFolder, readLockFile, writeConfigFile, writeLockFile } from '../lib/config.js';
import { findLocalMods, getInstallation, hasInstallation } from '../lib/configurationHelper.js';
import fs from 'fs/promises';
import path from 'path';
import chalk from 'chalk';
Expand All @@ -14,7 +14,7 @@ export const removeAction = async (mods: string[], options: RemoveOptions, logge
const configuration = await ensureConfiguration(options.config, logger);
const installations = await readLockFile(options, logger);
const matches = findLocalMods(mods, configuration);
const modsDir = getModsDir(options.config, configuration.modsFolder);
const modsDir = getModsFolder(options.config, configuration);

if (options.dryRun) {
logger.log(chalk.yellow('Running in dry-run mode. Nothing will actually be removed.'));
Expand Down
3 changes: 2 additions & 1 deletion src/actions/scan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { scan, ScanOptions } from './scan.js';
import { chance } from 'jest-chance';
import { ModInstall, ModsJson, Platform } from '../lib/modlist.types.js';
import { scan as scanLib } from '../lib/scan.js';
import { ensureConfiguration, readLockFile, writeConfigFile, writeLockFile } from '../lib/config.js';
import { ensureConfiguration, getModsFolder, readLockFile, writeConfigFile, writeLockFile } from '../lib/config.js';
import { generateModsJson } from '../../test/modlistGenerator.js';
import { generateScanResult, ScanResultGeneratorOverrides } from '../../test/generateScanResult.js';
import { shouldAddScanResults } from '../interactions/shouldAddScanResults.js';
Expand Down Expand Up @@ -55,6 +55,7 @@ describe('The Scan action', () => {
throw new Error('process.exit');
});
vi.mocked(getModFiles).mockResolvedValueOnce([]);
vi.mocked(getModsFolder).mockReturnValue(context.randomConfiguration.modsFolder);
});
describe('when there are unexpected errors', () => {
it<LocalTestContext>('logs them correctly', async ({ options, logger }) => {
Expand Down
Loading

0 comments on commit f42ceb4

Please sign in to comment.