import { IS_SERVER, isomorphicGlobal } from '../check-environment'; import DI, { DDNames } from '../DI'; import { GlobalVariable } from '../pattern/global-page-data'; export interface DebugConfig { isDebugMode: boolean; } declare const process: any; declare const location: any; declare const window: any; const output = (IS_SERVER? console.error : console.info).bind(console); function printInfo(why: string) { output('debug mode is %s[%s]: %s', currentDebug? 'ON' : 'OFF', typeof currentDebug, why); } let currentDebug; export function isDebugMode() { if (currentDebug === undefined) { currentDebug = DI.get(DDNames.isDebugMode, null); if (typeof currentDebug === 'boolean') { // nothing to do } else if (IS_SERVER) { currentDebug = process.env.NODE_ENV !== 'production'; printInfo('process.env.NODE_ENV(' + process.env.NODE_ENV + ') !== production'); } else { currentDebug = GlobalVariable.get(isomorphicGlobal, 'isDebug'); if (typeof currentDebug !== 'boolean') { if (/\bDEBUG\b/.test(location.href)) { currentDebug = true; printInfo('location.href with DEBUG'); } else { currentDebug = window['isDebug'] || false; printInfo('window.isDebug'); } } else { printInfo('GlobalVariable::isDebug'); } } currentDebug = !!(currentDebug as any); DI.overwrite(DDNames.isDebugMode, currentDebug); } return DI.get(DDNames.isDebugMode); } export function setDebugMode(status: boolean) { console.log('this is%s debug mode', status? '' : ' not'); return DI.set(DDNames.isDebugMode, !!(status as any)); }