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

add: apply husky #19

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
25 changes: 6 additions & 19 deletions lib/core/importer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { glob } from 'glob';
import { resolve } from 'path';
import {
AUTO_CONTROLLER_WATERMARK,
AUTO_INJECTABLE_WATERMARK,
} from '../interfaces';
import { AUTO_CONTROLLER_WATERMARK, AUTO_INJECTABLE_WATERMARK } from '../interfaces';

type ClassType = new (...args: any[]) => any;

Expand All @@ -22,9 +19,7 @@ export class Importer {
static async loadProviders(patterns: string[]): Promise<AutoProviders> {
const importer = new Importer(patterns);
const pathNames = await importer.matchGlob();
const results = await Promise.all(
pathNames.map((pathName) => importer.importProvider(pathName)),
);
const results = await Promise.all(pathNames.map((pathName) => importer.importProvider(pathName)));
return {
providers: results.flatMap((result) => result.providers),
};
Expand All @@ -33,9 +28,7 @@ export class Importer {
static async loadControllers(patterns: string[]): Promise<AutoControllers> {
const importer = new Importer(patterns);
const pathNames = await importer.matchGlob();
const results = await Promise.all(
pathNames.map((pathName) => importer.importController(pathName)),
);
const results = await Promise.all(pathNames.map((pathName) => importer.importController(pathName)));
return {
controllers: results.flatMap((result) => result.controllers),
};
Expand All @@ -45,9 +38,7 @@ export class Importer {
const exports: Record<string, unknown> = await import(pathName);
const providers = Object.values(exports)
.filter((value) => typeof value === 'function')
.filter((value) =>
Reflect.hasMetadata(AUTO_INJECTABLE_WATERMARK, value as ClassType),
) as ClassType[];
.filter((value) => Reflect.hasMetadata(AUTO_INJECTABLE_WATERMARK, value as ClassType)) as ClassType[];
return {
providers,
};
Expand All @@ -57,18 +48,14 @@ export class Importer {
const exports: Record<string, unknown> = await import(pathName);
const controllers = Object.values(exports)
.filter((value) => typeof value === 'function')
.filter((value) =>
Reflect.hasMetadata(AUTO_CONTROLLER_WATERMARK, value as ClassType),
) as ClassType[];
.filter((value) => Reflect.hasMetadata(AUTO_CONTROLLER_WATERMARK, value as ClassType)) as ClassType[];
return {
controllers,
};
}

public async matchGlob() {
const globs = this.patterns.map((pattern) =>
glob(resolve(process.cwd(), pattern)),
);
const globs = this.patterns.map((pattern) => glob(resolve(process.cwd(), pattern)));
return (await Promise.all(globs)).flat();
}
}
7 changes: 2 additions & 5 deletions lib/decorators/component-scan.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ import { AutoModule } from '../auto.module';
import { Module } from '@nestjs/common';
import * as path from 'path';

export function ComponentScan(
paths: string[] = [getRootPath()],
): ClassDecorator {
export function ComponentScan(paths: string[] = [getRootPath()]): ClassDecorator {
return function (target: any) {
const originalMetadata = {
imports: Reflect.getMetadata(MODULE_OPTIONS.IMPORTS, target) || [],
controllers:
Reflect.getMetadata(MODULE_OPTIONS.CONTROLLERS, target) || [],
controllers: Reflect.getMetadata(MODULE_OPTIONS.CONTROLLERS, target) || [],
providers: Reflect.getMetadata(MODULE_OPTIONS.PROVIDERS, target) || [],
};

Expand Down
2 changes: 1 addition & 1 deletion lib/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './auto-injectable.constant';
export * from './auto-controller.constant';
export * from './auto-controller.constant';
Loading