itivrutaha 3.2.1 → 3.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,61 @@
1
+ import { type ChalkInstance } from 'chalk';
2
+ export interface Scope<ScopeName> {
3
+ name: ScopeName;
4
+ color: ChalkInstance;
5
+ }
6
+ export interface LogType<Name> {
7
+ name: Name;
8
+ emoji: string;
9
+ color: ChalkInstance;
10
+ }
11
+ export interface UnifiedData<ScopeName> {
12
+ scope?: ScopeName;
13
+ msg: string | Error;
14
+ [key: string]: any;
15
+ }
16
+ export interface Config<ScopeName, LogTypeName> {
17
+ theme: string;
18
+ timeFormat: string;
19
+ scopes: Scope<ScopeName>[];
20
+ types: LogType<LogTypeName>[];
21
+ typeFilterFn: () => string[];
22
+ }
23
+ export declare function makeConfig<const T extends Config<string, string>>(config: T): T;
24
+ export declare const defaultConfig: {
25
+ readonly timeFormat: "hh:mm:ss dd-MM-yyyy";
26
+ readonly theme: `:time ${string} :scope :emoji :type :msg :data`;
27
+ readonly typeFilterFn: () => string[];
28
+ readonly scopes: [{
29
+ readonly name: "app";
30
+ readonly color: ChalkInstance;
31
+ }];
32
+ readonly types: [{
33
+ readonly emoji: "✅";
34
+ readonly name: "success";
35
+ readonly color: ChalkInstance;
36
+ }, {
37
+ readonly emoji: "ℹ️";
38
+ readonly name: "info";
39
+ readonly color: ChalkInstance;
40
+ }, {
41
+ readonly emoji: "👍";
42
+ readonly name: "okay";
43
+ readonly color: ChalkInstance;
44
+ }, {
45
+ readonly emoji: "✍️";
46
+ readonly name: "note";
47
+ readonly color: ChalkInstance;
48
+ }, {
49
+ readonly emoji: "🧐";
50
+ readonly name: "verbose";
51
+ readonly color: ChalkInstance;
52
+ }, {
53
+ readonly emoji: "⚠️";
54
+ readonly name: "warning";
55
+ readonly color: ChalkInstance;
56
+ }, {
57
+ readonly emoji: "🚨";
58
+ readonly name: "error";
59
+ readonly color: ChalkInstance;
60
+ }];
61
+ };
package/dist/config.js ADDED
@@ -0,0 +1,61 @@
1
+ /*
2
+ * Contains configuration related code.
3
+ * Created On 15 January 2024
4
+ */
5
+ import chalk, {} from 'chalk';
6
+ export function makeConfig(config) {
7
+ return config;
8
+ }
9
+ export const defaultConfig = makeConfig({
10
+ timeFormat: 'hh:mm:ss dd-MM-yyyy',
11
+ theme: `:time ${chalk.gray.dim('•')} :scope :emoji :type :msg :data`,
12
+ typeFilterFn: () => {
13
+ const environment = process.env['ITIVRUTAHA_LOG_FILTER'] || process.env['itivrutaha_log_filter'];
14
+ if (!environment)
15
+ return ['success', 'info', 'okay', 'note', 'verbose', 'warning', 'error'];
16
+ return environment.trim().toLowerCase().split(',');
17
+ },
18
+ scopes: [
19
+ {
20
+ name: 'app',
21
+ color: chalk.redBright
22
+ }
23
+ ],
24
+ types: [
25
+ {
26
+ emoji: '✅',
27
+ name: 'success',
28
+ color: chalk.greenBright,
29
+ },
30
+ {
31
+ emoji: 'ℹ️',
32
+ name: 'info',
33
+ color: chalk.blueBright,
34
+ },
35
+ {
36
+ emoji: '👍',
37
+ name: 'okay',
38
+ color: chalk.gray,
39
+ },
40
+ {
41
+ emoji: '✍️',
42
+ name: 'note',
43
+ color: chalk.magentaBright,
44
+ },
45
+ {
46
+ emoji: '🧐',
47
+ name: 'verbose',
48
+ color: chalk.cyanBright,
49
+ },
50
+ {
51
+ emoji: '⚠️',
52
+ name: 'warning',
53
+ color: chalk.yellowBright
54
+ },
55
+ {
56
+ emoji: '🚨',
57
+ name: 'error',
58
+ color: chalk.redBright
59
+ },
60
+ ]
61
+ });
@@ -0,0 +1,2 @@
1
+ export { defaultConfig } from './config.js';
2
+ export { itivrutaha, type ExtractScopes } from './itivrutaha.js';
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ /*
2
+ * Entry file for itivrutaha logging module.
3
+ * Created On 29 October 2023
4
+ */
5
+ export { defaultConfig } from './config.js';
6
+ export { itivrutaha } from './itivrutaha.js';
@@ -0,0 +1,3 @@
1
+ import { type Config, type UnifiedData } from './config.js';
2
+ export declare function itivrutaha<Scope extends string, LogTypeName extends string>(config: Config<Scope, LogTypeName>): { [Type in LogTypeName]: (msgOrData: string | UnifiedData<Scope> | Error, data?: any, scope?: Scope) => void; };
3
+ export type ExtractScopes<T> = T extends ReturnType<typeof itivrutaha<infer S extends string, any>> ? S : never;
@@ -0,0 +1,15 @@
1
+ /*
2
+ * Contains an exportable logger function.
3
+ * Created On 29 October 2023
4
+ */
5
+ import {} from './config.js';
6
+ import { render } from './renderer.js';
7
+ export function itivrutaha(config) {
8
+ // calculate the log type filter values at load
9
+ const typeFilter = config.typeFilterFn();
10
+ // return all the log functions
11
+ return config.types.reduce((previous, current) => ({
12
+ ...previous,
13
+ [current.name]: render(config, current, typeFilter)
14
+ }), {});
15
+ }
@@ -0,0 +1,2 @@
1
+ import { type Config, type LogType, type UnifiedData } from './config.js';
2
+ export declare function render<ScopeName, LogTypeName extends string>(config: Config<ScopeName, LogTypeName>, type: LogType<LogTypeName>, filter: LogTypeName[]): (msgOrData: string | UnifiedData<ScopeName> | Error, data?: any, scope?: ScopeName) => void;
@@ -0,0 +1,52 @@
1
+ /*
2
+ * Renders a single log line by consuming an object.
3
+ * Created On 15 January 2024
4
+ */
5
+ import {} from './config.js';
6
+ import { filterObject } from './utilts.js';
7
+ import * as variables from './variables/index.js';
8
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ function line(config, type, msg, scopeName, data) {
10
+ // filter internal keys from data
11
+ if (data)
12
+ data = filterObject(data, ['msg', 'scope']);
13
+ console.log(config.theme
14
+ .replace(/:time/g, variables.time(config))
15
+ .replace(/:scope/g, variables.scope(config, scopeName))
16
+ .replace(/:emoji/g, variables.emoji(type))
17
+ .replace(/:type/g, variables.type(type))
18
+ .replace(/:msg/g, msg instanceof Error ? msg.message : msg)
19
+ .replace(/:data/g, variables.data(data)));
20
+ if (msg instanceof Error) {
21
+ console.log(msg);
22
+ }
23
+ // whether there's an error instance in data
24
+ for (const key in data) {
25
+ const value = data[key];
26
+ if (value instanceof Error) {
27
+ console.log(value);
28
+ }
29
+ }
30
+ }
31
+ export function render(config, type, filter) {
32
+ // consume all the log objects
33
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
+ return (msgOrData, data, scope) => {
35
+ // filter based on filter function from config
36
+ if (!filter.includes(type.name))
37
+ return;
38
+ if (msgOrData instanceof Error || typeof msgOrData == 'string') {
39
+ // seperate arguments
40
+ line(config, type, msgOrData, scope, data);
41
+ }
42
+ else {
43
+ // unified data
44
+ // ensure scope & data are not given in both placces
45
+ if (scope)
46
+ throw Error('Specifying scope as argument is not allowed');
47
+ if (data)
48
+ throw Error('Specifying data separately is not allowed');
49
+ line(config, type, msgOrData.msg, msgOrData.scope, msgOrData);
50
+ }
51
+ };
52
+ }
@@ -0,0 +1 @@
1
+ export declare function filterObject(obj: Record<string, any>, keysToExclude: string[]): Record<string, any>;
package/dist/utilts.js ADDED
@@ -0,0 +1,8 @@
1
+ /*
2
+ * Contains small utility functions used across this project.
3
+ * Created On 15 January 2024
4
+ */
5
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
+ export function filterObject(obj, keysToExclude) {
7
+ return Object.fromEntries(Object.entries(obj).filter(([key]) => !keysToExclude.includes(key)));
8
+ }
@@ -0,0 +1 @@
1
+ export declare function data(input: any): string;
@@ -0,0 +1,40 @@
1
+ /*
2
+ * Renders the ":data" variable.
3
+ * Created On 15 January 2024
4
+ */
5
+ import chalk from 'chalk';
6
+ import { highlight } from 'cli-highlight';
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
+ function formatJSON(data) {
9
+ return JSON.stringify(data)
10
+ .replace(/{"/g, '{ "')
11
+ .replace(/"}/g, '" }')
12
+ .replace(/,"/g, ', "')
13
+ .replace(/":/g, '": ');
14
+ }
15
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
+ export function data(input) {
17
+ if (!input)
18
+ return '';
19
+ let str = '';
20
+ for (const key in input) {
21
+ const value = input[key];
22
+ const keyBlock = `${chalk.cyanBright(key)}${chalk.whiteBright('=')}`;
23
+ if (value instanceof Error) {
24
+ str = str.concat(keyBlock).concat(chalk.magentaBright(value.message)).concat(' ');
25
+ }
26
+ else if (typeof value == 'object') {
27
+ str = str.concat(keyBlock).concat(highlight(formatJSON(value), {
28
+ language: 'json',
29
+ ignoreIllegals: true,
30
+ })).concat(' ');
31
+ }
32
+ else if (typeof value == 'string') {
33
+ str = str.concat(keyBlock).concat(chalk.magentaBright(value)).concat(' ');
34
+ }
35
+ else {
36
+ str = str.concat(keyBlock).concat(chalk.magentaBright(JSON.stringify(value))).concat(' ');
37
+ }
38
+ }
39
+ return str.trim();
40
+ }
@@ -0,0 +1,2 @@
1
+ import { type LogType } from '../config.js';
2
+ export declare function emoji<LogTypeName extends string>(type: LogType<LogTypeName>): string;
@@ -0,0 +1,8 @@
1
+ /*
2
+ * Renders the ":emoji" variable if an emoji is specified.
3
+ * Created On 15 January 2024
4
+ */
5
+ import {} from '../config.js';
6
+ export function emoji(type) {
7
+ return type.emoji.trim();
8
+ }
@@ -0,0 +1,5 @@
1
+ export * from './time.js';
2
+ export * from './type.js';
3
+ export * from './data.js';
4
+ export * from './emoji.js';
5
+ export * from './scope.js';
@@ -0,0 +1,9 @@
1
+ /*
2
+ * Exports all render functions neatly as single package.
3
+ * Created On 15 January 2024
4
+ */
5
+ export * from './time.js';
6
+ export * from './type.js';
7
+ export * from './data.js';
8
+ export * from './emoji.js';
9
+ export * from './scope.js';
@@ -0,0 +1,2 @@
1
+ import { type Config } from '../config.js';
2
+ export declare function scope<ScopeName, LogTypeName extends string>(config: Config<ScopeName, LogTypeName>, scopeName?: ScopeName): string;
@@ -0,0 +1,11 @@
1
+ /*
2
+ * Renders ":scope" variable with the current log line's scope.
3
+ * Created On 15 January 2024
4
+ */
5
+ import {} from '../config.js';
6
+ export function scope(config, scopeName) {
7
+ const scope = scopeName ? config.scopes.find(sco => sco.name == scopeName) : config.scopes[0];
8
+ if (!scope)
9
+ throw new Error('Scope not found');
10
+ return scope.color(scope.name);
11
+ }
@@ -0,0 +1,2 @@
1
+ import { type Config } from '../config.js';
2
+ export declare function time<ScopeName, LogTypeName extends string>(config: Config<ScopeName, LogTypeName>): string;
@@ -0,0 +1,10 @@
1
+ /*
2
+ * Renders the ":time" variable printing current time.
3
+ * Created On 15 January 2024
4
+ */
5
+ import chalk from 'chalk';
6
+ import format from 'date-format';
7
+ import {} from '../config.js';
8
+ export function time(config) {
9
+ return chalk.gray.dim(format.asString(config.timeFormat));
10
+ }
@@ -0,0 +1,2 @@
1
+ import { type LogType } from '../config.js';
2
+ export declare function type<LogTypeName extends string>(type: LogType<LogTypeName>): string;
@@ -0,0 +1,8 @@
1
+ /*
2
+ * Renders the ":type" variable.
3
+ * Created On 15 January 2024
4
+ */
5
+ import {} from '../config.js';
6
+ export function type(type) {
7
+ return type.color(type.name);
8
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "itivrutaha",
3
3
  "description": "( इतिवृत्तः ) — An 👌 easy-to-use, 🛠️ configurable, Bun-first ⚡ console logger.",
4
- "version": "3.2.1",
4
+ "version": "3.2.2",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "main": "dist/index.js",