forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.ts
More file actions
153 lines (140 loc) · 3.97 KB
/
extension.ts
File metadata and controls
153 lines (140 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import * as vscode from 'vscode'
import { DevtoolsPanel } from './DevtoolsPanel'
import { log } from './utils/Logger'
import * as path from 'path'
const DevtoolBackend = require('overmind-devtools-client/DevtoolBackend')
function onNewPortSubmit(newPort: string) {
// @ts-ignore
const vscode = (window.vscode =
// @ts-ignore
window.vscode || acquireVsCodeApi())
vscode.postMessage({
command: 'newPort',
text: newPort,
})
}
function onRestart() {
// @ts-ignore
const vscode = (window.vscode =
// @ts-ignore
window.vscode || acquireVsCodeApi())
vscode.postMessage({
command: 'restart',
})
}
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
const storage = {
get(key: string) {
return new Promise((resolve) => {
resolve(context.workspaceState.get(key))
})
},
set(key: string, value: any) {
return new Promise<void>((resolve) => {
context.workspaceState.update(key, value)
resolve()
})
},
clear() {
return new Promise<void>((resolve) => {
resolve()
})
},
}
const devtoolBackend = DevtoolBackend.create({
onRelaunch() {
devtoolBackend.close()
startDevtools()
},
storage,
})
const devtoolsPanel = new DevtoolsPanel({
context,
onMessage: (command, text) => {
switch (command) {
case 'newPort':
storage.set('overmind.port', text).then(() => startDevtools())
break
case 'restart':
devtoolBackend.close()
startDevtools()
break
}
},
onDispose() {
devtoolBackend.close()
},
})
function startDevtools(panel?: vscode.WebviewPanel) {
storage.get('overmind.port').then((port = 3031) => {
devtoolBackend
.connect(port)
.then(() => {
const content = (panel: vscode.WebviewPanel) => {
let scriptFile: vscode.Uri | string
if (
process.env.VSCODE_DEBUG_MODE ||
vscode.workspace
.getConfiguration()
.get('overmind.devmode.enabled')
) {
scriptFile = <string>(
vscode.workspace.getConfiguration().get('overmind.devmode.url')
)
} else {
const onDiskPath = vscode.Uri.file(
path.join(context.extensionPath, 'devtoolsDist', 'bundle.js')
)
scriptFile = panel.webview.asWebviewUri(onDiskPath)
}
return devtoolBackend
.getMarkup(scriptFile, port, onNewPortSubmit, onRestart)
.replace(
'</head>',
`
<style>
:root {
--colors-background: var(--vscode-editor-background) !important;
--colors-foreground: var(--vscode-activityBar-background) !important;
--colors-border: var(--vscode-dropdown-border) !important;
--colors-text: var(--vscode-editor-foreground) !important;
--colors-highlight: var(--vscode-breadcrumb-focusForeground) !important;
}
</style>
</head>
`
)
}
devtoolsPanel.show(content)
})
.catch(() => {
devtoolsPanel.show(
devtoolBackend.getChangePortMarkup(
port,
onNewPortSubmit,
onRestart
),
panel
)
})
})
}
context.subscriptions.push(
vscode.commands.registerCommand('overmind-devtools.start', () =>
startDevtools()
)
)
if (vscode.window.registerWebviewPanelSerializer) {
vscode.window.registerWebviewPanelSerializer(DevtoolsPanel.viewType, {
async deserializeWebviewPanel(webViewPanel: vscode.WebviewPanel) {
startDevtools(webViewPanel)
},
})
}
}
// this method is called when your extension is deactivated
export function deactivate() {
log('Extension deactivated')
}