Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb41824e

Browse files
authored
Merge pull request#417 from coderoad/feature/hooks
Feature/hooks
2 parents50b2719 +31ae47f commitb41824e

File tree

15 files changed

+126
-103
lines changed

15 files changed

+126
-103
lines changed

‎src/actions/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ export { default as onValidateSetup } from './onValidateSetup'
55
export{defaultasonRunReset}from'./onRunReset'
66
export{defaultasonErrorPage}from'./onErrorPage'
77
export{onRunTest,onTestPass}from'./onTest'
8-
export{onSetupActions,onSolutionActions}from'./onActions'
98
export{onOpenLogs}from'./onOpenLogs'

‎src/actions/onActions.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

‎src/channel.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Context from './services/context/context'
55
importloggerfrom'./services/logger'
66
import{openWorkspace}from'./services/workspace'
77
import*asactionsfrom'./actions'
8+
import*ashooksfrom'./services/hooks'
89

910
interfaceChannel{
1011
receive(action:T.Action):Promise<void>
@@ -56,14 +57,12 @@ class Channel implements Channel {
5657
// load step actions (git commits, commands, open files)
5758
case'SETUP_ACTIONS':
5859
awaitvscode.commands.executeCommand(COMMANDS.SET_CURRENT_POSITION,action.payload.position)
59-
actions.onSetupActions({actions:action.payload.actions,send:this.send})
60+
hooks.onSetupEnter(action.payload.actions)
6061
return
6162
// load solution step actions (git commits, commands, open files)
6263
case'SOLUTION_ACTIONS':
6364
awaitvscode.commands.executeCommand(COMMANDS.SET_CURRENT_POSITION,action.payload.position)
64-
awaitactions.onSolutionActions({actions:action.payload.actions,send:this.send})
65-
// run test following solution to update position
66-
actions.onRunTest()
65+
hooks.onSolutionEnter(action.payload.actions)
6766
return
6867
case'EDITOR_SYNC_POSITION':
6968
// update progress when a level is deemed complete in the client

‎src/commands.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as T from 'typings'
22
import*asTTfrom'typings/tutorial'
33
import*asvscodefrom'vscode'
44
importcreateTestRunnerfrom'./services/testRunner'
5-
import{onSetupActions}from'./actions/onActions'
65
importcreateWebViewfrom'./services/webview'
76
importloggerfrom'./services/logger'
7+
import*ashooksfrom'./services/hooks'
88

99
exportconstCOMMANDS={
1010
START:'coderoad.start',
@@ -19,6 +19,16 @@ interface CreateCommandProps {
1919
workspaceState:vscode.Memento
2020
}
2121

22+
letsendToClient=(action:T.Action):void=>{
23+
// function is replaced when webclient loads
24+
}
25+
26+
// This makes it easier to pass the send
27+
// function throughout the codebase
28+
exportconstsend=(action:T.Action):void=>{
29+
sendToClient(action)
30+
}
31+
2232
exportconstcreateCommands=({ extensionPath, workspaceState}:CreateCommandProps):{[key:string]:any}=>{
2333
// React panel webview
2434
letwebview:any
@@ -36,41 +46,37 @@ export const createCommands = ({ extensionPath, workspaceState }: CreateCommandP
3646
extensionPath,
3747
workspaceState,
3848
})
49+
// make send to client function exportable
50+
// as "send".
51+
sendToClient=webview.send
3952
}
4053
},
4154
[COMMANDS.CONFIG_TEST_RUNNER]:async(data:TT.Tutorial)=>{
42-
consttestRunnerConfig=data.config.testRunner
43-
constsetup=testRunnerConfig.setup||testRunnerConfig.actions// TODO: deprecate and remove config.actions
44-
if(setup){
45-
// setup tutorial test runner commits
46-
// assumes git already exists
47-
awaitonSetupActions({
48-
actions:setup,
49-
send:webview.send,
50-
dir:testRunnerConfig.directory||testRunnerConfig.path,
51-
})// TODO: deprecate and remove config.path
55+
constsetupActions=data.config.setup
56+
if(setupActions){
57+
hooks.onInit(setupActions)
5258
}
5359
testRunner=createTestRunner(data,{
5460
onSuccess:(position:T.Position)=>{
5561
logger('test pass position',position)
5662
// send test pass message back to client
57-
webview.send({type:'TEST_PASS',payload:{position:{ ...position,complete:true}}})
63+
send({type:'TEST_PASS',payload:{position:{ ...position,complete:true}}})
5864
},
5965
onFail:(position:T.Position,failSummary:T.TestFail):void=>{
6066
// send test fail message back to client with failure message
61-
webview.send({type:'TEST_FAIL',payload:{ position,fail:failSummary}})
67+
send({type:'TEST_FAIL',payload:{ position,fail:failSummary}})
6268
},
6369
onError:(position:T.Position)=>{
6470
// TODO: send test error message back to client
6571
constmessage='Error with test runner'
66-
webview.send({type:'TEST_ERROR',payload:{ position, message}})
72+
send({type:'TEST_ERROR',payload:{ position, message}})
6773
},
6874
onRun:(position:T.Position)=>{
6975
// send test run message back to client
70-
webview.send({type:'TEST_RUNNING',payload:{ position}})
76+
send({type:'TEST_RUNNING',payload:{ position}})
7177
},
7278
onLoadSubtasks:({ summary})=>{
73-
webview.send({type:'LOAD_SUBTASK_RESULTS',payload:{ summary}})
79+
send({type:'LOAD_SUBTASK_RESULTS',payload:{ summary}})
7480
},
7581
})
7682
},
@@ -85,7 +91,7 @@ export const createCommands = ({ extensionPath, workspaceState }: CreateCommandP
8591
testRunner({position:currentPosition,onSuccess:callbacks?.onSuccess, subtasks})
8692
},
8793
[COMMANDS.ENTER]:()=>{
88-
webview.send({type:'KEY_PRESS_ENTER'})
94+
send({type:'KEY_PRESS_ENTER'})
8995
},
9096
}
9197
}

‎src/services/hooks/index.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import*asTTfrom'typings/tutorial'
2+
import*asgitfrom'../git'
3+
importloadCommitsfrom'./utils/loadCommits'
4+
importloadWatchersfrom'./utils/loadWatchers'
5+
importopenFilesfrom'./utils/openFiles'
6+
importrunCommandsfrom'./utils/runCommands'
7+
importrunVSCodeCommandsfrom'./utils/runVSCodeCommands'
8+
import{onErrorastelemetryOnError}from'../telemetry'
9+
import{onRunTest}from'../../actions/onTest'
10+
11+
exportconstonInit=async(actions:TT.StepActions):Promise<void>=>{
12+
awaitloadCommits(actions.commits)
13+
awaitrunCommands(actions.commands)
14+
awaitrunVSCodeCommands(actions.vscodeCommands)
15+
}
16+
17+
exportconstonLevelEnter=async(actions:TT.StepActions):Promise<void>=>{
18+
awaitloadCommits(actions.commits)
19+
awaitrunCommands(actions.commands)
20+
}
21+
22+
exportconstonSetupEnter=async(actions:TT.StepActions):Promise<void>=>{
23+
// TODO: set position
24+
awaitloadCommits(actions.commits)
25+
awaitopenFiles(actions.files)
26+
awaitloadWatchers(actions.watchers)
27+
awaitrunCommands(actions.commands)
28+
awaitrunVSCodeCommands(actions.vscodeCommands)
29+
}
30+
31+
exportconstonSolutionEnter=async(actions:TT.StepActions):Promise<void>=>{
32+
// TODO: set position
33+
awaitgit.clear()
34+
awaitloadCommits(actions.commits)
35+
awaitopenFiles(actions.files)
36+
awaitrunCommands(actions.commands)
37+
awaitrunVSCodeCommands(actions.vscodeCommands)
38+
awaitonRunTest()
39+
}
40+
41+
exportconstonError=async(error:Error):Promise<void>=>{
42+
telemetryOnError(error)
43+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import*asgitfrom'../../git'
2+
3+
constloadCommits=async(commits:string[]):Promise<void>=>{
4+
if(commits){
5+
// load the current list of commits for validation
6+
for(constcommitofcommits){
7+
awaitgit.loadCommit(commit)
8+
}
9+
}
10+
}
11+
12+
exportdefaultloadCommits

‎src/actions/utils/loadWatchers.tsrenamed to‎src/services/hooks/utils/loadWatchers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import*aschokidarfrom'chokidar'
22
import*asvscodefrom'vscode'
3-
import{COMMANDS}from'../../commands'
4-
import{WORKSPACE_ROOT}from'../../environment'
3+
import{COMMANDS}from'../../../commands'
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
@@ -14,7 +14,7 @@ const disposeWatcher = (watcher: string) => {
1414
deletewatcherObject[watcher]
1515
}
1616

17-
constloadWatchers=(watchers:string[]):void=>{
17+
constloadWatchers=(watchers:string[]=[]):void=>{
1818
if(!watchers.length){
1919
// remove all watchers
2020
for(constwatcherofObject.keys(watcherObject)){

‎src/actions/utils/openFiles.tsrenamed to‎src/services/hooks/utils/openFiles.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import{join}from'path'
22
import*asvscodefrom'vscode'
3-
import{COMMANDS}from'../../commands'
43

5-
constopenFiles=async(files:string[]):Promise<void>=>{
4+
constopenFiles=async(files:string[]=[]):Promise<void>=>{
65
if(!files.length){
76
return
87
}

‎src/actions/utils/runCommands.tsrenamed to‎src/services/hooks/utils/runCommands.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
import*asTfrom'typings'
2-
import{exec}from'../../services/node'
1+
import{exec}from'../../node'
2+
import{send}from'../../../commands'
33

4-
interfaceRunCommands{
5-
commands:string[]
6-
send:(action:T.Action)=>void
7-
dir?:string
8-
}
9-
10-
construnCommands=async({ commands, send, dir}:RunCommands):Promise<void>=>{
4+
construnCommands=async(commands:string[]=[]):Promise<void>=>{
115
if(!commands.length){
126
return
137
}
@@ -19,10 +13,10 @@ const runCommands = async ({ commands, send, dir }: RunCommands): Promise<void>
1913
send({type:'COMMAND_START',payload:{process:{ ...process,status:'RUNNING'}}})
2014
letresult:{stdout:string;stderr:string}
2115
try{
22-
result=awaitexec({ command, dir})
16+
result=awaitexec({ command})
2317
console.log(result)
2418
}catch(error){
25-
console.log(`Test failed:${error.message}`)
19+
console.error(`Command failed:${error.message}`)
2620
send({type:'COMMAND_FAIL',payload:{process:{ ...process,status:'FAIL'}}})
2721
return
2822
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import*asvscodefrom'vscode'
2+
import*asTTfrom'typings/tutorial'
3+
4+
// what are VSCode commands?
5+
// - https://code.visualstudio.com/api/references/vscode-api#commands
6+
// a list of commands:
7+
// - https://code.visualstudio.com/api/references/commands (note many take params)
8+
// - https://code.visualstudio.com/docs/getstarted/keybindings (anything keybound is a command)
9+
10+
construnVSCodeCommands=async(commands:TT.VSCodeCommand[]=[]):Promise<void>=>{
11+
if(!commands.length){
12+
return
13+
}
14+
for(constcommandofcommands){
15+
if(typeofcommand==='string'){
16+
// string named commands
17+
awaitvscode.commands.executeCommand(command)
18+
}elseif(Array.isArray(command)){
19+
// array commands with params
20+
const[name,params]=command
21+
awaitvscode.commands.executeCommand(name,params)
22+
}
23+
}
24+
}
25+
26+
exportdefaultrunVSCodeCommands

‎src/services/reset/lastHash.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,9 @@ describe('lastHash', () => {
6565
consttutorial:TT.Tutorial={
6666
config:{
6767
//@ts-ignore
68-
testRunner:{
69-
setup:{
70-
commits:['abcdef2','abcdef3'],
71-
},
68+
testRunner:{},
69+
setup:{
70+
commits:['abcdef2','abcdef3'],
7271
},
7372
},
7473
levels:[

‎src/services/reset/lastHash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const getLastCommitHash = (position: T.Position, tutorial: TT.Tutorial | null):
2828
level=levels[levelIndex-1]
2929
}else{
3030
// use init commit
31-
constconfigCommits=tutorial.config.testRunner.setup?.commits
31+
constconfigCommits=tutorial.config.setup?.commits
3232
if(!configCommits){
3333
thrownewError('No commits found to reset back to')
3434
}

‎src/services/testRunner/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const createTestRunner = (data: TT.Tutorial, callbacks: Callbacks): ((params: an
7474
}
7575
}
7676
logger('COMMAND',command)
77-
result=awaitexec({ command,dir:testRunnerConfig.directory||testRunnerConfig.path})// TODO: remove config.path later
77+
result=awaitexec({ command,dir:testRunnerConfig.directory})
7878
}catch(err){
7979
result={stdout:err.stdout,stderr:err.stack}
8080
}

‎src/services/webview/render.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const getNonce = (): string => {
1212
returntext
1313
}
1414

15-
asyncfunctionrender(panel:vscode.WebviewPanel,rootPath:string){
15+
asyncfunctionrender(panel:vscode.WebviewPanel,rootPath:string):Promise<void>{
1616
try{
1717
// load copied index.html from web app build
1818
constdom=awaitJSDOM.fromFile(path.join(rootPath,'index.html'))

‎typings/tutorial.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type TutorialConfig = {
1111
testRunner:TestRunnerConfig
1212
repo:TutorialRepo
1313
dependencies?:TutorialDependency[]
14+
setup?:StepActions
1415
reset?:ConfigReset
1516
}
1617

@@ -60,6 +61,7 @@ export type StepActions = {
6061
files?:string[]
6162
watchers?:string[]
6263
filter?:string
64+
vscodeCommands?:VSCodeCommand[]
6365
}
6466

6567
exportinterfaceTestRunnerArgs{
@@ -70,10 +72,7 @@ export interface TestRunnerArgs {
7072
exportinterfaceTestRunnerConfig{
7173
command:string
7274
args:TestRunnerArgs
73-
path?:string// deprecated
7475
directory?:string
75-
actions?:StepActions// deprecated
76-
setup?:StepActions
7776
}
7877

7978
exportinterfaceTutorialRepo{
@@ -90,3 +89,5 @@ export interface TutorialDependency {
9089
exportinterfaceTutorialAppVersions{
9190
vscode:string
9291
}
92+
93+
exporttypeVSCodeCommand=string|[string,any]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp