Skip to content

Commit

Permalink
Merge branch 'fix/exit-code-1-on-error' into release/cmd-1.1.1-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
vCaisim committed Oct 3, 2024
2 parents 0445099 + 1ab94c1 commit 85a7bad
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 47 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ The `currents cache` command allows you to archive files from specified location

To cache files, run `npx currents cache set --key <record-key> --id <id> --path <path-1,path-2,...path-n>`.

To download files, run `npx currents cache set --key <record-key> --preset last-run`.
To download files, run `npx currents cache get --key <record-key> --id <id>`.

For more examples and usage options, run `npx currents cache --help`.

Expand Down
2 changes: 1 addition & 1 deletion packages/cmd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ npx currents cache set --key <record-key> --id <id> --path <path-1,path-2,...pat
To download files, use the following command:

```sh
npx currents cache set --key <record-key> --preset last-run
npx currents cache get --key <record-key> --id <id>
```

For more examples and usage options, run `npx currents cache --help`.
Expand Down
24 changes: 15 additions & 9 deletions packages/cmd/src/commands/cache/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@ export type CacheGetCommandOpts = ReturnType<
>;

export async function getCacheGetHandler(options: CacheGetCommandOpts) {
await commandHandler(async (opts) => {
setCacheGetCommandConfig(cacheGetCommandOptsToConfig(opts));
const config = getCacheCommandConfig();
await commandHandler(
async (opts) => {
setCacheGetCommandConfig(cacheGetCommandOptsToConfig(opts));
const config = getCacheCommandConfig();

debug('Config: %o', {
...config.values,
recordKey: config.values?.recordKey ? '*****' : undefined,
});
debug('Config: %o', {
...config.values,
recordKey: config.values?.recordKey ? '*****' : undefined,
});

await handleGetCache();
}, options);
await handleGetCache();
},
options,
{
failOnError: options.fail,
}
);
}
3 changes: 3 additions & 0 deletions packages/cmd/src/commands/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
idOption,
matrixIndexOption,
matrixTotalOption,
noFailOption,
outputDirOption,
pathOption,
presetOption,
Expand Down Expand Up @@ -62,6 +63,7 @@ export const getCacheSetCommand = () => {
.addOption(pwOutputDirOption)
.addOption(matrixIndexOption)
.addOption(matrixTotalOption)
.addOption(noFailOption)
.action(getCacheSetHandler);

return command;
Expand All @@ -79,6 +81,7 @@ export const getCacheGetCommand = () => {
.addOption(debugOption)
.addOption(matrixIndexOption)
.addOption(matrixTotalOption)
.addOption(noFailOption)
.action(getCacheGetHandler);

return command;
Expand Down
5 changes: 5 additions & 0 deletions packages/cmd/src/commands/cache/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,8 @@ function validatePositiveInteger(value: string) {
}
return parsedValue;
}

export const noFailOption = new Option(
'--no-fail',
'Do not fail the process if the command fails'
);
24 changes: 15 additions & 9 deletions packages/cmd/src/commands/cache/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@ export type CacheSetCommandOpts = ReturnType<
>;

export async function getCacheSetHandler(options: CacheSetCommandOpts) {
await commandHandler(async (opts) => {
setCacheSetCommandConfig(cacheSetCommandOptsToConfig(opts));
const config = getCacheCommandConfig();
await commandHandler(
async (opts) => {
setCacheSetCommandConfig(cacheSetCommandOptsToConfig(opts));
const config = getCacheCommandConfig();

debug('Config: %o', {
...config.values,
recordKey: config.values?.recordKey ? '*****' : undefined,
});
debug('Config: %o', {
...config.values,
recordKey: config.values?.recordKey ? '*****' : undefined,
});

await handleSetCache();
}, options);
await handleSetCache();
},
options,
{
failOnError: options.fail,
}
);
}
46 changes: 23 additions & 23 deletions packages/cmd/src/commands/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommanderError } from '@commander-js/extra-typings';
import { ValidationError, Warning } from '@lib';
import { ValidationError } from '@lib';
import { error, warnWithNoTrace } from '@logger';
import { isAxiosError } from 'axios';
import { enableDebug } from '../debug';
Expand All @@ -16,36 +16,36 @@ export function parseCommaSeparatedList(

export async function commandHandler<T extends Record<string, unknown>>(
action: (options: T) => Promise<void>,
options: T
commandOptions: T,
options?: {
failOnError?: boolean;
}
) {
try {
if (options.debug) {
if (commandOptions.debug) {
enableDebug();
}
await action(options);
await action(commandOptions);
process.exit(0);
} catch (e) {
if (e instanceof CommanderError) {
error(e.message);
process.exit(e.exitCode);
}

if (e instanceof ValidationError) {
error(e.message);
process.exit(1);
}

if (e instanceof Warning) {
warnWithNoTrace(e.message);
process.exit(1);
}
const failOnError = options?.failOnError ?? true;
let exitCode = e instanceof CommanderError ? e.exitCode : 1;

if (isAxiosError(e)) {
error(e.message);
process.exit(1);
if (
e instanceof CommanderError ||
e instanceof ValidationError ||
isAxiosError(e)
) {
if (failOnError) {
error(e.message);
} else {
warnWithNoTrace(e.message);
exitCode = 0;
}
} else {
error('Script execution failed: %o', e);
}

error('Script execution failed: %o', e);
process.exit(1);
process.exit(exitCode);
}
}
2 changes: 0 additions & 2 deletions packages/cmd/src/lib/error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export class ValidationError extends Error {}

export class Warning extends Error {}
3 changes: 1 addition & 2 deletions packages/cmd/src/services/cache/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { retrieveCache } from '../../api';
import { PRESETS } from '../../commands/cache/options';
import { getCacheCommandConfig } from '../../config/cache';
import { getCI } from '../../env/ciProvider';
import { Warning } from '../../lib';
import { unzipBuffer } from './fs';
import { MetaFile } from './lib';
import { download } from './network';
Expand Down Expand Up @@ -52,7 +51,7 @@ export async function handleGetCache() {
e.response?.status &&
(e.response?.status === 403 || e.response?.status === 404)
) {
throw new Warning(`Cache with ID "${result.cacheId}" not found`);
throw new Error(`Cache with ID "${result.cacheId}" not found`);
}
}

Expand Down

0 comments on commit 85a7bad

Please sign in to comment.