Skip to content

Commit

Permalink
add cache clean command
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmurdoch committed Nov 13, 2024
1 parent ef1b515 commit 09725a7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
15 changes: 12 additions & 3 deletions test/helpers/foundry/foundryup.mts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,26 @@ import {
noop,
} from './helpers.mts';
import { Dir } from 'node:fs';
import { opendir, symlink, unlink, copyFile } from 'node:fs/promises';
import { opendir, symlink, unlink, copyFile, rm } from 'node:fs/promises';
import { createHash } from 'node:crypto';

const parsedArgs = parseArgs();

const CACHE_DIR = join(homedir(), '.cache', 'metamask-extension');

if (parsedArgs.command === 'cache clean') {
await rm(CACHE_DIR, { recursive: true, force: true });
say('done!');
process.exit(0);
}

const {
repo,
version: { version, tag },
arch,
platform,
binaries,
} = parseArgs();
} = parsedArgs.options;

printBanner();

Expand All @@ -34,7 +44,6 @@ say(
const ext = platform === Platform.Windows ? BinFormat.Zip : BinFormat.Tar;
const BIN_ARCHIVE_URL = `https://github.com/${repo}/releases/download/${tag}/foundry_${version}_${platform}_${arch}.${ext}`;
const BIN_DIR = join(process.cwd(), 'node_modules', '.bin');
const CACHE_DIR = join(homedir(), '.cache', 'metamask-extension');

say(`downloading ${binaries.join(', ')}`);
const url = new URL(BIN_ARCHIVE_URL);
Expand Down
28 changes: 23 additions & 5 deletions test/helpers/foundry/helpers.mts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Stream } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import { extract as extractTar } from 'tar';
import { Source, Open as Unzip } from 'unzipper';
import { Argv, InferredOptionTypes } from 'yargs';
import yargs from 'yargs/yargs';

export function noop() {}
Expand All @@ -43,6 +44,9 @@ export enum Binary {

type Options = ReturnType<typeof getOptions>;
type OptionsKeys = keyof Options;
type ParsedOptions = {
[key in OptionsKeys]: InferredOptionTypes<Options>[key];
};

export function parseArgs(args: string[] = argv.slice(2)) {
const { $0, _, ...parsed } = yargs()
Expand All @@ -54,19 +58,33 @@ export function parseArgs(args: string[] = argv.slice(2)) {
.scriptName('yarn foundryup')
// wrap output at a maximum of 120 characters or `process.stdout.columns`
.wrap(Math.min(120, stdout.columns))
// enable the `--config` command, which allows the user to specify a custom
// config file containing webpack options
.config()
.parserConfiguration({
'strip-aliased': true,
'strip-dashed': true,
})
// enable ENV parsing, which allows the user to specify foundryup options
// via environment variables prefixed with `FOUNDRYUP_`
.env('FOUNDRYUP')
.options(getOptions())
.command(['$0', 'install'], 'Install foundry binaries', getOptions())
.command('cache', '', (yargs) => {
yargs.command('clean', 'Remove the shared cache files').demandCommand();
})
.parseSync(args);
return parsed as { [key in OptionsKeys]: (typeof parsed)[key] };

const command = _.join(' ');
switch (command) {
case 'cache clean':
return {
command,
} as const;
case '':
case 'install':
return {
command: 'install',
options: parsed as ParsedOptions,
} as const;
}
throw new Error(`Unknown command: '${command}'`);
}

function getOptions() {
Expand Down

0 comments on commit 09725a7

Please sign in to comment.