Skip to content

Commit 7b5f2f9

Browse files
Gamadriladuh95
andauthored
fix: recreate cache folder if necessary (#200)
Fixes: #198 Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 662ae90 commit 7b5f2f9

6 files changed

+45
-0
lines changed

sources/commands/Hydrate.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Command, Option, UsageError} from 'clipanion';
2+
import {mkdir} from 'fs/promises';
23
import path from 'path';
34

45
import * as folderUtils from '../folderUtils';
@@ -63,6 +64,9 @@ export class HydrateCommand extends Command<Context> {
6364
else
6465
this.context.stdout.write(`Hydrating ${name}@${reference}...\n`);
6566

67+
// Recreate the folder in case it was deleted somewhere else:
68+
await mkdir(installFolder, {recursive: true});
69+
6670
await tar.x({file: fileName, cwd: installFolder}, [`${name}/${reference}`]);
6771

6872
if (this.activate) {

sources/commands/Prepare.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Command, Option, UsageError} from 'clipanion';
2+
import {mkdir} from 'fs/promises';
23
import path from 'path';
34

45
import * as folderUtils from '../folderUtils';
@@ -117,6 +118,8 @@ export class PrepareCommand extends Command<Context> {
117118
this.context.stdout.write(`Packing the selected tools in ${path.basename(outputPath)}...\n`);
118119

119120
const {default: tar} = await import(/* webpackMode: 'eager' */ `tar`);
121+
// Recreate the folder in case it was deleted somewhere else:
122+
await mkdir(baseInstallFolder, {recursive: true});
120123
await tar.c({gzip: true, cwd: baseInstallFolder, file: path.resolve(outputPath)}, installLocations.map(location => {
121124
return path.relative(baseInstallFolder, location);
122125
}));

tests/main.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,44 @@ it(`should support hydrating package managers from cached archives`, async () =>
404404
});
405405
});
406406

407+
it(`should support hydrating package managers if cache folder was removed`, async () => {
408+
await xfs.mktempPromise(async cwd => {
409+
await expect(runCli(cwd, [`prepare`, `yarn@2.2.2`, `-o`])).resolves.toMatchObject({
410+
exitCode: 0,
411+
stderr: ``,
412+
});
413+
414+
// Use a new cache
415+
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
416+
417+
// Simulate cache removal
418+
await xfs.removePromise(npath.toPortablePath(process.env.COREPACK_HOME));
419+
420+
// Disable the network to make sure we don't succeed by accident
421+
process.env.COREPACK_ENABLE_NETWORK = `0`;
422+
423+
try {
424+
await expect(runCli(cwd, [`hydrate`, `corepack.tgz`])).resolves.toMatchObject({
425+
stdout: `Hydrating yarn@2.2.2...\nAll done!\n`,
426+
stderr: ``,
427+
exitCode: 0,
428+
});
429+
430+
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
431+
packageManager: `yarn@2.2.2`,
432+
});
433+
434+
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
435+
stdout: `2.2.2\n`,
436+
stderr: ``,
437+
exitCode: 0,
438+
});
439+
} finally {
440+
delete process.env.COREPACK_ENABLE_NETWORK;
441+
}
442+
});
443+
});
444+
407445
it(`should support hydrating multiple package managers from cached archives`, async () => {
408446
await xfs.mktempPromise(async cwd => {
409447
await expect(runCli(cwd, [`prepare`, `yarn@2.2.2`, `pnpm@5.8.0`, `-o`])).resolves.toMatchObject({
3.44 MB
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)