Skip to content

Commit

Permalink
feat: Read rest of options from custom configuration file (#391)
Browse files Browse the repository at this point in the history
## Problem

Not all of the options that can be set through CLI flags are able to be
read in from the custom config file. These being:
- `useMaintainers`
- `useRootMaintainers`
- `preserveBlockPosition`

It would improve dev experience to also set these in the custom config
and have them be respected like the other options.

## Changes

- Update `generate` to read in the options mentioned above from custom
config
- Connect the `CustomConfig`, `GlobalOptions` and `Options` types
- Small refactors
  • Loading branch information
Djontleman authored Jan 12, 2024
1 parent 6590d1a commit 80756fe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
24 changes: 8 additions & 16 deletions src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ora from 'ora';
import { posix as path } from 'path';
import fs from 'fs';
import { sync } from 'fast-glob';
import { Command, getGlobalOptions } from '../utils/getGlobalOptions';
import { Command, GlobalOptions, getGlobalOptions } from '../utils/getGlobalOptions';
import {
OUTPUT,
INCLUDES,
Expand All @@ -21,7 +21,6 @@ const debug = logger('generate');
type Generate = (options: GenerateInput) => Promise<ownerRule[]>;
type GenerateInput = {
rootDir: string;
verifyPaths?: boolean;
useMaintainers?: boolean;
useRootMaintainers?: boolean;
includes?: string[];
Expand Down Expand Up @@ -97,45 +96,38 @@ export const generate: Generate = async ({ rootDir, includes, useMaintainers = f
}
};

interface Options {
output?: string;
verifyPaths?: boolean;
useMaintainers?: boolean;
useRootMaintainers?: boolean;
groupSourceComments?: boolean;
preserveBlockPosition?: boolean;
includes?: string[];
customRegenerationCommand?: string;
interface Options extends GlobalOptions {
check?: boolean;
}

export const command = async (options: Options, command: Command): Promise<void> => {
const globalOptions = await getGlobalOptions(command);

const { verifyPaths, useMaintainers, useRootMaintainers, check, preserveBlockPosition } = options;
const { check } = options;

const { output = globalOptions.output || OUTPUT } = options;
const output = options.output || globalOptions.output || OUTPUT;

const loader = ora('generating codeowners...').start();

const useMaintainers = globalOptions.useMaintainers || options.useMaintainers;
const useRootMaintainers = globalOptions.useRootMaintainers || options.useRootMaintainers;
const groupSourceComments = globalOptions.groupSourceComments || options.groupSourceComments;

const preserveBlockPosition = globalOptions.preserveBlockPosition || options.preserveBlockPosition;
const customRegenerationCommand = globalOptions.customRegenerationCommand || options.customRegenerationCommand;

debug('Options:', {
...globalOptions,
output,
useMaintainers,
useRootMaintainers,
groupSourceComments,
preserveBlockPosition,
customRegenerationCommand,
output,
});

try {
const ownerRules = await generate({
rootDir: __dirname,
verifyPaths,
useMaintainers,
useRootMaintainers,
...globalOptions,
Expand Down
4 changes: 3 additions & 1 deletion src/utils/getCustomConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import packageJSON from '../../package.json';
import { logger } from './debug';

const debug = logger('customConfiguration');

export type CustomConfig = {
includes?: string[];
output?: string;
useMaintainers?: boolean;
useRootMaintainers?: boolean;
groupSourceComments?: boolean;
output?: string;
preserveBlockPosition?: boolean;
customRegenerationCommand?: string;
};

Expand Down
10 changes: 3 additions & 7 deletions src/utils/getGlobalOptions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { getCustomConfiguration } from './getCustomConfiguration';
interface GlobalOptions {
includes?: string[];
output?: string;
customRegenerationCommand?: string;
groupSourceComments?: boolean;
}
import { CustomConfig, getCustomConfiguration } from './getCustomConfiguration';

export type GlobalOptions = CustomConfig;
export interface Command {
parent: Partial<GlobalOptions>;
}
Expand Down

0 comments on commit 80756fe

Please sign in to comment.