1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 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 16const fs = require('fs'); 17var peg = require('pegjs'); 18 19function usageAndExit() { 20 console.error(` 21State Management Library 22================ 23 24Comments out stateMgmtProfiler.[start, stop] calls. 25 26Usage: 27Disable all stateMgmtProfiler.[start, stop] from a file. 28node disableProfiler.js <filePath> 29 30WARNING: 31Original file is overwritten. 32`); 33 process.exit(-1); 34} 35 36// load parser sources and compile them 37// returns the parser object 38function makeParser() { 39 let pegjsSources = ''; 40 try { 41 pegjsSources = fs.readFileSync('disableProfiler.pegjs') + '\n'; 42 } catch (err) { 43 console.error("While trying to read: 'disableProfiler.pegjs': " + err); 44 process.exit(-1); 45 } 46 console.log(` Read parser sources 'disableProfiler.pegjs': ${pegjsSources.length} Bytes.`) 47 48 // generate the parser from parser.pegjs source file: 49 var parser; 50 try { 51 parser = peg.generate(pegjsSources, 52 { 53 'allowedStartRules': [ 54 'program' 55 ] 56 }); 57 } catch (err) { 58 console.error('Failed to compile parser. Error: ' + err); 59 process.exit(-1); 60 } 61 return parser; 62} 63 64// load the specified source file and return the buffer 65function loadFile(fileName) { 66 let content = ''; 67 try { 68 content = fs.readFileSync(fileName) + '\n'; 69 } catch (err) { 70 console.error(`While trying to read source file ${fileName}: ${err}`); 71 process.exit(-1); 72 } 73 console.log(` Read source file ${fileName}: ${content.length} Bytes.`) 74 return content; 75} 76 77function saveFile(fileName, content) { 78 fs.writeFileSync(fileName, content); 79 console.log(`Calls for stateMgmtProfiler disabled. '${fileName}': ${content.length} Bytes. OK.`); 80} 81 82 83function main() { 84 85 if (process.argv.length < 3) { 86 usageAndExit(); 87 } 88 89 let fileName = process.argv[2]; 90 91 console.log(`Loading file to disable stateMgmtProfiler calls. File: ${fileName}`); 92 93 let parser = makeParser(); 94 let content = loadFile(fileName); 95 96 let newContent; 97 try { 98 newContent = parser.parse(content); 99 } catch (err) { 100 console.error(`Failed disabling stateMgmtProfiler calls. Error: ${err}`); 101 process.exit(-1); 102 } 103 104 saveFile(fileName, newContent); 105} 106 107main();