Skip to content

Commit cc1f0bc

Browse files
committed
Add AGENTS.md
Signed-off-by: Florian Hotze <dev@florianhotze.com>
1 parent 6fee6cb commit cc1f0bc

1 file changed

Lines changed: 148 additions & 0 deletions

File tree

AGENTS.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# AGENTS.md - openHAB JavaScript Library Development Guide
2+
3+
## Overview
4+
5+
This repository contains the openHAB JavaScript library (openhab-js), a high-level pure JavaScript API for interacting with openHAB Core's Java APIs.
6+
It is designed to be used in the openHAB JavaScript Scripting add-on, running in a GraalVM JavaScript environment.
7+
8+
**Key Resources:**
9+
10+
- [openHAB Core Javadoc](https://www.openhab.org/javadoc/latest/)
11+
- [openHAB JavaScript Scripting add-on](https://github.com/openhab/openhab-addons/tree/main/bundles/org.openhab.automation.jsscripting)
12+
- [GraalVM JavaScript](https://github.com/oracle/graaljs)
13+
14+
## Project Structure
15+
16+
The repository is organized as follows:
17+
18+
```text
19+
repo root folder
20+
├── src/ # Core library source code (CommonJS)
21+
│ ├── actions/ # Built-in actions and notification builders
22+
│ ├── items/ # Item management, persistence, semantics, metadata
23+
│ ├── rules/ # Rule, condition, trigger, and operation builders
24+
│ ├── index.js # Main entrypoint using lazy getters
25+
│ ├── time.js # Date/time utilities (wraps @js-joda/core)
26+
│ └── globals.d.ts # Compiler-facing global types
27+
├── test/ # Tests and mocks
28+
│ ├── openhab.mock.js # Java host environment stubs
29+
│ ├── *.spec.js # Jest test files
30+
│ └── *.test.js # Mocha test files (Legacy)
31+
├── build/ # Build, bundler, and type config
32+
│ ├── tsconfig.json # TypeScript declaration generation
33+
│ ├── webpack.config.js # Primary UMD module bundling
34+
│ └── @globals-webpack.config.js # Global namespace injection config
35+
├── dist/ # Built Webpack bundles for openHAB add-on
36+
├── types/ # Generated .d.ts declaration files
37+
└── docs/ # JSDoc output (deployed to GitHub Pages)
38+
```
39+
## Runtime Environment Constraints
40+
41+
`openhab-js` runs in the openHAB JavaScript Scripting add-on environment, which is powered by GraalVM JavaScript running inside a Java host.
42+
This environment has specific restrictions:
43+
44+
> [!WARNING]
45+
>
46+
> - **No Node.js core libraries**: Standard Node.js modules (such as `node:http`, `fs`, `path`, etc.) are **not** available. Only pure ECMAScript and bundled dependencies (like `@js-joda/core`) can be used.
47+
> - **Synchronous Execution Only**: The runtime environment is synchronous. Promises, `async`/`await` functions, and asynchronous timers (`setTimeout`, `setInterval`) are **not supported** and must not be used in the core code.
48+
49+
## General Build & Development Instructions
50+
51+
### Environment Setup
52+
53+
- The recommended Node.js version is defined in **[.nvmrc](file:///home/florianh/gitrepos/openhab-js/.nvmrc)**. It is recommended to use a version manager such as `nvm` (run `nvm use`).
54+
- Install dependencies using npm:
55+
56+
```bash
57+
npm install
58+
```
59+
60+
### Standard Development Scripts
61+
62+
- **Linting**:
63+
- Lint files in `src/` using ESLint (with JavaScript Standard Style):
64+
65+
```bash
66+
npm run lint
67+
```
68+
69+
- Auto-fix style issues:
70+
71+
```bash
72+
npm run lint:fix
73+
```
74+
75+
- **Testing**:
76+
- Run the full test suite (Mocha + Jest):
77+
78+
```bash
79+
npm test
80+
```
81+
82+
- **Bundling**:
83+
- Bundle the source with Webpack into the `dist/` directory:
84+
85+
```bash
86+
npm run webpack
87+
```
88+
89+
- **Type Declarations**:
90+
- Generate `.d.ts` type declarations under `types/`:
91+
92+
```bash
93+
npm run types
94+
```
95+
96+
- Verify the generated type definitions for syntax and compilation:
97+
98+
```bash
99+
npm run types:test
100+
```
101+
102+
- **Documentation**:
103+
- Build the HTML JSDoc documentation under `docs/`:
104+
105+
```bash
106+
npm run docs
107+
```
108+
109+
- **Full Production Build**:
110+
- Perform tests, bundle, generate types, and compile JSDoc:
111+
112+
```bash
113+
npm run build
114+
```
115+
116+
## Testing Guidelines
117+
118+
- **Write Jest tests**: All new unit tests should be written using **Jest** and placed in the **[test/](file:///home/florianh/gitrepos/openhab-js/test)** folder (or subfolders) with the extension `*.spec.js`.
119+
- **Legacy Mocha tests**: Legacy test files with the extension `*.test.js` run under Mocha. These are still present in the repository but will be migrated or removed in the future. Avoid adding new Mocha tests.
120+
- **Test Mocks**: Ensure that tests stub out Java-specific hosts (like `@runtime`) using the provided mock modules under the `test/` directory.
121+
122+
## Type Definition Architecture and Guidelines
123+
124+
This section documents the architecture and constraints for type definition generation and global type resolution in `openhab-js`.
125+
126+
### Separation of Compiler-Facing and Public-Facing Globals
127+
128+
There is a strict separation between the globals used during type generation (`npm run types`) and those exposed to public package consumers:
129+
130+
#### Compiler-Facing (`src/globals.d.ts`)
131+
132+
- **Purpose**: Used only by `tsc` when generating `.d.ts` files from the JS source.
133+
- **Location**: [src/globals.d.ts](file:///home/florianh/gitrepos/openhab-js/src/globals.d.ts)
134+
- **Configuration**: Included in [build/tsconfig.json](file:///home/florianh/gitrepos/openhab-js/build/tsconfig.json).
135+
- **Key Constraint**: Because JS files are CommonJS modules exporting constructor values, `src/globals.d.ts` must use `InstanceType<typeof import(...)>` to extract class instance types. Otherwise, the compiler will fail to compile.
136+
137+
#### Public-Facing (`types/openhab-js.d.ts`)
138+
139+
- **Purpose**: Used by external library consumers (and VS Code) when importing `openhab-js`.
140+
- **Location**: [types/openhab-js.d.ts](file:///home/florianh/gitrepos/openhab-js/types/openhab-js.d.ts)
141+
- **Configuration**: Exposed via `"types"` in `package.json`.
142+
- **Key Constraint**: Because the generated declaration files under `types/` are true TS modules exporting actual class/type definitions, this file **must not** use `InstanceType<typeof ...>` for classes. Instead, it must use direct type imports (e.g. `import('./items/items').Item`) to allow editor engines like VS Code to resolve class member autocompletion instantly.
143+
144+
### Preventing Output Overwrite Conflicts (`TS5055`)
145+
146+
- The `types/` output directory must **never** be included in the compilation input patterns/`include` array of [build/tsconfig.json](file:///home/florianh/gitrepos/openhab-js/build/tsconfig.json).
147+
- If a file in the input graph imports or references files under `types/` during build time, `tsc` loads them as compilation inputs. When `tsc` then attempts to output the generated declarations to `types/`, it raises `error TS5055: Cannot write file because it would overwrite input file`.
148+
- To avoid this, always keep `src/globals.d.ts` importing directly from the source `.js` files using relative paths within `src/` (e.g., `./items/items` instead of `../types/items/items`).

0 commit comments

Comments
 (0)