Skip to content

Commit

Permalink
fiX: rclnodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
dnikitop committed Oct 17, 2024
1 parent 21b0694 commit 702a9fe
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions rostsd_gen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function generateAll() {

// write interfaces.d.ts file
const interfacesFilePath = path.join(__dirname, '../types/interfaces.d.ts');
const classesFilePath = path.join(__dirname, '../types/index.js');
const classesFilePath = path.join(__dirname, '../types/interfaces.js');
const fd = fs.openSync(interfacesFilePath, 'w');
const jfd = fs.openSync(classesFilePath, 'w');
savePkgInfoAsTSD(pkgInfos, fd, jfd);
Expand Down Expand Up @@ -97,6 +97,7 @@ function savePkgInfoAsTSD(pkgInfos, fd, jfd) {
};
const servicesMap = {};
const actionsMap = {};
const constMap = {}

fs.writeSync(fd, '/* eslint-disable camelcase */\n');
fs.writeSync(fd, '/* eslint-disable max-len */\n');
Expand Down Expand Up @@ -124,6 +125,17 @@ function savePkgInfoAsTSD(pkgInfos, fd, jfd) {
// create message interface
saveMsgAsTSD(rosInterface, fd);
saveMsgConstructorAsTSD(rosInterface, fd, jfd);
if(rosInterface.ROSMessageDef.constants.length > 0){
if(!constMap[type.pkgName]){
constMap[type.pkgName] = {}
}
if(!constMap[type.pkgName][type.subFolder]){
constMap[type.pkgName][type.subFolder] = []
}
if(!constMap[type.pkgName][type.subFolder].includes(type.interfaceName)){
constMap[type.pkgName][type.subFolder].push(type.interfaceName)
}
}
messagesMap[fullInterfaceName] = fullInterfacePath;
} else if (isSrvInterface(rosInterface)) {
if (
Expand Down Expand Up @@ -165,6 +177,19 @@ function savePkgInfoAsTSD(pkgInfos, fd, jfd) {
}

fs.writeSync(jfd, 'module.exports = {\n')
Object.keys(constMap).forEach(pkgName => {
fs.writeSync(jfd, ` ${pkgName}: {\n`);
Object.keys(constMap[pkgName]).forEach(subFolder => {
fs.writeSync(jfd, ` ${subFolder}: {\n`);
for (const interfaceName of constMap[pkgName][subFolder]){
fs.writeSync(jfd, ` ${interfaceName}Constants: ${pkgName}_${subFolder}_${interfaceName}Constants,\n`);
}
fs.writeSync(jfd, ` },\n`);
})
fs.writeSync(jfd, ` },\n`);
})
fs.writeSync(jfd, `};\n`);


// write messages type mappings
fs.writeSync(fd, ' type MessagesMap = {\n');
Expand Down Expand Up @@ -306,20 +331,24 @@ function saveMsgFieldsAsTSD(

function saveMsgConstructorAsTSD(rosMsgInterface, fd, jfd) {
const type = rosMsgInterface.type();
const pkgName = type.pkgName;
const subFolder = type.subFolder;
const msgName = type.interfaceName;

if(rosMsgInterface.ROSMessageDef.constants.length > 0){
fs.writeSync(fd, ` export enum ${msgName}Constants {\n`);
fs.writeSync(jfd,`class ${msgName}Constants {}\n`)
fs.writeSync(jfd,`class ${pkgName}_${subFolder}_${msgName}Constants {}\n`)
for (const constant of rosMsgInterface.ROSMessageDef.constants) {
if(primitiveType2JSName(constant.type) === "string"){
fs.writeSync(fd, ` ${constant.name} = "${constant.value}",\n`);
fs.writeSync(jfd,` ${msgName}Constants.${constant.name} = ${constant.value}\n`)
fs.writeSync(jfd,`${pkgName}_${subFolder}_${msgName}Constants.${constant.name} = "${constant.value}"\n`)
} else {
fs.writeSync(fd, ` ${constant.name} = ${constant.value},\n`);
fs.writeSync(jfd,`${pkgName}_${subFolder}_${msgName}Constants.${constant.name} = ${constant.value}\n`)
}
}
fs.writeSync(fd, ' }\n');
fs.writeSync(jfd, '\n');
}


Expand Down Expand Up @@ -618,3 +647,5 @@ const tsdGenerator = {
};

module.exports = tsdGenerator;

generateAll()

0 comments on commit 702a9fe

Please sign in to comment.