Skip to content

Commit

Permalink
build: refactor package exports/imports
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed Oct 1, 2024
1 parent 871f2a7 commit 982f193
Show file tree
Hide file tree
Showing 107 changed files with 1,451 additions and 512 deletions.
2 changes: 1 addition & 1 deletion .attw.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"color": true,
"emoji": true,
"format": "ascii",
"ignoreRules": ["cjs-resolves-to-esm"],
"ignoreRules": ["cjs-resolves-to-esm", "internal-resolution-error"],
"summary": true
}
12 changes: 4 additions & 8 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,25 @@
"failFast": false,
"flagWords": [],
"ignorePaths": [
"**/*.log",
"!scratch.*",
"**/*.patch",
"**/*.snap",
"**/*.wasm",
"**/.*ignore",
"**/.gitconfig",
"**/CHANGELOG.md",
"**/LICENSE.md",
"**/RELEASE_NOTES.md",
"**/scratch.*",
".cspell.json",
".dictionary.txt",
".git/",
".husky/_/",
".vscode/settings.json",
".yarn/",
"dist/",
"patches/",
"yarn.lock"
],
"ignoreRegExpList": [
"/@flex-development\\/.*/",
"/from\\s+(['\"]).*\\1/",
"import\\(.*\\)"
],
"ignoreRegExpList": [],
"ignoreWords": [],
"language": "en-US",
"patterns": [],
Expand Down
4 changes: 4 additions & 0 deletions .dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cefc
commitlintrc
dedupe
dessant
devlop
docast
dohm
dprint
Expand All @@ -16,12 +17,15 @@ kaisugi
libc
lintstagedrc
mkbuild
mlly
nvmrc
pathe
pkgs
pnpx
preid
remarkrc
shfmt
tscu
unstub
vates
vfile
Expand Down
20 changes: 3 additions & 17 deletions .dprint.jsonc
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
{
"$schema": "https://dprint.dev/schemas/v0.json",
"excludes": [
"!**/__fixtures__/**/dist/",
"!**/__fixtures__/**/node_modules/",
"!**/typings/**/dist/",
"!scratch.*",
"**/*.patch",
"**/*.snap",
"**/*config.*.timestamp*",
"**/.temp/",
"**/.vercel/",
"**/__fixtures__/git/**/*.txt",
"**/__tests__/benchmark.json",
"**/__tests__/report.json",
"**/__tests__/typecheck.json",
"**/coverage/",
"**/dist/",
"**/node_modules",
"**/tsconfig*temp.json",
"**/CHANGELOG.md",
"**/LICENSE.md",
"**/RELEASE_NOTES.md",
".git/",
".husky/_/",
".yarn/",
"CHANGELOG.md",
"LICENSE.md",
"RELEASE_NOTES.md",
"yarn.lock"
],
"exec": {
Expand Down
1 change: 1 addition & 0 deletions .env.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
[ -f $PWD/.env.repo ] && source $PWD/.env.repo
HOMEBREW_BREWFILE=./Brewfile
NODE_NO_WARNINGS=1
NODE_OPTIONS='--experimental-strip-types --experimental-transform-types'
13 changes: 6 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ pids
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
yarn-debug.log*
yarn-error.log*
!**/__fixtures__/**/node_modules/
!**/__fixtures__/node_modules/
!**/__fixtures__/**/*.log

# Yarn 2 (Not using Zero-Installs)
# https://yarnpkg.com/getting-started/qa#which-files-should-be-gitignored
Expand All @@ -44,10 +45,8 @@ yarn-error.log*

# Testing
# ------------------------------------------------------------------------------
**/*config.*.timestamp*
**/__tests__/benchmark.json
**/__tests__/report.*
**/__tests__/typecheck.json
**/.vitest-reports/
**/__tests__/reports/*
**/coverage/
**/tsconfig*temp.json
*.lcov
Expand All @@ -59,10 +58,8 @@ codecov

# Builds
# ------------------------------------------------------------------------------
**/.temp/
**/.vitepress/cache
**/dist/
!**/__fixtures__/**/*.log
!**/__fixtures__/**/dist/
!**/typings/**/dist/

Expand All @@ -76,4 +73,6 @@ codecov

# Misc
# ------------------------------------------------------------------------------
**/*config.*.timestamp*
**/.temp/
**/scratch.*
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20
22.9.0
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"emmet.triggerExpansionOnTab": true,
"eslint.enable": true,
"eslint.lintTask.enable": true,
"eslint.nodePath": "node_modules",
"eslint.options": {},
"eslint.runtime": "node",
"eslint.trace.server": "messages",
Expand Down
152 changes: 82 additions & 70 deletions __tests__/reporters/notifier.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
/**
* @file Reporters - Notifier
* @module tests/reporters/Notifier
* @see https://vitest.dev/advanced/reporters#exported-reporters
*/

import { cast, isArray, type OneOrMany } from '@flex-development/tutils'
import ci from 'is-ci'
import notifier from 'node-notifier'
import type NotificationCenter from 'node-notifier/notifiers/notificationcenter'
import type { Notification } from 'node-notifier/notifiers/notificationcenter'
import { performance } from 'node:perf_hooks'
import { promisify } from 'node:util'
import { dedent } from 'ts-dedent'
import type { File, Reporter, Task, Test, Vitest } from 'vitest'
import type { RunnerTask, RunnerTestCase, RunnerTestFile } from 'vitest'
import type { Vitest } from 'vitest/node'
import type { Reporter } from 'vitest/reporters'

/**
* Custom reporter that sends a notification when all tests have been ran.
* Test report summary notifier.
*
* @see https://vitest.dev/config/#reporters
* @see {@linkcode Reporter}
*
* @implements {Reporter}
*/
class Notifier implements Reporter {
/**
* Test reporter context.
* Reporter context.
*
* @see {@linkcode Vitest}
*
* @public
* @instance
* @member {Vitest} ctx
*/
public ctx!: Vitest
Expand All @@ -31,6 +37,7 @@ class Notifier implements Reporter {
* Test run end time (in milliseconds).
*
* @public
* @instance
* @member {number} end
*/
public end!: number
Expand All @@ -39,34 +46,77 @@ class Notifier implements Reporter {
* Test run start time (in milliseconds).
*
* @public
* @instance
* @member {number} start
*/
public start!: number

/**
* Send a notification after all tests have ran (in non ci/cd environments).
*
* @see {@linkcode RunnerTestFile}
*
* @public
* @instance
*
* @async
*
* @param {RunnerTestFile[] | undefined} [files=this.ctx.state.getFiles()]
* List of test files
* @param {unknown[] | undefined} [errors=this.ctx.state.getUnhandledErrors()]
* List of unhandled errors
* @return {undefined}
*/
public async onFinished(
files: RunnerTestFile[] = this.ctx.state.getFiles(),
errors: unknown[] = this.ctx.state.getUnhandledErrors()
): Promise<undefined> {
this.end = performance.now()
return void await (ci || this.reportSummary(files, errors))
}

/**
* Initialize the reporter.
*
* @see {@linkcode Vitest}
*
* @public
* @instance
*
* @param {Vitest} ctx
* Reporter context
* @return {undefined}
*/
public onInit(ctx: Vitest): undefined {
return void (this.ctx = ctx, this.start = performance.now())
}

/**
* Send a notification.
*
* @protected
* @see {@linkcode RunnerTestFile}
*
* @public
* @instance
*
* @async
*
* @param {File[]} [files=this.ctx.state.getFiles()]
* File objects
* @param {unknown[]} [errors=this.ctx.state.getUnhandledErrors()]
* Errors
* @return {Promise<void>}
* Nothing
* @param {RunnerTestFile[] | undefined} [files=this.ctx.state.getFiles()]
* List of test files
* @param {unknown[] | undefined} [errors=this.ctx.state.getUnhandledErrors()]
* List of unhandled errors
* @return {Promise<undefined>}
*/
protected async notify(
files: File[] = this.ctx.state.getFiles(),
public async reportSummary(
files: RunnerTestFile[] = this.ctx.state.getFiles(),
errors: unknown[] = this.ctx.state.getUnhandledErrors()
): Promise<void> {
): Promise<undefined> {
/**
* Tests that have been run.
*
* @const {Test[]} tests
* @const {RunnerTestCase[]} tests
*/
const tests: Test[] = this.tests(files)
const tests: RunnerTestCase[] = this.tests(files)

/**
* Total number of failed tests.
Expand Down Expand Up @@ -123,70 +173,32 @@ class Notifier implements Reporter {
title = '\u2705 Passed'
}

// send notification
return void (await promisify<NotificationCenter.Notification>(
notifier.notify.bind(notifier)
)({
return void await promisify<Notification>(notifier.notify.bind(notifier))({
message,
sound: true,
timeout: 10,
timeout: 15,
title
}))
}

/**
* Send a notification after all tests have ran (in non ci/cd environments).
*
* @public
*
* @async
*
* @param {File[]} [files=this.ctx.state.getFiles()]
* File objects
* @param {unknown[]} [errors=this.ctx.state.getUnhandledErrors()]
* Errors
* @return {Promise<void>}
* Nothing
*/
public async onFinished(
files: File[] = this.ctx.state.getFiles(),
errors: unknown[] = this.ctx.state.getUnhandledErrors()
): Promise<void> {
this.end = performance.now()
return void (await this.notify(files, errors))
})
}

/**
* Initializes the reporter.
* Convert tasks to a list of test cases.
*
* @public
*
* @param {Vitest} context
* Test reporter context
* @return {void}
* Nothing
*/
public onInit(context: Vitest): void {
this.ctx = context
return void ((this.start = performance.now()) && (this.end = 0))
}

/**
* Returns an array of {@linkcode Test} objects.
* @see {@linkcode RunnerTask}
* @see {@linkcode RunnerTestCase}
*
* @protected
* @instance
*
* @param {OneOrMany<Task>} [tasks=[]]
* @param {RunnerTask | RunnerTask[]} [tasks=[]]
* Tasks to collect tests from
* @return {Test[]}
* `Test` object array
* @return {RunnerTestCase[]}
* List of runner test cases
*/
protected tests(tasks: OneOrMany<Task> = []): Test[] {
return (isArray<Task>(tasks) ? tasks : [tasks]).flatMap(task => {
return task.type === 'custom'
? [cast(task)]
: task.type === 'test'
? [task]
protected tests(tasks: RunnerTask | RunnerTask[] = []): RunnerTestCase[] {
return (Array.isArray(tasks) ? tasks : [tasks]).flatMap(task => {
return task.type === 'custom' || task.type === 'test'
? [task as unknown as RunnerTestCase]
: 'tasks' in task
? task.tasks.flatMap(task => this.tests(task))
: []
Expand Down
1 change: 0 additions & 1 deletion build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { defineBuildConfig, type Config } from '@flex-development/mkbuild'
* @const {Config} config
*/
const config: Config = defineBuildConfig({
charset: 'utf8',
dts: 'only',
tsconfig: 'tsconfig.build.json'
})
Expand Down
Loading

0 comments on commit 982f193

Please sign in to comment.