Skip to content

Commit 1b64bff

Browse files
committed
Set chalk options automatically from options.cjs set
This _seems_ to work, but the resulting interaction between CommonJS and ESM feels a bit Zalgo-y. It may be better for modules to keep setting global options and chalk options in parallel while the former remains CommonJS, but I wanted to see what simplification would look like.
1 parent f1beca2 commit 1b64bff

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

lib/worker/base.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {workerData} from 'node:worker_threads';
55

66
import setUpCurrentlyUnhandled from 'currently-unhandled';
77

8-
import {set as setChalk} from '../chalk.js';
98
import nowAndTimers from '../now-and-timers.cjs';
109
import providerManager from '../provider-manager.js';
1110
import Runner from '../runner.js';
@@ -22,7 +21,6 @@ const currentlyUnhandled = setUpCurrentlyUnhandled();
2221

2322
const run = async options => {
2423
setOptions(options);
25-
setChalk(options.chalkOptions);
2624

2725
if (options.chalkOptions.level > 0) {
2826
const {stdout, stderr} = process;

lib/worker/options.cjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
'use strict';
2+
const chalk = import('../chalk.js'); // eslint-disable-line node/no-unsupported-features/es-syntax
3+
let setChalk;
4+
chalk.then(chalk => {
5+
setChalk = chalk.set;
6+
});
7+
28
let options = null;
39
exports.get = () => {
410
if (!options) {
@@ -14,4 +20,11 @@ exports.set = newOptions => {
1420
}
1521

1622
options = newOptions;
23+
if (options.chalkOptions) {
24+
if (setChalk) {
25+
setChalk(options.chalkOptions);
26+
} else {
27+
chalk.then(chalk => chalk.set(options.chalkOptions));
28+
}
29+
}
1730
};

test-tap/assert.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ import {test} from 'tap';
77
import * as assert from '../lib/assert.js';
88
import {set as setChalk} from '../lib/chalk.js';
99
import * as snapshotManager from '../lib/snapshot-manager.js';
10-
import {set as setOptions} from '../lib/worker/options.cjs';
1110

12-
const options = {chalkOptions: {level: 0}};
13-
setOptions(options);
14-
setChalk(options.chalkOptions);
11+
setChalk({level: 0});
1512

1613
let lastFailure = null;
1714
let lastPassed = false;

test-tap/test-try-commit.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import delay from 'delay';
22
import {test} from 'tap';
33

4-
import {set as setChalk} from '../lib/chalk.js';
54
import ContextRef from '../lib/context-ref.js';
65
import {set as setOptions} from '../lib/worker/options.cjs';
76

87
import {newAva} from './helper/ava-test.js';
98

10-
const options = {chalkOptions: {level: 0}};
11-
setOptions(options);
12-
setChalk(options.chalkOptions);
9+
setOptions({chalkOptions: {level: 0}});
1310

1411
test('try-commit works', async t => {
1512
const ava = newAva();

test/snapshot-regenerate-report/test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ import path from 'node:path';
33

44
import test from '@ava/test';
55

6-
import {set as setChalk} from '../../lib/chalk.js';
76
import {load} from '../../lib/snapshot-manager.js';
87
import {set as setOptions} from '../../lib/worker/options.cjs';
98
import {cwd, fixture} from '../helpers/exec.js';
109

11-
setChalk({level: 0});
12-
setOptions({});
10+
setOptions({chalkOptions: {level: 0}});
1311

1412
test('snapshot report can be regenerated from .snap file', async t => {
1513
const workingDir = cwd();

0 commit comments

Comments
 (0)