Skip to content

Commit

Permalink
feat: Add script to download npm dependencies and update installer sc…
Browse files Browse the repository at this point in the history
…ript
  • Loading branch information
KIRA009 committed Apr 9, 2024
1 parent e06f319 commit 16f4d8b
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 19 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ pip3 install poetry
poetry install
poetry shell
alembic upgrade head
cd openadapt/app/dashboard
nvm install 21
nvm use
npm install
poetry run dashbaord
pytest
```
Expand Down
41 changes: 41 additions & 0 deletions install/install_openadapt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ $pythonMaxVersion = "3.10.12" # Change this if a different Higher version are su
$pythonInstaller = "python-3.10.11-amd64.exe"
$pythonInstallerLoc = "https://www.python.org/ftp/python/3.10.11/python-3.10.11-amd64.exe"

$nvmCmd = "nvm"
$nvmInstaller = "nvm-setup.exe"
$nvmInstallerLoc = "https://github.com/coreybutler/nvm-windows/releases/download/1.1.12/nvm-setup.exe"

$gitCmd = "git"
$gitInstaller = "Git-2.40.1-64-bit.exe"
$gitInstallerLoc = "https://github.com/git-for-windows/git/releases/download/v2.40.1.windows.1/Git-2.40.1-64-bit.exe"
Expand Down Expand Up @@ -226,6 +230,39 @@ function GetPythonCMD {
exit
}

# Check and Install NVM and return the nvm command
function GetNVMCMD {
$nvmExists = CheckCMDExists $nvmCmd
if (!$nvmExists) {
# Install NVM
Write-Host "Downloading NVM installer..."
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $nvmInstallerLoc -OutFile $nvmInstaller
$exists = Test-Path -Path $nvmInstaller -PathType Leaf
if (!$exists) {
Write-Host "Failed to download NVM installer" -ForegroundColor Red
exit
}

Write-Host "Installing NVM..."
Start-Process -FilePath $nvmInstaller -Verb runAs -ArgumentList '/VERYSILENT /NORESTART /NOCANCEL /SP-' -Wait
Remove-Item $nvmInstaller

RefreshPathVariables

# Make sure NVM is now available
$nvmExists = CheckCMDExists $nvmCmd
if (!$nvmExists) {
Write-Host "Error after installing NVM. Uninstalling..."
Start-Process -FilePath $nvmInstaller -Verb runAs -ArgumentList '/VERYSILENT /NORESTART /NOCANCEL /SP-' -Wait
Cleanup
exit
}
}
# Return the nvm command
return $nvmCmd
}


# Check and Install Git and return the git command
function GetGitCMD {
Expand Down Expand Up @@ -310,6 +347,9 @@ RunAndCheck "$tesseract --version" "check TesseractOCR"
$python = GetPythonCMD
RunAndCheck "$python --version" "check Python"

$nvm = GetNVMCMD
RunAndCheck "$nvm --version" "check NVM"

$git = GetGitCMD
RunAndCheck "$git --version" "check Git"

Expand All @@ -321,6 +361,7 @@ Set-Location .\OpenAdapt
RunAndCheck "pip install poetry" "Run ``pip install poetry``"
RunAndCheck "poetry install" "Run ``poetry install``"
RunAndCheck "poetry run alembic upgrade head" "Run ``alembic upgrade head``" -SkipCleanup:$true
RunAndCheck "poetry run dashboard" "Install dashboard dependencies" -SkipCleanup:$true
RunAndCheck "poetry run pytest" "Run ``Pytest``" -SkipCleanup:$true
Write-Host "OpenAdapt installed Successfully!" -ForegroundColor Green
Start-Process powershell -Verb RunAs -ArgumentList "-NoExit", "-Command", "Set-Location -Path '$pwd'; poetry shell"
Expand Down
25 changes: 25 additions & 0 deletions install/install_openadapt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ set -x
pythonCmd="python3.10"
pythonVerStr="Python 3.10*"
pythonInstallerLoc="https://www.python.org/ftp/python/3.10.11/python-3.10.11-macos11.pkg"
nvmCmd="nvm"
nvmInstallerLoc="https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh"

# Set default values when no parameters are provided
BRANCH=${BRANCH:-main}
Expand Down Expand Up @@ -93,6 +95,27 @@ CheckPythonExists() {
exit 1
}

CheckNVMExists() {
if CheckCMDExists $nvmCmd; then
return
fi

# Install NVM
echo Installing NVM

curl -o- $nvmInstallerLoc | bash

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion

if ! CheckCMDExists $nvmCmd; then
echo "Failed to install NVM"
Cleanup
exit 1
fi
}

################################ INSTALLATION ################################

# Download brew
Expand Down Expand Up @@ -125,6 +148,7 @@ if ! CheckCMDExists "tesseract"; then
fi

CheckPythonExists
CheckNVMExists

[ -d "OpenAdapt" ] && mv OpenAdapt OpenAdapt-$(date +%Y-%m-%d_%H-%M-%S)
RunAndCheck "git clone $REPO_URL" "Clone git repo"
Expand All @@ -134,6 +158,7 @@ RunAndCheck "git checkout $BRANCH" "Checkout branch $BRANCH"
RunAndCheck "pip3.10 install poetry" "Install Poetry"
RunAndCheck "poetry install" "Install Python dependencies"
RunAndCheck "poetry run alembic upgrade head" "Update database"
RunAndCheck "poetry run dashboard" "Install dashboard dependencies"
RunAndCheck "poetry run pytest" "Run tests"
if [ -z "$SKIP_POETRY_SHELL" ]; then
RunAndCheck "poetry shell" "Activate virtual environment"
Expand Down
20 changes: 20 additions & 0 deletions openadapt/app/dashboard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Script to install dependencies needed for the dashboard."""

import os
import subprocess
import sys


def _run(bash_script: str) -> int:
return subprocess.call(bash_script, shell=True)


def entrypoint() -> None:
"""Entrypoint for the installation script."""
cwd = os.path.dirname(os.path.realpath(__file__))
os.chdir(cwd)

if sys.platform == "win32":
_run("powershell -File entrypoint.ps1")
return
_run("source ./entrypoint.sh")
6 changes: 2 additions & 4 deletions openadapt/app/dashboard/api/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
from fastapi import FastAPI

from openadapt.app.cards import is_recording, quick_record, stop_record
from openadapt.db.crud import get_all_recordings
from openadapt.db.db import Session
from openadapt.db import crud
from openadapt.models import Recording

app = FastAPI()
db = Session()


@app.get("/api/recordings", response_model=None)
def get_recordings() -> dict[str, list[Recording]]:
"""Get all recordings."""
recordings = get_all_recordings()
recordings = crud.get_all_recordings()
return {"recordings": recordings}


Expand Down
3 changes: 3 additions & 0 deletions openadapt/app/dashboard/entrypoint.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nvm install 21
nvm use 21
npm install
9 changes: 9 additions & 0 deletions openadapt/app/dashboard/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion

nvm install 21
nvm use
npm install
33 changes: 23 additions & 10 deletions openadapt/app/dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,30 @@ Promise.all([checkPort(DASHBOARD_CLIENT_PORT), checkPort(DASHBOARD_SERVER_PORT)]

function spawnChildProcess() {
// Spawn child process to run `npm run dev`
const childProcess = spawn('npm', ['run', 'dev'], { stdio: 'inherit' })
// wait for 1 second
setTimeout(() => {
import('open').then(({ default: open }) => {
open(`http://localhost:${DASHBOARD_CLIENT_PORT}`)
})
}, 3000)
let childProcess;

if (process.platform === 'win32') {
childProcess = spawn('npm', ['run', 'dev:windows'], { stdio: 'inherit', shell: true })
} else {
childProcess = spawn('npm', ['run', 'dev'], { stdio: 'inherit' })
}

childProcess.on('spawn', () => {
// wait for 3 seconds before opening the browser
setTimeout(() => {
import('open').then(({ default: open }) => {
open(`http://localhost:${DASHBOARD_CLIENT_PORT}`)
})
}, 3000)
})

// Handle SIGTERM signal
process.on('SIGTERM', () => {
childProcess.kill('SIGTERM')
// Handle SIG signals
const signals = ['SIGINT', 'SIGTERM', 'SIGQUIT', 'SIGHUP', 'SIGKILL']

signals.forEach(signal => {
process.on(signal, () => {
childProcess.kill(signal)
})
})

// Listen for child process error event
Expand Down
3 changes: 3 additions & 0 deletions openadapt/app/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
"private": true,
"scripts": {
"fastapi-dev": "python3 -m uvicorn api.index:app --port $DASHBOARD_SERVER_PORT --reload",
"fastapi-dev:windows": "python -m uvicorn api.index:app --port %DASHBOARD_SERVER_PORT% --reload",
"next-dev": "next dev -p $DASHBOARD_CLIENT_PORT",
"next-dev:windows": "next dev -p %DASHBOARD_CLIENT_PORT%",
"dev": "concurrently \"npm run next-dev\" \"npm run fastapi-dev\"",
"dev:windows": "concurrently \"npm run next-dev:windows\" \"npm run fastapi-dev:windows\"",
"build": "next build",
"start": "next start",
"lint": "next lint",
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ replay = "openadapt.replay:start"
app = "openadapt.app.tray:_run"
reset = "openadapt.scripts.reset_db:reset_db"
capture = "openadapt.capture:test"
dashboard = 'openadapt.app.dashboard:entrypoint'


[tool.black]
Expand Down

0 comments on commit 16f4d8b

Please sign in to comment.