Skip to content

Commit 8310e47

Browse files
feat(overmind-devtools-client): allow changing port
1 parent ebcc2c0 commit 8310e47

File tree

7 files changed

+124
-39
lines changed

7 files changed

+124
-39
lines changed

packages/node_modules/overmind-devtools-client/DevtoolBackend.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ class DevtoolBackend {
1111
this.onDevtoolMessage = this.onDevtoolMessage.bind(this)
1212
}
1313
connect(port) {
14+
if (this.devtoolServer) {
15+
this.devtoolServer.close()
16+
}
17+
1418
return new Promise((resolve, reject) => {
1519
this.devtoolServer = new WebSocket.Server({
1620
port,
@@ -211,7 +215,7 @@ class DevtoolBackend {
211215
</html>
212216
`
213217
}
214-
getMarkup(scriptSource, port) {
218+
getMarkup(scriptSource, port, onPortSubmit, onRestart) {
215219
return `<!DOCTYPE html>
216220
<html lang="en">
217221
<head>
@@ -230,6 +234,18 @@ class DevtoolBackend {
230234
<script type="text/javascript">
231235
window['__OVERMIND_DEVTOOLS_BACKEND_PORT__'] = "${port}";
232236
</script>
237+
<script type="text/javascript">
238+
${onPortSubmit.toString()}
239+
240+
${onRestart.toString()}
241+
242+
function handleFormSubmit(event) {
243+
event.preventDefault()
244+
var input = document.querySelector('#port-input')
245+
input.setAttribute('disabled', true)
246+
${onPortSubmit.name}(input.value)
247+
}
248+
</script>
233249
</head>
234250
<body>
235251
<script type="text/javascript" src="${scriptSource}"></script>

packages/node_modules/overmind-devtools-client/index.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,46 @@
1313
href="https://fonts.googleapis.com/css?family=Nunito:400,700"
1414
rel="stylesheet"
1515
/>
16+
<script>
17+
/*
18+
THIS CODE IS COPIED FOR DEVELOPMENT PURPOSES, A BIT RISKY, BUT HAVE NO OTHER SOLUTION
19+
*/
20+
const vscode =
21+
window.vscode || (window.acquireVsCodeApi && window.acquireVsCodeApi())
22+
23+
function onPortSubmit(newPort) {
24+
if (vscode) {
25+
vscode.postMessage({
26+
command: 'newPort',
27+
text: newPort,
28+
})
29+
} else {
30+
const { ipcRenderer } = require('electron')
31+
32+
ipcRenderer.send('newPort', newPort)
33+
}
34+
}
35+
36+
function onRestart() {
37+
if (vscode) {
38+
vscode.postMessage({
39+
command: 'restart',
40+
})
41+
} else {
42+
const { ipcRenderer } = require('electron')
43+
44+
ipcRenderer.send('restart')
45+
}
46+
}
47+
48+
function handleFormSubmit(event) {
49+
event.preventDefault()
50+
var input = document.querySelector('#port-input')
51+
input.setAttribute('disabled', true)
52+
console.log(input)
53+
onPortSubmit(input.value)
54+
}
55+
</script>
1656
</head>
1757
<body></body>
1858
</html>

packages/node_modules/overmind-devtools-client/src/components/Devtools/index.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ const Devtools: FunctionComponent = () => {
5858
</Fragment>
5959
)}
6060
</pre>
61+
<h3>Change port</h3>
62+
<input
63+
id="port-input"
64+
className={styles.newPort}
65+
placeholder="Port..."
66+
onKeyDown={(event) => {
67+
if (event.keyCode === 13) {
68+
// @ts-ignore
69+
handleFormSubmit(event)
70+
}
71+
}}
72+
/>
6173
</div>
6274
) : (
6375
<Workspace />

packages/node_modules/overmind-devtools-client/src/components/Devtools/styles.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,12 @@ export const code = css({
3131
padding: '10px',
3232
background: colors.foreground,
3333
})
34+
35+
export const newPort = css({
36+
border: 0,
37+
padding: '1rem',
38+
borderRadius: 3,
39+
outline: 'none',
40+
fontSize: 18,
41+
width: 75,
42+
})

packages/node_modules/overmind-devtools-client/src/overmind/effects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const utils = {
77
return window.confirm(text)
88
},
99
getPort() {
10-
return Number(window['__OVERMIND_DEVTOOLS_BACKEND_PORT__'] || 3032)
10+
return Number(window['__OVERMIND_DEVTOOLS_BACKEND_PORT__'] || 3031)
1111
},
1212
}
1313

packages/node_modules/overmind-devtools-vscode/src/extension.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@ import * as path from 'path'
55

66
const DevtoolBackend = require('overmind-devtools-client/DevtoolBackend')
77

8+
function onNewPortSubmit(newPort: string) {
9+
// @ts-ignore
10+
const vscode = (window.vscode =
11+
// @ts-ignore
12+
window.vscode || acquireVsCodeApi())
13+
vscode.postMessage({
14+
command: 'newPort',
15+
text: newPort,
16+
})
17+
}
18+
19+
function onRestart() {
20+
// @ts-ignore
21+
const vscode = (window.vscode =
22+
// @ts-ignore
23+
window.vscode || acquireVsCodeApi())
24+
vscode.postMessage({
25+
command: 'restart',
26+
})
27+
}
28+
829
// this method is called when your extension is activated
930
// your extension is activated the very first time the command is executed
1031
export function activate(context: vscode.ExtensionContext) {
@@ -79,9 +100,11 @@ export function activate(context: vscode.ExtensionContext) {
79100
}
80101

81102
devtoolsPanel.show(
82-
devtoolBackend.getMarkup(scriptFile, port).replace(
83-
'</head>',
84-
`
103+
devtoolBackend
104+
.getMarkup(scriptFile, port, onNewPortSubmit, onRestart)
105+
.replace(
106+
'</head>',
107+
`
85108
<style>
86109
:root {
87110
--colors-background: var(--vscode-editor-background) !important;
@@ -93,34 +116,13 @@ export function activate(context: vscode.ExtensionContext) {
93116
</style>
94117
</head>
95118
`
96-
),
119+
),
97120
panel
98121
)
99122
})
100123
.catch(() => {
101124
devtoolsPanel.show(
102-
devtoolBackend.getChangePortMarkup(
103-
port,
104-
function onNewPortSubmit(newPort: string) {
105-
// @ts-ignore
106-
const vscode = (window.vscode =
107-
// @ts-ignore
108-
window.vscode || acquireVsCodeApi())
109-
vscode.postMessage({
110-
command: 'newPort',
111-
text: newPort,
112-
})
113-
},
114-
function onRestart() {
115-
// @ts-ignore
116-
const vscode = (window.vscode =
117-
// @ts-ignore
118-
window.vscode || acquireVsCodeApi())
119-
vscode.postMessage({
120-
command: 'restart',
121-
})
122-
}
123-
),
125+
devtoolBackend.getChangePortMarkup(port, onNewPortSubmit, onRestart),
124126
panel
125127
)
126128
})

packages/node_modules/overmind-devtools/src/main.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,25 @@ function createWindow() {
2525
storage,
2626
})
2727

28+
function onPortSubmit(newPort) {
29+
const { ipcRenderer } = require('electron')
30+
31+
ipcRenderer.send('newPort', newPort)
32+
}
33+
34+
function onRestart() {
35+
const { ipcRenderer } = require('electron')
36+
37+
ipcRenderer.send('restart')
38+
}
39+
2840
function openDevtools(port) {
2941
if (process.env.NODE_ENV === 'production') {
3042
mainWindow.loadURL(
3143
'data:text/html;charset=UTF-8,' +
32-
encodeURIComponent(devtoolBackend.getMarkup('bundle.js', port)),
44+
encodeURIComponent(
45+
devtoolBackend.getMarkup('bundle.js', port, onPortSubmit, onRestart)
46+
),
3347
{
3448
baseURLForDataURL: `file://${path.join(
3549
__dirname,
@@ -57,16 +71,8 @@ function createWindow() {
5771
encodeURIComponent(
5872
devtoolBackend.getChangePortMarkup(
5973
port,
60-
function onPortSubmit(newPort) {
61-
const { ipcRenderer } = require('electron')
62-
63-
ipcRenderer.send('newPort', newPort)
64-
},
65-
function onRestart() {
66-
const { ipcRenderer } = require('electron')
67-
68-
ipcRenderer.send('restart')
69-
}
74+
onPortSubmit,
75+
onRestart
7076
)
7177
),
7278
{

0 commit comments

Comments
 (0)