Skip to content

Commit

Permalink
feat: support NO_COLOR (#213)
Browse files Browse the repository at this point in the history
Fixes #211
  • Loading branch information
mrgrain committed Aug 13, 2022
1 parent 7d74485 commit 1abd267
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export class EsbuildBundler {
const { buildFn = buildSync } = this.props;
wrapWithEsbuildBinaryPath(buildFn, this.props.esbuildBinaryPath)({
entryPoints,
color: process.env.NO_COLOR ? Boolean(process.env.NO_COLOR) : undefined,
...(this.props?.buildOptions || {}),
...this.getOutputOptions(outputDir, { normalize, join }),
});
Expand Down
1 change: 1 addition & 0 deletions src/inline-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ abstract class BaseInlineCode extends InlineCode {

try {
const transformedCode = wrapWithEsbuildBinaryPath(transformFn, esbuildBinaryPath)(code, {
color: process.env.NO_COLOR ? Boolean(process.env.NO_COLOR) : undefined,
logLevel: 'warning',
...transformOptions,
});
Expand Down
60 changes: 59 additions & 1 deletion test/bundler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ describe('bundling', () => {
});
});


describe('Given a custom build function', () => {
it('should call my build function', () => {
const customBuild = jest.fn().mockImplementation(() => ({
Expand Down Expand Up @@ -245,4 +244,63 @@ describe('bundling', () => {

});
});

describe('with process.env.NO_COLOR', () => {
describe.each([
['1', true],
['0', true], // NO_COLOR spec says any value
['', undefined], // except empty string
[undefined, undefined],
])('set to %j', (noColorValue, derivedColor) => {
beforeEach(() => {
process.env.NO_COLOR = noColorValue;
if (noColorValue === undefined) {
delete process.env.NO_COLOR;
}
});
afterEach(() => {
delete process.env.NO_COLOR;
});

it(`should set the color option to "${derivedColor}"`, () => {
const customBuild = jest.fn();

const bundler = new EsbuildBundler(
['index.ts'],
{
buildOptions: { absWorkingDir: '/project', outdir: 'js' },
buildFn: customBuild,
},
);

bundler.local?.tryBundle('cdk.out/123456', bundler);

expect(customBuild).toHaveBeenCalledWith(
expect.objectContaining({
color: derivedColor,
}),
);
});

it('should respect an explicit option', () => {
const customBuild = jest.fn();

const bundler = new EsbuildBundler(
['index.ts'],
{
buildOptions: { absWorkingDir: '/project', outdir: 'js', color: false },
buildFn: customBuild,
},
);

bundler.local?.tryBundle('cdk.out/123456', bundler);

expect(customBuild).toHaveBeenCalledWith(
expect.objectContaining({
color: false,
}),
);
});
});
});
});
54 changes: 54 additions & 0 deletions test/inline-code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,58 @@ describe('using transformerProps', () => {
});
});
});

describe('with process.env.NO_COLOR', () => {
describe.each([
['1', true],
['0', true], // NO_COLOR spec says any value
['', undefined], // except empty string
[undefined, undefined],
])('set to %j', (noColorValue, derivedColor) => {
beforeEach(() => {
process.env.NO_COLOR = noColorValue;
if (noColorValue === undefined) {
delete process.env.NO_COLOR;
}
});
afterEach(() => {
delete process.env.NO_COLOR;
});

it(`should set the color option to "${derivedColor}"`, () => {
const transformFn = jest.fn(transformSync);

const code = new InlineTypeScriptCode('let x: number = 1', {
transformFn,
});
code.bind(new Stack());

expect(transformFn).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
color: derivedColor,
}),
);
});

it('should respect an explicit option', () => {
const transformFn = jest.fn(transformSync);

const code = new InlineTypeScriptCode('let x: number = 1', {
transformFn,
transformOptions: {
color: false,
},
});
code.bind(new Stack());

expect(transformFn).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
color: false,
}),
);
});
});
});
});

0 comments on commit 1abd267

Please sign in to comment.