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.
- Install Node.js (^20 || ^22 || ^24): Use the existing version if available
- Install dependencies:
npm install-- takes ~5-25 seconds (varies by cache). NEVER CANCEL - Build the project:
npm run build-- takes ~5 seconds. NEVER CANCEL - Run tests:
npm run test-- takes ~1 second with 12 tests. NEVER CANCEL
- ALWAYS run these commands in order when starting work:
npm installnpm run buildnpm run testnpm run lint
- Build and validate EVERY change: After any code modification, always run
npm run build && npm run test && npm run lint - NEVER skip linting: Run
npm run lintbefore committing or the CI (.github/workflows/build.yml) will fail - Use lint:fix for automatic fixes:
npm run lint:fixto automatically fix ESLint issues
- BLE functionality requires Linux-based OS only (Raspbian, Ubuntu, etc.)
- Windows and macOS are NOT supported for BLE operations (use OpenAPI instead)
- Node.js versions: Must use ^20, ^22, or ^24 (currently using v20.19.4)
- ES Modules: This project uses
"type": "module"- always use ES import/export syntax
- Basic functionality test: After any changes to core classes, run this validation:
const { SwitchBotBLE, SwitchBotOpenAPI } = require('./dist/index.js'); const ble = new SwitchBotBLE(); // Should not throw const api = new SwitchBotOpenAPI('test', 'test'); // Should not throw
- Complete test suite:
npm run test(12 tests) -- should always pass before committing - Test coverage:
npm run test-coverageto see coverage report (~15% coverage is normal) - Documentation generation:
npm run docsgenerates TypeDoc documentation in ./docs/
- npm install: ~5-25 seconds (varies by cache) -- NEVER CANCEL. Set timeout to 5+ minutes for safety
- npm run build: ~5 seconds -- NEVER CANCEL. Set timeout to 2+ minutes for safety
- npm run test: ~1 second (12 tests) -- NEVER CANCEL. Set timeout to 2+ minutes for safety
- npm run lint: ~3 seconds -- NEVER CANCEL. Set timeout to 2+ minutes for safety
- npm run docs: ~2 seconds -- NEVER CANCEL. Set timeout to 2+ minutes for safety
- src/index.ts: Main export file - exports SwitchBotBLE, SwitchBotOpenAPI, and device classes
- src/switchbot-ble.ts: Bluetooth Low Energy interface for direct device control
- src/switchbot-openapi.ts: HTTP API interface for cloud-based SwitchBot control
- src/device.ts: Individual device classes (WoHand, WoCurtain, WoSmartLock, etc.)
- src/types/: TypeScript type definitions for all device interfaces
- src/settings.ts: Configuration constants and API URLs
- dist/: Compiled JavaScript output (generated by
npm run build)
- package.json: Main project config - scripts, dependencies, ES module config
- tsconfig.json: TypeScript compilation settings (target: ES2022, module: ES2022)
- eslint.config.js: ESLint configuration using @antfu/eslint-config
- jest.config.js: Test configuration (uses Vitest, not Jest)
- .gitignore: Excludes dist/, node_modules/, coverage/, and build artifacts
- README.md: Main project documentation and installation instructions
- BLE.md: Comprehensive BLE usage documentation and device examples
- OpenAPI.md: OpenAPI usage documentation and authentication setup
- CHANGELOG.md: Version history and release notes
- docs/: Generated TypeDoc API documentation (HTML format)
- Add device class: Create new class in src/device.ts extending SwitchbotDevice
- Update exports: Add export to src/index.ts
- Add type definitions: Create types in src/types/ if needed
- Test basic instantiation: Ensure the device class can be imported and instantiated
- Always run full build and test cycle:
npm run build && npm run test && npm run lint
- OpenAPI changes: Modify src/switchbot-openapi.ts
- BLE changes: Modify src/switchbot-ble.ts
- Update type definitions: Modify corresponding files in src/types/
- Always verify exports: Check that new functionality is exported in src/index.ts
- Test import functionality: Verify new exports can be imported correctly
- Noble (BLE): @stoprocent/noble for Bluetooth functionality - Linux only
- HTTP requests: Uses undici for HTTP calls (not axios or fetch)
- Async operations: Uses async-mutex for concurrency control
- Adding dependencies: Use
npm install --savefor runtime deps,--save-devfor dev deps
- Build succeeds:
npm run buildcompletes without errors - All tests pass:
npm run testshows all 12 tests passing - Linting passes:
npm run lintshows no errors - Documentation builds:
npm run docsgenerates without warnings - Basic import works: Can import and instantiate main classes
- SwitchBotBLE instantiation:
new SwitchBotBLE()should not throw errors - SwitchBotOpenAPI instantiation:
new SwitchBotOpenAPI('token', 'secret')should not throw - Module exports: All exported classes should be importable from main package
- TypeScript compilation: No TypeScript errors in dist/ output
- Documentation completeness: Check that new public APIs appear in generated docs
- GitHub Actions: Build runs on push to 'latest' branch and PRs
- External workflows: Uses OpenWonderLabs/.github/.github/workflows/nodejs-build-and-test.yml
- Required checks: Build, test, and lint must all pass
- Coverage reporting: Test coverage is tracked and reported
- Check OS: BLE only works on Linux-based systems
- Install noble prerequisites: May need additional system libraries for @stoprocent/noble
- Use OpenAPI instead: For Windows/macOS development, use SwitchBotOpenAPI class
- Check Node.js version: Must be ^20, ^22, or ^24
- Clean and rebuild:
npm run clean && npm install && npm run build - TypeScript errors: Check tsconfig.json settings and type definitions
- Run individual tests: Use
npm run test:watchfor interactive testing - Check imports: Ensure all imports use correct ES module syntax
- Verify exports: Check that src/index.ts exports all necessary components
- Auto-fix: Use
npm run lint:fixto automatically fix many issues - ESLint config: Review eslint.config.js for current rules
- Import sorting: ESLint enforces specific import order - use lint:fix
npm run build # Clean and compile TypeScript
npm run test # Run test suite with Vitest
npm run lint # Run ESLint on src/**/*.ts
npm run lint:fix # Auto-fix ESLint issues
npm run docs # Generate TypeDoc documentation
npm run clean # Remove dist/ directory
npm run watch # Build and link for development- SwitchBotBLE: Bluetooth interface class
- SwitchBotOpenAPI: HTTP API interface class
- SwitchbotDevice: Base device class
- Device classes: WoHand, WoCurtain, WoSmartLock, etc.
- Type definitions: All device status and response types
- @stoprocent/noble: BLE functionality (Linux only)
- undici: HTTP client for API requests
- async-mutex: Concurrency control
- TypeScript: Language and compilation
- Vitest: Testing framework
- ESLint: Code linting with @antfu/eslint-config
- TypeDoc: API documentation generation