notifications: add firmware version check

This adds a firmware version check that shows an error message if the
connected hub is running an older Pybricks firmware version.

Issue: https://github.com/pybricks/support/issues/482
This commit is contained in:
David Lechner
2021-09-15 16:49:51 -05:00
committed by David Lechner
parent 094807ccb5
commit 788b77fda0
13 changed files with 106 additions and 14 deletions
+20
View File
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2021 The Pybricks Authors
import { pythonVersionToSemver } from './version';
describe('pythonVersionToSemver', () => {
test.each([
['v1.0.0', 'v1.0.0'],
['v1.0.0a1', 'v1.0.0-alpha.1'],
['v1.0.0b2', 'v1.0.0-beta.2'],
['v1.0.0c3', 'v1.0.0-candidate.3'],
['v1.0.0f4', 'v1.0.0-final.4'],
])('valid version %s', (version, expected) => {
expect(pythonVersionToSemver(version)).toBe(expected);
});
test('invalid version', () => {
expect(() => pythonVersionToSemver('not a version')).toThrow();
});
});
+25
View File
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2021 The Pybricks Authors
import * as semver from 'semver';
/**
* Converts a Python short version string (e.g. '1.0.0b1') to a valid semver
* string (e.g. 1.0.0-beta.1).
*
* @param version The Python version string.
* @returns A modified version string that is a valid semver.
*/
export function pythonVersionToSemver(version: string): string {
const newVersion = version
.replace('a', '-alpha.')
.replace('b', '-beta.')
.replace('c', '-candidate.')
.replace('f', '-final.');
if (!semver.valid(newVersion)) {
throw new Error('invalid version');
}
return newVersion;
}