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 onTutorialConfig
Signed-off-by: shmck <[email protected]>
  • Loading branch information
ShMcK committed Jul 20, 2020
commit ae3ee13736743e18c0651cc3e75ba2192ed2096e
1 change: 1 addition & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as onStartup } from './onStartup'
export { default as onTutorialConfig } from './onTutorialConfig'
export { default as onErrorPage } from './onErrorPage'
export { default as onTestPass } from './onTestPass'
121 changes: 121 additions & 0 deletions src/actions/onTutorialConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import * as vscode from 'vscode'
import * as T from 'typings'
import * as TT from 'typings/tutorial'
import * as E from 'typings/error'
import { satisfies } from 'semver'
import { onEvent } from '../services/telemetry'
import { version, compareVersions } from '../services/dependencies'
import Context from '../services/context/context'
import tutorialConfig from './utils/tutorialConfig'

const onTutorialConfig = async (action: T.Action, context: Context, workspaceState: vscode.Memento, send: any) => {
try {
const data: TT.Tutorial = action.payload.tutorial

onEvent('tutorial_start', {
tutorial_id: data.id,
tutorial_version: data.version,
tutorial_title: data.summary.title,
})

// validate extension version
const expectedAppVersion = data.config?.appVersions?.vscode
if (expectedAppVersion) {
const extension = vscode.extensions.getExtension('coderoad.coderoad')
if (extension) {
const currentAppVersion = extension.packageJSON.version
const satisfied = satisfies(currentAppVersion, expectedAppVersion)
if (!satisfied) {
const error: E.ErrorMessage = {
type: 'UnmetExtensionVersion',
message: `Expected CodeRoad v${expectedAppVersion}, but found v${currentAppVersion}`,
}
send({ type: 'TUTORIAL_CONFIGURE_FAIL', payload: { error } })
return
}
}
}

// setup tutorial config (save watcher, test runner, etc)
await context.setTutorial(workspaceState, data)

// validate dependencies
const dependencies = data.config.dependencies
if (dependencies && dependencies.length) {
for (const dep of dependencies) {
// check dependency is installed
const currentVersion: string | null = await version(dep.name)
if (!currentVersion) {
// use a custom error message
const error: E.ErrorMessage = {
type: 'MissingTutorialDependency',
message: dep.message || `Process "${dep.name}" is required but not found. It may need to be installed`,
actions: [
{
label: 'Check Again',
transition: 'TRY_AGAIN',
},
],
}
send({ type: 'TUTORIAL_CONFIGURE_FAIL', payload: { error } })
return
}

// check dependency version
const satisfiedDependency = await compareVersions(currentVersion, dep.version)

if (!satisfiedDependency) {
const error: E.ErrorMessage = {
type: 'UnmetTutorialDependency',
message: `Expected ${dep.name} to have version ${dep.version}, but found version ${currentVersion}`,
actions: [
{
label: 'Check Again',
transition: 'TRY_AGAIN',
},
],
}
send({ type: 'TUTORIAL_CONFIGURE_FAIL', payload: { error } })
return
}

if (satisfiedDependency !== true) {
const error: E.ErrorMessage = satisfiedDependency || {
type: 'UnknownError',
message: `Something went wrong comparing dependency for ${name}`,
actions: [
{
label: 'Try Again',
transition: 'TRY_AGAIN',
},
],
}
send({ type: 'TUTORIAL_CONFIGURE_FAIL', payload: { error } })
return
}
}
}

const error: E.ErrorMessage | void = await tutorialConfig({ data }).catch((error: Error) => ({
type: 'UnknownError',
message: `Location: tutorial config.\n\n${error.message}`,
}))

// has error
if (error && error.type) {
send({ type: 'TUTORIAL_CONFIGURE_FAIL', payload: { error } })
return
}

// report back to the webview that setup is complete
send({ type: 'TUTORIAL_CONFIGURED' })
} catch (e) {
const error = {
type: 'UnknownError',
message: `Location: EditorTutorialConfig.\n\n ${e.message}`,
}
send({ type: 'TUTORIAL_CONFIGURE_FAIL', payload: { error } })
}
}

export default onTutorialConfig
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as E from 'typings/error'
import * as TT from 'typings/tutorial'
import * as vscode from 'vscode'
import { COMMANDS } from '../commands'
import * as git from '../services/git'
import { DISABLE_RUN_ON_SAVE } from '../environment'
import { COMMANDS } from '../../commands'
import * as git from '../../services/git'
import { DISABLE_RUN_ON_SAVE } from '../../environment'

interface TutorialConfigParams {
data: TT.Tutorial
Expand Down
116 changes: 4 additions & 112 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ import * as T from 'typings'
import * as TT from 'typings/tutorial'
import * as E from 'typings/error'
import * as vscode from 'vscode'
import { satisfies } from 'semver'
import { setupActions, solutionActions } from './actions/setupActions'
import tutorialConfig from './actions/tutorialConfig'
import tutorialConfig from './actions/utils/tutorialConfig'
import { COMMANDS } from './commands'
import Context from './services/context/context'
import logger from './services/logger'
import { version, compareVersions } from './services/dependencies'
import { version } from './services/dependencies'
import { openWorkspace, checkWorkspaceEmpty } from './services/workspace'
import { showOutput } from './services/testRunner/output'
import { exec } from './services/node'
import reset from './services/reset'
import getLastCommitHash from './services/reset/lastHash'
import { onEvent } from './services/telemetry'

import * as actions from './actions'

interface Channel {
Expand Down Expand Up @@ -58,114 +57,7 @@ class Channel implements Channel {
return
// configure test runner, language, git
case 'EDITOR_TUTORIAL_CONFIG':
try {
const data: TT.Tutorial = action.payload.tutorial

onEvent('tutorial_start', {
tutorial_id: data.id,
tutorial_version: data.version,
tutorial_title: data.summary.title,
})

// validate extension version
const expectedAppVersion = data.config?.appVersions?.vscode
if (expectedAppVersion) {
const extension = vscode.extensions.getExtension('coderoad.coderoad')
if (extension) {
const currentAppVersion = extension.packageJSON.version
const satisfied = satisfies(currentAppVersion, expectedAppVersion)
if (!satisfied) {
const error: E.ErrorMessage = {
type: 'UnmetExtensionVersion',
message: `Expected CodeRoad v${expectedAppVersion}, but found v${currentAppVersion}`,
}
this.send({ type: 'TUTORIAL_CONFIGURE_FAIL', payload: { error } })
return
}
}
}

// setup tutorial config (save watcher, test runner, etc)
await this.context.setTutorial(this.workspaceState, data)

// validate dependencies
const dependencies = data.config.dependencies
if (dependencies && dependencies.length) {
for (const dep of dependencies) {
// check dependency is installed
const currentVersion: string | null = await version(dep.name)
if (!currentVersion) {
// use a custom error message
const error: E.ErrorMessage = {
type: 'MissingTutorialDependency',
message:
dep.message || `Process "${dep.name}" is required but not found. It may need to be installed`,
actions: [
{
label: 'Check Again',
transition: 'TRY_AGAIN',
},
],
}
this.send({ type: 'TUTORIAL_CONFIGURE_FAIL', payload: { error } })
return
}

// check dependency version
const satisfiedDependency = await compareVersions(currentVersion, dep.version)

if (!satisfiedDependency) {
const error: E.ErrorMessage = {
type: 'UnmetTutorialDependency',
message: `Expected ${dep.name} to have version ${dep.version}, but found version ${currentVersion}`,
actions: [
{
label: 'Check Again',
transition: 'TRY_AGAIN',
},
],
}
this.send({ type: 'TUTORIAL_CONFIGURE_FAIL', payload: { error } })
return
}

if (satisfiedDependency !== true) {
const error: E.ErrorMessage = satisfiedDependency || {
type: 'UnknownError',
message: `Something went wrong comparing dependency for ${name}`,
actions: [
{
label: 'Try Again',
transition: 'TRY_AGAIN',
},
],
}
this.send({ type: 'TUTORIAL_CONFIGURE_FAIL', payload: { error } })
return
}
}
}

const error: E.ErrorMessage | void = await tutorialConfig({ data }).catch((error: Error) => ({
type: 'UnknownError',
message: `Location: tutorial config.\n\n${error.message}`,
}))

// has error
if (error && error.type) {
this.send({ type: 'TUTORIAL_CONFIGURE_FAIL', payload: { error } })
return
}

// report back to the webview that setup is complete
this.send({ type: 'TUTORIAL_CONFIGURED' })
} catch (e) {
const error = {
type: 'UnknownError',
message: `Location: EditorTutorialConfig.\n\n ${e.message}`,
}
this.send({ type: 'TUTORIAL_CONFIGURE_FAIL', payload: { error } })
}
actions.onTutorialConfig(action, this.context, this.workspaceState, this.send)
return
case 'EDITOR_TUTORIAL_CONTINUE_CONFIG':
try {
Expand Down