Skip to content

Commit

Permalink
feat: env-doctor script (#10078)
Browse files Browse the repository at this point in the history
_incidental_

## Description
Adds `yarn doctor` which calls `scripts/env-doctor.sh` to diagnose and repair common problems with the development environment.

I made this using [Aider](https://aider.chat):

```
Main model: claude-3-5-sonnet-20240620 with diff edit format, infinite output

# 2024-09-12 08:25:25.666744
+/add scripts/env-doctor.sh

# 2024-09-12 08:31:22.746300
+make env-doctor run a series of diagnostics on the development environment. each diagnostic has one remedy that is offered if its diagnostic test has an error exit code. each remedy is shown to the user and they are prompted to confirm whether to execute it. Each diagnostic and remedy has a description displayed to the user instead of the code. A user can reply "Yes" or"No", or "All" which answers Yes to the remaining questions. Each answer has an alias of its first letter (Y, N, A).

# 2024-09-12 08:35:00.044883
+add a check that `node --version` has the same version as in `.node-version`

# 2024-09-12 08:36:58.653105
+use fnm instead of nvm

# 2024-09-12 08:39:32.091136
+add a run_recommendation function that does not have a diagnostic and one of the recommendations is to run scripts/configure-vscode.sh to "Configure VS Code with recommended settings"

# 2024-09-12 08:45:13.008441
+add a last diagnostic "Build repo" that runs "yarn build". If that fails the remedy offered is to "git clean -fdx && yarn install && yarn build". If a remedy exits non-zero then alert the user to seek more help and exit the script.

# 2024-09-12 08:46:03.698422
+replace the NPM check with a check that `yarn --version` matches the version in the 'packageManager' field of the root package.json

# 2024-09-12 08:47:23.858173
+add a recommendation to run "yarn doctor" within a3p-integration
```

I wish Aider would include the chat message in the commit. paul-gauthier/aider@16856bb looks like it was supposed to. 

### Security Considerations
n/a

### Scaling Considerations
n/a

### Documentation Considerations
Maybe docs.agoric.com gets some advice to use `yarn doctor`. It could save some of the setup steps.

### Testing Considerations

Needs some testing on different machines. Though it doesn't have to be perfect. I expect it will get iterated upon whenever someone runs into a problem.

I've tried it a few times locally. Once I had `"build": "exit 1"` to test the build remedy.

### Upgrade Considerations
n/a
  • Loading branch information
mergify[bot] committed Sep 17, 2024
2 parents 3aa0690 + 7d9f90e commit 0c8c6c4
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"docs:markdown-for-agoric-documentation-repo": "run-s docs:markdown-build 'docs:update-functions-path md'",
"docs:markdown-build": "typedoc --plugin typedoc-plugin-markdown --tsconfig tsconfig.build.json",
"docs:update-functions-path": "node ./scripts/update-typedoc-functions-path.cjs",
"doctor": "./scripts/env-doctor.sh",
"lerna": "lerna",
"link-cli": "yarn run create-agoric-cli",
"create-agoric-cli": "node ./scripts/create-agoric-cli.cjs",
Expand Down
128 changes: 128 additions & 0 deletions scripts/env-doctor.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/bin/bash

set -e

# Check if running on macOS
if [[ "$(uname)" != "Darwin" ]]; then
echo "Error: This script only works on macOS."
echo "Your current operating system is: $(uname)"
exit 1
fi

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Function to run a diagnostic and its remedy
run_diagnostic() {
local description="$1"
local diagnostic="$2"
local remedy_description="$3"
local remedy="$4"

echo -e "${YELLOW}Diagnostic:${NC} $description"
if eval "$diagnostic"; then
echo -e "${GREEN}✓ Passed${NC}"
else
echo -e "${RED}✗ Failed${NC}"
echo -e "${YELLOW}Remedy:${NC} $remedy_description"
if [[ $AUTO_YES != "true" ]]; then
read -p "Do you want to apply this remedy? (Yes/No/All) " answer
case ${answer:0:1} in
y | Y) ;;
a | A) AUTO_YES=true ;;
*) return ;;
esac
fi
if ! eval "$remedy"; then
echo -e "${RED}Remedy failed. Please seek more help.${NC}"
exit 1
fi
echo "Remedy applied."
fi
echo
}

# Diagnostics and remedies
run_diagnostic \
"Check if fnm is installed" \
"command -v fnm >/dev/null 2>&1" \
"Install fnm" \
"brew install fnm"

run_diagnostic \
"Check if Node.js is installed" \
"command -v node >/dev/null 2>&1" \
"Install Node.js using fnm" \
"fnm install --lts && fnm use lts-latest"

run_diagnostic \
"Check if Node.js version matches .node-version" \
"[ \"$(node --version)\" = \"v$(cat .node-version)\" ]" \
"Install the correct Node.js version using fnm" \
"fnm install $(cat .node-version) && fnm use $(cat .node-version)"

run_diagnostic \
"Check if Yarn version matches package.json" \
"[ \"$(yarn --version)\" = \"$(node -p "require('./package.json').packageManager.split('@')[1]")\" ]" \
"Install correct Yarn version" \
"corepack enable && yarn set version $(node -p "require('./package.json').packageManager.split('@')[1]")"

run_diagnostic \
"Check if Git is installed" \
"command -v git >/dev/null 2>&1" \
"Install Git" \
"brew install git"

run_diagnostic \
"Check if VSCode is installed" \
"command -v code >/dev/null 2>&1" \
"Install Visual Studio Code" \
"brew install --cask visual-studio-code"

run_diagnostic \
"Check if Docker is installed" \
"command -v docker >/dev/null 2>&1" \
"Install Docker" \
"brew install --cask docker"

run_diagnostic \
"Build repo" \
"yarn build" \
"Clean, reinstall dependencies, and rebuild" \
"git clean -fdx "**/node_modules" "**/bundles" && yarn install && yarn build"

# Function to run a recommendation
run_recommendation() {
local description="$1"
local recommendation="$2"

echo -e "${BLUE}Recommendation:${NC} $description"
if [[ $AUTO_YES != "true" ]]; then
read -p "Do you want to apply this recommendation? (Yes/No/All) " answer
case ${answer:0:1} in
y | Y) ;;
a | A) AUTO_YES=true ;;
*) return ;;
esac
fi
eval "$recommendation"
echo "Recommendation applied."
echo
}

echo -e "${GREEN}Environment check complete.${NC}"

# Recommendations
run_recommendation \
"Configure VS Code with recommended settings" \
"./scripts/configure-vscode.sh"

run_recommendation \
"Run yarn doctor in a3p-integration" \
"cd a3p-integration && yarn doctor && cd .."

echo -e "${GREEN}All checks and recommendations complete.${NC}"

0 comments on commit 0c8c6c4

Please sign in to comment.