Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use volta, add sync script #10

Merged
merged 5 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"ignorePaths": [],
"dictionaryDefinitions": [],
"dictionaries": [],
"words": ["hass"],
"words": ["hass", "bunx"],
"ignoreWords": [],
"import": []
}
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"unicorn",
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"sourceType": "module",
"project": ["tsconfig.json"]
Expand Down
33 changes: 22 additions & 11 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,36 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Bun
uses: oven-sh/setup-bun@v1
- name: Install package manager (from package.json)
run: |
corepack enable
corepack install

- name: Install deps
run: bun install --frozen-lockfile

- name: Formatting
run: bun run prettier --check .
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version-file: package.json
cache: 'yarn'

- name: Code style
run: bun run lint
- name: Install deps
run: yarn install --frozen-lockfile --immutable

- name: Tests
run: bun run coverage
run: yarn coverage

- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}

- name: Code styles
run: yarn lint

- name: Formatting
run: yarn prettier --check .

# - name: Typecheck
# run: yarn typecheck

- name: Build
run: bun run build
run: yarn build
33 changes: 22 additions & 11 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,36 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Bun
uses: oven-sh/setup-bun@v1
- name: Install package manager (from package.json)
run: |
corepack enable
corepack install

- name: Install deps
run: bun install --frozen-lockfile

- name: Formatting
run: bun run prettier --check .
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version-file: package.json
cache: 'yarn'

- name: Code style
run: bun run lint
- name: Install deps
run: yarn install --frozen-lockfile --immutable

- name: Tests
run: bun run coverage
run: yarn coverage

- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}

- name: Code styles
run: yarn lint

- name: Formatting
run: yarn prettier --check .

# - name: Typecheck
# run: yarn typecheck

- name: Build
run: bun run build
run: yarn build
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,8 @@ will interact with the HomeAssistant websocket to fulfill its automation goals.

These tools need to be installed on your machine:

- [Bun](https://bun.sh/) - TypeScript runtime and package manager
- [Docker desktop](https://www.docker.com/products/docker-desktop/) - For packaging the application

#### Alternatives

Instead of Bun, you may feel free to use Volta instead.

- [Volta](https://volta.sh/) - Autonomously manages Node and Yarn versions
- [Docker desktop](https://www.docker.com/products/docker-desktop/) - For packaging the application

### Clone

Expand All @@ -43,12 +37,12 @@ Change directory to the repository root:
cd automation-standalone
```

### Install dependencies
### Install

Install dependencies with Yarn:
Install dependencies using Yarn:

```bash
bun install
yarn
```

### Configure
Expand All @@ -59,24 +53,32 @@ Create a `.env` file from the `.env.dist` example file.
cp .env.dist .env
```

Then, configure each variable in `.env`.
Then, configure each variable in `.env` so that the application can connect to your HA instance.

## Usage

### Sync

Synchronize the latest DA packages and write types based on your HA instance

```bash
yarn sync
```

### Run

Run your automations locally

```bash
bun run dev
yarn dev
```

### Test

Run all your tests

```bash
bun run test
yarn test
```

## Publication
Expand All @@ -86,15 +88,15 @@ bun run test
Build the application

```bash
bun run build
yarn build
```

### Publish

Build and publish your application to a docker registry

```bash
bun run publish
yarn publish
```

### Deploy
Expand Down
Binary file removed bun.lockb
Binary file not shown.
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@
"type": "module",
"scripts": {
"dev": "bun --hot --watch src/main.ts",
"sync": "bun tools/scripts/upgrade-da-packages-and-write-types.ts",
"build": "docker build . -f tools/docker/Dockerfile -t automation-prod",
"build:dist": "bun build src/main.ts --target=bun --outfile=dist/server.js",
"start": "docker run --env-file .env automation-prod",
"test": "vitest",
"coverage": "vitest --coverage",
"lint": "eslint src/ --ext .js,.ts,.tsx,.mts --max-warnings 0",
"format": "prettier --write .",
"fix": "bun run format && bun run lint --fix",
"fix": "yarn format && yarn lint --fix",
"typecheck": "tsc --noEmit",
"types": "bun --env-file .env type-writer",
"prepare": "husky install || true"
},
"trustedDependencies": [
"husky"
],
"lint-staged": {
"*.@(sh|bash|zsh|fish)": [
"shellcheck",
Expand All @@ -37,17 +34,19 @@
"dependencies": {
"@digital-alchemy/core": "^0.3.11",
"@digital-alchemy/hass": "^0.3.14",
"@digital-alchemy/synapse": "^0.3.5"
"@digital-alchemy/synapse": "^0.3.5",
"dayjs": "^1.11.10"
},
"devDependencies": {
"@cspell/eslint-plugin": "^8.7.0",
"@digital-alchemy/type-writer": "^0.3.8",
"@types/async": "^3.2.24",
"@types/bun": "^1",
"@types/bun": "^1.1.0",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.7",
"@typescript-eslint/eslint-plugin": "7.6.0",
"@typescript-eslint/parser": "7.6.0",
"@vitest/coverage-v8": "^0.34.6",
"@vitest/coverage-v8": "^1.5.0",
"bun": "^1.1.4",
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
Expand All @@ -60,7 +59,7 @@
"eslint-plugin-sonarjs": "^0.25.1",
"eslint-plugin-sort-keys-fix": "^1.1.2",
"eslint-plugin-unicorn": "^52.0.0",
"eslint-plugin-vitest": "^0.3.25",
"eslint-plugin-vitest": "^0.5.3",
"husky": "^8.0.3",
"lint-staged": "^15.1.0",
"prettier": "^3.2.5",
Expand All @@ -73,5 +72,6 @@
"volta": {
"node": "20.12.2",
"yarn": "4.1.1"
}
},
"packageManager": "[email protected]"
}
73 changes: 40 additions & 33 deletions tools/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,62 +1,70 @@
###########################################################
## ##
# # Base ##
## Global variables ##
## ##
###########################################################

# see all versions at https://hub.docker.com/r/oven/bun/tags
ARG BUN_VERSION=1
ARG BUN_VERSION="1"

# Official Bun image
FROM oven/bun:${BUN_VERSION} as base

# Bun uses NODE_ENV for backward compatibility with Node
ENV NODE_ENV="production"

# Simplicity first
WORKDIR /app
# see all versions at https://hub.docker.com/r/bitnami/node/tags
ARG NODE_VERSION="20"

###########################################################
## ##
## BUILDER ##
## ##
###########################################################

FROM base as builder
#FROM bitnami/node:${NODE_VERSION} as builder
#FROM imbios/bun-node:latest-20-debian as builder

FROM bitnami/node:${NODE_VERSION} as builder

# Act as CI system: no interactive tty, no stdin/stdout, no watch processes
ENV CI="true"

# Copy only relevant files for dev and prod
COPY package.json bun.lockb /app/prod/
COPY package.json bun.lockb tsconfig.json vitest.config.ts .prettier* .eslint* .cspell.json /app/dev/
# Ensure we treat everything as production
ENV NODE_ENV="production"

# Pacakage management happens through Node and Yarn
RUN curl https://get.volta.sh | bash
ENV VOLTA_HOME "/root/.volta"
ENV PATH "$VOLTA_HOME/bin:$PATH"

# Copy source code
COPY src/ /app/dev/src/
# Copy only relevant files
COPY package.json yarn.lock .yarnrc.yml tsconfig.json vitest.config.ts .prettier* .eslint* .cspell.json /app/
COPY src/ /app/src/

# Build and pre-flight checks
RUN cd /app/dev \
&& bun install --frozen-lockfile \
# Note: `--watch false` is a workaround for https://github.com/vitest-dev/vitest/issues/1288
&& bun test --watch false \
&& bun run prettier --check . \
&& bun run lint \
RUN cd /app/ \
&& yarn install --immutable \
&& yarn test \
&& yarn prettier --check . \
&& yarn lint \
# Note: Can not do typecheck unless we pass in credentials during build time
# && bun run types \
# && bun run typecheck \
&& bun run build:dist

# Prod deps only
RUN cd /app/prod \
&& bun install --production --frozen-lockfile
# && yarn types \
# && yarn typecheck \
&& yarn build:dist \
&& du -sh node_modules \
&& yarn workspaces focus --production \
&& du -sh node_modules

###########################################################
## ##
## PRODUCTION ##
## ##
###########################################################

FROM base as prod

# Official Bun image
FROM oven/bun:${BUN_VERSION} as automation-prod

# Bun uses NODE_ENV for backward compatibility with Node
ENV NODE_ENV="production"

# Simplicity first
WORKDIR /app

# Open Container Initiative (OCI) labels
LABEL org.opencontainers.image.title="Automation Standalone" \
Expand All @@ -70,9 +78,8 @@ LABEL org.opencontainers.image.title="Automation Standalone" \
org.opencontainers.image.licenses="MIT"

# Copy the distributable files and production specific dependencies
COPY --from=builder /app/dev/dist .
COPY --from=builder /app/prod/package.json .
COPY --from=builder /app/prod/node_modules node_modules
COPY --from=builder /app/dist /app/package.json ./
COPY --from=builder /app/node_modules node_modules

# Run the app
USER bun
Expand Down
11 changes: 11 additions & 0 deletions tools/scripts/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { execSync, ExecSyncOptions } from 'child_process'

export function runCommandSyncWithTTY(command: string): void {
try {
// Using stdio: 'inherit' to forward stdin, stdout, and stderr
const options: ExecSyncOptions = { stdio: 'inherit' }
execSync(command, options)
} catch (error) {
console.error(`Error executing command: ${error}`)
}
}

Check warning on line 11 in tools/scripts/run.ts

View check run for this annotation

Codecov / codecov/patch

tools/scripts/run.ts#L2-L11

Added lines #L2 - L11 were not covered by tests
4 changes: 4 additions & 0 deletions tools/scripts/upgrade-da-packages-and-write-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { runCommandSyncWithTTY } from './run'

runCommandSyncWithTTY('yarn up "@digital-alchemy/*"')
runCommandSyncWithTTY('bunx type-writer')

Check warning on line 4 in tools/scripts/upgrade-da-packages-and-write-types.ts

View check run for this annotation

Codecov / codecov/patch

tools/scripts/upgrade-da-packages-and-write-types.ts#L2-L4

Added lines #L2 - L4 were not covered by tests
Loading