Skip to content

Commit 1d3e082

Browse files
committed
feat: migrate to ESM
1 parent 7c9a668 commit 1d3e082

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+5641
-9306
lines changed

.eslintrc

+8-9
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
"jest": true
88
},
99
"parser": "@typescript-eslint/parser",
10-
"extends": [
11-
"eslint:recommended",
12-
"plugin:@typescript-eslint/recommended",
13-
"plugin:prettier/recommended",
14-
"prettier"
15-
],
16-
"plugins": [
17-
"import"
18-
],
1910
"parserOptions": {
2011
"project": "tsconfig.json",
2112
"sourceType": "module"
2213
},
14+
"plugins": [
15+
"import"
16+
],
17+
"extends": [
18+
"eslint:recommended",
19+
"plugin:@typescript-eslint/recommended",
20+
"plugin:prettier/recommended"
21+
],
2322
"rules": {
2423
"linebreak-style": ["error", "unix"],
2524
"no-empty": 1,

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ npm install
5959
# build the dist
6060
npm run build
6161
# run the repl (this allows you to import from ./src)
62-
npm run ts-node
62+
npm run tsx
6363
# run the tests
6464
npm run test
6565
# lint the source code

benches/index.ts

+25-14
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
#!/usr/bin/env ts-node
1+
#!/usr/bin/env tsx
22

3-
import fs from 'fs';
4-
import path from 'path';
3+
import fs from 'node:fs';
4+
import path from 'node:path';
5+
import url from 'node:url';
56
import si from 'systeminformation';
6-
import loggerText from './logger_text';
7-
import loggerStructured from './logger_structured';
8-
import loggerHierarchy from './logger_hierarchy';
9-
import loggerFiltered from './logger_filtered';
10-
import loggerHandlers from './logger_handlers';
7+
import { benchesPath } from './utils/utils.js';
8+
import loggerText from './logger_text.js';
9+
import loggerStructured from './logger_structured.js';
10+
import loggerHierarchy from './logger_hierarchy.js';
11+
import loggerFiltered from './logger_filtered.js';
12+
import loggerHandlers from './logger_handlers.js';
1113

1214
async function main(): Promise<void> {
13-
await fs.promises.mkdir(path.join(__dirname, 'results'), { recursive: true });
15+
await fs.promises.mkdir(path.join(benchesPath, 'results'), {
16+
recursive: true,
17+
});
1418
await loggerText();
1519
await loggerStructured();
1620
await loggerHierarchy();
1721
await loggerFiltered();
1822
await loggerHandlers();
1923
const resultFilenames = await fs.promises.readdir(
20-
path.join(__dirname, 'results'),
24+
path.join(benchesPath, 'results'),
2125
);
2226
const metricsFile = await fs.promises.open(
23-
path.join(__dirname, 'results', 'metrics.txt'),
27+
path.join(benchesPath, 'results', 'metrics.txt'),
2428
'w',
2529
);
2630
let concatenating = false;
2731
for (const resultFilename of resultFilenames) {
2832
if (/.+_metrics\.txt$/.test(resultFilename)) {
2933
const metricsData = await fs.promises.readFile(
30-
path.join(__dirname, 'results', resultFilename),
34+
path.join(benchesPath, 'results', resultFilename),
3135
);
3236
if (concatenating) {
3337
await metricsFile.write('\n');
@@ -43,9 +47,16 @@ async function main(): Promise<void> {
4347
system: 'model, manufacturer',
4448
});
4549
await fs.promises.writeFile(
46-
path.join(__dirname, 'results', 'system.json'),
50+
path.join(benchesPath, 'results', 'system.json'),
4751
JSON.stringify(systemData, null, 2),
4852
);
4953
}
5054

51-
void main();
55+
if (import.meta.url.startsWith('file:')) {
56+
const modulePath = url.fileURLToPath(import.meta.url);
57+
if (process.argv[1] === modulePath) {
58+
void main();
59+
}
60+
}
61+
62+
export default main;

benches/logger_filtered.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import path from 'path';
1+
import path from 'node:path';
2+
import url from 'node:url';
23
import b from 'benny';
34
import Logger, { LogLevel, formatting } from '@';
4-
import { BenchHandler, suiteCommon } from './utils';
5+
import { BenchHandler, suiteCommon } from './utils/index.js';
6+
7+
const filePath = url.fileURLToPath(import.meta.url);
58

69
async function main() {
710
const msg = 'Hello World';
811
const data = { foo: 'bar', bar: () => 'foo' };
912
const summary = await b.suite(
10-
path.basename(__filename, path.extname(__filename)),
13+
path.basename(filePath, path.extname(filePath)),
1114
b.add('log level', () => {
1215
const logger = new Logger('root', LogLevel.WARN, [
1316
new BenchHandler(formatting.formatter),
@@ -30,8 +33,11 @@ async function main() {
3033
return summary;
3134
}
3235

33-
if (require.main === module) {
34-
void main();
36+
if (import.meta.url.startsWith('file:')) {
37+
const modulePath = url.fileURLToPath(import.meta.url);
38+
if (process.argv[1] === modulePath) {
39+
void main();
40+
}
3541
}
3642

3743
export default main;

benches/logger_handlers.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import os from 'os';
2-
import path from 'path';
3-
import fs from 'fs';
1+
import os from 'node:os';
2+
import path from 'node:path';
3+
import url from 'node:url';
4+
import fs from 'node:fs';
45
import b from 'benny';
56
import Logger, { LogLevel, StreamHandler, ConsoleErrHandler } from '@';
6-
import { suiteCommon } from './utils';
7+
import { suiteCommon } from './utils/index.js';
8+
9+
const filePath = url.fileURLToPath(import.meta.url);
710

811
async function main() {
912
const msg = 'Hello World';
@@ -18,7 +21,7 @@ async function main() {
1821
const stderr = fs.createWriteStream(path.join(tmpDir, 'stderr'));
1922
process.stderr.write = stderr.write.bind(stderr);
2023
const summary = await b.suite(
21-
path.basename(__filename, path.extname(__filename)),
24+
path.basename(filePath, path.extname(filePath)),
2225
b.add('console.error', () => {
2326
const logger = new Logger('root', LogLevel.NOTSET, [
2427
new ConsoleErrHandler(),
@@ -43,8 +46,11 @@ async function main() {
4346
return summary;
4447
}
4548

46-
if (require.main === module) {
47-
void main();
49+
if (import.meta.url.startsWith('file:')) {
50+
const modulePath = url.fileURLToPath(import.meta.url);
51+
if (process.argv[1] === modulePath) {
52+
void main();
53+
}
4854
}
4955

5056
export default main;

benches/logger_hierarchy.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import path from 'path';
1+
import path from 'node:path';
2+
import url from 'node:url';
23
import b from 'benny';
34
import Logger, { LogLevel, formatting } from '@';
4-
import { BenchHandler, suiteCommon } from './utils';
5+
import { BenchHandler, suiteCommon } from './utils/index.js';
6+
7+
const filePath = url.fileURLToPath(import.meta.url);
58

69
async function main() {
710
const msg = 'Hello World';
811
const data = { foo: 'bar', bar: () => 'foo' };
912
const summary = await b.suite(
10-
path.basename(__filename, path.extname(__filename)),
13+
path.basename(filePath, path.extname(filePath)),
1114
b.add('1-levels', () => {
1215
const logger1 = new Logger('1', LogLevel.NOTSET, [
1316
new BenchHandler(formatting.formatter),
@@ -51,8 +54,11 @@ async function main() {
5154
return summary;
5255
}
5356

54-
if (require.main === module) {
55-
void main();
57+
if (import.meta.url.startsWith('file:')) {
58+
const modulePath = url.fileURLToPath(import.meta.url);
59+
if (process.argv[1] === modulePath) {
60+
void main();
61+
}
5662
}
5763

5864
export default main;

benches/logger_structured.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import type { LogRecord } from '@/types';
2-
import path from 'path';
2+
import path from 'node:path';
3+
import url from 'node:url';
34
import b from 'benny';
45
import Logger, {
56
LogLevel,
67
formatting,
78
levelToString,
89
evalLogDataValue,
910
} from '@';
10-
import { BenchHandler, suiteCommon } from './utils';
11+
import { BenchHandler, suiteCommon } from './utils/index.js';
12+
13+
const filePath = url.fileURLToPath(import.meta.url);
1114

1215
async function main() {
1316
const msg = 'Hello World';
1417
const data = { foo: 'bar', bar: () => 'foo' };
1518
const summary = await b.suite(
16-
path.basename(__filename, path.extname(__filename)),
19+
path.basename(filePath, path.extname(filePath)),
1720
b.add('formatting default', () => {
1821
const logger = new Logger('root', LogLevel.NOTSET, [
1922
new BenchHandler(formatting.jsonFormatter),
@@ -82,8 +85,11 @@ async function main() {
8285
return summary;
8386
}
8487

85-
if (require.main === module) {
86-
void main();
88+
if (import.meta.url.startsWith('file:')) {
89+
const modulePath = url.fileURLToPath(import.meta.url);
90+
if (process.argv[1] === modulePath) {
91+
void main();
92+
}
8793
}
8894

8995
export default main;

benches/logger_text.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import path from 'path';
1+
import path from 'node:path';
2+
import url from 'node:url';
23
import b from 'benny';
34
import Logger, { LogLevel, formatting } from '@';
45
import { BenchHandler, suiteCommon } from './utils';
56

7+
const filePath = url.fileURLToPath(import.meta.url);
8+
69
async function main() {
710
const msg = 'Hello World';
811
const data = { foo: 'bar', bar: () => 'foo' };
912
const summary = await b.suite(
10-
path.basename(__filename, path.extname(__filename)),
13+
path.basename(filePath, path.extname(filePath)),
1114
b.add('formatting default', () => {
1215
const logger = new Logger('root', LogLevel.NOTSET, [
1316
new BenchHandler(formatting.formatter),
@@ -62,8 +65,11 @@ async function main() {
6265
return summary;
6366
}
6467

65-
if (require.main === module) {
66-
void main();
68+
if (import.meta.url.startsWith('file:')) {
69+
const modulePath = url.fileURLToPath(import.meta.url);
70+
if (process.argv[1] === modulePath) {
71+
void main();
72+
}
6773
}
6874

6975
export default main;

benches/results/logger_filtered.chart.html

+7-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</head>
2929
<body>
3030
<div class="container">
31-
<canvas id="chart1687502300189" width="16" height="9"></canvas>
31+
<canvas id="chart1691768933186" width="16" height="9"></canvas>
3232
</div>
3333
<script>
3434
const format = (num) => {
@@ -51,18 +51,18 @@
5151
chunked.map((chunk) => chunk.join('')).join(' ') + fractionStr
5252
)
5353
}
54-
const ctx1687502300189 = document
55-
.getElementById('chart1687502300189')
54+
const ctx1691768933186 = document
55+
.getElementById('chart1691768933186')
5656
.getContext('2d')
57-
const chart1687502300189 = new Chart(ctx1687502300189, {
57+
const chart1691768933186 = new Chart(ctx1691768933186, {
5858
type: 'bar',
5959
data: {
6060
labels: ["log level","keys regex"],
6161
datasets: [
6262
{
63-
data: [167936103,166306609],
64-
backgroundColor: ["hsl(120, 85%, 55%)","hsl(118.836, 85%, 55%)"],
65-
borderColor: ["hsl(120, 85%, 55%)","hsl(118.836, 85%, 55%)"],
63+
data: [123096006,88210570],
64+
backgroundColor: ["hsl(120, 85%, 55%)","hsl(85.992, 85%, 55%)"],
65+
borderColor: ["hsl(120, 85%, 55%)","hsl(85.992, 85%, 55%)"],
6666
borderWidth: 2,
6767
},
6868
],

0 commit comments

Comments
 (0)