Skip to content

Commit

Permalink
Merge pull request #1805 from adamclark64/feat-flat-configuration
Browse files Browse the repository at this point in the history
feat(config): allow for flat generate option configuration
  • Loading branch information
kamilmysliwiec committed Oct 27, 2022
2 parents 3ab5a4e + 6b31f51 commit 6a933a8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
8 changes: 7 additions & 1 deletion actions/generate.action.ts
Expand Up @@ -14,6 +14,7 @@ import {
askForProjectName,
moveDefaultProjectToStart,
shouldAskForProject,
shouldGenerateFlat,
shouldGenerateSpec,
} from '../lib/utils/project-utils';
import { AbstractAction } from './abstract.action';
Expand All @@ -34,6 +35,7 @@ const generateFiles = async (inputs: Input[]) => {
const appName = inputs.find((option) => option.name === 'project')!
.value as string;
const spec = inputs.find((option) => option.name === 'spec');
const flat = inputs.find((option) => option.name === 'flat');

const collection: AbstractCollection = CollectionFactory.create(
collectionOption || configuration.collection || Collection.NESTJS,
Expand All @@ -49,6 +51,7 @@ const generateFiles = async (inputs: Input[]) => {
: configuration.sourceRoot;

const specValue = spec!.value as boolean;
const flatValue = !!flat as boolean;
const specOptions = spec!.options as any;
let generateSpec = shouldGenerateSpec(
configuration,
Expand All @@ -57,6 +60,7 @@ const generateFiles = async (inputs: Input[]) => {
specValue,
specOptions.passedAsInput,
);
let generateFlat = shouldGenerateFlat(configuration, appName, flatValue);

// If you only add a `lib` we actually don't have monorepo: true BUT we do have "projects"
// Ensure we don't run for new app/libs schematics
Expand Down Expand Up @@ -98,11 +102,13 @@ const generateFiles = async (inputs: Input[]) => {
specValue,
specOptions.passedAsInput,
);
generateFlat = shouldGenerateFlat(configuration, answers.appNames, flatValue);
}
}

schematicOptions.push(new SchematicOption('sourceRoot', sourceRoot));
schematicOptions.push(new SchematicOption('spec', generateSpec));
schematicOptions.push(new SchematicOption('flat', generateFlat));
try {
const schematicInput = inputs.find((input) => input.name === 'schematic');
if (!schematicInput) {
Expand All @@ -117,7 +123,7 @@ const generateFiles = async (inputs: Input[]) => {
};

const mapSchematicOptions = (inputs: Input[]): SchematicOption[] => {
const excludedInputNames = ['schematic', 'spec'];
const excludedInputNames = ['schematic', 'spec', 'flat'];
const options: SchematicOption[] = [];
inputs.forEach((input) => {
if (!excludedInputNames.includes(input.name) && input.value !== undefined) {
Expand Down
1 change: 1 addition & 0 deletions lib/configuration/configuration.ts
Expand Up @@ -32,6 +32,7 @@ interface PluginOptions {

interface GenerateOptions {
spec?: boolean | Record<string, boolean>;
flat?: boolean;
}

export interface ProjectConfiguration {
Expand Down
21 changes: 21 additions & 0 deletions lib/utils/project-utils.ts
Expand Up @@ -68,6 +68,27 @@ export function shouldGenerateSpec(
return specValue;
}

export function shouldGenerateFlat(
configuration: Required<Configuration>,
appName: string,
flatValue: boolean,
): boolean {
// CLI parameters have the highest priority
if (flatValue === true) {
return flatValue;
}

const flatConfiguration = getValueOrDefault(
configuration,
'generateOptions.flat',
appName || '',
);
if (typeof flatConfiguration === 'boolean') {
return flatConfiguration;
}
return flatValue;
}

export async function askForProjectName(
promptQuestion: string,
projects: string[],
Expand Down

0 comments on commit 6a933a8

Please sign in to comment.