Skip to content

Refactor editor #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor send actions
Signed-off-by: shmck <[email protected]>
  • Loading branch information
ShMcK committed Jul 20, 2020
commit 91feec8e8a9301039f2bef2ebf87fce6ffa6b33e
2 changes: 2 additions & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as onErrorPage } from './onErrorPage'
export { default as onTestPass } from './onTestPass'
26 changes: 26 additions & 0 deletions src/actions/onErrorPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as T from 'typings'
import { readFile } from '../services/node'
import logger from '../services/logger'

const onErrorPage = async (action: T.Action) => {
// Error middleware
if (action?.payload?.error?.type) {
// load error markdown message
const error = action.payload.error
const errorMarkdown = await readFile(__dirname, '..', '..', 'errors', `${action.payload.error.type}.md`).catch(
() => {
// onError(new Error(`Error Markdown file not found for ${action.type}`))
},
)

// log error to console for safe keeping
logger(`ERROR:\n ${errorMarkdown}`)

if (errorMarkdown) {
// add a clearer error message for the user
error.message = `${errorMarkdown}\n\n${error.message}`
}
}
}

export default onErrorPage
16 changes: 16 additions & 0 deletions src/actions/onTestPass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as git from '../services/git'
import * as T from 'typings'
import Context from '../services/context/context'

const onTestPass = (action: T.Action, context: Context) => {
const tutorial = context.tutorial.get()
if (!tutorial) {
throw new Error('Error with current tutorial. Tutorial may be missing an id.')
}
// update local storage stepProgress
const progress = context.progress.setStepComplete(tutorial, action.payload.position.stepId)
context.position.setPositionFromProgress(tutorial, progress)
git.saveCommit('Save progress')
}

export default onTestPass
7 changes: 0 additions & 7 deletions src/actions/saveCommit.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/actions/tutorialConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as E from 'typings/error'
import * as TT from 'typings/tutorial'
import * as vscode from 'vscode'
import { COMMANDS } from '../editor/commands'
import { COMMANDS } from '../commands'
import * as git from '../services/git'
import { DISABLE_RUN_ON_SAVE } from '../environment'

Expand Down
2 changes: 1 addition & 1 deletion src/actions/utils/loadWatchers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as chokidar from 'chokidar'
import * as vscode from 'vscode'
import { COMMANDS } from '../../editor/commands'
import { COMMANDS } from '../../commands'
import { WORKSPACE_ROOT } from '../../environment'

// NOTE: vscode createFileWatcher doesn't seem to detect changes outside of vscode
Expand Down
2 changes: 1 addition & 1 deletion src/actions/utils/openFiles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from 'path'
import * as vscode from 'vscode'
import { COMMANDS } from '../../editor/commands'
import { COMMANDS } from '../../commands'

const openFiles = async (files: string[]) => {
if (!files.length) {
Expand Down
62 changes: 17 additions & 45 deletions src/channel/index.ts → src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,20 @@ import * as E from 'typings/error'
import * as vscode from 'vscode'
import fetch from 'node-fetch'
import { satisfies } from 'semver'
import saveCommit from '../actions/saveCommit'
import { setupActions, solutionActions } from '../actions/setupActions'
import tutorialConfig from '../actions/tutorialConfig'
import { COMMANDS } from '../editor/commands'
import Context from './context'
import { readFile } from 'fs'
import { join } from 'path'
import { promisify } from 'util'
import logger from '../services/logger'
import { version, compareVersions } from '../services/dependencies'
import { openWorkspace, checkWorkspaceEmpty } from '../services/workspace'
import { showOutput } from '../services/testRunner/output'
import { exec } from '../services/node'
import { WORKSPACE_ROOT, TUTORIAL_URL } from '../environment'
import reset from '../services/reset'
import getLastCommitHash from '../services/reset/lastHash'
import { onEvent } from '../services/telemetry'

const readFileAsync = promisify(readFile)
import { setupActions, solutionActions } from './actions/setupActions'
import tutorialConfig from './actions/tutorialConfig'
import { COMMANDS } from './commands'
import Context from './services/context/context'
import logger from './services/logger'
import { version, compareVersions } from './services/dependencies'
import { openWorkspace, checkWorkspaceEmpty } from './services/workspace'
import { showOutput } from './services/testRunner/output'
import { exec } from './services/node'
import { WORKSPACE_ROOT, TUTORIAL_URL } from './environment'
import reset from './services/reset'
import getLastCommitHash from './services/reset/lastHash'
import { onEvent } from './services/telemetry'
import * as actions from './actions'

interface Channel {
receive(action: T.Action): Promise<void>
Expand Down Expand Up @@ -359,39 +354,16 @@ class Channel implements Channel {
}
// send to webview
public send = async (action: T.Action): Promise<void> => {
// Error middleware
if (action?.payload?.error?.type) {
// load error markdown message
const error = action.payload.error
const errorMarkdownFile = join(__dirname, '..', '..', 'errors', `${action.payload.error.type}.md`)
const errorMarkdown = await readFileAsync(errorMarkdownFile).catch(() => {
// onError(new Error(`Error Markdown file not found for ${action.type}`))
})

// log error to console for safe keeping
logger(`ERROR:\n ${errorMarkdown}`)

if (errorMarkdown) {
// add a clearer error message for the user
error.message = `${errorMarkdown}\n\n${error.message}`
}
}

// load error page if error action is triggered
actions.onErrorPage(action)
// action may be an object.type or plain string
const actionType: string = typeof action === 'string' ? action : action.type

logger(`EXT TO CLIENT: "${actionType}"`)

switch (actionType) {
case 'TEST_PASS':
const tutorial = this.context.tutorial.get()
if (!tutorial) {
throw new Error('Error with current tutorial. Tutorial may be missing an id.')
}
// update local storage stepProgress
const progress = this.context.progress.setStepComplete(tutorial, action.payload.position.stepId)
this.context.position.setPositionFromProgress(tutorial, progress)
saveCommit()
actions.onTestPass(action, this.context)
}

// send message
Expand Down
8 changes: 4 additions & 4 deletions src/editor/commands.ts → src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as T from 'typings'
import * as TT from 'typings/tutorial'
import * as vscode from 'vscode'
import createTestRunner from '../services/testRunner'
import { setupActions } from '../actions/setupActions'
import createWebView from '../services/webview'
import logger from '../services/logger'
import createTestRunner from './services/testRunner'
import { setupActions } from './actions/setupActions'
import createWebView from './services/webview'
import logger from './services/logger'

export const COMMANDS = {
START: 'coderoad.start',
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode'
import { createCommands } from './editor/commands'
import { createCommands } from './commands'
import * as telemetry from './services/telemetry'

let onDeactivate = () => {}
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as T from 'typings'
import * as TT from 'typings/tutorial'
import * as vscode from 'vscode'
import Storage from '../../services/storage'
import Storage from '../../storage'

const defaultValue: T.Progress = {
levels: {},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as TT from 'typings/tutorial'
import * as vscode from 'vscode'
import Storage from '../../services/storage'
import Storage from '../../storage'

// Tutorial
class Tutorial {
Expand Down
5 changes: 5 additions & 0 deletions src/services/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { WORKSPACE_ROOT } from '../../environment'

const asyncExec = promisify(cpExec)
const asyncRemoveFile = promisify(fs.unlink)
const asyncReadFile = promisify(fs.readFile)

interface ExecParams {
command: string
Expand All @@ -24,3 +25,7 @@ export const exists = (...paths: string[]): boolean | never => {
export const removeFile = (...paths: string[]) => {
return asyncRemoveFile(join(WORKSPACE_ROOT, ...paths))
}

export const readFile = (...paths: string[]) => {
return asyncReadFile(join(...paths))
}