Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelkoniecznybh committed Dec 6, 2024
1 parent d2ddef5 commit 55c59ba
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 285 deletions.
108 changes: 54 additions & 54 deletions packages/cli/src/commands/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,67 @@ import { useReadConfiguration } from "../hooks/use-read-config-file.js";
import { useValidateJsonPath } from "../hooks/use-validate-json-path.js";

export const options = z.object({
verbose: z.boolean().default(false).describe("Verbose output"),
filter: LicenseStatusSchema.optional().describe(
"Filter by license status - whitelist, blacklist, or unknown",
),
json: z
.union([z.string(), z.boolean()])
.optional()
.describe(
`Save the result to a JSON file. If no path is not provided, a file named ${JSON_RESULT_FILE_NAME} will be created in the current directory.`,
),
production: z
.boolean()
.describe(`Don't check licenses in development dependencies`),
defaultConfig: z // pacsalCase options are converted to kebab-case, so the flag is actually --default-config
.boolean()
.describe("Run audit with default whitelist/blacklist configuration"),
filterRegex: z
.string()
.optional()
.describe(
"Filter packages by a regex pattern for example: --filter-regex babel",
),
bail: z
.number()
.optional()
.describe(
"Flag is used to control the exit behavior of the CI process based on the number of blacklisted licenses. by default it is set to Infinity",
),
verbose: z.boolean().default(false).describe("Verbose output"),
filter: LicenseStatusSchema.optional().describe(
"Filter by license status - whitelist, blacklist, or unknown",
),
json: z
.union([z.string(), z.boolean()])
.optional()
.describe(
`Save the result to a JSON file. If no path is not provided, a file named ${JSON_RESULT_FILE_NAME} will be created in the current directory.`,
),
production: z
.boolean()
.describe(`Don't check licenses in development dependencies`),
defaultConfig: z // pacsalCase options are converted to kebab-case, so the flag is actually --default-config
.boolean()
.describe("Run audit with default whitelist/blacklist configuration"),
filterRegex: z
.string()
.optional()
.describe(
"Filter packages by a regex pattern for example: --filter-regex babel",
),
bail: z
.number()
.optional()
.describe(
"Flag is used to control the exit behavior of the CI process based on the number of blacklisted licenses. by default it is set to Infinity",
),
});

export type Options = {
options: z.infer<typeof options>;
options: z.infer<typeof options>;
};

export default function Index({ options }: Options) {
const { configFile, error } = useReadConfiguration({
useDefaults: options.defaultConfig,
});
const validateJsonResult = useValidateJsonPath(options.json);
const { configFile, error } = useReadConfiguration({
useDefaults: options.defaultConfig,
});
const validateJsonResult = useValidateJsonPath(options.json);

if (error) {
return <ConfigErrorHandler error={error} />;
}
if (error) {
return <ConfigErrorHandler error={error} />;
}

if (configFile?.config && validateJsonResult.validated) {
return (
<Box flexDirection="column">
<Text>Loaded configuration file: {configFile.filepath}</Text>
<AuditLicenses
config={configFile.config}
json={validateJsonResult.path}
flags={options}
/>
</Box>
);
}
if (configFile?.config && validateJsonResult.validated) {
return (
<Box flexDirection="column">
<Text>Loaded configuration file: {configFile.filepath}</Text>
<AuditLicenses
config={configFile.config}
json={validateJsonResult.path}
flags={options}
/>
</Box>
);
}

return (
<Box>
<Spinner />
<Text>Reading configuration...</Text>
</Box>
);
return (
<Box>
<Spinner />
<Text>Reading configuration...</Text>
</Box>
);
}
166 changes: 83 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,94 @@ 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) {
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>
);
}
Loading

0 comments on commit 55c59ba

Please sign in to comment.