Skip to content

Commit

Permalink
added null safety
Browse files Browse the repository at this point in the history
  • Loading branch information
AmeerArsala committed Sep 27, 2024
1 parent b54f208 commit ad136dd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
12 changes: 8 additions & 4 deletions lib/detect-libc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ let commandOut = '';
const safeCommand = () => {
if (!commandOut) {
return new Promise((resolve) => {
childProcess.exec(command, (err, out) => {
commandOut = err ? ' ' : out;
resolve(commandOut);
});
// null check
if (childProcess) {
childProcess.exec(command, (err, out) => {
commandOut = err ? ' ' : out;
resolve(commandOut);
});
}
});
}
return commandOut;
Expand All @@ -29,6 +32,7 @@ const safeCommand = () => {
const safeCommandSync = () => {
if (!commandOut) {
try {
// No null check needed since the error will be caught
commandOut = childProcess.execSync(command, { encoding: 'utf8' });
} catch (_err) {
commandOut = ' ';
Expand Down
19 changes: 11 additions & 8 deletions lib/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const LDD_PATH = '/usr/bin/ldd';
* @param {string} path
* @returns {string}
*/
const readFileSync = (path) => fs.readFileSync(path, 'utf-8');
const readFileSync = (path) => fs ? fs.readFileSync(path, 'utf-8') : null;

/**
* Read the content of a file
Expand All @@ -26,13 +26,16 @@ const readFileSync = (path) => fs.readFileSync(path, 'utf-8');
* @returns {Promise<string>}
*/
const readFile = (path) => new Promise((resolve, reject) => {
fs.readFile(path, 'utf-8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
// null check
if (fs) {
fs.readFile(path, 'utf-8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
}
});

module.exports = {
Expand Down

0 comments on commit ad136dd

Please sign in to comment.