Skip to content

Commit

Permalink
feat: add exit codes to some catch statements
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelkoniecznybh committed Dec 9, 2024
1 parent 55ee0ae commit 4219155
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 120 deletions.
167 changes: 84 additions & 83 deletions packages/cli/src/components/audit-licenses/audit-licenses.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { auditLicenses } from "@brainhubeu/license-auditor-core";
import type {
ConfigType,
LicenseAuditResult,
LicenseStatus,
ConfigType,
LicenseAuditResult,
LicenseStatus,
} from "@license-auditor/data";
import { Box, Text, useApp } from "ink";
import { useEffect, useState } from "react";
Expand All @@ -14,94 +14,95 @@ import AuditResult from "./audit-result.js";
import ErrorBox from "./error-box.js";

export type AuditLicensesProps = {
flags: {
verbose: boolean;
filter?: LicenseStatus | undefined;
filterRegex?: string | undefined;
bail?: number | undefined;
production: boolean | undefined;
};
config: ConfigType;
json: string | undefined;
flags: {
verbose: boolean;
filter?: LicenseStatus | undefined;
filterRegex?: string | undefined;
bail?: number | undefined;
production: boolean | undefined;
};
config: ConfigType;
json: string | undefined;
};

export default function AuditLicenses({
config,
json,
flags: { verbose, filter, production, filterRegex, bail },
config,
json,
flags: { verbose, filter, production, filterRegex, bail },
}: AuditLicensesProps) {
const [working, setWorking] = useState(true);
const [error, setError] = useState<string | null>(null);
const [warning, setWarning] = useState<string | null>(null);
const [result, setResult] = useState<LicenseAuditResult | null>(null);
const { exit } = useApp();
const [working, setWorking] = useState(true);
const [error, setError] = useState<string | null>(null);
const [warning, setWarning] = useState<string | null>(null);
const [result, setResult] = useState<LicenseAuditResult | null>(null);
const { exit } = useApp();

useEffect(() => {
setWorking(true);
useEffect(() => {
setWorking(true);

const getResults = async () => {
try {
const parsedEnv = envSchema.safeParse(process.env);
if (parsedEnv.error) {
setError(parsedEnv.error.message);
return;
}
const { warning, ...result } = await auditLicenses({
cwd: parsedEnv.data.ROOT_DIR,
config,
filterRegex,
production,
verbose,
});
setResult(result);
if (warning) {
setWarning(warning);
}
setWorking(false);
exit();
} catch (err) {
setError(
err instanceof Error ? err.message : "An unknown error occurred",
);
setWorking(false);
exit();
}
};
void getResults();
}, [exit, config, production, filterRegex, verbose]);
const getResults = async () => {
try {
const parsedEnv = envSchema.safeParse(process.env);
if (parsedEnv.error) {
setError(parsedEnv.error.message);
return;
}
const { warning, ...result } = await auditLicenses({
cwd: parsedEnv.data.ROOT_DIR,
config,
filterRegex,
production,
verbose,
});
setResult(result);
if (warning) {
setWarning(warning);
}
setWorking(false);
exit();
} catch (err) {
setError(
err instanceof Error ? err.message : "An unknown error occurred",
);
setWorking(false);
exit();
}
};
void getResults();
}, [exit, config, production, filterRegex, verbose]);

useEffect(() => {
if (result && json) {
saveResultToJson(result, json);
}
}, [json, result]);
useEffect(() => {
if (result && json) {
saveResultToJson(result, json);
}
}, [json, result]);

if (error) {
return (
<>
<ErrorBox>An error occurred while auditing licenses: {error}</ErrorBox>
{!verbose && (
<Text color="red">
Run the command with --verbose flag to get full error output
</Text>
)}
</>
);
}
if (error) {
process.exitCode = 1;
return (
<>
<ErrorBox>An error occurred while auditing licenses: {error}</ErrorBox>
{!verbose && (
<Text color="red">
Run the command with --verbose flag to get full error output
</Text>
)}
</>
);
}

if (working || !result) {
return <SpinnerWithLabel label="Processing licenses..." />;
}
if (working || !result) {
return <SpinnerWithLabel label="Processing licenses..." />;
}

return (
<Box flexDirection="column">
<AuditResult
result={result}
warning={warning}
overrides={config.overrides}
flags={{ verbose, filter, bail }}
/>
<AdditionalInfo verbose={verbose} />
</Box>
);
return (
<Box flexDirection="column">
<AuditResult
result={result}
warning={warning}
overrides={config.overrides}
flags={{ verbose, filter, bail }}
/>
<AdditionalInfo verbose={verbose} />
</Box>
);
}
75 changes: 38 additions & 37 deletions packages/cli/src/components/config-error-handler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,56 @@ import { useState } from "react";
import Init from "../commands/init.js";
import type { SelectItem } from "../constants/select-constants.js";
import {
ReadConfigErrorType,
type ReadConfigurationError,
ReadConfigErrorType,
type ReadConfigurationError,
} from "../hooks/use-read-config-file.js";

interface ReadConfigurationErrorProps {
error: ReadConfigurationError;
error: ReadConfigurationError;
}

export const booleanSelectItems: SelectItem<boolean>[] = [
{
label: "Yes",
value: true,
},
{
label: "No",
value: false,
},
{
label: "Yes",
value: true,
},
{
label: "No",
value: false,
},
] as const;

export function ConfigErrorHandler({ error }: ReadConfigurationErrorProps) {
switch (error.type) {
case ReadConfigErrorType.NotFound:
return <ConfigFileNotFoundHandler message={error.message} />;
default:
return <Text>{error.message}</Text>;
}
process.exitCode = 1;
switch (error.type) {
case ReadConfigErrorType.NotFound:
return <ConfigFileNotFoundHandler message={error.message} />;
default:
return <Text>{error.message}</Text>;
}
}

function ConfigFileNotFoundHandler({ message }: { message: string }) {
const { exit } = useApp();
const [shouldCreateConfig, setShouldCreateConfig] = useState(false);
const { exit } = useApp();
const [shouldCreateConfig, setShouldCreateConfig] = useState(false);

if (shouldCreateConfig) {
return <Init />;
}
if (shouldCreateConfig) {
return <Init />;
}

return (
<Box flexDirection="column">
<Text>{message}</Text>
<Text>Would you like to create a configuration file now? (Y/n)</Text>
<SelectInput
items={booleanSelectItems}
onSelect={(item) => {
if (!item.value) {
exit();
}
setShouldCreateConfig(item.value);
}}
/>
</Box>
);
return (
<Box flexDirection="column">
<Text>{message}</Text>
<Text>Would you like to create a configuration file now? (Y/n)</Text>
<SelectInput
items={booleanSelectItems}
onSelect={(item) => {
if (!item.value) {
exit();
}
setShouldCreateConfig(item.value);
}}
/>
</Box>
);
}

0 comments on commit 4219155

Please sign in to comment.