feat: initial commit
This commit is contained in:
61
packages/bolt/app/utils/classNames.ts
Normal file
61
packages/bolt/app/utils/classNames.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Copyright (c) 2018 Jed Watson.
|
||||
* Licensed under the MIT License (MIT), see:
|
||||
*
|
||||
* @link http://jedwatson.github.io/classnames
|
||||
*/
|
||||
|
||||
type ClassNamesArg = undefined | string | Record<string, boolean> | ClassNamesArg[];
|
||||
|
||||
/**
|
||||
* A simple JavaScript utility for conditionally joining classNames together.
|
||||
*
|
||||
* @param args A series of classes or object with key that are class and values
|
||||
* that are interpreted as boolean to decide whether or not the class
|
||||
* should be included in the final class.
|
||||
*/
|
||||
export function classNames(...args: ClassNamesArg[]): string {
|
||||
let classes = '';
|
||||
|
||||
for (const arg of args) {
|
||||
classes = appendClass(classes, parseValue(arg));
|
||||
}
|
||||
|
||||
return classes;
|
||||
}
|
||||
|
||||
function parseValue(arg: ClassNamesArg) {
|
||||
if (typeof arg === 'string' || typeof arg === 'number') {
|
||||
return arg;
|
||||
}
|
||||
|
||||
if (typeof arg !== 'object') {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (Array.isArray(arg)) {
|
||||
return classNames(...arg);
|
||||
}
|
||||
|
||||
let classes = '';
|
||||
|
||||
for (const key in arg) {
|
||||
if (arg[key]) {
|
||||
classes = appendClass(classes, key);
|
||||
}
|
||||
}
|
||||
|
||||
return classes;
|
||||
}
|
||||
|
||||
function appendClass(value: string, newClass: string | undefined) {
|
||||
if (!newClass) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (value) {
|
||||
return value + ' ' + newClass;
|
||||
}
|
||||
|
||||
return value + newClass;
|
||||
}
|
||||
87
packages/bolt/app/utils/logger.ts
Normal file
87
packages/bolt/app/utils/logger.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
export type DebugLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error';
|
||||
|
||||
type LoggerFunction = (...messages: any[]) => void;
|
||||
|
||||
interface Logger {
|
||||
trace: LoggerFunction;
|
||||
debug: LoggerFunction;
|
||||
info: LoggerFunction;
|
||||
warn: LoggerFunction;
|
||||
error: LoggerFunction;
|
||||
setLevel: (level: DebugLevel) => void;
|
||||
}
|
||||
|
||||
let currentLevel: DebugLevel = import.meta.env.VITE_LOG_LEVEL ?? 'warn';
|
||||
|
||||
export const logger: Logger = {
|
||||
trace: (...messages: any[]) => log('trace', undefined, messages),
|
||||
debug: (...messages: any[]) => log('debug', undefined, messages),
|
||||
info: (...messages: any[]) => log('info', undefined, messages),
|
||||
warn: (...messages: any[]) => log('warn', undefined, messages),
|
||||
error: (...messages: any[]) => log('error', undefined, messages),
|
||||
setLevel,
|
||||
};
|
||||
|
||||
export function createScopedLogger(scope: string): Logger {
|
||||
return {
|
||||
trace: (...messages: any[]) => log('trace', scope, messages),
|
||||
debug: (...messages: any[]) => log('debug', scope, messages),
|
||||
info: (...messages: any[]) => log('info', scope, messages),
|
||||
warn: (...messages: any[]) => log('warn', scope, messages),
|
||||
error: (...messages: any[]) => log('error', scope, messages),
|
||||
setLevel,
|
||||
};
|
||||
}
|
||||
|
||||
function setLevel(level: DebugLevel) {
|
||||
if ((level === 'trace' || level === 'debug') && import.meta.env.PROD) {
|
||||
return;
|
||||
}
|
||||
|
||||
currentLevel = level;
|
||||
}
|
||||
|
||||
function log(level: DebugLevel, scope: string | undefined, messages: any[]) {
|
||||
const levelOrder: DebugLevel[] = ['trace', 'debug', 'info', 'warn', 'error'];
|
||||
|
||||
if (levelOrder.indexOf(level) >= levelOrder.indexOf(currentLevel)) {
|
||||
const labelBackgroundColor = getColorForLevel(level);
|
||||
const labelTextColor = level === 'warn' ? 'black' : 'white';
|
||||
|
||||
const labelStyles = getLabelStyles(labelBackgroundColor, labelTextColor);
|
||||
const scopeStyles = getLabelStyles('#77828D', 'white');
|
||||
|
||||
const styles = [labelStyles];
|
||||
|
||||
if (typeof scope === 'string') {
|
||||
styles.push('', scopeStyles);
|
||||
}
|
||||
|
||||
console.log(`%c${level.toUpperCase()}${scope ? `%c %c${scope}` : ''}`, ...styles, ...messages);
|
||||
}
|
||||
}
|
||||
|
||||
function getLabelStyles(color: string, textColor: string) {
|
||||
return `background-color: ${color}; color: white; border: 4px solid ${color}; color: ${textColor};`;
|
||||
}
|
||||
|
||||
function getColorForLevel(level: DebugLevel): string {
|
||||
switch (level) {
|
||||
case 'trace':
|
||||
case 'debug': {
|
||||
return '#77828D';
|
||||
}
|
||||
case 'info': {
|
||||
return '#1389FD';
|
||||
}
|
||||
case 'warn': {
|
||||
return '#FFDB6C';
|
||||
}
|
||||
case 'error': {
|
||||
return '#EE4744';
|
||||
}
|
||||
default: {
|
||||
return 'black';
|
||||
}
|
||||
}
|
||||
}
|
||||
6
packages/bolt/app/utils/markdown.ts
Normal file
6
packages/bolt/app/utils/markdown.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import rehypeRaw from 'rehype-raw';
|
||||
import remarkGfm from 'remark-gfm';
|
||||
import type { PluggableList } from 'unified';
|
||||
|
||||
export const remarkPlugins = [remarkGfm] satisfies PluggableList;
|
||||
export const rehypePlugins = [rehypeRaw] satisfies PluggableList;
|
||||
19
packages/bolt/app/utils/promises.ts
Normal file
19
packages/bolt/app/utils/promises.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
export function withResolvers<T>(): PromiseWithResolvers<T> {
|
||||
if (typeof Promise.withResolvers === 'function') {
|
||||
return Promise.withResolvers();
|
||||
}
|
||||
|
||||
let resolve!: (value: T | PromiseLike<T>) => void;
|
||||
let reject!: (reason?: any) => void;
|
||||
|
||||
const promise = new Promise<T>((_resolve, _reject) => {
|
||||
resolve = _resolve;
|
||||
reject = _reject;
|
||||
});
|
||||
|
||||
return {
|
||||
resolve,
|
||||
reject,
|
||||
promise,
|
||||
};
|
||||
}
|
||||
23
packages/bolt/app/utils/stripIndent.ts
Normal file
23
packages/bolt/app/utils/stripIndent.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
export function stripIndents(value: string): string;
|
||||
export function stripIndents(strings: TemplateStringsArray, ...values: any[]): string;
|
||||
export function stripIndents(arg0: string | TemplateStringsArray, ...values: any[]) {
|
||||
if (typeof arg0 !== 'string') {
|
||||
const processedString = arg0.reduce((acc, curr, i) => {
|
||||
acc += curr + (values[i] ?? '');
|
||||
return acc;
|
||||
}, '');
|
||||
|
||||
return _stripIndents(processedString);
|
||||
}
|
||||
|
||||
return _stripIndents(arg0);
|
||||
}
|
||||
|
||||
function _stripIndents(value: string) {
|
||||
return value
|
||||
.split('\n')
|
||||
.map((line) => line.trim())
|
||||
.join('\n')
|
||||
.trimStart()
|
||||
.replace(/[\r\n]$/, '');
|
||||
}
|
||||
Reference in New Issue
Block a user