Skip to content

Commit

Permalink
Merge pull request #19 from tiny-nestjs/feature/husky
Browse files Browse the repository at this point in the history
add: apply husky
  • Loading branch information
Nhahan authored Aug 11, 2023
2 parents 186f3f0 + 46f2318 commit 1841621
Show file tree
Hide file tree
Showing 6 changed files with 507 additions and 29 deletions.
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

0 comments on commit 1841621

Please sign in to comment.