Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
Support for destPathsToIgnore, fixes #124
Browse files Browse the repository at this point in the history
  • Loading branch information
city41 committed Oct 11, 2023
1 parent 82e43c2 commit a04b7ac
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 1 deletion.
94 changes: 94 additions & 0 deletions src/main/export/__integration__/export.integration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os from 'node:os';
import path from '../../util/universalPath';
import nodePath from 'path';
import fs from 'node:fs';
import fsp from 'node:fs/promises';
import { defaultUpdateDbs } from '../../settings/defaultUpdateDbs';
Expand All @@ -14,6 +15,7 @@ import type { Settings } from '../../settings/types';
import { exportToDirectory } from '../export';
import { exists } from '../../util/fs';
import { getSetting } from '../../settings';
import mkdirp from 'mkdirp';

const TMP_DIR = path.resolve(os.tmpdir(), 'ammister-integration-tests-export');

Expand Down Expand Up @@ -219,6 +221,8 @@ describe('export integration', function () {
}
case 'exportOptimization':
return Promise.resolve('space');
case 'destPathsToIgnore':
return Promise.resolve([]);
default:
return Promise.resolve('mock-setting');
}
Expand Down Expand Up @@ -357,6 +361,96 @@ describe('export integration', function () {
);
});

it('should not delete files in the destPathsToIgnore array', async function () {
const exportDir = path.resolve(TMP_DIR, 'exported-plan-with-ignores');
const fileToLeaveAlone = 'games/mame/donttouch.txt';

// @ts-expect-error doesnt know it is a mock
getSetting.mockImplementation((settingKey: keyof Settings) => {
switch (settingKey) {
case 'rootDir':
return Promise.resolve(TMP_DIR);
case 'downloadRoms':
return Promise.resolve(false);
case 'updateDbs': {
const updateDbs = defaultUpdateDbs.map((udb) => {
return {
...udb,
enabled: udb.db_id === 'theypsilon_unofficial_distribution',
};
});

return Promise.resolve(updateDbs);
}
case 'exportOptimization':
return Promise.resolve('space');
case 'destPathsToIgnore':
return Promise.resolve([fileToLeaveAlone]);
default:
return Promise.resolve('mock-setting');
}
});

const pathToMake = nodePath.dirname(
path.resolve(exportDir, fileToLeaveAlone)
);
await mkdirp(pathToMake);

await fsp.writeFile(
path.resolve(exportDir, fileToLeaveAlone),
'leave me alone!'
);

const ignoredFileStatBefore = await fsp.stat(
path.resolve(exportDir, fileToLeaveAlone)
);

expect(ignoredFileStatBefore.isFile()).toBe(true);

const updateCatalogCallback = jest.fn().mockReturnValue(true);
await updateCatalog(updateCatalogCallback);

const catalog = updateCatalogCallback.mock.lastCall?.[0]
.catalog as Catalog;
const { updatedAt, ...rest } = catalog;
const catalogEntries = Object.values(rest).flat(1);
const firstEntry = catalogEntries[0];

const plan: Plan = {
createdAt: Date.now(),
updatedAt: Date.now(),
directoryName: 'integration',
isExpanded: true,
games: [
{
directoryName: 'foo',
isExpanded: true,
games: [
{
directoryName: 'bar',
isExpanded: true,
games: [
{
db_id: firstEntry.db_id,
relFilePath: firstEntry.files.mra.relFilePath,
},
],
},
],
},
],
};

const exportCallback = jest.fn().mockReturnValue(true);
await exportToDirectory(plan, exportDir, exportCallback);

const ignoredFileStatAfter = await fsp.stat(
path.resolve(exportDir, fileToLeaveAlone)
);

expect(ignoredFileStatAfter.isFile()).toBe(true);
});

// This was added to confirm Jotego's cores (which lack the date in their name) still work,
// the first stab at handling this caused ambiguous export operations, so would delete cores
// that should not be deleted
Expand Down
24 changes: 23 additions & 1 deletion src/main/export/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ async function doExport(
clientFactory: (logger: winston.Logger) => FileClient
) {
const catalog = await getCurrentCatalog();
const destPathsToIgnore =
(await settings.getSetting<string[]>('destPathsToIgnore')) ?? [];

if (!catalog) {
throw new Error('doExport: no current catalog');
Expand Down Expand Up @@ -679,7 +681,27 @@ async function doExport(
[getExistingDestPaths.name]: destRomPaths,
for: 'games/mame',
});
const fileOperations = buildFileOperations(srcPaths, destPaths);

const finalDestPaths = destPaths.filter((dp) => {
if (isDestExactFileOperationPath(dp)) {
if (
destPathsToIgnore.some(
(dpti) => dpti.toLowerCase() === dp.relPath.toLowerCase()
)
) {
exportLogger.info({
destPathsToIgnore: dp.relPath,
message:
'removing this paths from destPaths as it is in destPathsToIgnore',
});
return false;
}
}

return true;
});

const fileOperations = buildFileOperations(srcPaths, finalDestPaths);

exportLogger.info({ [buildFileOperations.name]: fileOperations });

Expand Down
7 changes: 7 additions & 0 deletions src/main/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ async function init(userDataPath: string): Promise<void> {
await setSetting('rootDir', userDataPath);
await setDefaultSetting('downloadRoms', false);
await setDefaultSetting('exportOptimization', 'space');
await setDefaultSetting('destPathsToIgnore', [
// this file is needed for people in Jotego's beta program.
// AMMister doesn't really know anything about it, but if we find it
// on a mister (or directory), we should leave it alone
// https://github.com/city41/AMMiSTer/issues/124
'games/mame/jtbeta.zip',
]);

const hasUpdateDbs = await hasSetting('updateDbs');

Expand Down
1 change: 1 addition & 0 deletions src/main/settings/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type Settings = {
mostRecentPlanDir?: string;
updateDbs: UpdateDbConfig[];
exportOptimization: ExportOptimization;
destPathsToIgnore: string[];
};

export type SettingChangeListener = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const mockSettings: Settings = {
recentPlans: [],
updateDbs: defaultUpdateDbs,
exportOptimization: 'space',
destPathsToIgnore: [],
};

export const Basic = () => {
Expand Down

0 comments on commit a04b7ac

Please sign in to comment.