Skip to content

Commit

Permalink
Fix: make sure the cache file is writable
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm committed Apr 6, 2024
1 parent f386ccd commit 56a6d70
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,21 @@ process.once('beforeExit', async () => {
});
});

const GLOBAL_CACHE_DIR = path.resolve(ROOT_DIR).startsWith(os.tmpdir())
? os.homedir()
: ROOT_DIR;
const GLOBAL_CACHE_FILE = [
path.resolve(ROOT_DIR),
os.homedir(),
]
.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.
Expand All @@ -62,7 +74,7 @@ export default class Constants {

static readonly GLOBAL_TEMP_DIR = GLOBAL_TEMP_DIR;

static readonly GLOBAL_CACHE_FILE = path.join(GLOBAL_CACHE_DIR, `${COMMAND_NAME}.cache`);
static readonly GLOBAL_CACHE_FILE = GLOBAL_CACHE_FILE;

/**
* A reasonable max of filesystem threads for operations such as:
Expand Down
16 changes: 16 additions & 0 deletions src/polyfill/fsPoly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,22 @@ export default class FsPoly {
await util.promisify(fs.close)(file);
}

static touchSync(filePath: string): void {
const dirname = path.dirname(filePath);
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname, { recursive: true });
}

// Create the file if it doesn't already exist
const file = fs.openSync(filePath, 'a');

// Ensure the file's `atime` and `mtime` are updated
const date = new Date();
fs.futimesSync(file, date, date);

fs.closeSync(file);
}

static async walk(pathLike: PathLike, callback?: FsWalkCallback): Promise<string[]> {
let output: string[] = [];

Expand Down

0 comments on commit 56a6d70

Please sign in to comment.