Skip to content

Commit 1b6ef99

Browse files
authored
feat: use volta, add sync script (#10)
* feat: use volta, add sync script * feat: add dayjs * chore: update ci * chore: support corepack * chore: skip typecheck in ci
1 parent 441d5a2 commit 1b6ef99

12 files changed

+216
-145
lines changed

.cspell.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"ignorePaths": [],
44
"dictionaryDefinitions": [],
55
"dictionaries": [],
6-
"words": ["hass"],
6+
"words": ["hass", "bunx"],
77
"ignoreWords": [],
88
"import": []
99
}

.eslintrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"unicorn",
1414
"prettier"
1515
],
16+
"parser": "@typescript-eslint/parser",
1617
"parserOptions": {
1718
"sourceType": "module",
1819
"project": ["tsconfig.json"]

.github/workflows/main.yaml

+22-11
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,36 @@ jobs:
1111
- name: Checkout
1212
uses: actions/checkout@v4
1313

14-
- name: Setup Bun
15-
uses: oven-sh/setup-bun@v1
14+
- name: Install package manager (from package.json)
15+
run: |
16+
corepack enable
17+
corepack install
1618
17-
- name: Install deps
18-
run: bun install --frozen-lockfile
19-
20-
- name: Formatting
21-
run: bun run prettier --check .
19+
- name: Setup Node
20+
uses: actions/setup-node@v4
21+
with:
22+
node-version-file: package.json
23+
cache: 'yarn'
2224

23-
- name: Code style
24-
run: bun run lint
25+
- name: Install deps
26+
run: yarn install --frozen-lockfile --immutable
2527

2628
- name: Tests
27-
run: bun run coverage
29+
run: yarn coverage
2830

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

36+
- name: Code styles
37+
run: yarn lint
38+
39+
- name: Formatting
40+
run: yarn prettier --check .
41+
42+
# - name: Typecheck
43+
# run: yarn typecheck
44+
3445
- name: Build
35-
run: bun run build
46+
run: yarn build

.github/workflows/pull-request.yaml

+22-11
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,36 @@ jobs:
1111
- name: Checkout
1212
uses: actions/checkout@v4
1313

14-
- name: Setup Bun
15-
uses: oven-sh/setup-bun@v1
14+
- name: Install package manager (from package.json)
15+
run: |
16+
corepack enable
17+
corepack install
1618
17-
- name: Install deps
18-
run: bun install --frozen-lockfile
19-
20-
- name: Formatting
21-
run: bun run prettier --check .
19+
- name: Setup Node
20+
uses: actions/setup-node@v4
21+
with:
22+
node-version-file: package.json
23+
cache: 'yarn'
2224

23-
- name: Code style
24-
run: bun run lint
25+
- name: Install deps
26+
run: yarn install --frozen-lockfile --immutable
2527

2628
- name: Tests
27-
run: bun run coverage
29+
run: yarn coverage
2830

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

36+
- name: Code styles
37+
run: yarn lint
38+
39+
- name: Formatting
40+
run: yarn prettier --check .
41+
42+
# - name: Typecheck
43+
# run: yarn typecheck
44+
3445
- name: Build
35-
run: bun run build
46+
run: yarn build

README.md

+17-15
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,8 @@ will interact with the HomeAssistant websocket to fulfill its automation goals.
1818

1919
These tools need to be installed on your machine:
2020

21-
- [Bun](https://bun.sh/) - TypeScript runtime and package manager
22-
- [Docker desktop](https://www.docker.com/products/docker-desktop/) - For packaging the application
23-
24-
#### Alternatives
25-
26-
Instead of Bun, you may feel free to use Volta instead.
27-
2821
- [Volta](https://volta.sh/) - Autonomously manages Node and Yarn versions
22+
- [Docker desktop](https://www.docker.com/products/docker-desktop/) - For packaging the application
2923

3024
### Clone
3125

@@ -43,12 +37,12 @@ Change directory to the repository root:
4337
cd automation-standalone
4438
```
4539

46-
### Install dependencies
40+
### Install
4741

48-
Install dependencies with Yarn:
42+
Install dependencies using Yarn:
4943

5044
```bash
51-
bun install
45+
yarn
5246
```
5347

5448
### Configure
@@ -59,24 +53,32 @@ Create a `.env` file from the `.env.dist` example file.
5953
cp .env.dist .env
6054
```
6155

62-
Then, configure each variable in `.env`.
56+
Then, configure each variable in `.env` so that the application can connect to your HA instance.
6357

6458
## Usage
6559

60+
### Sync
61+
62+
Synchronize the latest DA packages and write types based on your HA instance
63+
64+
```bash
65+
yarn sync
66+
```
67+
6668
### Run
6769

6870
Run your automations locally
6971

7072
```bash
71-
bun run dev
73+
yarn dev
7274
```
7375

7476
### Test
7577

7678
Run all your tests
7779

7880
```bash
79-
bun run test
81+
yarn test
8082
```
8183

8284
## Publication
@@ -86,15 +88,15 @@ bun run test
8688
Build the application
8789

8890
```bash
89-
bun run build
91+
yarn build
9092
```
9193

9294
### Publish
9395

9496
Build and publish your application to a docker registry
9597

9698
```bash
97-
bun run publish
99+
yarn publish
98100
```
99101

100102
### Deploy

bun.lockb

-260 KB
Binary file not shown.

package.json

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@
77
"type": "module",
88
"scripts": {
99
"dev": "bun --hot --watch src/main.ts",
10+
"sync": "bun tools/scripts/upgrade-da-packages-and-write-types.ts",
1011
"build": "docker build . -f tools/docker/Dockerfile -t automation-prod",
1112
"build:dist": "bun build src/main.ts --target=bun --outfile=dist/server.js",
1213
"start": "docker run --env-file .env automation-prod",
1314
"test": "vitest",
1415
"coverage": "vitest --coverage",
1516
"lint": "eslint src/ --ext .js,.ts,.tsx,.mts --max-warnings 0",
1617
"format": "prettier --write .",
17-
"fix": "bun run format && bun run lint --fix",
18+
"fix": "yarn format && yarn lint --fix",
1819
"typecheck": "tsc --noEmit",
19-
"types": "bun --env-file .env type-writer",
2020
"prepare": "husky install || true"
2121
},
22-
"trustedDependencies": [
23-
"husky"
24-
],
2522
"lint-staged": {
2623
"*.@(sh|bash|zsh|fish)": [
2724
"shellcheck",
@@ -37,17 +34,19 @@
3734
"dependencies": {
3835
"@digital-alchemy/core": "^0.3.11",
3936
"@digital-alchemy/hass": "^0.3.14",
40-
"@digital-alchemy/synapse": "^0.3.5"
37+
"@digital-alchemy/synapse": "^0.3.5",
38+
"dayjs": "^1.11.10"
4139
},
4240
"devDependencies": {
4341
"@cspell/eslint-plugin": "^8.7.0",
4442
"@digital-alchemy/type-writer": "^0.3.8",
4543
"@types/async": "^3.2.24",
46-
"@types/bun": "^1",
44+
"@types/bun": "^1.1.0",
4745
"@types/jest": "^29.5.12",
46+
"@types/node": "^20.12.7",
4847
"@typescript-eslint/eslint-plugin": "7.6.0",
4948
"@typescript-eslint/parser": "7.6.0",
50-
"@vitest/coverage-v8": "^0.34.6",
49+
"@vitest/coverage-v8": "^1.5.0",
5150
"bun": "^1.1.4",
5251
"eslint": "8.57.0",
5352
"eslint-config-prettier": "9.1.0",
@@ -60,7 +59,7 @@
6059
"eslint-plugin-sonarjs": "^0.25.1",
6160
"eslint-plugin-sort-keys-fix": "^1.1.2",
6261
"eslint-plugin-unicorn": "^52.0.0",
63-
"eslint-plugin-vitest": "^0.3.25",
62+
"eslint-plugin-vitest": "^0.5.3",
6463
"husky": "^8.0.3",
6564
"lint-staged": "^15.1.0",
6665
"prettier": "^3.2.5",
@@ -73,5 +72,6 @@
7372
"volta": {
7473
"node": "20.12.2",
7574
"yarn": "4.1.1"
76-
}
75+
},
76+
"packageManager": "[email protected]"
7777
}

tools/docker/Dockerfile

+40-33
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,70 @@
11
###########################################################
22
## ##
3-
# # Base ##
3+
## Global variables ##
44
## ##
55
###########################################################
66

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

10-
# Official Bun image
11-
FROM oven/bun:${BUN_VERSION} as base
12-
13-
# Bun uses NODE_ENV for backward compatibility with Node
14-
ENV NODE_ENV="production"
15-
16-
# Simplicity first
17-
WORKDIR /app
10+
# see all versions at https://hub.docker.com/r/bitnami/node/tags
11+
ARG NODE_VERSION="20"
1812

1913
###########################################################
2014
## ##
2115
## BUILDER ##
2216
## ##
2317
###########################################################
2418

25-
FROM base as builder
19+
#FROM bitnami/node:${NODE_VERSION} as builder
20+
#FROM imbios/bun-node:latest-20-debian as builder
21+
22+
FROM bitnami/node:${NODE_VERSION} as builder
2623

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

30-
# Copy only relevant files for dev and prod
31-
COPY package.json bun.lockb /app/prod/
32-
COPY package.json bun.lockb tsconfig.json vitest.config.ts .prettier* .eslint* .cspell.json /app/dev/
27+
# Ensure we treat everything as production
28+
ENV NODE_ENV="production"
29+
30+
# Pacakage management happens through Node and Yarn
31+
RUN curl https://get.volta.sh | bash
32+
ENV VOLTA_HOME "/root/.volta"
33+
ENV PATH "$VOLTA_HOME/bin:$PATH"
3334

34-
# Copy source code
35-
COPY src/ /app/dev/src/
35+
# Copy only relevant files
36+
COPY package.json yarn.lock .yarnrc.yml tsconfig.json vitest.config.ts .prettier* .eslint* .cspell.json /app/
37+
COPY src/ /app/src/
3638

3739
# Build and pre-flight checks
38-
RUN cd /app/dev \
39-
&& bun install --frozen-lockfile \
40-
# Note: `--watch false` is a workaround for https://github.com/vitest-dev/vitest/issues/1288
41-
&& bun test --watch false \
42-
&& bun run prettier --check . \
43-
&& bun run lint \
40+
RUN cd /app/ \
41+
&& yarn install --immutable \
42+
&& yarn test \
43+
&& yarn prettier --check . \
44+
&& yarn lint \
4445
# Note: Can not do typecheck unless we pass in credentials during build time
45-
# && bun run types \
46-
# && bun run typecheck \
47-
&& bun run build:dist
48-
49-
# Prod deps only
50-
RUN cd /app/prod \
51-
&& bun install --production --frozen-lockfile
46+
# && yarn types \
47+
# && yarn typecheck \
48+
&& yarn build:dist \
49+
&& du -sh node_modules \
50+
&& yarn workspaces focus --production \
51+
&& du -sh node_modules
5252

5353
###########################################################
5454
## ##
5555
## PRODUCTION ##
5656
## ##
5757
###########################################################
5858

59-
FROM base as prod
59+
60+
# Official Bun image
61+
FROM oven/bun:${BUN_VERSION} as automation-prod
62+
63+
# Bun uses NODE_ENV for backward compatibility with Node
64+
ENV NODE_ENV="production"
65+
66+
# Simplicity first
67+
WORKDIR /app
6068

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

7280
# Copy the distributable files and production specific dependencies
73-
COPY --from=builder /app/dev/dist .
74-
COPY --from=builder /app/prod/package.json .
75-
COPY --from=builder /app/prod/node_modules node_modules
81+
COPY --from=builder /app/dist /app/package.json ./
82+
COPY --from=builder /app/node_modules node_modules
7683

7784
# Run the app
7885
USER bun

tools/scripts/run.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { execSync, ExecSyncOptions } from 'child_process'
2+
3+
export function runCommandSyncWithTTY(command: string): void {
4+
try {
5+
// Using stdio: 'inherit' to forward stdin, stdout, and stderr
6+
const options: ExecSyncOptions = { stdio: 'inherit' }
7+
execSync(command, options)
8+
} catch (error) {
9+
console.error(`Error executing command: ${error}`)
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { runCommandSyncWithTTY } from './run'
2+
3+
runCommandSyncWithTTY('yarn up "@digital-alchemy/*"')
4+
runCommandSyncWithTTY('bunx type-writer')

0 commit comments

Comments
 (0)