Skip to content

Commit c4a0b72

Browse files
Add comprehensive GitHub Copilot instructions for node-switchbot repository (#300)
This PR adds a comprehensive `.github/copilot-instructions.md` file that provides GitHub Copilot coding agents with detailed instructions on how to work effectively in the node-switchbot codebase. **Validated Commands and Timing**: Every command has been tested from a clean environment with actual timing measurements: - `npm install`: ~5-25 seconds (varies by cache) - `npm run build`: ~5 seconds (TypeScript compilation) - `npm run test`: ~1 second (12 tests) - `npm run lint`: ~3 seconds (ESLint validation) **Platform-Specific Requirements**: Clear documentation of constraints: - BLE functionality requires Linux-based OS only (Raspbian, Ubuntu, etc.) - Windows and macOS are NOT supported for BLE operations - Node.js versions: ^20, ^22, or ^24 required - ES Modules: Project uses `"type": "module"` **Development Workflow**: Step-by-step instructions for common tasks: - Bootstrap and setup procedures - Build, test, and lint validation cycles - Adding new device support - Working with both BLE and OpenAPI interfaces **Manual Validation Scenarios**: Specific tests to run after making changes: ```javascript // Basic functionality validation const { SwitchBotBLE, SwitchBotOpenAPI } = require('./dist/index.js'); const ble = new SwitchBotBLE(); // Should not throw const api = new SwitchBotOpenAPI('test', 'test'); // Should not throw ``` **Timeout Warnings**: Explicit "NEVER CANCEL" warnings with timeout recommendations for all build operations to prevent premature cancellation of long-running commands. The instructions include comprehensive information about: - Source code organization (`src/switchbot-ble.ts`, `src/switchbot-openapi.ts`, device classes) - Configuration files (TypeScript, ESLint, package.json) - Build output structure (`dist/` directory) - Documentation generation (TypeDoc) All instructions have been thoroughly validated by: - Following step-by-step procedures from a clean environment - Testing basic functionality scenarios - Verifying ES module imports work correctly - Confirming full development workflow chain - Measuring actual timing for all operations The instructions follow the required imperative tone ("Run [this command]", "Do not do [this]") and provide exhaustive guidance for GitHub Copilot agents to work effectively in this TypeScript Node.js library for controlling SwitchBot devices via Bluetooth Low Energy and OpenAPI. Fixes #299. <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs. --------- Update Workflow Co-Authored-By: copilot-swe-agent[bot] <[email protected]> Co-Authored-By: donavanbecker <[email protected]>
1 parent d3d9ad6 commit c4a0b72

File tree

197 files changed

+866
-737
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

197 files changed

+866
-737
lines changed

.github/copilot-instructions.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# Node-SwitchBot Development Instructions
2+
3+
Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the info here.
4+
5+
## Working Effectively
6+
7+
### Bootstrap and Setup
8+
- Install Node.js (^20 || ^22 || ^24): Use the existing version if available
9+
- Install dependencies: `npm install` -- takes ~5-25 seconds (varies by cache). **NEVER CANCEL**
10+
- Build the project: `npm run build` -- takes ~5 seconds. **NEVER CANCEL**
11+
- Run tests: `npm run test` -- takes ~1 second with 12 tests. **NEVER CANCEL**
12+
13+
### Development Workflow
14+
- **ALWAYS run these commands in order when starting work:**
15+
1. `npm install`
16+
2. `npm run build`
17+
3. `npm run test`
18+
4. `npm run lint`
19+
- **Build and validate EVERY change**: After any code modification, always run `npm run build && npm run test && npm run lint`
20+
- **NEVER skip linting**: Run `npm run lint` before committing or the CI (.github/workflows/build.yml) will fail
21+
- **Use lint:fix for automatic fixes**: `npm run lint:fix` to automatically fix ESLint issues
22+
23+
### Platform Requirements and Constraints
24+
- **BLE functionality requires Linux-based OS only** (Raspbian, Ubuntu, etc.)
25+
- **Windows and macOS are NOT supported** for BLE operations (use OpenAPI instead)
26+
- **Node.js versions**: Must use ^20, ^22, or ^24 (currently using v20.19.4)
27+
- **ES Modules**: This project uses `"type": "module"` - always use ES import/export syntax
28+
29+
### Testing and Validation
30+
- **Basic functionality test**: After any changes to core classes, run this validation:
31+
```javascript
32+
const { SwitchBotBLE, SwitchBotOpenAPI } = require('./dist/index.js');
33+
const ble = new SwitchBotBLE(); // Should not throw
34+
const api = new SwitchBotOpenAPI('test', 'test'); // Should not throw
35+
```
36+
- **Complete test suite**: `npm run test` (12 tests) -- should always pass before committing
37+
- **Test coverage**: `npm run test-coverage` to see coverage report (~15% coverage is normal)
38+
- **Documentation generation**: `npm run docs` generates TypeDoc documentation in ./docs/
39+
40+
### Build and Timing Expectations
41+
- **npm install**: ~5-25 seconds (varies by cache) -- **NEVER CANCEL**. Set timeout to 5+ minutes for safety
42+
- **npm run build**: ~5 seconds -- **NEVER CANCEL**. Set timeout to 2+ minutes for safety
43+
- **npm run test**: ~1 second (12 tests) -- **NEVER CANCEL**. Set timeout to 2+ minutes for safety
44+
- **npm run lint**: ~3 seconds -- **NEVER CANCEL**. Set timeout to 2+ minutes for safety
45+
- **npm run docs**: ~2 seconds -- **NEVER CANCEL**. Set timeout to 2+ minutes for safety
46+
47+
## Project Structure and Key Files
48+
49+
### Source Code Organization
50+
- **src/index.ts**: Main export file - exports SwitchBotBLE, SwitchBotOpenAPI, and device classes
51+
- **src/switchbot-ble.ts**: Bluetooth Low Energy interface for direct device control
52+
- **src/switchbot-openapi.ts**: HTTP API interface for cloud-based SwitchBot control
53+
- **src/device.ts**: Individual device classes (WoHand, WoCurtain, WoSmartLock, etc.)
54+
- **src/types/**: TypeScript type definitions for all device interfaces
55+
- **src/settings.ts**: Configuration constants and API URLs
56+
- **dist/**: Compiled JavaScript output (generated by `npm run build`)
57+
58+
### Configuration Files
59+
- **package.json**: Main project config - scripts, dependencies, ES module config
60+
- **tsconfig.json**: TypeScript compilation settings (target: ES2022, module: ES2022)
61+
- **eslint.config.js**: ESLint configuration using @antfu/eslint-config
62+
- **jest.config.js**: Test configuration (uses Vitest, not Jest)
63+
- **.gitignore**: Excludes dist/, node_modules/, coverage/, and build artifacts
64+
65+
### Documentation
66+
- **README.md**: Main project documentation and installation instructions
67+
- **BLE.md**: Comprehensive BLE usage documentation and device examples
68+
- **OpenAPI.md**: OpenAPI usage documentation and authentication setup
69+
- **CHANGELOG.md**: Version history and release notes
70+
- **docs/**: Generated TypeDoc API documentation (HTML format)
71+
72+
## Common Development Tasks
73+
74+
### Adding New Device Support
75+
- **Add device class**: Create new class in src/device.ts extending SwitchbotDevice
76+
- **Update exports**: Add export to src/index.ts
77+
- **Add type definitions**: Create types in src/types/ if needed
78+
- **Test basic instantiation**: Ensure the device class can be imported and instantiated
79+
- **Always run full build and test cycle**: `npm run build && npm run test && npm run lint`
80+
81+
### API Changes and Extensions
82+
- **OpenAPI changes**: Modify src/switchbot-openapi.ts
83+
- **BLE changes**: Modify src/switchbot-ble.ts
84+
- **Update type definitions**: Modify corresponding files in src/types/
85+
- **Always verify exports**: Check that new functionality is exported in src/index.ts
86+
- **Test import functionality**: Verify new exports can be imported correctly
87+
88+
### Working with Dependencies
89+
- **Noble (BLE)**: @stoprocent/noble for Bluetooth functionality - Linux only
90+
- **HTTP requests**: Uses undici for HTTP calls (not axios or fetch)
91+
- **Async operations**: Uses async-mutex for concurrency control
92+
- **Adding dependencies**: Use `npm install --save` for runtime deps, `--save-dev` for dev deps
93+
94+
## Validation and Quality Assurance
95+
96+
### Pre-commit Checklist
97+
1. **Build succeeds**: `npm run build` completes without errors
98+
2. **All tests pass**: `npm run test` shows all 12 tests passing
99+
3. **Linting passes**: `npm run lint` shows no errors
100+
4. **Documentation builds**: `npm run docs` generates without warnings
101+
5. **Basic import works**: Can import and instantiate main classes
102+
103+
### Manual Testing Scenarios
104+
- **SwitchBotBLE instantiation**: `new SwitchBotBLE()` should not throw errors
105+
- **SwitchBotOpenAPI instantiation**: `new SwitchBotOpenAPI('token', 'secret')` should not throw
106+
- **Module exports**: All exported classes should be importable from main package
107+
- **TypeScript compilation**: No TypeScript errors in dist/ output
108+
- **Documentation completeness**: Check that new public APIs appear in generated docs
109+
110+
### CI/CD Integration
111+
- **GitHub Actions**: Build runs on push to 'latest' branch and PRs
112+
- **External workflows**: Uses OpenWonderLabs/.github/.github/workflows/nodejs-build-and-test.yml
113+
- **Required checks**: Build, test, and lint must all pass
114+
- **Coverage reporting**: Test coverage is tracked and reported
115+
116+
## Troubleshooting Common Issues
117+
118+
### BLE Not Working
119+
- **Check OS**: BLE only works on Linux-based systems
120+
- **Install noble prerequisites**: May need additional system libraries for @stoprocent/noble
121+
- **Use OpenAPI instead**: For Windows/macOS development, use SwitchBotOpenAPI class
122+
123+
### Build Failures
124+
- **Check Node.js version**: Must be ^20, ^22, or ^24
125+
- **Clean and rebuild**: `npm run clean && npm install && npm run build`
126+
- **TypeScript errors**: Check tsconfig.json settings and type definitions
127+
128+
### Test Failures
129+
- **Run individual tests**: Use `npm run test:watch` for interactive testing
130+
- **Check imports**: Ensure all imports use correct ES module syntax
131+
- **Verify exports**: Check that src/index.ts exports all necessary components
132+
133+
### Linting Errors
134+
- **Auto-fix**: Use `npm run lint:fix` to automatically fix many issues
135+
- **ESLint config**: Review eslint.config.js for current rules
136+
- **Import sorting**: ESLint enforces specific import order - use lint:fix
137+
138+
## Frequently Referenced Information
139+
140+
### Package Scripts (from package.json)
141+
```bash
142+
npm run build # Clean and compile TypeScript
143+
npm run test # Run test suite with Vitest
144+
npm run lint # Run ESLint on src/**/*.ts
145+
npm run lint:fix # Auto-fix ESLint issues
146+
npm run docs # Generate TypeDoc documentation
147+
npm run clean # Remove dist/ directory
148+
npm run watch # Build and link for development
149+
```
150+
151+
### Main Exports (from src/index.ts)
152+
- **SwitchBotBLE**: Bluetooth interface class
153+
- **SwitchBotOpenAPI**: HTTP API interface class
154+
- **SwitchbotDevice**: Base device class
155+
- **Device classes**: WoHand, WoCurtain, WoSmartLock, etc.
156+
- **Type definitions**: All device status and response types
157+
158+
### Dependencies Summary
159+
- **@stoprocent/noble**: BLE functionality (Linux only)
160+
- **undici**: HTTP client for API requests
161+
- **async-mutex**: Concurrency control
162+
- **TypeScript**: Language and compilation
163+
- **Vitest**: Testing framework
164+
- **ESLint**: Code linting with @antfu/eslint-config
165+
- **TypeDoc**: API documentation generation

.github/workflows/beta-release.yml

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,20 @@ on:
77

88
jobs:
99
build_and_test:
10-
uses: OpenWonderLabs/.github/.github/workflows/nodejs-build-and-test.yml@latest
10+
uses: homebridge/.github/.github/workflows/nodejs-build-and-test.yml@latest
1111
with:
12-
enable_coverage: true
12+
enable_coverage: false
1313
secrets:
1414
token: ${{ secrets.GITHUB_TOKEN }}
15-
1615
lint:
1716
needs: build_and_test
18-
uses: OpenWonderLabs/.github/.github/workflows/eslint.yml@latest
17+
uses: homebridge/.github/.github/workflows/eslint.yml@latest
1918

2019
publish:
2120
needs: lint
22-
if: ${{ github.repository == 'OpenWonderLabs/node-switchbot' }}
2321
permissions:
2422
id-token: write
25-
uses: OpenWonderLabs/.github/.github/workflows/npm-publish-esm.yml@latest
23+
uses: homebridge/.github/.github/workflows/npm-publish-esm.yml@latest
2624
with:
2725
tag: 'beta'
2826
dynamically_adjust_version: true
@@ -33,24 +31,22 @@ jobs:
3331

3432
pre-release:
3533
needs: publish
36-
if: ${{ github.repository == 'OpenWonderLabs/node-switchbot' }}
37-
uses: OpenWonderLabs/.github/.github/workflows/pre-release.yml@latest
34+
uses: homebridge/.github/.github/workflows/pre-release.yml@latest
3835
with:
3936
npm_version: ${{ needs.publish.outputs.NPM_VERSION }}
4037
body: |
4138
**Beta Release**
4239
**Version**: v${{ needs.publish.outputs.NPM_VERSION }}
43-
[How To Test Beta Releases](https://github.com/OpenWonderLabs/homebridge-switchbot/wiki/Beta-Version)
40+
[How To Test Beta Releases](https://github.com/OpenWonderLabs/homebridge-air/wiki/Beta-Version)
4441
4542
github-releases-to-discord:
4643
name: Discord Webhooks
4744
needs: [build_and_test,publish]
48-
if: ${{ github.repository == 'OpenWonderLabs/node-switchbot' }}
49-
uses: OpenWonderLabs/.github/.github/workflows/discord-webhooks.yml@latest
45+
uses: homebridge/.github/.github/workflows/discord-webhooks.yml@latest
5046
with:
51-
title: "Node-SwitchBot Beta Release"
47+
title: "Node-SwitchBot Module Beta Release"
5248
description: |
5349
Version `v${{ needs.publish.outputs.NPM_VERSION }}`
54-
url: "https://github.com/homebridge/camera-utils/releases/tag/v${{ needs.publish.outputs.NPM_VERSION }}"
50+
url: "https://github.com/OpenWonderLabs/homebridge-air/releases/tag/v${{ needs.publish.outputs.NPM_VERSION }}"
5551
secrets:
56-
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_URL_BETA || secrets.DISCORD_WEBHOOK_URL_LATEST }}
52+
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_URL_LATEST }}

.github/workflows/build.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

.github/workflows/changerelease.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

.github/workflows/dependabot.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

.github/workflows/labeler.yml

Lines changed: 0 additions & 16 deletions
This file was deleted.

.github/workflows/release-drafter.yml

Lines changed: 0 additions & 14 deletions
This file was deleted.

.github/workflows/release.yml

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,75 @@
1-
name: Release
1+
name: Unified Release
22

33
on:
4-
release:
5-
types: [published]
4+
push:
5+
branches:
6+
#- "alpha-*"
7+
#- "beta-*"
8+
- latest
9+
workflow_dispatch:
610

711
jobs:
8-
build_and_test:
9-
uses: OpenWonderLabs/.github/.github/workflows/nodejs-build-and-test.yml@latest
12+
# 1️⃣ Determine release type, ESM status, and branch name
13+
determine-release-type:
14+
uses: homebridge/.github/.github/workflows/determine-release-type.yml@latest
1015
with:
11-
enable_coverage: true
16+
ref_name: ${{ github.ref_name }}
17+
18+
# 2️⃣ Update version and changelog using the scripts
19+
update-version:
20+
needs: determine-release-type
21+
uses: homebridge/.github/.github/workflows/update-version.yml@latest
22+
with:
23+
release_type: ${{ needs.determine-release-type.outputs.release_type }}
24+
is_esm: ${{ needs.determine-release-type.outputs.is_esm == 'true' }}
1225
secrets:
13-
token: ${{ secrets.GITHUB_TOKEN }}
26+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
1427

15-
publish:
16-
needs: build_and_test
17-
if: ${{ github.repository == 'OpenWonderLabs/node-switchbot' }}
28+
# 3️⃣ Publish to NPM and create GitHub release
29+
publish-release:
30+
needs: [determine-release-type, update-version]
1831
permissions:
1932
id-token: write
20-
uses: OpenWonderLabs/.github/.github/workflows/npm-publish-esm.yml@latest
33+
contents: write
34+
uses: homebridge/.github/.github/workflows/publish-release.yml@latest
35+
with:
36+
release_type: ${{ needs.determine-release-type.outputs.release_type }}
37+
version: ${{ needs.update-version.outputs.version }}
38+
is_esm: ${{ needs.determine-release-type.outputs.is_esm == 'true' }}
39+
secrets:
40+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
41+
42+
# 4️⃣ Promote branch if this is a prerelease (alpha/beta)
43+
promote-branch:
44+
needs: [determine-release-type, publish-release]
45+
if: ${{ needs.determine-release-type.outputs.release_type != 'latest' && needs.determine-release-type.outputs.release_type != 'skip' }}
46+
uses: homebridge/.github/.github/workflows/promote-branch.yml@latest
47+
with:
48+
branch_name: ${{ needs.determine-release-type.outputs.branch_name }}
49+
release_type: ${{ needs.determine-release-type.outputs.release_type }}
50+
is_esm: ${{ needs.determine-release-type.outputs.is_esm == 'true' }}
2151
secrets:
22-
npm_auth_token: ${{ secrets.npm_token }}
52+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
53+
54+
# 5️⃣ Notify if any previous job fails
55+
workflow-failure:
56+
if: ${{ failure() }}
57+
needs: [determine-release-type, update-version, publish-release, promote-branch]
58+
uses: homebridge/.github/.github/workflows/report-failure.yml@latest
59+
with:
60+
workflow_name: ${{ github.workflow }}
61+
job_name: ${{ github.job }}
62+
run_url: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
2363

64+
# 6️⃣ Post to Discord
2465
github-releases-to-discord:
2566
name: Discord Webhooks
26-
needs: [build_and_test,publish]
27-
if: ${{ github.repository == 'OpenWonderLabs/node-switchbot' }}
28-
uses: OpenWonderLabs/.github/.github/workflows/discord-webhooks.yml@latest
67+
needs: [determine-release-type, update-version, publish-release]
68+
uses: homebridge/.github/.github/workflows/discord-webhooks.yml@latest
2969
with:
30-
title: "Node-SwitchBot Release"
70+
title: "Node-SwitchBot Module Release"
3171
description: |
32-
Version `v${{ needs.publish.outputs.NPM_VERSION }}`
33-
url: "https://github.com/homebridge/camera-utils/releases/tag/v${{ needs.publish.outputs.NPM_VERSION }}"
72+
Version `v${{ needs.update-version.outputs.version }}`
73+
url: "https://github.com/OpenWonderLabs/node-switchbot/releases/tag/v${{ needs.update-version.outputs.version }}"
3474
secrets:
3575
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_URL_LATEST }}

0 commit comments

Comments
 (0)