Skip to content

Commit

Permalink
refactor(models): add global options (#76)
Browse files Browse the repository at this point in the history
This PR includes a small refactoring to align with #73
  • Loading branch information
BioPhoton authored Oct 2, 2023
1 parent dd8ddae commit 28966a2
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { yargsCli } from './lib/cli';
import { yargsGlobalOptionsDefinition } from './lib/options';
import { options } from './lib/options';
import { middlewares } from './lib/middlewares';
import { commands } from './lib/commands';

export { options } from './lib/options';
export { middlewares } from './lib/middlewares';
export { commands } from './lib/commands';

export const cli = (args: string[]) =>
yargsCli(args, {
usageMessage: 'Code PushUp CLI',
scriptName: 'code-pushup',
options: yargsGlobalOptionsDefinition(),
options,
middlewares,
commands,
});
6 changes: 3 additions & 3 deletions packages/cli/src/lib/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { join } from 'path';
import { describe, expect, it } from 'vitest';
import { yargsCli } from './cli';
import { CommandBase } from './implementation/base-command-config';
import { getDirname } from './implementation/utils';
import { middlewares } from './middlewares';
import { yargsGlobalOptionsDefinition } from './options';
import { options as defaultOptions } from './options';
import { CommandBase } from './implementation/model';

const __dirname = getDirname(import.meta.url);
const withDirName = (path: string) => join(__dirname, path);
const validConfigPath = withDirName('implementation/mock/cli-config.mock.js');

const options = yargsGlobalOptionsDefinition();
const options = defaultOptions;
const demandCommand: [number, string] = [0, 'no command required'];

describe('CLI arguments parsing', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/lib/collect/command-object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { join } from 'node:path';
import { yargsCli } from '../cli';
import { getDirname, logErrorBeforeThrow } from '../implementation/utils';
import { middlewares } from '../middlewares';
import { yargsGlobalOptionsDefinition } from '../options';
import { yargsGlobalOptionsDefinition } from '../implementation/global-options';
import { yargsCollectCommandObject } from './command-object';

const command = {
Expand Down
11 changes: 0 additions & 11 deletions packages/cli/src/lib/implementation/base-command-config.ts

This file was deleted.

7 changes: 3 additions & 4 deletions packages/cli/src/lib/implementation/config-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { bundleRequire } from 'bundle-require';
import { stat } from 'fs/promises';

import { GlobalCliArgs, globalCliArgsSchema } from '@quality-metrics/models';
import { CommandBase, commandBaseSchema } from './base-command-config';
import { GlobalOptions, globalOptionsSchema } from '../model';
import { CommandBase, commandBaseSchema } from './model';

export class ConfigParseError extends Error {
constructor(configPath: string) {
Expand All @@ -13,7 +12,7 @@ export class ConfigParseError extends Error {
export async function configMiddleware<T = unknown>(
processArgs: T,
): Promise<CommandBase> {
const globalCfg: GlobalCliArgs = globalCliArgsSchema.parse(processArgs);
const globalCfg: GlobalOptions = globalOptionsSchema.parse(processArgs);
const { configPath } = globalCfg;
try {
const stats = await stat(configPath);
Expand Down
26 changes: 26 additions & 0 deletions packages/cli/src/lib/implementation/global-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { GlobalOptions } from '../model';
import { Options } from 'yargs';

export function yargsGlobalOptionsDefinition(): Record<
keyof GlobalOptions,
Options
> {
return {
interactive: {
describe: 'When false disables interactive input prompts for options.',
type: 'boolean',
default: true,
},
verbose: {
describe:
'When true creates more verbose output. This is helpful when debugging.',
type: 'boolean',
default: false,
},
configPath: {
describe: 'Path the the config file, e.g. code-pushup.config.js',
type: 'string',
default: 'code-pushup.config.js',
},
};
}
24 changes: 24 additions & 0 deletions packages/cli/src/lib/implementation/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
globalOptionsSchema as coreGlobalOptionsSchema,
refineCoreConfig,
unrefinedCoreConfigSchema,
} from '@quality-metrics/models';
import { z } from 'zod';

export const globalOptionsSchema = coreGlobalOptionsSchema.merge(
z.object({
interactive: z
.boolean({
description:
'flag if interactivity should be considered. Useful for CI runs.',
})
.default(true),
}),
);

export type GlobalOptions = z.infer<typeof globalOptionsSchema>;

export const commandBaseSchema = refineCoreConfig(
globalOptionsSchema.merge(unrefinedCoreConfigSchema),
);
export type CommandBase = z.infer<typeof commandBaseSchema>;
15 changes: 15 additions & 0 deletions packages/cli/src/lib/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { globalOptionsSchema as coreGlobalOptionsSchema } from '@quality-metrics/models';
import { z } from 'zod';

export const globalOptionsSchema = coreGlobalOptionsSchema.merge(
z.object({
interactive: z
.boolean({
description:
'flag if interactivity should be considered. Useful for CI runs.',
})
.default(true),
}),
);

export type GlobalOptions = z.infer<typeof globalOptionsSchema>;
25 changes: 4 additions & 21 deletions packages/cli/src/lib/options.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
import { Options } from 'yargs';
import { yargsGlobalOptionsDefinition } from './implementation/global-options';

export function yargsGlobalOptionsDefinition(): Record<string, Options> {
return {
interactive: {
describe: 'When false disables interactive input prompts for options.',
type: 'boolean',
default: true,
},
verbose: {
describe:
'When true creates more verbose output. This is helpful when debugging.',
type: 'boolean',
default: false,
},
configPath: {
describe: 'Path the the config file, e.g. code-pushup.config.js',
type: 'string',
default: 'code-pushup.config.js',
},
};
}
export const options = {
...yargsGlobalOptionsDefinition(),
};
2 changes: 1 addition & 1 deletion packages/models/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { CategoryConfig, categoryConfigSchema } from './lib/category-config';
export { GlobalCliArgs, globalCliArgsSchema } from './lib/global-cli-options';
export { GlobalOptions, globalOptionsSchema } from './lib/global-options';
export {
CoreConfig,
coreConfigSchema,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { z } from 'zod';
import { generalFilePathSchema } from './implementation/schemas';

export const globalCliArgsSchema = z.object({
interactive: z
.boolean({
description:
'flag if interactivity should be considered. Useful for CI runs.',
})
.default(true),
export const globalOptionsSchema = z.object({
verbose: z
.boolean({
description: 'Outputs additional information for a run',
Expand All @@ -20,4 +14,4 @@ export const globalCliArgsSchema = z.object({
.default('code-pushup.config.js'),
});

export type GlobalCliArgs = z.infer<typeof globalCliArgsSchema>;
export type GlobalOptions = z.infer<typeof globalOptionsSchema>;
4 changes: 2 additions & 2 deletions packages/utils/src/lib/collect/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CoreConfig, GlobalCliArgs, Report } from '@quality-metrics/models';
import { CoreConfig, GlobalOptions, Report } from '@quality-metrics/models';
import { executePlugins } from './implementation/execute-plugin';
import { calcDuration } from './implementation/utils';

Expand All @@ -17,7 +17,7 @@ export class CollectOutputError extends Error {
}
}

export type CollectOptions = GlobalCliArgs & CoreConfig;
export type CollectOptions = GlobalOptions & CoreConfig;

/**
* Run audits, collect plugin output and aggregate it into a JSON object
Expand Down

0 comments on commit 28966a2

Please sign in to comment.