Skip to content

Commit

Permalink
[#85] Correct project issue and peer dependencies (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
d4ins authored Nov 22, 2023
1 parent 896c8b9 commit 1740a7b
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 120 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ npx nest-task jarvis

### Receiving the list of tasks

**IMPORTANT.** In purpose for your tasks run correctly you need to build your project firstly.

To receive a list of tasks
```Bash
npx nest-task info
Expand All @@ -142,6 +144,8 @@ npx nest-task jarvis

### Running task

**IMPORTANT.** In purpose for your tasks run correctly you need to build your project firstly.

To create a task you need to run:
```Bash
npx nest-task run --name <name> <other-arguments>
Expand Down Expand Up @@ -178,17 +182,18 @@ key `task` in this file on top level. The object should have the following forma

Or if you use `projects` feature from Nest.js then your `nest-cli.json` should look like this:
`<root>/nest-cli.json`

```json
{
"task": {
"convention": "kebab-case",
"projects" : {
"example": {
"path": "src/example/tasks",
"entryPoint": "main.ts"
"projects": {
"example": {
"task": {
"path": "src/tasks",
"entryPoint": "main.ts",
"convention": "kebab-case"
}
}
}
}
}
```

Expand Down
28 changes: 13 additions & 15 deletions lib/core/core.perform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,20 @@ export class _Perform {

const argument = _ArgumentsManager.taskArguments[arg.name];

if (argument === undefined) {
dto[arg.name] = argument;
continue;
}

if (argument === null) {
dto[arg.name] = argument;
continue;
switch (arg.type) {
case "boolean":
dto[arg.name] = Boolean(argument);
break;
case "number":
dto[arg.name] = Number(argument);
break;
case "string":
case "undefined":
case "null":
default:
dto[arg.name] = argument;
break;
}

if (!arg.metadata.reflectedType) {
dto[arg.name] = argument;
continue;
}

dto[arg.name] = arg.metadata.reflectedType(argument);
}

try {
Expand Down
18 changes: 5 additions & 13 deletions lib/core/core.task.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { defaultMetadataStorage } from "class-transformer/cjs/storage.js";

import { Interfaces } from "@interfaces/index.js";
import { Patches } from "@patches/index.js";

import { _Decorators } from "./decorators/index.js";

import { _Types } from "./core.types.js";

export class _Task {
private nameMemo: string | undefined;

Expand All @@ -24,7 +20,7 @@ export class _Task {

private providersMemo: Interfaces.General.AnyClass[] | undefined;

private argsMemo: _Types.Task.Argument[] = [];
private argsMemo: _Decorators.Types.Property.Property[] = [];

private dtoInitialised: boolean = false;

Expand Down Expand Up @@ -128,32 +124,28 @@ export class _Task {
return this.providersMemo;
}

public get args(): _Types.Task.Argument[] {
public get args(): _Decorators.Types.Property.Property[] {
if (!this.dto) {
return this.argsMemo;
}

if (!this.argsInitialised) {
this.argsInitialised = true;

const argKeys = Patches.Reflect.getMetadata<string[] | undefined>(
const args = Patches.Reflect.getMetadata<_Decorators.Types.Property.Property[] | undefined>(
_Decorators.Enums.Metadata.Dto.Property,
this.dto,
);

if (!argKeys?.length) {
if (!args?.length) {
return this.argsMemo;
}

if (!this.dto) {
return this.argsMemo;
}

for (const arg of argKeys) {
const metadata = defaultMetadataStorage.findTypeMetadata(this.dto, arg);

this.argsMemo.push({ metadata, name: arg, type: metadata.reflectedType.name });
}
this.argsMemo = [...args];
}

return this.argsMemo;
Expand Down
9 changes: 0 additions & 9 deletions lib/core/core.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { INestApplication } from "@nestjs/common";
import { TypeMetadata } from "class-transformer/cjs/storage.js";

import { Interfaces } from "@interfaces/index.js";

Expand All @@ -9,12 +8,4 @@ export namespace _Types {

export type Argument = string | number | boolean | undefined | null | INestApplication;
}

export namespace Task {
export interface Argument {
name: string;
type: string;
metadata: TypeMetadata;
}
}
}
11 changes: 9 additions & 2 deletions lib/core/decorators/decorators.property.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { Interfaces } from "@interfaces/index.js";
import { Patches } from "@patches/index.js";

import { _Types } from "./decorators.types.js";
import { _Enums } from "./decorators.enums.js";

export function _Property(): PropertyDecorator {
return (target: object, propertyKey: string | symbol): void => {
const metadata = Patches.Reflect.getMetadata<(string | symbol)[] | undefined>(
const type = Patches.Reflect.getMetadata<Interfaces.Reflect.DesignType>(
_Enums.Metadata.BuildIn.DesignType,
target,
propertyKey,
);
const metadata = Patches.Reflect.getMetadata<_Types.Property.Property[] | undefined>(
_Enums.Metadata.Dto.Property,
target.constructor,
);
const value = metadata ?? [];

value.push(propertyKey);
value.push({ name: String(propertyKey), type: <_Types.Property.Types>type.name });

Reflect.defineMetadata(_Enums.Metadata.Dto.Property, value, target.constructor);
};
Expand Down
9 changes: 9 additions & 0 deletions lib/core/decorators/decorators.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ export namespace _Types {

export type MetadataKeys = keyof Metadata;
}

export namespace Property {
export type Types = "string" | "number" | "boolean" | "null" | "undefined";

export interface Property {
name: string;
type: Types;
}
}
}
20 changes: 19 additions & 1 deletion lib/core/project-configuration/project-configuration.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,25 @@ export class _Setup {
private async createConfiguration(read: _Read, configuration: _Types.Configuration.Approximate): Promise<void> {
this.processProjectAndTryToConfigure(configuration);

await this.save(read, configuration);
const clonedConfiguration = cloneDeep(read.configuration);

if (this.projectName) {
if (!clonedConfiguration.projects) {
throw new _Errors.MissingProjectConfiguration(this.projectName);
}

const project = clonedConfiguration.projects[this.projectName];

if (!project) {
throw new _Errors.MissingProjectConfiguration(this.projectName);
}

project.task = <_Types.Task>configuration.task;
} else {
clonedConfiguration.task = <_Types.Task>configuration.task;
}

await this.save(read, clonedConfiguration);
}

private async createEntrypoint(configuration: _Types.Configuration.Approximate): Promise<void> {
Expand Down
2 changes: 2 additions & 0 deletions lib/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

import * as $_General from "./interfaces.general.js";
import * as $_InstanceOf from "./interfaces.instance-of.js";
import * as $_Reflect from "./interfaces.reflect.js";

export namespace Interfaces {
export import General = $_General._General;
export import InstanceOf = $_InstanceOf._InstanceOf;
export import Reflect = $_Reflect._Reflect;
}
5 changes: 5 additions & 0 deletions lib/interfaces/interfaces.reflect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export namespace _Reflect {
export interface DesignType {
name: string;
}
}
7 changes: 1 addition & 6 deletions lib/messages/messages.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ import { Core } from "@core/index.js";

export namespace _Types {
export namespace FoundTasks {
export interface Argument {
name: string;
type: string;
}

export interface Options extends Core.Decorators.Types.Descriptable {
args?: Argument[];
args?: Core.Decorators.Types.Property.Property[];
}
}

Expand Down
Loading

0 comments on commit 1740a7b

Please sign in to comment.