Skip to content

Commit

Permalink
Fix: don't test default cache paths when one provided (#1145)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored May 31, 2024
1 parent 4e9033a commit aa0d2d9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 30 deletions.
23 changes: 2 additions & 21 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,13 @@ process.once('beforeExit', async () => {
});
});

const GLOBAL_CACHE_FILE = [
path.resolve(ROOT_DIR),
os.homedir(),
process.cwd(),
]
.filter((dir) => dir && !dir.startsWith(os.tmpdir()))
.map((dir) => path.join(dir, `${COMMAND_NAME}.cache`))
.sort((a, b) => (fs.existsSync(a) ? 1 : 0) - (fs.existsSync(b) ? 1 : 0))
.find((file) => {
try {
fsPoly.touchSync(file);
return true;
} catch {
return false;
}
});

/**
* A static class of constants that are determined at startup, to be used widely.
*/
export default class Constants {
static readonly COMMAND_NAME = COMMAND_NAME;
static readonly ROOT_DIR = ROOT_DIR;

static readonly AUTHOR = PACKAGE_JSON.author;
static readonly COMMAND_NAME = COMMAND_NAME;

static readonly HOMEPAGE = PACKAGE_JSON.homepage;

Expand All @@ -75,8 +58,6 @@ export default class Constants {

static readonly GLOBAL_TEMP_DIR = GLOBAL_TEMP_DIR;

static readonly GLOBAL_CACHE_FILE = GLOBAL_CACHE_FILE;

/**
* A reasonable max of filesystem threads for operations such as:
* @example
Expand Down
49 changes: 41 additions & 8 deletions src/igir.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os from 'node:os';
import path from 'node:path';

import async from 'async';
import chalk from 'chalk';
import isAdmin from 'is-admin';
Expand Down Expand Up @@ -81,15 +84,14 @@ export default class Igir {
if (this.options.getDisableCache()) {
this.logger.trace('disabling the file cache');
FileCache.disable();
}
const cachePath = process.env.NODE_ENV !== 'test'
? this.options.getCachePath() ?? Constants.GLOBAL_CACHE_FILE
: undefined;
if (cachePath !== undefined) {
this.logger.trace(`loading the file cache at '${cachePath}'`);
await FileCache.loadFile(cachePath);
} else {
this.logger.trace('not using a file for the file cache');
const cachePath = await this.getCachePath();
if (cachePath !== undefined && process.env.NODE_ENV !== 'test') {
this.logger.trace(`loading the file cache at '${cachePath}'`);
await FileCache.loadFile(cachePath);
} else {
this.logger.trace('not using a file for the file cache');
}
}

// Scan and process input files
Expand Down Expand Up @@ -204,6 +206,37 @@ export default class Igir {
Timer.cancelAll();
}

private async getCachePath(): Promise<string | undefined> {
const defaultFileName = `${Constants.COMMAND_NAME}.cache`;

// Try to use the provided path
let cachePath = this.options.getCachePath();
if (cachePath !== undefined && await FsPoly.isDirectory(cachePath)) {
cachePath = path.join(cachePath, defaultFileName);
this.logger.warn(`A directory was provided for cache path instead of a file, using '${cachePath}' instead`);
}
if (cachePath !== undefined) {
if (await FsPoly.isWritable(cachePath)) {
return cachePath;
}
this.logger.warn('Provided cache path isn\'t writable, using the default path');
}

// Otherwise, use a default path
return [
path.join(path.resolve(Constants.ROOT_DIR), defaultFileName),
path.join(os.homedir(), defaultFileName),
path.join(process.cwd(), defaultFileName),
]
.filter((filePath) => filePath && !filePath.startsWith(os.tmpdir()))
.find(async (filePath) => {
if (await FsPoly.exists(filePath)) {
return true;
}
return FsPoly.isWritable(filePath);
});
}

private async processDATScanner(): Promise<DAT[]> {
if (this.options.shouldDir2Dat()) {
return [];
Expand Down
2 changes: 1 addition & 1 deletion src/modules/argumentsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ export default class ArgumentsParser {
})
.option('cache-path', {
group: groupHelpDebug,
description: 'Location for the file checksum cache',
description: 'Location for the file checksum cache file',
type: 'string',
coerce: ArgumentsParser.getLastValue, // don't allow string[] values
requiresArg: true,
Expand Down
14 changes: 14 additions & 0 deletions src/polyfill/fsPoly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@ export default class FsPoly {
}
}

static async isWritable(filePath: string): Promise<boolean> {
const exists = await this.exists(filePath);
try {
await this.touch(filePath);
return true;
} catch {
return false;
} finally {
if (!exists) {
await this.rm(filePath);
}
}
}

static makeLegal(filePath: string, pathSep = path.sep): string {
let replaced = filePath
// Make the filename Windows legal
Expand Down

0 comments on commit aa0d2d9

Please sign in to comment.