|
1 | | -import { |
2 | | - createDebug as _createDebug, |
3 | | - enable as _enable, |
4 | | - disable, |
5 | | - enabled, |
6 | | - namespaces, |
7 | | -} from './core.ts' |
8 | | -import { humanize, selectColor } from './utils.ts' |
| 1 | +import { createDebug as createPlainDebug } from './plain.ts' |
| 2 | +import { selectColor } from './utils.ts' |
9 | 3 | import type { Debugger, DebugOptions } from './types.ts' |
10 | 4 |
|
11 | 5 | const colors: string[] = [ |
@@ -131,89 +125,16 @@ function formatArgs( |
131 | 125 | args.splice(lastC, 0, c) |
132 | 126 | } |
133 | 127 |
|
134 | | -/** |
135 | | - * Invokes `console.debug()` when available. |
136 | | - * No-op when `console.debug` is not a "function". |
137 | | - * If `console.debug` is not available, falls back |
138 | | - * to `console.log`. |
139 | | - */ |
140 | | -const log = console.debug || console.log || (() => {}) |
141 | | - |
142 | | -const defaultOptions: Omit<Required<DebugOptions>, 'color'> = { |
143 | | - useColors: true, |
144 | | - |
145 | | - formatArgs, |
146 | | - formatters: { |
147 | | - /** |
148 | | - * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. |
149 | | - */ |
150 | | - j(v) { |
151 | | - try { |
152 | | - return JSON.stringify(v) |
153 | | - } catch (error: any) { |
154 | | - return `[UnexpectedJSONParseError]: ${error.message}` |
155 | | - } |
156 | | - }, |
157 | | - }, |
158 | | - inspectOpts: {}, |
159 | | - humanize, |
160 | | - |
161 | | - log, |
162 | | -} |
163 | | - |
164 | 128 | export function createDebug( |
165 | 129 | namespace: string, |
166 | 130 | options?: DebugOptions, |
167 | 131 | ): Debugger { |
168 | 132 | const color = (options && options.color) ?? selectColor(colors, namespace) |
169 | | - return _createDebug( |
| 133 | + return createPlainDebug( |
170 | 134 | namespace, |
171 | | - Object.assign(defaultOptions, { color }, options), |
| 135 | + Object.assign({ useColors: true, color, formatArgs }, options), |
172 | 136 | ) |
173 | 137 | } |
174 | 138 |
|
175 | | -function load(): string { |
176 | | - let r: string | null | undefined |
177 | | - try { |
178 | | - // TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context. |
179 | | - r = localStorage.getItem('debug') || localStorage.getItem('DEBUG') |
180 | | - } catch { |
181 | | - // Swallow |
182 | | - // XXX (@Qix-) should we be logging these? |
183 | | - } |
184 | | - |
185 | | - // If debug isn't set in LS, and we're in Electron, try to load $DEBUG |
186 | | - if (!r && typeof process !== 'undefined' && 'env' in process) { |
187 | | - r = process.env.DEBUG |
188 | | - } |
189 | | - |
190 | | - return r || '' |
191 | | -} |
192 | | - |
193 | | -function save(namespaces: string) { |
194 | | - try { |
195 | | - if (namespaces) { |
196 | | - localStorage.setItem('debug', namespaces) |
197 | | - } else { |
198 | | - localStorage.removeItem('debug') |
199 | | - } |
200 | | - } catch { |
201 | | - // Swallow |
202 | | - // XXX (@Qix-) should we be logging these? |
203 | | - } |
204 | | -} |
205 | | - |
206 | | -/** |
207 | | - * Enables a debug mode by namespaces. This can include modes |
208 | | - * separated by a colon and wildcards. |
209 | | - */ |
210 | | -function enable(namespaces: string): void { |
211 | | - save(namespaces) |
212 | | - _enable(namespaces) |
213 | | -} |
214 | | - |
215 | | -// eslint-disable-next-line unicorn/no-top-level-side-effects |
216 | | -_enable(load()) |
217 | | - |
| 139 | +export { disable, enable, enabled, namespaces } from './plain.ts' |
218 | 140 | export type * from './types.ts' |
219 | | -export { disable, enable, enabled, namespaces } |
0 commit comments