Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace command inputs and options by a dictionary-like high-level data abstraction #2180

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
b262fad
refactor(commands): type safe available builders list
micalevisk Jul 16, 2023
fcda32b
refactor: replace command inputs by dictionary-like data abstraction
micalevisk Jul 16, 2023
4aa56a5
style: fix formatting
micalevisk Jul 17, 2023
ffb3c19
refactor: rename input container
micalevisk Jul 22, 2023
62ec68c
refactor: drop input in favor of command storage abstraction
micalevisk Jul 22, 2023
53e0fae
style: fix formatting
micalevisk Jul 22, 2023
027b76f
style: remove unused type assertion
micalevisk Jul 22, 2023
b36c6af
refactor: remove useless non-null assertion operator
micalevisk Jul 22, 2023
27f7db8
refactor: drop `toArray` from `CommandStorage`
micalevisk Jul 22, 2023
603512f
feat: type coupling command class with their action
micalevisk Jul 22, 2023
2ceb047
refactor: better naming to command storage abstraction file name
micalevisk Jul 22, 2023
79e7cfc
refactor: remove useless from command generate action
micalevisk Jul 22, 2023
519ecbf
refactor: remove useless type assertion
micalevisk Jul 22, 2023
9ae3d3e
Merge branch 'master' into refactor/proper-abstraction-to-command-inputs
micalevisk Jul 22, 2023
5a3f6e5
fix: minor changes to met the current behavior
micalevisk Jul 22, 2023
da4dd70
refactor: make default values more clear to command actions
micalevisk Jul 30, 2023
0cd95b5
refactor: move partial handler funcion from `GenerateAction`
micalevisk Jul 30, 2023
b5afd0e
Merge branch 'master' into refactor/proper-abstraction-to-command-inputs
micalevisk Aug 5, 2023
de97d47
refactor: rename `CommandStorage` to `CommandContext`
micalevisk Oct 29, 2023
a738f1f
Merge branch 'master' into refactor/proper-abstraction-to-command-inputs
micalevisk Oct 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions actions/abstract.action.ts
@@ -1,9 +1,9 @@
import { Input } from '../commands';
import { CommandStorage } from '../commands';

export abstract class AbstractAction {
public abstract handle(
inputs?: Input[],
options?: Input[],
inputs?: CommandStorage,
options?: CommandStorage,
extraFlags?: string[],
): Promise<void>;
}
45 changes: 29 additions & 16 deletions actions/add.action.ts
@@ -1,5 +1,5 @@
import * as chalk from 'chalk';
import { Input } from '../commands';
import { CommandStorage, CommandStorageEntry } from '../commands';
import { getValueOrDefault } from '../lib/compiler/helpers/get-value-or-default';
import {
AbstractPackageManager,
Expand All @@ -23,7 +23,11 @@ import { AbstractAction } from './abstract.action';
const schematicName = 'nest-add';

export class AddAction extends AbstractAction {
public async handle(inputs: Input[], options: Input[], extraFlags: string[]) {
public async handle(
inputs: CommandStorage,
options: CommandStorage,
extraFlags: string[],
) {
const libraryName = this.getLibraryName(inputs);
const packageName = this.getPackageName(libraryName);
const collectionName = this.getCollectionName(libraryName, packageName);
Expand All @@ -32,10 +36,13 @@ export class AddAction extends AbstractAction {
const packageInstallSuccess =
skipInstall || (await this.installPackage(collectionName, tagName));
if (packageInstallSuccess) {
const sourceRootOption: Input = await this.getSourceRoot(
inputs.concat(options),
);
options.push(sourceRootOption);
const sourceRootOption: CommandStorageEntry = await this.getSourceRoot([
inputs,
options,
]);
if (sourceRootOption) {
options.add(sourceRootOption);
}

await this.addLibrary(collectionName, options, extraFlags);
} else {
Expand All @@ -50,12 +57,20 @@ export class AddAction extends AbstractAction {
}
}

private async getSourceRoot(inputs: Input[]): Promise<Input> {
private async getSourceRoot(
storages: CommandStorage[],
): Promise<CommandStorageEntry> {
const configuration = await loadConfiguration();
const configurationProjects = configuration.projects;

const appName = inputs.find((option) => option.name === 'project')!
.value as string;
let appName: string | undefined;
for (const storage of storages) {
const maybeProject = storage.get<string>('project');
if (maybeProject) {
appName = maybeProject.value;
break;
}
}

let sourceRoot = appName
? getValueOrDefault(configuration, 'sourceRoot', appName)
Expand Down Expand Up @@ -117,15 +132,15 @@ export class AddAction extends AbstractAction {

private async addLibrary(
collectionName: string,
options: Input[],
options: CommandStorage,
extraFlags: string[],
) {
console.info(MESSAGES.LIBRARY_INSTALLATION_STARTS);
const schematicOptions: SchematicOption[] = [];
schematicOptions.push(
new SchematicOption(
'sourceRoot',
options.find((option) => option.name === 'sourceRoot')!.value as string,
options.get<string>('sourceRoot', true).value,
),
);
const extraFlagsString = extraFlags ? extraFlags.join(' ') : undefined;
Expand All @@ -146,15 +161,13 @@ export class AddAction extends AbstractAction {
}
}

private getLibraryName(inputs: Input[]): string {
const libraryInput: Input = inputs.find(
(input) => input.name === 'library',
) as Input;
private getLibraryName(inputs: CommandStorage): string {
const libraryInput = inputs.get<string>('library');

if (!libraryInput) {
throw new Error('No library found in command input');
}
return libraryInput.value as string;
return libraryInput.value;
}

private getPackageName(library: string): string {
Expand Down
44 changes: 17 additions & 27 deletions actions/build.action.ts
@@ -1,7 +1,7 @@
import * as chalk from 'chalk';
import { join } from 'path';
import * as ts from 'typescript';
import { Input } from '../commands';
import { CommandStorage } from '../commands';
import { AssetsManager } from '../lib/compiler/assets-manager';
import { getBuilder } from '../lib/compiler/helpers/get-builder';
import { getTscConfigPath } from '../lib/compiler/helpers/get-tsc-config.path';
Expand Down Expand Up @@ -36,19 +36,14 @@ export class BuildAction extends AbstractAction {
protected readonly assetsManager = new AssetsManager();
protected readonly workspaceUtils = new WorkspaceUtils();

public async handle(commandInputs: Input[], commandOptions: Input[]) {
public async handle(
commandInputs: CommandStorage,
commandOptions: CommandStorage,
) {
try {
const watchModeOption = commandOptions.find(
(option) => option.name === 'watch',
);
const watchMode = !!(watchModeOption && watchModeOption.value);

const watchAssetsModeOption = commandOptions.find(
(option) => option.name === 'watchAssets',
);
const watchAssetsMode = !!(
watchAssetsModeOption && watchAssetsModeOption.value
);
const watchMode = commandOptions.get<boolean>('watch')?.value ?? false;
const watchAssetsMode =
commandOptions.get<boolean>('watchAssets')?.value ?? false;

await this.runBuild(
commandInputs,
Expand All @@ -67,19 +62,16 @@ export class BuildAction extends AbstractAction {
}

public async runBuild(
commandInputs: Input[],
commandOptions: Input[],
commandInputs: CommandStorage,
commandOptions: CommandStorage,
watchMode: boolean,
watchAssetsMode: boolean,
isDebugEnabled = false,
onSuccess?: () => void,
) {
const configFileName = commandOptions.find(
(option) => option.name === 'config',
)!.value as string;
const configFileName = commandOptions.get<string>('config')?.value;
const configuration = await this.loader.load(configFileName);
const appName = commandInputs.find((input) => input.name === 'app')!
.value as string;
const appName = commandInputs.get<string>('app', true).value;

const pathToTsconfig = getTscConfigPath(
configuration,
Expand Down Expand Up @@ -151,7 +143,7 @@ export class BuildAction extends AbstractAction {
appName: string,
pathToTsconfig: string,
watchMode: boolean,
options: Input[],
options: CommandStorage,
tsOptions: ts.CompilerOptions,
onSuccess: (() => void) | undefined,
) {
Expand Down Expand Up @@ -180,7 +172,7 @@ export class BuildAction extends AbstractAction {
private async runWebpack(
configuration: Required<Configuration>,
appName: string,
commandOptions: Input[],
commandOptions: CommandStorage,
pathToTsconfig: string,
debug: boolean,
watchMode: boolean,
Expand Down Expand Up @@ -216,7 +208,7 @@ export class BuildAction extends AbstractAction {

private async runTsc(
watchMode: boolean,
options: Input[],
options: CommandStorage,
configuration: Required<Configuration>,
pathToTsconfig: string,
appName: string,
Expand All @@ -229,10 +221,8 @@ export class BuildAction extends AbstractAction {
this.tsConfigProvider,
this.tsLoader,
);
const isPreserveWatchOutputEnabled = options.find(
(option) =>
option.name === 'preserveWatchOutput' && option.value === true,
)?.value as boolean | undefined;
const isPreserveWatchOutputEnabled =
options.get<boolean>('preserveWatchOutput')?.value ?? false;
watchCompiler.run(
configuration,
pathToTsconfig,
Expand Down