- Notifications
You must be signed in to change notification settings - Fork39
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Refactor editor#396
Changes fromall commits
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
9139d8f
refactor activate/deactivate
ShMcK91feec8
refactor send actions
ShMcK7f12a0f
refactor onEditorStartup
ShMcKae3ee13
refactor onTutorialConfig
ShMcK15166d1
refactor onTutorialContinueConfig
ShMcK72ef62c
refactor onValidateSetup
ShMcKa9d6561
refactor onRunReset
ShMcK0ec0902
refactor setup/solution actions
ShMcKFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
8 changes: 8 additions & 0 deletionssrc/actions/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export { default as onStartup } from './onStartup' | ||
export { default as onTutorialConfig } from './onTutorialConfig' | ||
export { default as onTutorialContinueConfig } from './onTutorialContinueConfig' | ||
export { default as onValidateSetup } from './onValidateSetup' | ||
export { default as onRunReset } from './onRunReset' | ||
export { default as onErrorPage } from './onErrorPage' | ||
export { default as onTestPass } from './onTestPass' | ||
export { onSetupActions, onSolutionActions } from './onActions' |
6 changes: 3 additions & 3 deletionssrc/actions/setupActions.ts → src/actions/onActions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletionssrc/actions/onErrorPage.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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 |
32 changes: 32 additions & 0 deletionssrc/actions/onRunReset.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import * as T from 'typings' | ||
import * as TT from 'typings/tutorial' | ||
import Context from '../services/context/context' | ||
import { exec } from '../services/node' | ||
import reset from '../services/reset' | ||
import getLastCommitHash from '../services/reset/lastHash' | ||
const onRunReset = async (context: Context) => { | ||
// reset to timeline | ||
const tutorial: TT.Tutorial | null = context.tutorial.get() | ||
const position: T.Position = context.position.get() | ||
// get last pass commit | ||
const hash = getLastCommitHash(position, tutorial?.levels || []) | ||
const branch = tutorial?.config.repo.branch | ||
if (!branch) { | ||
console.error('No repo branch found for tutorial') | ||
return | ||
} | ||
// load timeline until last pass commit | ||
reset({ branch, hash }) | ||
// if tutorial.config.reset.command, run it | ||
if (tutorial?.config?.reset?.command) { | ||
await exec({ command: tutorial.config.reset.command }) | ||
} | ||
} | ||
export default onRunReset |
79 changes: 79 additions & 0 deletionssrc/actions/onStartup.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import * as vscode from 'vscode' | ||
import * as T from 'typings' | ||
import * as TT from 'typings/tutorial' | ||
import * as E from 'typings/error' | ||
import Context from '../services/context/context' | ||
import { WORKSPACE_ROOT, TUTORIAL_URL } from '../environment' | ||
import fetch from 'node-fetch' | ||
import logger from '../services/logger' | ||
const onStartup = async ( | ||
context: Context, | ||
workspaceState: vscode.Memento, | ||
send: (action: T.Action) => Promise<void>, | ||
) => { | ||
try { | ||
// check if a workspace is open, otherwise nothing works | ||
const noActiveWorkspace = !WORKSPACE_ROOT.length | ||
if (noActiveWorkspace) { | ||
const error: E.ErrorMessage = { | ||
type: 'NoWorkspaceFound', | ||
message: '', | ||
actions: [ | ||
{ | ||
label: 'Open Workspace', | ||
transition: 'REQUEST_WORKSPACE', | ||
}, | ||
], | ||
} | ||
send({ type: 'NO_WORKSPACE', payload: { error } }) | ||
return | ||
} | ||
const env = { | ||
machineId: vscode.env.machineId, | ||
sessionId: vscode.env.sessionId, | ||
} | ||
// load tutorial from url | ||
if (TUTORIAL_URL) { | ||
try { | ||
const tutorialRes = await fetch(TUTORIAL_URL) | ||
const tutorial = await tutorialRes.json() | ||
send({ type: 'START_TUTORIAL_FROM_URL', payload: { tutorial } }) | ||
return | ||
} catch (e) { | ||
console.log(`Failed to load tutorial from url ${TUTORIAL_URL} with error "${e.message}"`) | ||
} | ||
} | ||
// continue from tutorial from local storage | ||
const tutorial: TT.Tutorial | null = context.tutorial.get() | ||
// no stored tutorial, must start new tutorial | ||
if (!tutorial || !tutorial.id) { | ||
send({ type: 'START_NEW_TUTORIAL', payload: { env } }) | ||
return | ||
} | ||
// load continued tutorial position & progress | ||
const { position, progress } = await context.setTutorial(workspaceState, tutorial) | ||
logger('CONTINUE STATE', position, progress) | ||
if (progress.complete) { | ||
// tutorial is already complete | ||
send({ type: 'TUTORIAL_ALREADY_COMPLETE', payload: { env } }) | ||
return | ||
} | ||
// communicate to client the tutorial & stepProgress state | ||
send({ type: 'LOAD_STORED_TUTORIAL', payload: { env, tutorial, progress, position } }) | ||
} catch (e) { | ||
const error = { | ||
type: 'UnknownError', | ||
message: `Location: Editor startup\n\n${e.message}`, | ||
} | ||
send({ type: 'EDITOR_STARTUP_FAILED', payload: { error } }) | ||
} | ||
} | ||
export default onStartup |
16 changes: 16 additions & 0 deletionssrc/actions/onTestPass.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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 |
121 changes: 121 additions & 0 deletionssrc/actions/onTutorialConfig.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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 |
29 changes: 29 additions & 0 deletionssrc/actions/onTutorialContinueConfig.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as vscode from 'vscode' | ||
import * as T from 'typings' | ||
import * as TT from 'typings/tutorial' | ||
import Context from '../services/context/context' | ||
import tutorialConfig from './utils/tutorialConfig' | ||
import { COMMANDS } from '../commands' | ||
const onTutorialContinueConfig = async (action: T.Action, context: Context, send: any) => { | ||
try { | ||
const tutorialContinue: TT.Tutorial | null = context.tutorial.get() | ||
if (!tutorialContinue) { | ||
throw new Error('Invalid tutorial to continue') | ||
} | ||
await tutorialConfig({ | ||
data: tutorialContinue, | ||
alreadyConfigured: true, | ||
}) | ||
// update the current stepId on startup | ||
vscode.commands.executeCommand(COMMANDS.SET_CURRENT_POSITION, action.payload.position) | ||
} catch (e) { | ||
const error = { | ||
type: 'UnknownError', | ||
message: `Location: Editor tutorial continue config.\n\n ${e.message}`, | ||
} | ||
send({ type: 'CONTINUE_FAILED', payload: { error } }) | ||
} | ||
} | ||
export default onTutorialContinueConfig |
54 changes: 54 additions & 0 deletionssrc/actions/onValidateSetup.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import*asEfrom'typings/error' | ||
import{version}from'../services/dependencies' | ||
import{checkWorkspaceEmpty}from'../services/workspace' | ||
constonValidateSetup=async(send:any)=>{ | ||
try{ | ||
// check workspace is selected | ||
constisEmptyWorkspace=awaitcheckWorkspaceEmpty() | ||
if(!isEmptyWorkspace){ | ||
consterror:E.ErrorMessage={ | ||
type:'WorkspaceNotEmpty', | ||
message:'', | ||
actions:[ | ||
{ | ||
label:'Open Workspace', | ||
transition:'REQUEST_WORKSPACE', | ||
}, | ||
{ | ||
label:'Check Again', | ||
transition:'RETRY', | ||
}, | ||
], | ||
} | ||
send({type:'VALIDATE_SETUP_FAILED',payload:{ error}}) | ||
return | ||
} | ||
// check Git is installed. | ||
// Should wait for workspace before running otherwise requires access to root folder | ||
constisGitInstalled=awaitversion('git') | ||
if(!isGitInstalled){ | ||
consterror:E.ErrorMessage={ | ||
type:'GitNotFound', | ||
message:'', | ||
actions:[ | ||
{ | ||
label:'Check Again', | ||
transition:'RETRY', | ||
}, | ||
], | ||
} | ||
send({type:'VALIDATE_SETUP_FAILED',payload:{ error}}) | ||
return | ||
} | ||
send({type:'SETUP_VALIDATED'}) | ||
}catch(e){ | ||
consterror={ | ||
type:'UknownError', | ||
message:e.message, | ||
} | ||
send({type:'VALIDATE_SETUP_FAILED',payload:{ error}}) | ||
} | ||
} | ||
exportdefaultonValidateSetup |
7 changes: 0 additions & 7 deletionssrc/actions/saveCommit.ts
This file was deleted.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.