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

Commit832aa8b

Browse files
committed
refactor commands and actions
1 parent803005b commit832aa8b

File tree

5 files changed

+106
-88
lines changed

5 files changed

+106
-88
lines changed

‎src/actions/setupActions.ts

Lines changed: 10 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import*asGfrom'typings/graphql'
2-
import{join}from'path'
32
import*asvscodefrom'vscode'
43
import*asgitfrom'../services/git'
54
importnodefrom'../services/node'
65

6+
importopenFilesfrom'./utils/openFiles'
7+
importloadListenersfrom'./utils/loadListeners'
8+
79
construnCommands=async(commands:string[])=>{
10+
if(!commands.length){
11+
return
12+
}
813
for(constcommandofcommands){
914
const{ stdout, stderr}=awaitnode.exec(command)
1015
if(stderr){
@@ -14,52 +19,9 @@ const runCommands = async (commands: string[]) => {
1419
}
1520
}
1621

17-
// collect active file watchers (listeners)
18-
constwatchers:{[key:string]:vscode.FileSystemWatcher}={}
19-
20-
constdisposeWatcher=(listener:string)=>{
21-
watchers[listener].dispose()
22-
deletewatchers[listener]
23-
}
24-
25-
constloadListeners=(listeners:string[],workspaceUri:vscode.Uri)=>{
26-
for(constlisteneroflisteners){
27-
if(!watchers[listener]){
28-
constrootUri=vscode.workspace.getWorkspaceFolder(workspaceUri)
29-
constpattern=newvscode.RelativePattern(rootUri!,listener)// eslint-disable-line
30-
console.log(pattern)
31-
constlisten=vscode.workspace.createFileSystemWatcher(pattern)
32-
watchers[listener]=listen
33-
watchers[listener].onDidChange(()=>{
34-
console.log('onDidChange')
35-
// trigger save
36-
vscode.commands.executeCommand('coderoad.run_test',null,()=>{
37-
// cleanup watcher on success
38-
disposeWatcher(listener)
39-
})
40-
})
41-
watchers[listener].onDidCreate(()=>{
42-
console.log('onDidCreate')
43-
// trigger save
44-
vscode.commands.executeCommand('coderoad.run_test',null,()=>{
45-
// cleanup watcher on success
46-
disposeWatcher(listener)
47-
})
48-
})
49-
watchers[listener].onDidDelete(()=>{
50-
console.log('onDidDelete')
51-
// trigger save
52-
vscode.commands.executeCommand('coderoad.run_test',null,()=>{
53-
// cleanup watcher on success
54-
disposeWatcher(listener)
55-
})
56-
})
57-
}
58-
}
59-
}
60-
6122
constsetupActions=async(workspaceRoot:vscode.WorkspaceFolder,actions:G.StepActions):Promise<void>=>{
6223
const{ commands, commits, files, listeners}=actions
24+
6325
// 1. run commits
6426
if(commits){
6527
for(constcommitofcommits){
@@ -68,50 +30,13 @@ const setupActions = async (workspaceRoot: vscode.WorkspaceFolder, actions: G.St
6830
}
6931

7032
// 2. open files
71-
if(files){
72-
for(constfilePathoffiles){
73-
try{
74-
// TODO: figure out why this does not work
75-
// try {
76-
// const absoluteFilePath = join(workspaceRoot.uri.path, filePath)
77-
// const doc = await vscode.workspace.openTextDocument(absoluteFilePath)
78-
// await vscode.window.showTextDocument(doc, vscode.ViewColumn.One)
79-
// // there are times when initialization leave the panel behind any files opened
80-
// // ensure the panel is redrawn on the right side first
81-
// // webview.createOrShow()
82-
// } catch (error) {
83-
// console.log(`Failed to open file ${filePath}`, error)
84-
// }
85-
constwr=vscode.workspace.rootPath
86-
if(!wr){
87-
thrownewError('No workspace root path')
88-
}
89-
constabsoluteFilePath=join(wr,filePath)
90-
constdoc=awaitvscode.workspace.openTextDocument(absoluteFilePath)
91-
awaitvscode.window.showTextDocument(doc,vscode.ViewColumn.One)
92-
// there are times when initialization leave the panel behind any files opened
93-
// ensure the panel is redrawn on the right side first
94-
vscode.commands.executeCommand('coderoad.open_webview')
95-
}catch(error){
96-
console.log(`Failed to open file${filePath}`,error)
97-
}
98-
}
99-
}
33+
openFiles(files||[])
10034

10135
// 3. start file watchers (listeners)
102-
if(listeners){
103-
loadListeners(listeners,workspaceRoot.uri)
104-
}else{
105-
// remove all watchers
106-
for(constlistenerofObject.keys(watchers)){
107-
disposeWatcher(listener)
108-
}
109-
}
36+
loadListeners(listeners||[],workspaceRoot.uri)
11037

11138
// 4. run command
112-
if(commands){
113-
awaitrunCommands(commands)
114-
}
39+
awaitrunCommands(commands||[])
11540
}
11641

11742
exportdefaultsetupActions

‎src/actions/tutorialConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const tutorialConfig = async ({ config, alreadyConfigured }: TutorialConfigParam
4242
// setup onSave hook
4343
vscode.workspace.onDidSaveTextDocument((document:vscode.TextDocument)=>{
4444
if(shouldRunTest(document)){
45-
vscode.commands.executeCommand('coderoad.run_test')
45+
vscode.commands.executeCommand(COMMANDS.RUN_TEST)
4646
}
4747
})
4848
}

‎src/actions/utils/loadListeners.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import*asvscodefrom'vscode'
2+
import{COMMANDS}from'../../editor/commands'
3+
4+
// collect active file watchers (listeners)
5+
constwatchers:{[key:string]:vscode.FileSystemWatcher}={}
6+
7+
constdisposeWatcher=(listener:string)=>{
8+
watchers[listener].dispose()
9+
deletewatchers[listener]
10+
}
11+
12+
constloadListeners=(listeners:string[],workspaceUri:vscode.Uri)=>{
13+
if(!listeners.length){
14+
// remove all watchers
15+
for(constlistenerofObject.keys(watchers)){
16+
disposeWatcher(listener)
17+
}
18+
}
19+
for(constlisteneroflisteners){
20+
if(!watchers[listener]){
21+
// const rootUri = vscode.workspace.getWorkspaceFolder(workspaceUri)
22+
// const pattern = listener // eslin
23+
// console.log(pattern)
24+
constlisten=vscode.workspace.createFileSystemWatcher('**/*.js')
25+
listen.onDidChange(()=>{
26+
console.log('onDidChange')
27+
// trigger save
28+
vscode.commands.executeCommand(COMMANDS.RUN_TEST,null,()=>{
29+
// cleanup watcher on success
30+
disposeWatcher(listener)
31+
})
32+
})
33+
listen.onDidCreate(()=>{
34+
console.log('onDidCreate')
35+
// trigger save
36+
vscode.commands.executeCommand(COMMANDS.RUN_TEST,null,()=>{
37+
// cleanup watcher on success
38+
disposeWatcher(listener)
39+
})
40+
})
41+
listen.onDidDelete(()=>{
42+
console.log('onDidDelete')
43+
// trigger save
44+
vscode.commands.executeCommand(COMMANDS.RUN_TEST,null,()=>{
45+
// cleanup watcher on success
46+
disposeWatcher(listener)
47+
})
48+
})
49+
watchers[listener]=listen
50+
}
51+
}
52+
}
53+
54+
exportdefaultloadListeners

‎src/actions/utils/openFiles.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import{join}from'path'
2+
import*asvscodefrom'vscode'
3+
import{COMMANDS}from'../../editor/commands'
4+
5+
constopenFiles=async(files:string[])=>{
6+
if(!files.length){
7+
return
8+
}
9+
for(constfilePathoffiles){
10+
try{
11+
// TODO: figure out why this does not work
12+
// try {
13+
// const absoluteFilePath = join(workspaceRoot.uri.path, filePath)
14+
// const doc = await vscode.workspace.openTextDocument(absoluteFilePath)
15+
// await vscode.window.showTextDocument(doc, vscode.ViewColumn.One)
16+
// // there are times when initialization leave the panel behind any files opened
17+
// // ensure the panel is redrawn on the right side first
18+
// // webview.createOrShow()
19+
// } catch (error) {
20+
// console.log(`Failed to open file ${filePath}`, error)
21+
// }
22+
constwr=vscode.workspace.rootPath
23+
if(!wr){
24+
thrownewError('No workspace root path')
25+
}
26+
constabsoluteFilePath=join(wr,filePath)
27+
constdoc=awaitvscode.workspace.openTextDocument(absoluteFilePath)
28+
awaitvscode.window.showTextDocument(doc,vscode.ViewColumn.One)
29+
// there are times when initialization leave the panel behind any files opened
30+
// ensure the panel is redrawn on the right side first
31+
vscode.commands.executeCommand(COMMANDS.OPEN_WEBVIEW)
32+
}catch(error){
33+
console.log(`Failed to open file${filePath}`,error)
34+
}
35+
}
36+
}
37+
38+
exportdefaultopenFiles

‎src/channel/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import tutorialConfig from '../actions/tutorialConfig'
77
importsetupActionsfrom'../actions/setupActions'
88
importsolutionActionsfrom'../actions/solutionActions'
99
importsaveCommitfrom'../actions/saveCommit'
10+
import{COMMANDS}from'../editor/commands'
1011

1112
interfaceChannel{
1213
receive(action:CR.Action):Promise<void>
@@ -117,14 +118,14 @@ class Channel implements Channel {
117118
return
118119
// load step actions (git commits, commands, open files)
119120
case'SETUP_ACTIONS':
120-
vscode.commands.executeCommand('coderoad.set_current_step',action.payload)
121+
vscode.commands.executeCommand(COMMANDS.SET_CURRENT_STEP,action.payload)
121122
setupActions(this.workspaceRoot,action.payload)
122123
return
123124
// load solution step actions (git commits, commands, open files)
124125
case'SOLUTION_ACTIONS':
125126
awaitsolutionActions(this.workspaceRoot,action.payload)
126127
// run test following solution to update position
127-
vscode.commands.executeCommand('coderoad.run_test',action.payload)
128+
vscode.commands.executeCommand(COMMANDS.RUN_TEST,action.payload)
128129
return
129130

130131
default:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp