Skip to content

Commit

Permalink
feat: added new generate option to control spec file name
Browse files Browse the repository at this point in the history
  • Loading branch information
hrkeni committed Mar 12, 2023
1 parent 0b60ca1 commit a4b0ed4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
8 changes: 7 additions & 1 deletion actions/generate.action.ts
Expand Up @@ -16,6 +16,7 @@ import {
shouldAskForProject,
shouldGenerateFlat,
shouldGenerateSpec,
shouldGenerateSpecFileSuffix,
} from '../lib/utils/project-utils';
import { AbstractAction } from './abstract.action';

Expand All @@ -36,6 +37,7 @@ const generateFiles = async (inputs: Input[]) => {
.value as string;
const spec = inputs.find((option) => option.name === 'spec');
const flat = inputs.find((option) => option.name === 'flat');
const specFileSuffix = inputs.find((option) => option.name === 'specFileSuffix');

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

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

// 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 @@ -107,12 +111,14 @@ const generateFiles = async (inputs: Input[]) => {
answers.appNames,
flatValue,
);
generateSpecFileSuffix = shouldGenerateSpecFileSuffix(configuration, appName, specFileSuffixValue)
}
}

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

const mapSchematicOptions = (inputs: Input[]): SchematicOption[] => {
const excludedInputNames = ['schematic', 'spec', 'flat'];
const excludedInputNames = ['schematic', 'spec', 'flat', 'specFileSuffix'];
const options: SchematicOption[] = [];
inputs.forEach((input) => {
if (!excludedInputNames.includes(input.name) && input.value !== undefined) {
Expand Down
8 changes: 8 additions & 0 deletions commands/generate.command.ts
Expand Up @@ -36,6 +36,10 @@ export class GenerateCommand extends AbstractCommand {
},
true,
)
.option(
'--spec-file-suffix [suffix]',
'Use a custom suffix for spec files.',
)
.option('--skip-import', 'Skip importing', () => true, false)
.option('--no-spec', 'Disable spec files generation.', () => {
return { value: false, passedAsInput: true };
Expand Down Expand Up @@ -71,6 +75,10 @@ export class GenerateCommand extends AbstractCommand {
: command.spec.passedAsInput,
},
});
options.push({
name: 'specFileSuffix',
value: command.specFileSuffix,
});
options.push({
name: 'collection',
value: command.collection,
Expand Down
1 change: 1 addition & 0 deletions lib/configuration/configuration.ts
Expand Up @@ -33,6 +33,7 @@ interface PluginOptions {
interface GenerateOptions {
spec?: boolean | Record<string, boolean>;
flat?: boolean;
specFileSuffix?: string;
}

export interface ProjectConfiguration {
Expand Down
1 change: 0 additions & 1 deletion lib/schematics/custom.collection.ts
@@ -1,4 +1,3 @@
import { description } from 'commander';
import { readFileSync } from 'fs';
import { dirname, join } from 'path';
import { AbstractCollection } from './abstract.collection';
Expand Down
25 changes: 25 additions & 0 deletions lib/utils/project-utils.ts
Expand Up @@ -89,6 +89,31 @@ export function shouldGenerateFlat(
return flatValue;
}

export function shouldGenerateSpecFileSuffix(
configuration: Required<Configuration>,
appName: string,
specFileSuffixValue: string,
): string {
// CLI parameters have the highest priority
if (specFileSuffixValue) {
return specFileSuffixValue;
}

const specFileSuffixConfiguration = getValueOrDefault(
configuration,
'generateOptions.specFileSuffix',
appName || '',
undefined,
undefined,
'spec',
);
if (typeof specFileSuffixConfiguration === 'string') {
return specFileSuffixConfiguration;
}
return specFileSuffixValue;
}


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

0 comments on commit a4b0ed4

Please sign in to comment.