Skip to content

Commit c8f0ae6

Browse files
authored
Remove compare-versions dependency (#1303)
This PR removes the external `compare-versions` dependency by implementing a custom version comparison function in the project's utils module. - Replaces the external `compare-versions` package with a custom implementation - Updates import statements to use the new local function - Removes the dependency from package.json Fix: #1301
1 parent 89b99e3 commit c8f0ae6

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed

index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const DistroUtils = require('./lib/distro.js');
1818
const RMWUtils = require('./lib/rmw.js');
1919
const { Clock, ROSClock } = require('./lib/clock.js');
2020
const ClockType = require('./lib/clock_type.js');
21-
const compareVersions = require('compare-versions');
21+
const { compareVersions } = require('./lib/utils.js');
2222
const Context = require('./lib/context.js');
2323
const debug = require('debug')('rclnodejs');
2424
const Duration = require('./lib/duration.js');
@@ -364,8 +364,7 @@ let rcl = {
364364

365365
const version = await getCurrentGeneratorVersion();
366366
const forced =
367-
version === null ||
368-
compareVersions.compare(version, generator.version(), '<');
367+
version === null || compareVersions(version, generator.version(), '<');
369368
if (forced) {
370369
debug(
371370
'The generator will begin to create JavaScript code from ROS IDL files...'

lib/utils.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,66 @@ function isClose(a, b, rtol = 1e-9, atol = 0.0) {
238238
return rtol >= relativeDiff;
239239
}
240240

241+
/**
242+
* Compare two semantic version strings.
243+
*
244+
* Supports version strings in the format: x.y.z or x.y.z.w
245+
* where x, y, z, w are integers.
246+
*
247+
* @param {string} version1 - First version string (e.g., '1.2.3')
248+
* @param {string} version2 - Second version string (e.g., '1.2.4')
249+
* @param {string} operator - Comparison operator: '<', '<=', '>', '>=', '==', '!='
250+
* @returns {boolean} Result of the comparison
251+
*
252+
* @example
253+
* compareVersions('1.2.3', '1.2.4', '<') // true
254+
* compareVersions('2.0.0', '1.9.9', '>') // true
255+
* compareVersions('1.2.3', '1.2.3', '==') // true
256+
* compareVersions('1.2.3', '1.2.3', '>=') // true
257+
*/
258+
function compareVersions(version1, version2, operator) {
259+
// Parse version strings into arrays of integers
260+
const v1Parts = version1.split('.').map((part) => parseInt(part, 10));
261+
const v2Parts = version2.split('.').map((part) => parseInt(part, 10));
262+
263+
// Pad arrays to same length with zeros
264+
const maxLength = Math.max(v1Parts.length, v2Parts.length);
265+
while (v1Parts.length < maxLength) v1Parts.push(0);
266+
while (v2Parts.length < maxLength) v2Parts.push(0);
267+
268+
// Compare each part
269+
let cmp = 0;
270+
for (let i = 0; i < maxLength; i++) {
271+
if (v1Parts[i] > v2Parts[i]) {
272+
cmp = 1;
273+
break;
274+
} else if (v1Parts[i] < v2Parts[i]) {
275+
cmp = -1;
276+
break;
277+
}
278+
}
279+
280+
// Apply operator
281+
switch (operator) {
282+
case '<':
283+
return cmp < 0;
284+
case '<=':
285+
return cmp <= 0;
286+
case '>':
287+
return cmp > 0;
288+
case '>=':
289+
return cmp >= 0;
290+
case '==':
291+
case '===':
292+
return cmp === 0;
293+
case '!=':
294+
case '!==':
295+
return cmp !== 0;
296+
default:
297+
throw new Error(`Invalid operator: ${operator}`);
298+
}
299+
}
300+
241301
module.exports = {
242302
// General utilities
243303
detectUbuntuCodename,
@@ -259,4 +319,6 @@ module.exports = {
259319
mkdirSync: ensureDirSync, // Alias for fs-extra compatibility
260320
removeSync,
261321
readJsonSync,
322+
323+
compareVersions,
262324
};

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
"@rclnodejs/ref-array-di": "^1.2.2",
7777
"@rclnodejs/ref-struct-di": "^1.1.1",
7878
"bindings": "^1.5.0",
79-
"compare-versions": "^6.1.1",
8079
"debug": "^4.4.0",
8180
"json-bigint": "^1.0.0",
8281
"node-addon-api": "^8.3.1",

rosidl_parser/rosidl_parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515
'use strict';
1616

17-
const compareVersions = require('compare-versions');
17+
const { compareVersions } = require('../lib/utils.js');
1818
const path = require('path');
1919
const execFile = require('child_process').execFile;
2020

2121
const pythonExecutable = require('./py_utils').getPythonExecutable('python3');
2222

2323
const contextSupportedVersion = '21.0.0.0';
2424
const currentVersion = process.version;
25-
const isContextSupported = compareVersions.compare(
25+
const isContextSupported = compareVersions(
2626
currentVersion.substring(1, currentVersion.length),
2727
contextSupportedVersion,
2828
'>='

0 commit comments

Comments
 (0)