Skip to content

Commit

Permalink
fix: recreate cache folder if necessary (#200)
Browse files Browse the repository at this point in the history
Fixes: #198
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
Gamadril and aduh95 authored Oct 28, 2022
1 parent 662ae90 commit 7b5f2f9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sources/commands/Hydrate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Command, Option, UsageError} from 'clipanion';
import {mkdir} from 'fs/promises';
import path from 'path';

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

// Recreate the folder in case it was deleted somewhere else:
await mkdir(installFolder, {recursive: true});

await tar.x({file: fileName, cwd: installFolder}, [`${name}/${reference}`]);

if (this.activate) {
Expand Down
3 changes: 3 additions & 0 deletions sources/commands/Prepare.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Command, Option, UsageError} from 'clipanion';
import {mkdir} from 'fs/promises';
import path from 'path';

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

const {default: tar} = await import(/* webpackMode: 'eager' */ `tar`);
// Recreate the folder in case it was deleted somewhere else:
await mkdir(baseInstallFolder, {recursive: true});
await tar.c({gzip: true, cwd: baseInstallFolder, file: path.resolve(outputPath)}, installLocations.map(location => {
return path.relative(baseInstallFolder, location);
}));
Expand Down
38 changes: 38 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,44 @@ it(`should support hydrating package managers from cached archives`, async () =>
});
});

it(`should support hydrating package managers if cache folder was removed`, async () => {
await xfs.mktempPromise(async cwd => {
await expect(runCli(cwd, [`prepare`, `yarn@2.2.2`, `-o`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
});

// Use a new cache
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());

// Simulate cache removal
await xfs.removePromise(npath.toPortablePath(process.env.COREPACK_HOME));

// Disable the network to make sure we don't succeed by accident
process.env.COREPACK_ENABLE_NETWORK = `0`;

try {
await expect(runCli(cwd, [`hydrate`, `corepack.tgz`])).resolves.toMatchObject({
stdout: `Hydrating yarn@2.2.2...\nAll done!\n`,
stderr: ``,
exitCode: 0,
});

await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
packageManager: `yarn@2.2.2`,
});

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
stdout: `2.2.2\n`,
stderr: ``,
exitCode: 0,
});
} finally {
delete process.env.COREPACK_ENABLE_NETWORK;
}
});
});

it(`should support hydrating multiple package managers from cached archives`, async () => {
await xfs.mktempPromise(async cwd => {
await expect(runCli(cwd, [`prepare`, `yarn@2.2.2`, `pnpm@5.8.0`, `-o`])).resolves.toMatchObject({
Expand Down
Binary file added tests/nock/_2yNDOq6UlHEKY9lwBEOig-1.dat
Binary file not shown.
Binary file added tests/nock/_2yNDOq6UlHEKY9lwBEOig-2.dat
Binary file not shown.
Binary file added tests/nock/_2yNDOq6UlHEKY9lwBEOig-3.dat
Binary file not shown.

0 comments on commit 7b5f2f9

Please sign in to comment.