Skip to content

Commit

Permalink
chore: update CI and fix type errors (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzhudev authored Oct 28, 2023
1 parent 9f8e3a1 commit 69e65fb
Show file tree
Hide file tree
Showing 28 changed files with 643 additions and 529 deletions.
35 changes: 0 additions & 35 deletions .circleci/config.yml

This file was deleted.

63 changes: 63 additions & 0 deletions .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: 🧪 CI Test

on:
push:
branches: [main, dev]
pull_request:
branches: [main, dev]

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 16

- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 8
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- name: Setup pnpm cache
uses: actions/cache@v3
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Setup ESLint cache
uses: actions/cache@v3
with:
path: .eslintcache
key: |
${{ runner.os }}-eslint-${{ hashFiles('.eslintrc.cjs') }}
restore-keys: |
${{ runner.os }}-eslint-
- name: Install dependencies
run: pnpm install

- name: 🩺 Run Test
run: pnpm run test

- name: 🌡️ Run ESLint
run: pnpm run lint

- name: 🧼 Type checking
run: pnpm typeCheck
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.20.0
18.17.0
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"$schema": "https://raw.githubusercontent.com/wzhudev/squirrel/master/src/schema/package.schema.json",
"name": "@wendellhu/redi",
"version": "0.12.8",
"version": "0.12.9",
"description": "A dependency library for TypeScript and JavaScript, along with a binding for React.",
"scripts": {
"test": "vitest --coverage",
"test": "vitest run --coverage",
"lint": "eslint --fix 'src/**/*.ts'",
"lint:prettier": "prettier --write '{src,test}/**/*.{ts,tsx}'",
"build": "squirrel",
Expand Down
32 changes: 17 additions & 15 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ export class IdentifierUndefinedError extends RediError {
}

/**
* get dependencies declared on a class
*
* @param registerTarget the class
* @returns dependencies
* @internal
*/
export function getDependencies<T>(registerTarget: Ctor<T>): DependencyDescriptor<any>[] {
const target = registerTarget as any
return target[DEPENDENCIES] || []
}

/**
* @internal
*/
export function getDependencyByIndex<T>(registerTarget: Ctor<T>, index: number): DependencyDescriptor<any> {
const allDependencies = getDependencies(registerTarget)
const dep = allDependencies.find((descriptor) => descriptor.paramIndex === index)
Expand All @@ -52,17 +52,7 @@ export function getDependencyByIndex<T>(registerTarget: Ctor<T>, index: number):
}

/**
* declare dependency relationship on a class
*
* if the IDependencyDescriptor already exists, just modify it without creating
* a new descriptor since differently decorators could be applied on a same
* constructor property
*
* @param registerTarget the class to be registered
* @param identifier dependency item identifier
* @param paramIndex index of the decorator constructor parameter
* @param quantity quantity of the dependency
* @param lookUp optional lookup
* @internal
*/
export function setDependency<T, U>(
registerTarget: Ctor<U>,
Expand Down Expand Up @@ -96,6 +86,13 @@ export function setDependency<T, U>(
}

const knownIdentifiers = new Set<string>()

/**
* Create a dependency identifier
*
* @param id name of the identifier
* @returns Identifier that could also be used as a decorator
*/
export function createIdentifier<T>(id: string): IdentifierDecorator<T> {
if (knownIdentifiers.has(id)) {
throw new RediError(`Identifier "${id}" already exists.`)
Expand All @@ -107,12 +104,17 @@ export function createIdentifier<T>(id: string): IdentifierDecorator<T> {
setDependency(registerTarget, decorator, index)
}) as IdentifierDecorator<T> // decorator as an identifier

// TODO: @wzhudev should assign a name to the function so it would be easy to debug in inspect tools
// decorator.name = `[redi]: ${id}`;
decorator.toString = () => id
decorator[IdentifierDecoratorSymbol] = true

return decorator
}

/**
* @internal
*/
/* istanbul ignore next */
export function TEST_ONLY_clearKnownIdentifiers(): void {
knownIdentifiers.clear()
Expand Down
7 changes: 4 additions & 3 deletions src/dependencyCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { RediError } from './error'
export type DependencyPair<T> = [DependencyIdentifier<T>, DependencyItem<T>]
export type DependencyClass<T> = [Ctor<T>]
export type Dependency<T = any> = DependencyPair<T> | DependencyClass<T>
export type DependencyOrInstance<T = any> = Dependency<T> | [Ctor<T> | DependencyIdentifier<T>, T]
export type DependencyWithInstance<T = any> = [Ctor<T> | DependencyIdentifier<T>, T]
export type DependencyOrInstance<T = any> = Dependency<T> | DependencyWithInstance<T>

export function isBareClassDependency<T>(thing: Dependency<T>): thing is DependencyClass<T> {
return thing.length === 1
Expand All @@ -23,7 +24,7 @@ export class DependencyNotFoundError extends RediError {
}

/**
* store unresolved dependencies in an injector
* Store unresolved dependencies in an injector.
*
* @internal
*/
Expand Down Expand Up @@ -103,7 +104,7 @@ export class DependencyCollection implements IDisposable {
}

/**
* store resolved dependencies
* Store resolved dependencies.
*
* @internal
*/
Expand Down
26 changes: 16 additions & 10 deletions src/dependencyDeclare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ import { setDependency } from './decorators'
import { normalizeFactoryDeps } from './dependencyDescriptor'
import { Ctor, FactoryDep } from './dependencyItem'

/**
* Register dependencies on a class.
*
* @param registerTarget The target constructor
* @param deps Dependencies
*/
export function setDependencies<U>(registerTarget: Ctor<U>, deps: FactoryDep<any>[]): void {
const normalizedDescriptors = normalizeFactoryDeps(deps)
normalizedDescriptors.forEach((descriptor) => {
setDependency(
registerTarget,
descriptor.identifier,
descriptor.paramIndex,
descriptor.quantity,
descriptor.lookUp
)
})
const normalizedDescriptors = normalizeFactoryDeps(deps)
normalizedDescriptors.forEach((descriptor) => {
setDependency(
registerTarget,
descriptor.identifier,
descriptor.paramIndex,
descriptor.quantity,
descriptor.lookUp
)
})
}
92 changes: 46 additions & 46 deletions src/dependencyDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,64 @@ import { RediError } from './error'
import { LookUp, Quantity } from './types'

export interface DependencyDescriptor<T> {
paramIndex: number
identifier: DependencyIdentifier<T>
quantity: Quantity
lookUp?: LookUp
withNew: boolean
paramIndex: number
identifier: DependencyIdentifier<T>
quantity: Quantity
lookUp?: LookUp
withNew: boolean
}

/**
* describes dependencies of a IDependencyItem
*/
export interface Dependencies {
dependencies: DependencyDescriptor<any>[]
dependencies: DependencyDescriptor<any>[]
}

export function normalizeFactoryDeps(deps?: FactoryDep<any>[]): DependencyDescriptor<any>[] {
if (!deps) {
return []
}
if (!deps) {
return []
}

return deps.map((dep, index) => {
if (!Array.isArray(dep)) {
return {
paramIndex: index,
identifier: dep,
quantity: Quantity.REQUIRED,
withNew: false,
}
}
return deps.map((dep, index) => {
if (!Array.isArray(dep)) {
return {
paramIndex: index,
identifier: dep,
quantity: Quantity.REQUIRED,
withNew: false,
}
}

const modifiers = dep.slice(0, dep.length - 1) as FactoryDepModifier[]
const identifier = dep[dep.length - 1] as DependencyIdentifier<any>
const modifiers = dep.slice(0, dep.length - 1) as FactoryDepModifier[]
const identifier = dep[dep.length - 1] as DependencyIdentifier<any>

let lookUp: LookUp | undefined = undefined
let quantity = Quantity.REQUIRED
let withNew = false
let lookUp: LookUp | undefined = undefined
let quantity = Quantity.REQUIRED
let withNew = false

;(modifiers as FactoryDepModifier[]).forEach((modifier: FactoryDepModifier) => {
if (modifier instanceof Self) {
lookUp = LookUp.SELF
} else if (modifier instanceof SkipSelf) {
lookUp = LookUp.SKIP_SELF
} else if (modifier instanceof Optional) {
quantity = Quantity.OPTIONAL
} else if (modifier instanceof Many) {
quantity = Quantity.MANY
} else if (modifier instanceof WithNew) {
withNew = true
} else {
throw new RediError(`unknown dep modifier ${modifier}.`)
}
})
;(modifiers as FactoryDepModifier[]).forEach((modifier: FactoryDepModifier) => {
if (modifier instanceof Self) {
lookUp = LookUp.SELF
} else if (modifier instanceof SkipSelf) {
lookUp = LookUp.SKIP_SELF
} else if (modifier instanceof Optional) {
quantity = Quantity.OPTIONAL
} else if (modifier instanceof Many) {
quantity = Quantity.MANY
} else if (modifier instanceof WithNew) {
withNew = true
} else {
throw new RediError(`unknown dep modifier ${modifier}.`)
}
})

return {
paramIndex: index,
identifier: identifier as DependencyIdentifier<any>,
quantity,
lookUp,
withNew,
}
})
return {
paramIndex: index,
identifier: identifier as DependencyIdentifier<any>,
quantity,
lookUp,
withNew,
}
})
}
18 changes: 9 additions & 9 deletions src/dependencyForwardRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import { DependencyIdentifier, NormalizedDependencyIdentifier } from './dependen
import { Ctor } from './dependencyItem'

export interface ForwardRef<T> {
unwrap(): Ctor<T>
unwrap(): Ctor<T>
}

export function forwardRef<T>(wrapper: () => Ctor<T>): ForwardRef<T> {
return {
unwrap: wrapper,
}
return {
unwrap: wrapper,
}
}

export function isForwardRef<T = any>(thing: unknown): thing is ForwardRef<T> {
return !!thing && typeof (thing as any).unwrap === 'function'
return !!thing && typeof (thing as any).unwrap === 'function'
}

export function normalizeForwardRef<T>(id: DependencyIdentifier<T>): NormalizedDependencyIdentifier<T> {
if (isForwardRef(id)) {
return id.unwrap()
}
if (isForwardRef(id)) {
return id.unwrap()
}

return id
return id
}
Loading

0 comments on commit 69e65fb

Please sign in to comment.