#!/bin/bash

# load NVM
export NVM_DIR="/root/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm

# Specify the target path
TARGET_PATH="/root/crawler"

# Check if the target path exists
if [ ! -d "$TARGET_PATH" ]; then
  echo "Error: Directory '$TARGET_PATH' does not exist."
  exit 1
fi

# Navigate to the specified directory
cd "$TARGET_PATH" || exit

# Check if package.json exists to ensure it's a Node.js project
if [ -f "package.json" ]; then
  # Run npm start
  echo "Running 'npm run start' in $TARGET_PATH"
  /root/.nvm/versions/node/v22.8.0/bin/npm run start
else
  echo "Error: 'package.json' not found in $TARGET_PATH."
  exit 1
fi