Skip to content

Commit 74445d3

Browse files
[fix]: Windows: Fix restart.ts to make it compatible with current Node.js (#2850)
* Windows: Fix restart.ts to make it compatible with current Node.js versions * formatting and use of execAsync * Fix powershell command * simplify regex --------- Co-authored-by: foxriver76 <[email protected]>
1 parent f54793e commit 74445d3

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

packages/controller/src/lib/restart.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { spawn } from 'node:child_process';
22
import os from 'node:os';
3-
import { getRootDir } from '@iobroker/js-controller-common-db/tools';
3+
import { execAsync, getRootDir } from '@iobroker/js-controller-common-db/tools';
44
import path from 'node:path';
55
import url from 'node:url';
66

@@ -9,25 +9,45 @@ import url from 'node:url';
99
*
1010
* @param callback callback to execute after restart is triggered
1111
*/
12-
export default function restart(callback?: () => void): void {
12+
export default async function restart(callback?: () => void): Promise<void> {
1313
let cmd;
1414
let args;
1515
if (os.platform() === 'win32') {
16-
// On Windows, we execute the controller entry point directly
17-
cmd = path.join(getRootDir(), 'iob.bat');
18-
args = ['restart'];
16+
// On Windows, we use powershell to restart the service, because execution of bat files is no more possible
17+
const envPath = path.join(getRootDir(), '.env').replaceAll('\\', '\\\\');
18+
cmd = `powershell -Command "$envPath = \\"${envPath}\\";
19+
$iobServiceName = \\"ioBroker\\";
20+
if (Test-Path $envPath) {
21+
foreach ($line in Get-Content $envPath) {
22+
$line = $line.Trim();
23+
if ($line -match \\"^\\s*iobservicename\\s*=\\s*(.+)\\s*$\\") {
24+
$iobServiceName = $matches[1].Trim(); break;
25+
}
26+
}
27+
}
28+
Write-Output \\"Restarting service $iobServiceName.exe\\";Restart-Service \\"$iobServiceName.exe\\" -Force"`;
29+
30+
// Remove line breaks, because the powershell command will fail otherwise
31+
cmd = cmd.replace(/[\r\n]+/gm, ' ');
32+
33+
try {
34+
await execAsync(cmd);
35+
} catch (e) {
36+
console.error(`Restart failed: ${e.message}`);
37+
}
1938
} else {
2039
// Unix has a global ioBroker binary that delegates to the init system
2140
// We need to call that, so we don't have two instances of ioBroker running
2241
cmd = 'iobroker';
2342
args = ['restart'];
43+
44+
const child = spawn(cmd, args, {
45+
detached: true,
46+
stdio: ['ignore', 'ignore', 'ignore'],
47+
windowsHide: true
48+
});
49+
child.unref();
2450
}
25-
const child = spawn(cmd, args, {
26-
detached: true,
27-
stdio: ['ignore', 'ignore', 'ignore'],
28-
windowsHide: true
29-
});
30-
child.unref();
3151
if (typeof callback === 'function') {
3252
setTimeout(() => callback(), 500);
3353
} else {

0 commit comments

Comments
 (0)