-
-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathinstall-scrypted-dependencies-win.ps1
129 lines (110 loc) · 4.41 KB
/
install-scrypted-dependencies-win.ps1
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
#Requires -RunAsAdministrator
# Set-PSDebug -Trace 1
# stop existing service if any
sc.exe stop scrypted.exe
# 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=20.18.0
# Install VC Redist, which is necessary for portable python
choco install -y vcredist140
# TODO: remove python install, and use portable python
# Install Python
choco upgrade -y python39
# Run py.exe with a specific version
$SCRYPTED_WINDOWS_PYTHON_VERSION="-3.9"
# Refresh environment variables for py and npx to work
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
# Workaround Windows Node no longer creating %APPDATA%\npm which causes npx to fail
# Fixed in newer versions of NPM but not the one bundled with Node 20
# https://github.com/nodejs/node/issues/53538
npm i -g npm
py $SCRYPTED_WINDOWS_PYTHON_VERSION -m pip install --upgrade pip
# besides debugpy, none of these dependencies are needed anymore?
# portable python includes typing and does not need typing_extensions.
# opencv-python-headless has wheels for windows.
py $SCRYPTED_WINDOWS_PYTHON_VERSION -m pip install debugpy typing_extensions typing opencv-python
$SCRYPTED_INSTALL_VERSION=[System.Environment]::GetEnvironmentVariable("SCRYPTED_INSTALL_VERSION","User")
if ($SCRYPTED_INSTALL_VERSION -eq $null) {
npx -y scrypted@latest install-server
} else {
npx -y scrypted@latest install-server $SCRYPTED_INSTALL_VERSION
}
$USER_HOME_ESCAPED = $env:USERPROFILE.replace('\', '\\')
$SCRYPTED_HOME = $env:USERPROFILE + '\.scrypted'
$SCRYPTED_HOME_ESCAPED_PATH = $SCRYPTED_HOME.replace('\', '\\')
npm install --prefix $SCRYPTED_HOME @koush/node-windows --save
$NPX_PATH = (Get-Command npx).Path
# The path needs double quotes to handle spaces in the directory path
$NPX_PATH_ESCAPED = '"' + $NPX_PATH.replace('\', '\\') + '"'
# On newer versions of NPM, the NPX might be a .ps1 file which doesn't work with child_process.spawn, change to .cmd
$NPX_PATH_ESCAPED = $NPX_PATH_ESCAPED.replace('.ps1', '.cmd')
$SERVICE_JS = @"
const fs = require('fs');
try {
fs.mkdirSync('C:\\WINDOWS\\system32\\config\\systemprofile\\AppData\\Roaming\\npm');
}
catch (e) {
}
const child_process = require('child_process');
child_process.spawn('$NPX_PATH_ESCAPED', ['-y', 'scrypted', 'serve'], {
stdio: 'inherit',
// allow spawning .cmd https://nodejs.org/en/blog/vulnerability/april-2024-security-releases-2
shell: true,
}).on('error', (err) => {
console.error('Error spawning child process', err);
});
"@
$SERVICE_JS_PATH = $SCRYPTED_HOME + '\service.js'
$SERVICE_JS_ESCAPED_PATH = $SERVICE_JS_PATH.replace('\', '\\')
$SERVICE_JS | Out-File -Encoding ASCII -FilePath $SERVICE_JS_PATH
$INSTALL_SERVICE_JS = @"
const Service = require('@koush/node-windows').Service;
const svc = new Service({
name: 'Scrypted',
description: 'Scrypted Home Automation',
script: '$($SERVICE_JS_ESCAPED_PATH)',
env: [
{
name: "USERPROFILE",
value: '$($USER_HOME_ESCAPED)'
},
{
name: "SCRYPTED_WINDOWS_PYTHON_VERSION",
value: '$($SCRYPTED_WINDOWS_PYTHON_VERSION)'
}
]
});
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', () => {
console.log('Service uninstalled, reinstalling');
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("start", () => {
console.log("Service started");
});
svc.on("error", (err) => {
console.log("Service error", err);
});
svc.install();
"@
$INSTALL_SERVICE_JS_PATH = $SCRYPTED_HOME + '\install-service.js'
$INSTALL_SERVICE_JS | Out-File -Encoding ASCII -FilePath $INSTALL_SERVICE_JS_PATH
node $INSTALL_SERVICE_JS_PATH
del $INSTALL_SERVICE_JS_PATH
Write-Output "Scrypted is now running at: https://localhost:10443/"
Write-Output "Note that it is https and that you'll be asked to approve/ignore the website certificate."