Skip to content

Commit

Permalink
feat: logger custom colors
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed May 7, 2021
1 parent cbdeaa3 commit 1e416a3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
2 changes: 2 additions & 0 deletions core/instrument/src/misc/jest-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
findJestConfig,
getRelatedTests,
} from '@component-controls/jest-extract';
import { log } from '@component-controls/logger';
import { resolveSnapshotFile } from '@component-controls/core/node-utils';
import { JestTests } from '@component-controls/core';
import { CachedFileResource } from './chached-file';
Expand Down Expand Up @@ -81,6 +82,7 @@ export const extractTests = async (
return cachedResults;
}
const projectFolder = findJestConfig(tests.testFiles[0]);
log('jest tests', tests.testFiles[0], [115, 245, 184]);
const testResults = await runProjectTests({
testFiles: tests.testFiles.map(
f => `.${path.sep}${path.relative(projectFolder, f)}`,
Expand Down
27 changes: 21 additions & 6 deletions core/logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,33 @@ const defaultOptions: LogOptions = {

let logOptions: LogOptions = { ...defaultOptions };

const output = (heading: string, text: string) => {
console.log(chalk.bgRgb(...logOptions.colors)(`@${heading}`), text);
const output = (
heading: string,
text: string,
bgColor?: [number, number, number],
) => {
console.log(
chalk.bgRgb(...(bgColor || logOptions.colors))(`@${heading}`),
text,
);
};

export const log = (heading: string, text: string): void => {
export const log = (
heading: string,
text: string,
bgColor?: [number, number, number],
): void => {
if (logOptions.logLevel === 'all') {
output(heading, text);
output(heading, text, bgColor);
}
};
export const error = (heading: string, text: string): void => {
export const error = (
heading: string,
text: string,
bgColor?: [number, number, number],
): void => {
if (logOptions.logLevel !== 'none') {
output(heading, text);
output(heading, text, bgColor);
}
};

Expand Down
1 change: 1 addition & 0 deletions plugins/cc-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"dependencies": {
"@component-controls/instrument": "^3.9.0",
"@component-controls/jest-axe-matcher": "^3.8.1",
"@component-controls/logger": "^3.8.2",
"@component-controls/store": "^3.9.0",
"@component-controls/test-renderers": "^3.9.0",
"@component-controls/webpack-compile": "^3.9.1",
Expand Down
3 changes: 3 additions & 0 deletions plugins/cc-cli/src/cli/save-data-template.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';
import fs from 'fs';
import { dynamicRequire } from '@component-controls/core/node-utils';
import { log } from '@component-controls/logger';
import { CliOptions, getTestFolder } from './utils';
import { TemplateOptions, DataImportOptions, relativeImport } from '../utils';
import { createDataTemplate } from '../data-templates/data-template';
Expand All @@ -21,7 +22,9 @@ export const saveDataTemplate = async <P extends TemplateOptions>(
.split('.')
.map((e, i) => (i === 1 ? 'data' : e))
.join('.');

const filePath = path.resolve(testFolder, dataName);
log('saving data', filePath, [184, 226, 255]);
let existing: Record<string, any> | undefined = undefined;
if (fs.existsSync(filePath)) {
if (overwrite) {
Expand Down
2 changes: 2 additions & 0 deletions plugins/cc-cli/src/cli/save-test-template.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';
import fs from 'fs';
import { log } from '@component-controls/logger';
import { CliOptions, getTestFolder } from './utils';
import { TemplateFunction, TemplateOptions } from '../utils';
import { saveDataTemplate } from './save-data-template';
Expand Down Expand Up @@ -29,6 +30,7 @@ export const saveTemplate = async <P extends TemplateOptions>(
);
return;
}
log('saving test', testFilePath, [115, 245, 184]);
const dataTemplate = await saveDataTemplate(options);

const content = await templateFn(
Expand Down

0 comments on commit 1e416a3

Please sign in to comment.