Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Commit

Permalink
feat: added format flags for unused, unimported and unresolved to pri…
Browse files Browse the repository at this point in the history
…nt them separately (#139)

* feat: added format flags for unused, unimported and unresolved to print them separately

* feat: removed unwanted console

---------

Co-authored-by: Aditya Vandan Sharma <adityavandansharma@Adityas-MacBook-Pro.local>
  • Loading branch information
AdityaVandan and Aditya Vandan Sharma authored Mar 2, 2023
1 parent a4b11db commit d7724a1
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 27 deletions.
6 changes: 6 additions & 0 deletions src/__tests__/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ async function exec(
cache = true,
clearCache = false,
showConfig = false,
showUnresolvedImports = false,
showUnusedFiles = false,
showUnusedDeps = false,
}: Partial<CliArguments> = {},
): Promise<{ exitCode: number | null; stdout: string; stderr: string }> {
const originalExit = process.exit;
Expand Down Expand Up @@ -86,6 +89,9 @@ async function exec(
cache,
clearCache,
showConfig,
showUnresolvedImports,
showUnusedFiles,
showUnusedDeps,
});

return { exitCode: exitCode ?? 0, stdout, stderr };
Expand Down
56 changes: 32 additions & 24 deletions src/__tests__/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,37 @@ test('npx unimported --help', async () => {
const { stdout, stderr } = execResults;

expect(stderr).toBe('');
expect(stdout.trim()).toMatchInlineSnapshot(`
"unimported [cwd]
scan your project for dead files

Positionals:
cwd The root directory that unimported should run from. [string]
Options:
--version Show version number [boolean]
--help Show help [boolean]
--cache Whether to use the cache. Disable the cache using
--no-cache. [boolean] [default: true]
--clear-cache Clears the cache file and then exits. [boolean]
-f, --flow Whether to strip flow types, regardless of @flow
pragma. [boolean]
--ignore-untracked Ignore files that are not currently tracked by git.
[boolean]
-i, --init Dump default settings to .unimportedrc.json. [boolean]
--show-config Show config and then exists. [boolean]
--show-preset Show preset and then exists. [string]
-u, --update Update the ignore-lists stored in .unimportedrc.json.
[boolean]
--config The path to the config file. [string]"
`);
expect(stdout.trim()).toMatchInlineSnapshot(`
"unimported [cwd]
scan your project for dead files
Positionals:
cwd The root directory that unimported should run from. [string]
Options:
--version Show version number [boolean]
--help Show help [boolean]
--cache Whether to use the cache. Disable the cache
using --no-cache. [boolean] [default: true]
--clear-cache Clears the cache file and then exits. [boolean]
-f, --flow Whether to strip flow types, regardless of
@flow pragma. [boolean]
--ignore-untracked Ignore files that are not currently tracked by
git. [boolean]
-i, --init Dump default settings to .unimportedrc.json.
[boolean]
--show-config Show config and then exists. [boolean]
--show-preset Show preset and then exists. [string]
-u, --update Update the ignore-lists stored in
.unimportedrc.json. [boolean]
--config The path to the config file. [string]
--show-unused-files formats and only prints unimported files
[boolean]
--show-unused-deps formats and only prints unused dependencies
[boolean]
--show-unresolved-imports formats and only prints unresolved imports
[boolean]"
`);
});
3 changes: 3 additions & 0 deletions src/__tests__/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ describe('printResults', () => {
ignoreUnused: [],
ignoreUnresolved: [],
},
showUnresolvedImports: false,
showUnusedDeps: false,
showUnusedFiles: false,
} as Context;

let restore: any;
Expand Down
21 changes: 21 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export interface Context {
config: Config;
moduleDirectory: string[];
cacheId?: string;
showUnusedFiles: boolean;
showUnusedDeps: boolean;
showUnresolvedImports: boolean;
}

const oraStub = {
Expand Down Expand Up @@ -304,6 +307,9 @@ export interface CliArguments {
showConfig: boolean;
showPreset?: string;
config?: string;
showUnusedFiles: boolean;
showUnusedDeps: boolean;
showUnresolvedImports: boolean;
}

if (process.env.NODE_ENV !== 'test') {
Expand Down Expand Up @@ -369,6 +375,21 @@ if (process.env.NODE_ENV !== 'test') {
type: 'string',
describe: 'The path to the config file.',
});

yargs.option('show-unused-files', {
type: 'boolean',
describe: 'formats and only prints unimported files',
});

yargs.option('show-unused-deps', {
type: 'boolean',
describe: 'formats and only prints unused dependencies',
});

yargs.option('show-unresolved-imports', {
type: 'boolean',
describe: 'formats and only prints unresolved imports',
});
},
function (argv: Arguments<CliArguments>) {
return main(argv);
Expand Down
27 changes: 24 additions & 3 deletions src/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export function printResults(result: ProcessedResult, context: Context): void {
return;
}

const { showUnresolved, showUnused, showUnimported } = chooseResults(context);
const { unresolved, unused, unimported } = result;

// render
Expand All @@ -79,7 +80,7 @@ export function printResults(result: ProcessedResult, context: Context): void {
),
);

if (unresolved.length > 0) {
if (showUnresolved && unresolved.length > 0) {
console.log(
formatList(
chalk.redBright(`${unresolved.length} unresolved imports`),
Expand All @@ -88,7 +89,7 @@ export function printResults(result: ProcessedResult, context: Context): void {
);
}

if (unused.length > 0) {
if (showUnused && unused.length > 0) {
console.log(
formatList(
chalk.blueBright(`${unused.length} unused dependencies`),
Expand All @@ -97,7 +98,7 @@ export function printResults(result: ProcessedResult, context: Context): void {
);
}

if (unimported.length > 0) {
if (showUnimported && unimported.length > 0) {
console.log(
formatList(
chalk.cyanBright(`${unimported.length} unimported files`),
Expand All @@ -112,3 +113,23 @@ export function printResults(result: ProcessedResult, context: Context): void {
)} to update ignore lists`,
);
}

function chooseResults(context: Context) {
const { showUnresolvedImports, showUnusedDeps, showUnusedFiles } = context;
const showAllResults =
// when all three flags are used
(showUnresolvedImports && showUnusedDeps && showUnusedFiles) ||
// when none flag is used
(!showUnresolvedImports && !showUnusedDeps && !showUnusedFiles);

const showUnresolved = showUnresolvedImports || showAllResults;
const showUnused = showUnusedDeps || showAllResults;
const showUnimported = showUnusedFiles || showAllResults;

return {
showAllResults,
showUnresolved,
showUnused,
showUnimported,
};
}

0 comments on commit d7724a1

Please sign in to comment.