8000 chore: Updated build system. · ShogunPanda/cronometro@2eab835 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 2eab835

Browse files
committed
chore: Updated build system.
1 parent 8670aae commit 2eab835

20 files changed

+109
-76
lines changed

.swcrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"env": {
3+
"targets": "node >= 16"
4+
},
5+
"jsc": {
6+
"parser": {
7+
"syntax": "typescript",
8+
"tsx": false,
9+
"dynamicImport": true
10+
}
11+
}
12+
}

package.json

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,38 @@
2626
"typings": "./types/index.d.ts",
2727
"types": "./types/index.d.ts",
2828
"scripts": {
29-
"prebuild": "rm -rf dist types && npm run lint",
30-
"build": "tsc -p . && esm-pkg-add-imports-extensions dist",
29+
"dev": "swc -w -d dist src",
30+
"prebuild": "rm -rf dist types && npm run typecheck && npm run lint",
31+
"build": "swc --delete-dir-on-start -d dist src",
3132
"format": "prettier -w src test",
33+
"typecheck": "tsc -p . --emitDeclarationOnly",
3234
"lint": "eslint src test",
33-
"test": "c8 --reporter=text --reporter=html esm-ts-tap -t 120 --reporter=spec --no-coverage test/*.test.ts",
34-
"test:ci": "c8 --reporter=text --reporter=json --check-coverage --branches 90 --functions 90 --lines 90 --statements 90 esm-ts-tap -t 120 --no-color --no-coverage test/*.test.ts",
35-
"ci": "npm run lint && npm run test:ci",
35+
"test": "c8 -c test/config/c8-local.json tap --rcfile=test/config/tap.yml test/*.test.ts",
36+
"test:ci": "c8 -c test/config/c8-ci.json tap --rcfile=test/config/tap.yml --no-color test/*.test.ts",
37+
"ci": "npm run build && npm run test:ci",
3638
"prepublishOnly": "npm run ci",
3739
"postpublish": "git push origin && git push origin -f --tags"
3840
},
3941
"dependencies": {
40-
"acquerello": "^1.0.0",
42+
"acquerello": "^1.0.6",
4143
"hdr-histogram-js": "^3.0.0",
42-
"table": "^6.7.1"
44+
"table": "^6.8.0"
4345
},
4446
"devDependencies": {
45-
"@cowtech/eslint-config": "^8.0.1",
46-
"@cowtech/esm-package-utils": "^0.9.3",
47-
"@types/node": "^17.0.2",
48-
"@types/sinon": "^10.0.2",
49-
"@types/tap": "^15.0.5",
50-
"c8": "^7.8.0",
51-
"prettier": "^2.3.2",
47+
"@cowtech/eslint-config": "^8.4.0",
48+
"@swc/cli": "^0.1.55",
49+
"@swc/core": "^1.2.150",
50+
"@types/node": "^17.0.21",
51+
"@types/sinon": "^10.0.11",
52+
"@types/tap": "^15.0.6",
53+
"c8": "^7.11.0",
54+
"chokidar": "^3.5.3",
55+
"prettier": "^2.5.1",
5256
"proxyquire": "^2.1.3",
5357
"sinon": "^12.0.1",
54-
"tap": "^15.0.9",
55-
"ts-node": "^10.2.0",
56-
"typescript": "^4.3.5"
58+
"tap": "^15.2.3",
59+
"ts-node": "^10.7.0",
60+
"typescript": "^4.6.2"
5761
},
5862
"engines": {
5963
"node": ">=14.15.0"

src/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
'use strict'
2-
3-
import { isMainThread, Worker, workerData } from 'worker_threads'
4-
import { Callback, Context, defaultOptions, Options, PrintOptions, Results, runnerPath, Tests } from './models'
5-
import { printResults } from './print'
1+
import { isMainThread, Worker, workerData } from 'node:worker_threads'
2+
import { Callback, Context, defaultOptions, Options, PrintOptions, Results, runnerPath, Tests } from './models.js'
3+
import { printResults } from './print.js'
64

75
type PromiseResolver<T> = (value: T) => void
86
type PromiseRejecter = (err: Error) => void
97

10-
export * from './models'
8+
export * from './models.js'
119

1210
function scheduleNextTest(context: Context): void {
1311
// We still have work to do

src/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Histogram } from 'hdr-histogram-js'
2-
import { resolve } from 'path'
2+
import { resolve } from 'node:path'
33

44
export interface PrintOptions {
55
colors?: boolean

src/print.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { clean, colorize } from 'acquerello'
22
import { table } from 'table'
3-
import { Results } from './models'
3+
import { Results } from './models.js'
44

55
const styles = ['red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'gray']
66

@@ -99,7 +99,7 @@ export function printResults(results: Results, colors: boolean, compare: boolean
9999
]
100100

101101
if (compare) {
102-
if (relative.match(/^[0.\s]+$/)) {
102+
if (/^[\s.0]+$/.test(relative)) {
103103
row.push('')
104104
} else {
105105
row.push(styler(`{{${color}}}+ ${relative} %{{-}}`))
@@ -118,7 +118,7 @@ export function printResults(results: Results, colors: boolean, compare: boolean
118118
styler('{{bold white}}Tolerance{{-}}')
119119
])
120120

121-
rows.splice(rows.length - 1, 0, [
121+
rows.splice(-1, 0, [
122122
styler('{{bold white}}Fastest test{{-}}'),
123123
styler('{{bold white}}Samples{{-}}'),
124124
styler('{{bold white}}Result{{-}}'),
@@ -127,7 +127,7 @@ export function printResults(results: Results, colors: boolean, compare: boolean
127127

128128
if (compare) {
129129
rows[0].push(styler(`{{bold white}}${compareHeader}{{-}}`))
130-
rows[rows.length - 2].push(styler(`{{bold white}}${compareHeader}{{-}}`))
130+
rows.at(-2)!.push(styler(`{{bold white}}${compareHeader}{{-}}`))
131131
}
132132

133133
currentLogger(

src/runner.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { isMainThread, parentPort, workerData } from 'worker_threads'
2-
import { runWorker } from './worker'
1+
import { isMainThread, parentPort, workerData } from 'node:worker_threads'
2+
import { runWorker } from './worker.js'
33

44
if (isMainThread) {
55
throw new Error('Do not run this file as main script.')
@@ -35,9 +35,9 @@ chain
3535
// Run the worker
3636
runWorker(workerData, value => parentPort!.postMessage(value), process.exit)
3737
})
38-
.catch(e => {
38+
.catch(error => {
3939
process.nextTick(() => {
40-
throw e
40+
throw error
4141
})
4242
})
4343
/* c8 ignore stop */

src/worker.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { build as buildHistogram } from 'hdr-histogram-js'
2-
import { Percentiles, percentiles, Result, SetupFunction, TestContext, TestFunction, WorkerContext } from './models'
2+
import { percentiles, Result, SetupFunction, TestContext, TestFunction, WorkerContext } from './models.js'
33

44
function noOp(): void {
55
// No-op
@@ -40,7 +40,7 @@ function handleTestIteration(context: TestContext, error?: Error | null): void {
4040
let stop = false
4141

4242
if (errorThreshold > 0) {
43-
const completedPercentage = Math.floor((executed / total) * 10000)
43+
const completedPercentage = Math.floor((executed / total) * 10_000)
4444

4545
// Check if abort the test earlier. It is checked every 5% after 10% of the iterations
4646
if (completedPercentage >= 1000 && completedPercentage % 500 === 0) {
@@ -63,10 +63,9 @@ function handleTestIteration(context: TestContext, error?: Error | null): void {
6363
max: histogram.maxValue,
6464
mean: histogram.mean,
6565
stddev: stdDev,
66-
percentiles: percentiles.reduce((accu: Percentiles, percentile) => {
67-
accu[percentile] = histogram.getValueAtPercentile(percentile)
68-
return accu
69-
}, {}),
66+
percentiles: Object.fromEntries(
67+
percentiles.map(percentile => [percentile, histogram.getValueAtPercentile(percentile)])
68+
),
7069
standardError: stdDev / Math.sqrt(executed)
7170
})
7271
}
@@ -88,13 +87,13 @@ function runTestIteration(context: TestContext): void {
8887
// The function is not a promise and has no arguments, so it's sync
8988
context.handler(null)
9089
}
91-
} catch (e) {
90+
} catch (error) {
9291
// If a error was thrown, only handle if the original function length is 0, which means it's a sync error, otherwise propagate
9392
if (context.test.length === 0) {
94-
return context.handler(e)
93+
return context.handler(error)
9594
}
9695

97-
throw e
96+
throw error
9897
}
9998
}
10099

test/asyncImport.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable @typescript-eslint/no-floating-promises */
22

33
import t from 'tap'
4-
import { isMainThread } from 'worker_threads'
5-
import { cronometro, percentiles } from '../src'
4+
import { isMainThread } from 'node:worker_threads'
5+
import { cronometro, percentiles } from '../src/index.js'
66

77
async function main(): Promise<void> {
88
await new Promise(resolve => setTimeout(resolve, 100))

test/config/c8-ci.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"check-coverage": true,
3+
"reporter": ["text", "json"],
4+
"branches": 90,
5+
"functions": 90,
6+
"lines": 90,
7+
"statements": 90
8+
}

test/config/c8-local.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"reporter": ["text", "html"]
3+
}

test/config/tap.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
jobs: 5
3+
timeout: 120
4+
reporter: spec
5+
coverage: false
6+
node-arg:
7+
- --loader=ts-node/esm

test/errorHandling.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable @typescript-eslint/no-floating-promises */
22

3+
import { isMainThread } from 'node:worker_threads'
34
import t from 'tap'
4-
import { isMainThread } from 'worker_threads'
5-
import { cronometro, percentiles } from '../src'
5+
import { cronometro, percentiles } from '../src/index.js'
66

77
if (!isMainThread) {
88
cronometro(
@@ -58,7 +58,7 @@ if (!isMainThread) {
5858
})
5959

6060
t.test('Runner cannot be run in main thread', async t => {
61-
await t.rejects(import('../src/runner'), { message: 'Do not run this file as main script.' })
61+
await t.rejects(import('../src/runner.js'), { message: 'Do not run this file as main script.' })
6262
})
6363

6464
t.only('Runner reports setup errors', async t => {

test/fixture/sample.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import cronometro from '../../dist'
1+
import cronometro from '../../dist/index.js'
22

3-
const pattern = /[123]/g
3+
const pattern = /[1-3]/g
44
const replacements: { [key: string]: string } = { 1: 'a', 2: 'b', 3: 'c' }
55

66
const subject =
@@ -13,7 +13,7 @@ cronometro(
1313
subject.replace(pattern, m => replacements[m])
1414
},
1515
multiple() {
16-
subject.replace(/1/g, 'a').replace(/2/g, 'b').replace(/3/g, 'c')
16+
subject.replaceAll('1', 'a').replaceAll('2', 'b').replaceAll('3', 'c')
1717
}
1818
},
1919
{ iterations: 5, errorThreshold: 0, print: { compare: true }, warmup: true },

test/genericErrorHandling.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable @typescript-eslint/no-floating-promises */
22

33
import t from 'tap'
4-
import { isMainThread } from 'worker_threads'
5-
import { cronometro } from '../src'
4+
import { isMainThread } from 'node:worker_threads'
5+
import { cronometro } from '../src/index.js'
66

77
if (!isMainThread) {
88
cronometro(undefined as any, () => false)

test/optionsValidation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-floating-promises */
22

33
import t from 'tap'
4-
import { cronometro } from '../src'
4+
import { cronometro } from '../src/index.js'
55

66
t.test('Options validation', async t => {
77
await t.rejects(

test/print.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import sinon from 'sinon'
44
import t from 'tap'
5-
import { isMainThread } from 'worker_threads'
6-
import { cronometro, defaultOptions, percentiles } from '../src'
7-
import { setLogger } from '../src/print'
5+
import { isMainThread } from 'node:worker_threads'
6+
import { cronometro, defaultOptions, percentiles } from '../src/index.js'
7+
import { setLogger } from '../src/print.js'
88

99
function removeStyle(source: string): string {
1010
// eslint-disable-next-line no-control-regex
11-
return source.replace(/\x1b\[\d+m/g, '')
11+
return source.replace(/\u001B\[\d+m/g, '')
1212
}
1313

1414
const loggerSpy = sinon.spy()
@@ -107,7 +107,7 @@ if (!isMainThread) {
107107
const output = loggerSpy.getCall(0).args[0]
108108

109109
// eslint-disable-next-line no-control-regex
110-
t.notMatch(output, /\x1b/)
110+
t.notMatch(output, /\u001B/)
111111

112112
t.end()
113113
}

test/success.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable @typescript-eslint/no-floating-promises */
22

33
import t from 'tap'
4-
import { isMainThread } from 'worker_threads'
5-
import { cronometro, percentiles } from '../src'
4+
import { isMainThread } from 'node:worker_threads'
5+
import { cronometro, percentiles } from '../src/index.js'
66

77
if (!isMainThread) {
88
cronometro(

test/unhandledErrorHandling.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable @typescript-eslint/no-floating-promises */
22

33
import t from 'tap'
4-
import { isMainThread } from 'worker_threads'
5-
import { Callback, cronometro, percentiles } from '../src'
4+
import { isMainThread } from 'node:worker_threads'
5+
import { Callback, cronometro, percentiles } from '../src/index.js'
66

77
if (!isMainThread) {
88
cronometro(

0 commit comments

Comments
 (0)
0