• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RobotWebTools / rclnodejs / #958

06 Mar 2025 04:45AM UTC coverage: 85.075% (-5.4%) from 90.471%
#958

push

web-flow
Update .npmignore (#1062)

This patch updates `.npmignore` to exclude files not needed in the npm package.

Fix: #1061

706 of 920 branches covered (76.74%)

Branch coverage included in aggregate %.

1728 of 1941 relevant lines covered (89.03%)

343.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.04
/lib/interface_loader.js
1
// Copyright (c) 2017 Intel Corporation. All rights reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
'use strict';
16

17
const path = require('path');
26✔
18
const fs = require('fs');
26✔
19
const generator = require('../rosidl_gen/index.js');
26✔
20

21
let interfaceLoader = {
26✔
22
  loadInterfaceByObject(obj) {
23
    //
24
    // `obj` param structure
25
    //
26
    // {
27
    //   package: 'std_msgs',
28
    //   type: 'msg',
29
    //   name: 'String',
30
    // }
31
    //
32
    if (
704✔
33
      typeof obj !== 'object' ||
3,464✔
34
      !obj ||
35
      !obj.package ||
36
      !obj.type ||
37
      !obj.name
38
    ) {
39
      throw new TypeError(
24✔
40
        'Should provide an object argument to get ROS message class'
41
      );
42
    }
43
    return this.loadInterface(obj.package, obj.type, obj.name);
680✔
44
  },
45

46
  loadInterfaceByString(name) {
47
    if (typeof name !== 'string') {
6,624!
48
      throw new TypeError(
×
49
        'Should provide a string argument to get ROS message class'
50
      );
51
    }
52

53
    // TODO(Kenny): more checks of the string argument
54
    if (name.indexOf('/') !== -1) {
6,624✔
55
      let [packageName, type, messageName] = name.split('/');
6,572✔
56
      return this.loadInterface(packageName, type, messageName);
6,572✔
57
    }
58

59
    // Suppose the name is a package, and traverse the path to collect the IDL files.
60
    let packagePath = path.join(generator.generatedRoot, name);
52✔
61

62
    let interfaces = fs.readdirSync(packagePath);
52✔
63
    if (interfaces.length > 0) {
52!
64
      return this.loadInterfaceByPath(packagePath, interfaces);
52✔
65
    }
66

67
    throw new TypeError(
×
68
      'A string argument in expected in "package/type/message" format'
69
    );
70
  },
71

72
  loadInterfaceByPath(packagePath, interfaces) {
73
    let interfaceInfos = [];
52✔
74

75
    interfaces.forEach((file) => {
52✔
76
      let results = file.match(/\w+__(\w+)__(\w+).js$/);
1,504✔
77
      let type = results[1];
1,504✔
78
      let name = results[2];
1,504✔
79
      let filePath = path.join(packagePath, file).normalize();
1,504✔
80
      interfaceInfos.push({ name, type, filePath });
1,504✔
81
    });
82

83
    let pkg = { srv: {}, msg: {}, action: {} };
52✔
84
    interfaceInfos.forEach((info) => {
52✔
85
      Object.defineProperty(pkg[info.type], info.name, {
1,504✔
86
        value: require(info.filePath),
87
      });
88
    });
89

90
    return pkg;
52✔
91
  },
92

93
  loadInterface(packageName, type, messageName) {
94
    if (arguments.length === 1) {
14,580✔
95
      const type = arguments[0];
7,328✔
96
      if (typeof type === 'object') {
7,328✔
97
        return this.loadInterfaceByObject(type);
704✔
98
      } else if (typeof type === 'string') {
6,624!
99
        return this.loadInterfaceByString(type);
6,624✔
100
      }
101
      throw new Error(`The message required does not exist: ${type}`);
×
102
    }
103

104
    if (packageName && type && messageName) {
7,252✔
105
      let filePath = path.join(
7,240✔
106
        generator.generatedRoot,
107
        packageName,
108
        packageName + '__' + type + '__' + messageName + '.js'
109
      );
110

111
      if (fs.existsSync(filePath)) {
7,240✔
112
        return require(filePath);
7,224✔
113
      }
114
    }
115
    throw new Error(
28✔
116
      `The message required does not exist: ${packageName}, ${type}, ${messageName} at ${generator.generatedRoot}`
117
    );
118
  },
119
};
120

121
module.exports = interfaceLoader;
26✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc