diff --git a/src/channel/index.ts b/src/channel/index.ts index db69db47..8284b269 100644 --- a/src/channel/index.ts +++ b/src/channel/index.ts @@ -6,6 +6,7 @@ import setupActions from '../actions/setupActions' import solutionActions from '../actions/solutionActions' import tutorialConfig from '../actions/tutorialConfig' import { COMMANDS } from '../editor/commands' +import logger from '../services/logger' import Context from './context' interface Channel { @@ -120,7 +121,7 @@ class Channel implements Channel { return default: - console.log(`No match for action type: ${actionType}`) + logger(`No match for action type: ${actionType}`) return } } diff --git a/src/environment.ts b/src/environment.ts new file mode 100644 index 00000000..8e9c75d0 --- /dev/null +++ b/src/environment.ts @@ -0,0 +1,13 @@ +require('dotenv').config({ + path: './.env', +}) + +interface Environment { + LOG: boolean +} + +const environment: Environment = { + LOG: (process.env.LOG || '').toLowerCase() === 'test', +} + +export default environment diff --git a/src/services/git/index.ts b/src/services/git/index.ts index 971678bc..4f58ac5d 100644 --- a/src/services/git/index.ts +++ b/src/services/git/index.ts @@ -1,4 +1,5 @@ import node from '../node' +import logger from '../logger' const gitOrigin = 'coderoad' @@ -48,7 +49,7 @@ export async function saveCommit(message: string): Promise { console.error(stderr) throw new Error('Error saving progress to Git') } - console.log('save with commit & continue stdout', stdout) + logger(['save with commit & continue stdout', stdout]) } export async function clear(): Promise { diff --git a/src/services/logger/index.ts b/src/services/logger/index.ts new file mode 100644 index 00000000..42310b89 --- /dev/null +++ b/src/services/logger/index.ts @@ -0,0 +1,14 @@ +import enviroment from '../../environment' + +const logger = (message: string | string[]) => { + if (!enviroment.LOG) { + return + } + if (Array.isArray(message)) { + message.forEach(console.log) + } else { + console.log(message) + } +} + +export default logger diff --git a/src/services/testRunner/index.ts b/src/services/testRunner/index.ts index 0f75dc86..20b507a0 100644 --- a/src/services/testRunner/index.ts +++ b/src/services/testRunner/index.ts @@ -1,5 +1,6 @@ import { getOutputChannel } from '../../editor/outputChannel' import node from '../../services/node' +import logger from '../../services/logger' import parser from './parser' import { debounce, throttle } from './throttle' @@ -27,7 +28,8 @@ const createTestRunner = (config: TestRunnerConfig, callbacks: Callbacks) => { if (!startTime) { return } - console.log('------------------- RUN TEST -------------------') + + logger('------------------- RUN TEST -------------------') // flag as running callbacks.onRun(payload) @@ -44,7 +46,8 @@ const createTestRunner = (config: TestRunnerConfig, callbacks: Callbacks) => { if (!debounce(startTime)) { return } - console.log('----------------- PROCESS TEST -----------------') + + logger('----------------- PROCESS TEST -----------------') const { stdout, stderr } = result