Skip to content

Commit

Permalink
Update Windows installation script (koush#503)
Browse files Browse the repository at this point in the history
* Update Windows installation script

* Fix sc.exe

* Workaround node-windows quirks with uninstall and start
  • Loading branch information
longzheng authored and nberardi committed Feb 24, 2023
1 parent 50c7118 commit 2531c29
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions docker/install-scrypted-dependencies-win.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
winget install -h --id "OpenJS.NodeJS.LTS"
winget install -h --id "Python.Python.3.10"
# Install Chocolatey
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

# Install node.js
choco upgrade -y nodejs-lts --version=18.13.0

# Install Node.js additional tools for Windows to compile native modules
# https://github.com/nodejs/node/blob/main/tools/msvs/install_tools/install_tools.bat#L55
choco upgrade -y python visualstudio2019-workload-vctools

# Refresh environment variables for py and npx to work
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")

py -m pip install --upgrade pip
py -m pip install aiofiles debugpy typing_extensions typing opencv-python
Expand All @@ -9,7 +20,6 @@ npx -y scrypted@latest install-server
$USER_HOME_ESCAPED = $env:USERPROFILE.replace('\', '\\')
$SCRYPTED_HOME = $env:USERPROFILE + '\.scrypted'
$SCRYPTED_HOME_ESCAPED_PATH = $SCRYPTED_HOME.replace('\', '\\')
npm install --global --production windows-build-tools
npm install --prefix $SCRYPTED_HOME node-windows@1.0.0-beta.8 --save

$SERVICE_JS = @"
Expand All @@ -19,7 +29,6 @@ try {
}
catch (e) {
}
const child_process = require('child_process');
child_process.spawn('npx.cmd', ['-y', 'scrypted', 'serve'], {
stdio: 'inherit',
Expand All @@ -31,9 +40,8 @@ $SERVICE_JS_ESCAPED_PATH = $SERVICE_JS_PATH.replace('\', '\\')
$SERVICE_JS | Out-File -Encoding ASCII -FilePath $SERVICE_JS_PATH

$INSTALL_SERVICE_JS = @"
var Service = require('node-windows').Service;
var svc = new Service({
const Service = require('node-windows').Service;
const svc = new Service({
name: 'Scrypted',
description: 'Scrypted Home Automation',
script: '$($SERVICE_JS_ESCAPED_PATH)',
Expand All @@ -44,24 +52,28 @@ var svc = new Service({
},
]
});
svc.on('alreadyuninstalled', () => {
svc.install();
svc.on('alreadyinstalled', () => {
console.log('Service already installed, uninstalling first');
// wait 5 seconds after uninstalling before deleting daemon to prevent unlink error
svc.uninstall(5);
});
svc.on('uninstall', () => {
svc.install();
console.log('Service uninstalled, reinstalling');
svc.install();
});
svc.on('error', () => {
svc.install();
svc.on("install", () => {
console.log("Service installed");
// wait 5 seconds for install to actually complete before attempting to start
// https://github.com/coreybutler/node-windows/issues/318#issuecomment-1232801990
setTimeout(() => {
console.log("Starting service");
svc.start();
}, 5000);
});
svc.on('install', () => {
svc.start();
svc.on("start", () => {
console.log("Service started");
});
svc.uninstall();
svc.install();
"@

$INSTALL_SERVICE_JS_PATH = $SCRYPTED_HOME + '\install-service.js'
Expand Down

0 comments on commit 2531c29

Please sign in to comment.