mirror of
https://github.com/bourquep/mysa2mqtt.git
synced 2025-10-24 08:20:46 +00:00
build: Build and publish a proper CLI tool with options, also packaged as a Docker image (#2)
* Renamed environment variables * Moved MqttSettings to main.tsx * Using Commander for CLI arguments * PinoLogger * Option for json log format * Updated mysa-js-sdk to latest version * Moved options to their own module * Extracted session file management to the session module * Added deviceId meta to thermostat instance logger * Display version from package.json; added copyright * Create README.md * Build with tsup * Update .gitignore * Remove prepublishOnly npm script * Distributed CLI executable is now working * Update README.md * Dockerfile * Minify the build output * Update README.md * Create initial Github workflow * Create release.config.mjs * Read package version at run-time, not build-time * Update README.md * Create CONTRIBUTING.md * WIP: docker CI job * Trying multiple tags * Enable docker build cache * Testing the docker build cache * Dockerfile: set npm version in final stage for better caching * Testing docker build cache * Moved VERSION arg to the final build stage * Finalized the `docker` build job * Added copyright header to all source files * Specify radix when parsing integer options
This commit is contained in:
146
src/options.ts
Normal file
146
src/options.ts
Normal file
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
mysa2mqtt
|
||||
Copyright (C) 2025 Pascal Bourque
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
import { Command, InvalidArgumentError, Option } from 'commander';
|
||||
import { configDotenv } from 'dotenv';
|
||||
import { readFileSync } from 'fs';
|
||||
import { dirname, join } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
configDotenv({
|
||||
path: ['.env', '.env.local'],
|
||||
override: true
|
||||
});
|
||||
|
||||
/**
|
||||
* Gets the package version at runtime.
|
||||
*
|
||||
* @returns The package version or 'unknown' if it cannot be read.
|
||||
*/
|
||||
function getPackageVersion(): string {
|
||||
try {
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
const packageJsonPath = join(__dirname, '..', 'package.json');
|
||||
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
|
||||
return packageJson.version || 'unknown';
|
||||
} catch {
|
||||
return 'unknown';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a required integer value.
|
||||
*
|
||||
* @param value - The value to parse.
|
||||
* @returns The parsed integer value.
|
||||
* @throws InvalidArgumentError if the value is not a valid integer.
|
||||
*/
|
||||
function parseRequiredInt(value: string) {
|
||||
const parsedValue = parseInt(value, 10);
|
||||
if (isNaN(parsedValue)) {
|
||||
throw new InvalidArgumentError('Must be a number.');
|
||||
}
|
||||
return parsedValue;
|
||||
}
|
||||
|
||||
const extraHelpText = `
|
||||
Copyright (c) 2025 Pascal Bourque
|
||||
Licensed under the MIT License
|
||||
|
||||
Source code and documentation available at: https://github.com/bourquep/mysa2mqtt
|
||||
`;
|
||||
|
||||
export const options = new Command('mysa2mqtt')
|
||||
.version(getPackageVersion())
|
||||
.description('Expose Mysa smart thermostats to home automation platforms via MQTT.')
|
||||
.addHelpText('afterAll', extraHelpText)
|
||||
.addOption(
|
||||
new Option('-l, --log-level <logLevel>', 'log level')
|
||||
.choices(['silent', 'fatal', 'error', 'warn', 'info', 'debug', 'trace'])
|
||||
.env('M2M_LOG_LEVEL')
|
||||
.default('info')
|
||||
.helpGroup('Configuration')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-f, --log-format <logFormat>', 'log format')
|
||||
.choices(['pretty', 'json'])
|
||||
.env('M2M_LOG_FORMAT')
|
||||
.default('pretty')
|
||||
.helpGroup('Configuration')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-H, --mqtt-host <mqttHost>', 'hostname of the MQTT broker')
|
||||
.env('M2M_MQTT_HOST')
|
||||
.makeOptionMandatory()
|
||||
.helpGroup('MQTT')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-P, --mqtt-port <mqttPort>', 'port of the MQTT broker')
|
||||
.env('M2M_MQTT_PORT')
|
||||
.argParser(parseRequiredInt)
|
||||
.default(1883)
|
||||
.helpGroup('MQTT')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-U, --mqtt-username <mqttUsername>', 'username of the MQTT broker')
|
||||
.env('M2M_MQTT_USERNAME')
|
||||
.helpGroup('MQTT')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-B, --mqtt-password <mqttPassword>', 'password of the MQTT broker')
|
||||
.env('M2M_MQTT_PASSWORD')
|
||||
.helpGroup('MQTT')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-u, --mysa-username <mysaUsername>', 'Mysa account username')
|
||||
.env('M2M_MYSA_USERNAME')
|
||||
.makeOptionMandatory()
|
||||
.helpGroup('Mysa')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-p, --mysa-password <mysaPassword>', 'Mysa account password')
|
||||
.env('M2M_MYSA_PASSWORD')
|
||||
.makeOptionMandatory()
|
||||
.helpGroup('Mysa')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-s, --mysa-session-file <mysaSessionFile>', 'Mysa session file')
|
||||
.env('M2M_MYSA_SESSION_FILE')
|
||||
.default('session.json')
|
||||
.helpGroup('Configuration')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-N, --mqtt-client-name <mqttClientName>', 'name of the MQTT client')
|
||||
.env('M2M_MQTT_CLIENT_NAME')
|
||||
.default('mysa2mqtt')
|
||||
.helpGroup('MQTT')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-T, --mqtt-topic-prefix <mqttTopicPrefix>', 'prefix of the MQTT topic')
|
||||
.env('M2M_MQTT_TOPIC_PREFIX')
|
||||
.default('mysa2mqtt')
|
||||
.helpGroup('MQTT')
|
||||
)
|
||||
.parse()
|
||||
.opts();
|
||||
Reference in New Issue
Block a user