Skip to content

Commit

Permalink
Fixed #30 and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sv443 committed Aug 8, 2021
1 parent cbb15c6 commit 5df8112
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ package-npm.json
.editorconfig
test.js
test.ts
docs.md
7 changes: 5 additions & 2 deletions SvCoreLib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,11 +627,14 @@ declare module "svcorelib" {

/**
* 🔹 Checks if the process is currently running in the debugger environment.
* This can be useful because some features like child processes and reading from stdin do not work in certain debuggers.
* Should support all major debuggers. 🔹
* This can be useful because some features like child processes and reading from stdin do not work in certain debuggers. 🔹
* ❗ This function should support all major debuggers but this isn't guaranteed!
* If it doesn't detect your debugger, pass the command line argument `--debug` or `--inspect` ❗
* @param {string} [checkArg] If provided, checks if this command line argument is present. Makes the function return `true` if it is.
* @returns true, if the process is currently running in a debugger, false if not.
* @since 1.9.0
* @version 1.13.0 Moved namespace
* @version 1.14.2 Added `inspector.url()` check for better results
*/
function inDebugger(): boolean;

Expand Down
37 changes: 28 additions & 9 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Otherwise, see the table of contents just below.
- [PatternInvalidError](#errorspatterninvaliderror) - GLOB pattern is invalid
- [NoStdinError](#errorsnostdinerror) - terminal doesn't have a stdin channel
- [InvalidMimeTypeError](#errorsinvalidmimetypeerror) - MIME type is not valid
- [SqlConnectionNotEstablishedError](#sqlconnectionnotestablishederror) - SQL connection is invalid
- [SqlConnectionNotEstablishedError](#errorssqlconnectionnotestablishederror) - SQL connection is invalid
- **[Objects](#objects)**
- [colors](#colors) - color text in the console
- [info](#info) - information about SCL
Expand Down Expand Up @@ -991,28 +991,47 @@ This namespace, accessed with `scl.system`, offers functions that refer to the s
> ### system.inDebugger()
> Checks if the process is currently running in the debugger environment.
> Checks if the process is currently running in a debugger.
> This can be useful because some features like child processes and reading from stdin do not work in most debuggers.
> Should support all major Node.js debuggers.
> Returns `true` if the current process runs in a debugger environment - else returns `false`
> Should support all major Node.js debuggers, but this is not guaranteed.
> Returns `true` if the current process runs in a debugger - else returns `false`
>
> If `checkArg` is provided, the function searches for a matching command line argument and returns `true` if it was found.
> This enables you to explicitly set the debugger state, for example if your debugger isn't properly detected by this function.
> ```ts
> scl.system.inDebugger(): boolean
> scl.system.inDebugger(checkArg?: string): boolean
> ```
>
> <br><details><summary><b>Example Code - click to show</b></summary>
> <br><details><summary><b>Basic example code - click to show</b></summary>
>
> ```js
> const { system, MenuPrompt } = require("svcorelib");
>
> if(!system.inDebugger())
> {
> // SCL's MenuPrompt doesn't work in some debuggers since it needs to read from process.stdin
> let mp = new MenuPrompt();
> const mp = new MenuPrompt();
> // ...
> }
> ```
>
> </details>
>
> <br><details><summary><b>Example with custom CLI argument - click to show</b></summary>
>
> ```js
> const { system } = require("svcorelib");
>
> console.log(process.argv); // [ '.../node.exe', '.../this_file.js', '--debugger-enabled' ]
>
> // explicitly test if `--debugger-enabled` is present in the CLI arguments
> if(system.inDebugger("--debugger-enabled"))
> {
> console.log("in debugger");
> }
> ```
>
> </details>
<br><br><br>
Expand All @@ -1022,7 +1041,7 @@ This namespace, accessed with `scl.system`, offers functions that refer to the s
> Prevents the process from being shut down.
> This can prevent people from exiting the process using CTRL+C.
> Using `process.exit()` in your script will still exit the process though!
> If you want the process to be able to be shut down again, use [`scl.yesShutdown()`](#yesshutdown).
> If you want the process to be able to be shut down again, use [`scl.yesShutdown()`](#systemyesshutdown).
>
> Note: this only listens for the signals "SIGINT" and "SIGTERM".
> Due to many OSes not supporting it, using "SIGKILL" will still kill the process.
Expand All @@ -1035,7 +1054,7 @@ This namespace, accessed with `scl.system`, offers functions that refer to the s
> ### system.yesShutdown()
> Removes the script shut down prevention that was previously enabled with [`scl.noShutdown()`](#noshutdown).
> Removes the script shut down prevention that was previously enabled with [`scl.noShutdown()`](#systemnoshutdown).
> ```ts
> scl.system.yesShutdown(): void
> ```
Expand Down
21 changes: 19 additions & 2 deletions src/functions/system/inDebugger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
function inDebugger()
const { url } = require("inspector");
const unused = require("../unused");

function inDebugger(checkArg)
{
return (typeof v8debug === "object" || /--debug|--inspect/.test(process.execArgv.join(" ")));
try
{
if(typeof checkArg === "string" && checkArg.length > 0)
return process.argv.join(" ").includes(checkArg);
}
catch(err)
{
unused(err);
}

return (
typeof v8debug === "object"
|| /--debug|--inspect/.test(process.execArgv.join(" "))
|| typeof url() === "string"
);
}

module.exports = inDebugger;
2 changes: 1 addition & 1 deletion src/functions/unused.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function unused(...any)
{
void(any);
void any;
return;
}

Expand Down

0 comments on commit 5df8112

Please sign in to comment.