Skip to content

Commit ef7d63a

Browse files
authored
refactor: reduce polyfills usage (#437)
1 parent 1e83901 commit ef7d63a

19 files changed

+57
-77
lines changed

src/helpers/find-file.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* c8 ignore start */
2-
import { EOL } from 'node:os';
3-
42
export const findFile = (error: Error) => {
5-
const stackLines = error.stack?.split(EOL) || [];
3+
const stackLines = error.stack?.split('\n') || [];
64

75
let file = '';
86

src/helpers/get-arg.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* c8 ignore start */
2-
import process from 'node:process';
2+
import { argv } from 'node:process';
33

4-
const [, , ...processArgs] = process.argv;
4+
const [, , ...processArgs] = argv;
55

66
/**
77
* Gets the value of an argument.

src/helpers/get-runtime.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* c8 ignore start */
22

3-
import process from 'node:process';
3+
import { version } from 'node:process';
44
import type { Configs } from '../@types/poku.js';
55

66
declare const Deno: unknown;
@@ -37,8 +37,6 @@ export const getRuntime = (
3737
};
3838

3939
export const nodeVersion =
40-
getRuntime() === 'node'
41-
? Number(process.version.match(/v(\d+)\./)?.[1])
42-
: undefined;
40+
getRuntime() === 'node' ? Number(version.match(/v(\d+)\./)?.[1]) : undefined;
4341

4442
/* c8 ignore stop */

src/helpers/hr.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
/* c8 ignore start */
2-
3-
import process from 'node:process';
4-
import { EOL } from 'node:os';
2+
import { stdout } from 'node:process';
53
import { write } from './logs.js';
64

75
export const hr = () => {
8-
const line = '⎯'.repeat(process.stdout.columns - 10 || 40);
6+
const line = '⎯'.repeat(stdout.columns - 10 || 40);
97

10-
write(`${EOL}\x1b[2m\x1b[90m${line}\x1b[0m${EOL}`);
8+
write(`\n\x1b[2m\x1b[90m${line}\x1b[0m\n`);
119
};
12-
1310
/* c8 ignore stop */

src/helpers/logs.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* c8 ignore start */
22

3-
import process from 'node:process';
4-
import { EOL } from 'node:os';
3+
import { stdout } from 'node:process';
54
import type { Configs } from '../@types/poku.js';
65

76
export const isQuiet = (configs?: Configs): boolean =>
@@ -10,7 +9,7 @@ export const isQuiet = (configs?: Configs): boolean =>
109
export const isDebug = (configs?: Configs): boolean => Boolean(configs?.debug);
1110

1211
export const write = (data: string | Uint8Array) =>
13-
process.stdout.write(`${String(data)}\n`);
12+
stdout.write(`${String(data)}\n`);
1413

1514
export const printOutput = (options: {
1615
output: string;
@@ -38,7 +37,7 @@ export const printOutput = (options: {
3837

3938
const mappedOutputs = outputs.map((current) => `${pad}${current}`);
4039

41-
write(mappedOutputs.join(EOL));
40+
write(mappedOutputs.join('\n'));
4241
};
4342

4443
/* c8 ignore stop */

src/helpers/parse-assertion.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import process from 'node:process';
1+
import { cwd as processCWD, env, exit } from 'node:process';
22
import path from 'node:path';
33
import assert from 'node:assert';
4-
import { EOL } from 'node:os';
54
import { format } from './format.js';
65
import { hr } from './hr.js';
76
import { findFile } from './find-file.js';
@@ -15,7 +14,7 @@ import { write } from './logs.js';
1514
/* c8 ignore next */
1615
import type { ParseAssertionOptions } from '../@types/assert.js';
1716

18-
const cwd = process.cwd();
17+
const cwd = processCWD();
1918

2019
export const parseResultType = (type?: unknown): string => {
2120
const recurse = (value: unknown): unknown => {
@@ -64,9 +63,8 @@ export const parseAssertion = async (
6463
cb: () => void | Promise<void>,
6564
options: ParseAssertionOptions
6665
) => {
67-
const isPoku =
68-
typeof process.env?.FILE === 'string' && process.env?.FILE.length > 0;
69-
const FILE = process.env.FILE;
66+
const isPoku = typeof env?.FILE === 'string' && env?.FILE.length > 0;
67+
const FILE = env.FILE;
7068
let preIdentation = '';
7169

7270
if (indentation.hasDescribe || indentation.hasTest) preIdentation += ' ';
@@ -125,7 +123,7 @@ export const parseAssertion = async (
125123

126124
file && write(`${format.dim(`${preIdentation} File`)} ${file}`);
127125
write(`${format.dim(`${preIdentation} Code`)} ${code}`);
128-
write(`${format.dim(`${preIdentation} Operator`)} ${operator}${EOL}`);
126+
write(`${format.dim(`${preIdentation} Operator`)} ${operator}\n`);
129127

130128
if (!options?.hideDiff) {
131129
const splitActual = parseResultType(actual).split('\n');
@@ -137,7 +135,7 @@ export const parseAssertion = async (
137135
);
138136

139137
write(
140-
`${EOL}${preIdentation} ${format.dim(`${options?.expected || 'Expected'}:`)}`
138+
`\n${preIdentation} ${format.dim(`${options?.expected || 'Expected'}:`)}`
141139
);
142140
splitExpected.forEach((line) =>
143141
write(`${preIdentation} ${format.bold(format.success(line))}`)
@@ -149,7 +147,7 @@ export const parseAssertion = async (
149147
hr();
150148
}
151149

152-
process.exit(1);
150+
exit(1);
153151
}
154152

155153
// Non-assertion errors

src/helpers/runner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* c8 ignore start */
22

3-
import process from 'node:process';
3+
import { platform } from 'node:process';
44
import { extname } from 'node:path';
55
import { getRuntime } from './get-runtime.js';
66
import type { Configs } from '../@types/poku.js';
77
import type { Runner } from '../@types/runner.js';
88

9-
export const isWindows = process.platform === 'win32';
9+
export const isWindows = platform === 'win32';
1010

1111
export const runner = (filename: string, configs?: Configs): string[] => {
1212
const runtime = getRuntime(configs);

src/modules/describe.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* c8 ignore next */
2-
import process from 'node:process';
2+
import { hrtime } from 'node:process';
33
import { format, backgroundColor } from '../helpers/format.js';
44
import { write } from '../helpers/logs.js';
55
/* c8 ignore next */
@@ -57,12 +57,12 @@ export async function describe(
5757

5858
if (typeof cb !== 'function') return;
5959

60-
const start = process.hrtime();
60+
const start = hrtime();
6161
const resultCb = cb();
6262

6363
/* c8 ignore next */
6464
if (resultCb instanceof Promise) await resultCb;
65-
const end = process.hrtime(start);
65+
const end = hrtime(start);
6666

6767
/* c8 ignore start */
6868
if (title) {

src/modules/it.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* c8 ignore next */
2-
import process from 'node:process';
2+
import { hrtime } from 'node:process';
33
/* c8 ignore next */
44
import { each } from '../configs/each.js';
55
/* c8 ignore next */
@@ -46,12 +46,12 @@ export async function it(
4646
}
4747
/* c8 ignore end */
4848

49-
const start = process.hrtime();
49+
const start = hrtime();
5050
const resultCb = cb();
5151

5252
/* c8 ignore next */
5353
if (resultCb instanceof Promise) await resultCb;
54-
const end = process.hrtime(start);
54+
const end = hrtime(start);
5555

5656
if (typeof each.after.cb === 'function' && each.after.test) {
5757
const afterResult = each.after.cb();

src/modules/list-files-sync.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
* This method will be removed in a future release to use `list-files.ts` instead.
55
*/
66

7-
import process from 'node:process';
7+
import { env } from 'node:process';
88
import { readdirSync, statSync } from 'node:fs';
99
import path from 'node:path';
1010
import type { Configs } from '../@types/list-files.js';
1111
import { escapeRegExp, sanitizePath } from './list-files.js';
1212

1313
const isDir = (fullPath: string) => statSync(fullPath).isDirectory();
1414

15-
const envFilter = process.env.FILTER?.trim()
16-
? new RegExp(escapeRegExp(process.env.FILTER), 'i')
15+
const envFilter = env.FILTER?.trim()
16+
? new RegExp(escapeRegExp(env.FILTER), 'i')
1717
: null;
1818

1919
const listFiles = (

src/modules/list-files.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* c8 ignore next */
2-
import process from 'node:process';
2+
import { env } from 'node:process';
33
import { sep, join } from 'node:path';
44
import { readdir, stat as fsStat } from '../polyfills/fs.js';
55
/* c8 ignore next */
@@ -28,8 +28,8 @@ export const escapeRegExp = (string: string) =>
2828
/* c8 ignore stop */
2929

3030
/* c8 ignore start */
31-
const envFilter = process.env.FILTER?.trim()
32-
? new RegExp(escapeRegExp(process.env.FILTER), 'i')
31+
const envFilter = env.FILTER?.trim()
32+
? new RegExp(escapeRegExp(env.FILTER), 'i')
3333
: undefined;
3434
/* c8 ignore stop */
3535

src/modules/poku.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
import process from 'node:process';
8-
import { EOL } from 'node:os';
98
import { runTests, runTestsParallel } from '../services/run-tests.js';
109
import { exit } from './exit.js';
1110
import { format } from '../helpers/format.js';
@@ -68,7 +67,7 @@ export async function poku(
6867
// Parallel
6968
if (showLogs) {
7069
hr();
71-
write(`${format.bold('Running the Test Suite in Parallel')}${EOL}`);
70+
write(`${format.bold('Running the Test Suite in Parallel')}\n`);
7271
}
7372

7473
try {
@@ -100,7 +99,7 @@ export async function poku(
10099
([file, time]) =>
101100
`${indentation.test}${format.success('✔')} ${format.dim(`${file}${time}ms`)}`
102101
)
103-
.join(EOL)
102+
.join('\n')
104103
);
105104
}
106105

@@ -111,7 +110,7 @@ export async function poku(
111110
([file, time]) =>
112111
`${indentation.test}${format.fail('✘')} ${format.dim(`${file}${time}ms`)}`
113112
)
114-
.join(EOL)
113+
.join('\n')
115114
);
116115
}
117116

src/modules/test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* c8 ignore next */
2-
import process from 'node:process';
2+
import { hrtime } from 'node:process';
33
/* c8 ignore next */
44
import { each } from '../configs/each.js';
55
/* c8 ignore next */
@@ -45,12 +45,12 @@ export async function test(
4545
}
4646
/* c8 ignore stop */
4747

48-
const start = process.hrtime();
48+
const start = hrtime();
4949
const resultCb = cb();
5050

5151
/* c8 ignore next */
5252
if (resultCb instanceof Promise) await resultCb;
53-
const end = process.hrtime(start);
53+
const end = hrtime(start);
5454

5555
if (typeof each.after.cb === 'function' && each.after.test) {
5656
const afterResult = each.after.cb();

src/services/each.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { EOL } from 'node:os';
21
import { format } from '../helpers/format.js';
32
import { write } from '../helpers/logs.js';
43
/* c8 ignore next */
@@ -32,12 +31,12 @@ const eachCore = async (
3231
);
3332
write(
3433
format.fail(
35-
` ├─ Who's trying to run this ${type}?${EOL} │ └─ ${format.underline(fileRelative)}`
34+
` ├─ Who's trying to run this ${type}?\n │ └─ ${format.underline(fileRelative)}`
3635
)
3736
);
3837

3938
error instanceof Error &&
40-
write(format.fail(` ├─ Message:${EOL} │ └─ ${error.message}`));
39+
write(format.fail(` ├─ Message:\n │ └─ ${error.message}`));
4140

4241
return false;
4342
}

src/services/pid.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* c8 ignore start */
22

33
import { spawn } from 'node:child_process';
4-
import { EOL } from 'node:os';
54
import { forceArray } from '../helpers/force-array.js';
65

76
export const setPortsAndPIDs = (portOrPID: number | number[]) =>
@@ -53,7 +52,7 @@ export const getPIDs = {
5352
]);
5453

5554
service.stdout.on('data', (data: Buffer) => {
56-
const output = data.toString().trim().split(EOL);
55+
const output = data.toString().trim().split('\n');
5756

5857
output.forEach((pid) => {
5958
if (pid) PIDs.add(Number(pid));
@@ -74,7 +73,7 @@ export const getPIDs = {
7473

7574
service.stdout.on('data', (data: Buffer) => {
7675
const output = data.toString().trim();
77-
const lines = output.trim().split(EOL);
76+
const lines = output.trim().split('\n');
7877

7978
/**
8079
* TODO: Chack line for "/:\d+\s+\w+\s+\d+\s+(\d+)/" regex match to safe support multiple Windows versions

src/services/run-test-file.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* c8 ignore start */ // c8 bug
2-
import process from 'node:process';
2+
import { cwd as processCWD, hrtime, env } from 'node:process';
33
import { relative } from 'node:path';
44
import { spawn } from 'node:child_process';
55
import { indentation } from '../configs/indentation.js';
@@ -11,7 +11,7 @@ import { beforeEach, afterEach } from './each.js';
1111
/* c8 ignore next */
1212
import type { Configs } from '../@types/poku.js';
1313

14-
const cwd = process.cwd();
14+
const cwd = processCWD();
1515
/* c8 ignore stop */ // c8 bug
1616

1717
/* c8 ignore next */ // c8 bug
@@ -49,8 +49,8 @@ export const runTestFile = (
4949
);
5050
}
5151

52-
const start = process.hrtime();
53-
let end: ReturnType<typeof process.hrtime>;
52+
const start = hrtime();
53+
let end: ReturnType<typeof hrtime>;
5454

5555
/* c8 ignore next */
5656
if (!(await beforeEach(fileRelative, configs))) return false;
@@ -61,7 +61,7 @@ export const runTestFile = (
6161
/* c8 ignore next */
6262
shell: isWindows,
6363
env: {
64-
...process.env,
64+
...env,
6565
FILE: configs?.parallel || configs?.deno?.cjs ? fileRelative : '',
6666
},
6767
});
@@ -71,7 +71,7 @@ export const runTestFile = (
7171
child.stderr.on('data', stdOut);
7272

7373
child.on('close', async (code) => {
74-
end = process.hrtime(start);
74+
end = hrtime(start);
7575

7676
const result = code === 0;
7777

@@ -95,7 +95,7 @@ export const runTestFile = (
9595

9696
/* c8 ignore start */
9797
child.on('error', (err) => {
98-
end = process.hrtime(start);
98+
end = hrtime(start);
9999

100100
const total = (end[0] * 1e3 + end[1] / 1e6).toFixed(6);
101101

0 commit comments

Comments
 (0)