Skip to content

Commit

Permalink
feat: expose createConsola and named exports
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Mar 28, 2023
1 parent bd03098 commit ef6e5e5
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 98 deletions.
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
"exports": {
".": {
"node": {
"types": "./dist/consola.node.d.ts",
"require": "./dist/consola.node.cjs",
"import": "./dist/consola.node.mjs"
"types": "./dist/index.d.ts",
"require": "./dist/index.cjs",
"import": "./dist/index.mjs"
},
"default": {
"types": "./dist/consola.browser.d.ts",
"require": "./dist/consola.browser.cjs",
"import": "./dist/consola.browser.mjs"
"types": "./dist/index.browser.d.ts",
"require": "./dist/index.browser.cjs",
"import": "./dist/index.browser.mjs"
}
}
},
"main": "./dist/consola.node.cjs",
"module": "./dist/consola.node.mjs",
"types": "./dist/consola.node.d.ts",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist",
"types"
Expand Down
19 changes: 0 additions & 19 deletions src/consola.browser.ts

This file was deleted.

50 changes: 0 additions & 50 deletions src/consola.node.ts

This file was deleted.

33 changes: 21 additions & 12 deletions src/consola.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { normalizeLogLevel } from "./log.levels";
import Types from "./log.types";
import { Types } from "./log.types";
import { isLogObj } from "./utils/index";
import type { ConsolaOptions } from "./types";

let paused = false;
const queue = [];

class Consola {
export class Consola {
_reporters: any;
_types: any;
_level: any;
Expand Down Expand Up @@ -356,13 +357,21 @@ class Consola {
}

// Legacy support
// Consola.prototype.add = Consola.prototype.addReporter
// Consola.prototype.remove = Consola.prototype.removeReporter
// Consola.prototype.clear = Consola.prototype.removeReporter
// Consola.prototype.withScope = Consola.prototype.withTag
// Consola.prototype.mock = Consola.prototype.mockTypes
// Consola.prototype.pause = Consola.prototype.pauseLogs
// Consola.prototype.resume = Consola.prototype.resumeLogs

// Export class
export default Consola;
// @ts-expect-error
Consola.prototype.add = Consola.prototype.addReporter;
// @ts-expect-error
Consola.prototype.remove = Consola.prototype.removeReporter;
// @ts-expect-error
Consola.prototype.clear = Consola.prototype.removeReporter;
// @ts-expect-error
Consola.prototype.withScope = Consola.prototype.withTag;
// @ts-expect-error
Consola.prototype.mock = Consola.prototype.mockTypes;
// @ts-expect-error
Consola.prototype.pause = Consola.prototype.pauseLogs;
// @ts-expect-error
Consola.prototype.resume = Consola.prototype.resumeLogs;

export function createConsola(options: Partial<ConsolaOptions>) {
return new Consola(options);
}
16 changes: 16 additions & 0 deletions src/index.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { createConsola } from "./consola";
import BrowserReporter from "./reporters/browser";

export * from "./index.shared";

function _createConsola() {
const consola = createConsola({
reporters: [new BrowserReporter({})],
});
return consola;
}

export const consola = (globalThis.consola =
globalThis.consola || _createConsola());

export default consola;
5 changes: 5 additions & 0 deletions src/index.shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { Consola, createConsola } from "./consola";
export { Types } from "./log.types";
export { LogLevels } from "./log.levels";
export { isLogObj } from "./utils";
export { assignGlobalConsola } from "./utils/global";
43 changes: 37 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
export { default as Consola } from "./consola";
export { default as Types } from "./log.types";
export { LogLevels } from "./log.levels";
export { isLogObj } from "./utils";
export { assignGlobalConsola } from "./utils/global";
import { isDebug, isTest, isCI } from "std-env";
import { LogLevels } from "./log.levels";
import type { LogLevel } from "./types";
import { BasicReporter, FancyReporter } from "./reporters";
import { createConsola } from "./index.shared";

export * from "./reporters";
export * from "./index.shared";

function _createConsola() {
// Log level
let level = _getDefaultLogLevel();
if (process.env.CONSOLA_LEVEL) {
level = Number.parseInt(process.env.CONSOLA_LEVEL) || level;
}

// Create new consola instance
const consola = createConsola({
level: level as LogLevel,
reporters: [isCI || isTest ? new BasicReporter({}) : new FancyReporter({})],
});

return consola;
}

function _getDefaultLogLevel() {
if (isDebug) {
return LogLevels.Debug;
}
if (isTest) {
return LogLevels.Warn;
}
return LogLevels.Info;
}

export const consola = (globalThis.consola =
globalThis.consola || _createConsola());

export default consola;
2 changes: 1 addition & 1 deletion src/log.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LogLevels } from "./log.levels";

export default {
export const Types = {
// Silent
silent: {
level: -1,
Expand Down
1 change: 0 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export function isPlainObject(obj) {
return Object.prototype.toString.call(obj) === "[object Object]";
}

// TODO: remove for consola@3
export function isLogObj(arg) {
// Should be plain object
if (!isPlainObject(arg)) {
Expand Down

0 comments on commit ef6e5e5

Please sign in to comment.