10000 Merge pull request #254 from coderoad/improve-logging · liuderchi/coderoad-vscode@4d7c100 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d7c100

Browse files
authored
Merge pull request coderoad#254 from coderoad/improve-logging
Improve logging
2 parents 1f8d8bd + b1298cc commit 4d7c100

File tree

23 files changed

+85
-61
lines changed

23 files changed

+85
-61
lines changed

src/actions/utils/loadWatchers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as chokidar from 'chokidar'
22
import * as vscode from 'vscode'
33
import { COMMANDS } from '../../editor/commands'
4-
import environment from '../../environment'
4+
import { WORKSPACE_ROOT } from '../../environment'
55

66
// NOTE: vscode createFileWatcher doesn't seem to detect changes outside of vscode
77
// such as `npm install` of a package. Went with chokidar instead
@@ -26,7 +26,7 @@ const loadWatchers = (watchers: string[]) => {
2626
// see how glob patterns are used in VSCode (not like a regex)
2727
// https://code.visualstudio.com/api/references/vscode-api#GlobPattern
2828
const fsWatcher: chokidar.FSWatcher = chokidar.watch(watcher, {
29-
cwd: environment.WORKSPACE_ROOT,
29+
cwd: WORKSPACE_ROOT,
3030
interval: 1000,
3131
})
3232

src/actions/utils/openFiles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const openFiles = async (files: string[]) => {
2020
// ensure the panel is redrawn on the right side first
2121
vscode.commands.executeCommand(COMMANDS.OPEN_WEBVIEW)
2222
} catch (error) {
23-
console.log(`Failed to open file ${filePath}`, error)
23+
console.log(`Failed to open file ${filePath}: ${error.message}`)
2424
}
2525
}
2626
}

src/actions/utils/runCommands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const runCommands = async (commands: string[], send: (action: T.Action) => void)
1515
try {
1616
result = await exec(command)
1717
} catch (error) {
18-
console.log(error)
18+
console.log(`Test failed: ${error.message}`)
1919
send({ type: 'COMMAND_FAIL', payload: { process: { ...process, status: 'FAIL' } } })
2020
return
2121
}

src/channel/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { openWorkspace, checkWorkspaceEmpty } from '../services/workspace'
1414
import { readFile } from 'fs'
1515
import { join } from 'path'
1616
import { promisify } from 'util'
17-
import environment from '../environment'
17+
import { WORKSPACE_ROOT } from '../environment'
1818

1919
const readFileAsync = promisify(readFile)
2020

@@ -45,12 +45,12 @@ class Channel implements Channel {
4545
const actionType: string = typeof action === 'string' ? action : action.type
4646
// const onError = (error: T.ErrorMessage) => this.send({ type: 'ERROR', payload: { error } })
4747

48-
// console.log(`ACTION: ${actionType}`)
48+
logger(`EXT RECEIVED: "${actionType}"`)
4949

5050
switch (actionType) {
5151
case 'EDITOR_STARTUP':
5252
// check if a workspace is open, otherwise nothing works
53-
const noActiveWorksapce = !environment.WORKSPACE_ROOT.length
53+
const noActiveWorksapce = !WORKSPACE_ROOT.length
5454
if (noActiveWorksapce) {
5555
const error: E.ErrorMessage = {
5656
type: 'NoWorkspaceFound',
@@ -260,7 +260,7 @@ class Channel implements Channel {
260260
})
261261

262262
// log error to console for safe keeping
263-
console.log(`ERROR:\n ${errorMarkdown}`)
263+
logger(`ERROR:\n ${errorMarkdown}`)
264264

265265
if (errorMarkdown) {
266266
// add a clearer error message for the user
@@ -270,6 +270,9 @@ class Channel implements Channel {
270270

271271
// action may be an object.type or plain string
272272
const actionType: string = typeof action === 'string' ? action : action.type
273+
274+
logger(`EXT TO CLIENT: "${actionType}"`)
275+
273276
switch (actionType) {
274277
case 'TEST_PASS':
275278
const tutorial = this.context.tutorial.get()

src/environment.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,22 @@ require('dotenv').config({
22
path: './web-app/.env',
33
})
44

5-
import * as vscode from 'vscode'
65
import { getWorkspaceRoot } from './services/workspace'
76

8-
interface Environment {
9-
VERSION: string
10-
NODE_ENV: string
11-
LOG: boolean
12-
API_URL: string
13-
SENTRY_DSN: string | null
14-
WORKSPACE_ROOT: string
15-
}
7+
// CodeRoad version
8+
export const VERSION: string = process.env.npm_package_version || 'unknown'
169

17-
const environment: Environment = {
18-
VERSION: process.env.VERSION || 'unknown',
19-
NODE_ENV: process.env.NODE_ENV || 'production',
20-
LOG: (process.env.LOG || '').toLowerCase() === 'true',
21-
API_URL: process.env.REACT_APP_GQL_URI || '',
22-
SENTRY_DSN: process.env.SENTRY_DSN || null,
23-
WORKSPACE_ROOT: getWorkspaceRoot(),
24-
}
10+
// Node env
11+
export type Env = 'test' | 'local' | 'development' | 'production'
12+
// @ts-ignore
13+
export const NODE_ENV: Env = process.env.NODE_ENV || 'production'
2514

26-
export default environment
15+
// toggle logging in development
16+
export const LOG: boolean =
17+
(process.env.REACT_APP_LOG || '').toLowerCase() === 'true' && process.env.NODE_ENV !== 'production'
18+
19+
// error logging tool
20+
export const SENTRY_DSN: string | null = process.env.SENTRY_DSN || null
21+
22+
// uri path to the users project workspace
23+
export const WORKSPACE_ROOT: string = getWorkspaceRoot()

src/services/logger/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import enviroment from '../../environment'
1+
import { LOG } from '../../environment'
22

33
const logger = (message: string | string[]) => {
4-
if (!enviroment.LOG) {
4+
if (!LOG) {
55
return
66
}
77
if (Array.isArray(message)) {

src/services/node/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import { exec as cpExec } from 'child_process'
22
import * as fs from 'fs'
33
import { join } from 'path'
44
import { promisify } from 'util'
5-
import environment from '../../environment'
5+
import { WORKSPACE_ROOT } from '../../environment'
66

77
const asyncExec = promisify(cpExec)
88

99
export const exec = (cmd: string): Promise<{ stdout: string; stderr: string }> | never => {
1010
return asyncExec(cmd, {
11-
cwd: environment.WORKSPACE_ROOT,
11+
cwd: WORKSPACE_ROOT,
1212
})
1313
}
1414

1515
export const exists = (...paths: string[]): boolean | never => {
16-
return fs.existsSync(join(environment.WORKSPACE_ROOT, ...paths))
16+
return fs.existsSync(join(WORKSPACE_ROOT, ...paths))
1717
}

src/services/sentry/init.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { init } from '@sentry/node'
2-
import environment from '../../environment'
2+
import { SENTRY_DSN, NODE_ENV } from '../../environment'
33

4-
if (environment.SENTRY_DSN) {
4+
if (SENTRY_DSN) {
55
init({
6-
dsn: environment.SENTRY_DSN,
7-
environment: environment.NODE_ENV,
6+
dsn: SENTRY_DSN,
7+
environment: NODE_ENV,
88
})
99
}

src/services/sentry/onError.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as sentry from '@sentry/node'
22
// import { Scope } from '@sentry/hub'
3-
import environment from '../../environment'
3+
import { VERSION } from '../../environment'
44

55
const onError = (error: Error) => {
66
// set user scope https://docs.sentry.io/enriching-error-data/scopes/?platform=node
77
sentry.withScope((scope: any) => {
8-
scope.setTag('VERSION', environment.VERSION)
8+
scope.setTag('VERSION', VERSION)
99
// if (user) {
1010
// scope.setUser({
1111
// id: user.id,

src/services/workspace/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode'
22
import * as fs from 'fs'
33
import { promisify } from 'util'
4-
import environment from '../../environment'
4+
import { WORKSPACE_ROOT } from '../../environment'
55

66
const readDir = promisify(fs.readdir)
77

@@ -13,7 +13,7 @@ export const openWorkspace = () => {
1313
export const checkWorkspaceEmpty = async () => {
1414
let files
1515
try {
16-
files = await readDir(environment.WORKSPACE_ROOT)
16+
files = await readDir(WORKSPACE_ROOT)
1717
} catch (error) {
1818
throw new Error('Failed to check workspace')
1919
}

0 306A commit comments

Comments
 (0)
0