Skip to content

Instantly share code, notes, and snippets.

@JanTristanH
Last active August 26, 2022 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JanTristanH/5982098b30ad8aae2eb9b33e161cfed4 to your computer and use it in GitHub Desktop.
Save JanTristanH/5982098b30ad8aae2eb9b33e161cfed4 to your computer and use it in GitHub Desktop.
// this script removes the @(restrict ..) from apipos-service.cds temporarily
const fs = require('fs');
const searchStr = ' @\(restrict'
const fileName = '../srv/apip-service.cds'
let fileContent = fs.readFileSync(__dirname + '/' + fileName, 'utf8');
fs.writeFileSync(__dirname + '/tmp-unmodified-api-service.cds', fileContent)
function findClosingBracketMatchIndex (str, pos) {
let openBracketCount = 0;
let givenBracketPosition = -1;
let index = -1;
for (let i = 0; i < str.length; i++) {
let char = str[i];
if (char === '(') {
openBracketCount++;
if (i === pos) {
givenBracketPosition = openBracketCount;
}
} else if (char === ')') {
if (openBracketCount === givenBracketPosition) {
index = i;
break;
}
openBracketCount--;
}
}
return index;
}
function insert(str, index, value) {
return str.substr(0, index) + value + str.substr(index);
}
while(fileContent.indexOf(searchStr) > 0){
let i = fileContent.indexOf(searchStr);
//insert */ after matching closing bracket
fileContent = insert(fileContent, findClosingBracketMatchIndex(fileContent, i + 2) + 1, '*/')
//insert /* at opening restrict
//add 1 to keep space before @
fileContent = insert(fileContent, i + 1, '/*')
}
//console.log(fileContent);
fs.writeFileSync(__dirname + '/' + fileName, fileContent)
// this script writes back the removed requires
const fs = require('fs');
const fileName = '../srv/api-service.cds'
let fileContent = fs.readFileSync(__dirname + '/tmp-unmodified-api-service.cds', 'utf8');
fs.writeFileSync(__dirname + '/' + fileName, fileContent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment