Skip to content

Commit

Permalink
feat: add json output mode for check command
Browse files Browse the repository at this point in the history
  • Loading branch information
fengkx committed Nov 16, 2023
1 parent 7a05dcc commit 167477c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ yarn-error.log*

# es-iterator-helpers
packages/manual/es-iterator-helpers/**/*.js

# yalc
.yalc
yalc.lock
17 changes: 16 additions & 1 deletion packages/tools/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ interface PmCommandOptions {
pm: PackageManager | 'auto'
}

interface CheckCommandOptions extends PmCommandOptions {
format: 'normal' | 'json'
}

const pmCommandOption = new Option('--pm [package manager]', 'specify which package manager to use')
.choices(['auto', 'npm', 'pnpm', 'yarn'])
.default('auto', 'detect package manager automatically');
Expand Down Expand Up @@ -72,16 +76,27 @@ const program = new Command('nolyfill');
.description('check if the project contains redundant polyfills that can be optimized by nolyfill')
.argument('[path]', 'project path to check')
.addOption(pmCommandOption)
.action(async (source: string | undefined, option: PmCommandOptions) => {
.addOption(new Option('-f --format [format]', 'output format for console')
.choices(['normal', 'json'])
.default('normal')
)
.action(async (source: string | undefined, option: CheckCommandOptions) => {
const projectPath = path.resolve(source ?? process.cwd());
const packageManager = option.pm === 'auto' ? await detectPackageManager(projectPath) : option.pm;
const format = option.format;

if (checkUnsupportedPM(packageManager)) {
return;
}

const packagesToBeOverride = await findPackagesCoveredByNolyfill(packageManager, projectPath);

if (format === 'json') {
const packagesNotCoveredByNolyfill = await findPackagesNotCoveredByNolyfill(packageManager, projectPath);
console.log(JSON.stringify({ packagesCoveredByNolyfill: packagesToBeOverride, packagesNotCoveredByNolyfill }));
return;
}

if (packagesToBeOverride.length === 0) {
const packagesNotCoveredByNolyfill = await findPackagesNotCoveredByNolyfill(packageManager, projectPath);

Expand Down

0 comments on commit 167477c

Please sign in to comment.