generate-ip 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,112 +1,192 @@
1
- # > generate-ip
2
-
3
- ### Randomly generate fake IPv4 addresses.
4
-
5
- <a href="#%EF%B8%8F-mit-license"><img height=31 src="https://img.shields.io/badge/License-MIT-orange.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
6
- <a href="https://www.npmjs.com/package/generate-ip?activeTab=versions"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.0.0-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
7
- <a href="https://www.npmjs.com/package/generate-ip?activeTab=code"><img height=31 src="https://img.shields.io/npm/unpacked-size/generate-ip?style=for-the-badge&logo=ebox&logoColor=white&labelColor=464646&color=blue"></a>
8
- <a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:generate-ip/generate-ip.js"><img height=31 src="https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fsonarcloud.io%2Fapi%2Fmeasures%2Fcomponent%3Fcomponent%3Dadamlui_js-utils%3Agenerate-ip%2Fgenerate-ip.js%26metricKeys%3Dvulnerabilities&query=%24.component.measures.0.value&style=for-the-badge&logo=sonarcloud&logoColor=white&labelColor=464646&label=Vulnerabilities&color=gold"></a>
9
-
10
- <br>
11
-
12
- <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
13
-
14
- ## ⚡ Installation
15
-
16
- As a **global utility**:
17
-
18
- ```
19
- $ npm install -g generate-ip
20
- ```
21
-
22
- As a **runtime dependency**, from your project root:
23
-
24
- ```
25
- $ npm install generate-ip
26
- ```
27
-
28
- <br>
29
-
30
- <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
31
-
32
- ## 🔌 API usage
33
-
34
- **generate-ip** can be imported into your app as an ECMAScript module or a CommonJS module.
35
-
36
- #### ESM:
37
-
38
- ```js
39
- import * as ip from 'generate-ip';
40
- ```
41
-
42
- #### CJS:
43
-
44
- ```js
45
- const ip = require('generate-ip');
46
- ```
47
-
48
- #
49
-
50
- ### `generateIPv4()`
51
-
52
- Generates **one** IPv4 address:
53
-
54
- ```js
55
- const ipResult = ip.generateIPv4();
56
- console.log(ipResult); // sample output: 36.42.224.208
57
- ```
58
-
59
- #
60
-
61
- ### `isValidIPv4(address)`
62
-
63
- Checks if given `address` is a valid IPv4 address:
64
-
65
- ```js
66
- const ipIsValid = ip.isValidIPv4('36.42.224.208');
67
- console.log(ipIsValid); // outputs `true`
68
- ```
69
-
70
- <br>
71
-
72
- <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
73
-
74
- ## 💻 Command line usage
75
-
76
- When installed globally, **generate-ip** can also be used from the command line. The basic command is:
77
-
78
- ```
79
- $ generate-ip
80
- ```
81
-
82
- #
83
-
84
- ### Command line options
85
-
86
- ```
87
- Info commands:
88
- -h, --help Display help screen.
89
- -v, --version Show version number.
90
- ```
91
-
92
- <br>
93
-
94
- <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
95
-
96
- ## 🏛️ MIT License
97
-
98
- **Copyright © 2024 [Adam Lui](https://github.com/adamlui)**
99
-
100
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
101
-
102
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
103
-
104
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
105
-
106
- <br>
107
-
108
- <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
109
-
110
- <a href="https://github.com/adamlui/js-utils">**Home**</a> /
111
- <a href="https://github.com/adamlui/js-utils/discussions">Discuss</a> /
112
- <a href="#-generate-ip">Back to top ↑</a>
1
+ # > generate-ip
2
+
3
+ ### Randomly generate IPv4 and IPv6 addresses.
4
+
5
+ <a href="#%EF%B8%8F-mit-license"><img height=31 src="https://img.shields.io/badge/License-MIT-orange.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
6
+ <a href="https://github.com/adamlui/js-utils/releases/tag/generate-ip-2.0.0"><img height=31 src="https://img.shields.io/badge/Latest_Build-2.0.0-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
7
+ <a href="https://www.npmjs.com/package/generate-ip?activeTab=code"><img height=31 src="https://img.shields.io/npm/unpacked-size/generate-ip?style=for-the-badge&logo=ebox&logoColor=white&labelColor=464646&color=blue"></a>
8
+ <a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:generate-ip/generate-ip.js"><img height=31 src="https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fsonarcloud.io%2Fapi%2Fmeasures%2Fcomponent%3Fcomponent%3Dadamlui_js-utils%3Agenerate-ip%2Fgenerate-ip.js%26metricKeys%3Dvulnerabilities&query=%24.component.measures.0.value&style=for-the-badge&logo=sonarcloud&logoColor=white&labelColor=464646&label=Vulnerabilities&color=gold"></a>
9
+
10
+ <br>
11
+
12
+ <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
13
+
14
+ ## ⚡ Installation
15
+
16
+ As a **global utility**:
17
+
18
+ ```
19
+ $ npm install -g generate-ip
20
+ ```
21
+
22
+ As a **dev dependency**, from your project root:
23
+
24
+ ```
25
+ $ npm install -D generate-ip
26
+ ```
27
+
28
+ As a **runtime dependency**, from your project root:
29
+
30
+ ```
31
+ $ npm install generate-ip
32
+ ```
33
+
34
+ <br>
35
+
36
+ <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
37
+
38
+ ## 🔌 API usage
39
+
40
+ **generate-ip**'s APIs can be imported into your app as ECMAScript modules or a CommonJS modules.
41
+
42
+ #### ESM:
43
+
44
+ ```js
45
+ import { ipv4, ipv6 } from 'generate-ip';
46
+ ```
47
+
48
+ #### CJS:
49
+
50
+ ```js
51
+ const { ipv4, ipv6 } = require('generate-ip');
52
+ ```
53
+
54
+ #
55
+
56
+ ### `ipv4` methods
57
+
58
+ Use the `ipv4` methods to generate and validate IPv4 addresses.
59
+
60
+ #
61
+
62
+ #### `ipv4.generate([options])`
63
+
64
+ Generates **one** IPv4 address:
65
+
66
+ ```js
67
+ const ip = ipv4.generate();
68
+ console.log(ip); // sample output: 36.42.224.208
69
+ ```
70
+
71
+ Available options (passed as object properties):
72
+
73
+ Name | Type | Description | Default Value
74
+ ------------|---------|--------------------------------------------------------------------------------|---------------
75
+ `verbose` | Integer | Show logging in console/terminal. | `true`
76
+
77
+ #
78
+
79
+ #### `ipv4.validate(address[, options])`
80
+
81
+ Checks if given `address` is a valid IPv4 address:
82
+
83
+ ```js
84
+ const ipIsValid = ipv4.validate('36.42.224.208');
85
+ console.log(ipIsValid); // outputs `true`
86
+ ```
87
+
88
+ Available options (passed as object properties):
89
+
90
+ Name | Type | Description | Default Value
91
+ ------------|---------|--------------------------------------------------------------------------------|---------------
92
+ `verbose` | Integer | Show logging in console/terminal. | `true`
93
+
94
+ #
95
+
96
+ ### `ipv6` methods
97
+
98
+ Use the `ipv6` methods to generate, format, and validate IPv6 addresses.
99
+
100
+ #
101
+
102
+ #### `ipv6.generate([options])`
103
+
104
+ Generates **one** IPv6 address, returned as a string:
105
+
106
+ ```js
107
+ const ip = ipv6.generate();
108
+ console.log(ip); // sample output: 1379:6748:810c:5e16:b6c9:ae2:939f:8f2a
109
+ ```
110
+
111
+ Available options (passed as object properties):
112
+
113
+ Name | Type | Description | Default Value
114
+ ---------------|---------|--------------------------------------------------------------------------------|---------------
115
+ `verbose` | Integer | Show logging in console/terminal. | `true`
116
+ `leadingZeros` | Integer | Include leading zeros in hex pieces. | `false`
117
+ `doubleColon` | Integer | Replace series of zeros w/ `::` | `true`
118
+
119
+ #
120
+
121
+
122
+ #### `ipv6.format(ipv6Address[, options])`
123
+
124
+ Formats an IPv6 address according to `options` passed, returning a string:
125
+
126
+ ```js
127
+ const address = '0d::ffff:192.1.56.10/96'
128
+ formattedAddress = ipv6.format(address, { leadingZeros: true, doubleColon: false });
129
+
130
+ /* outputs:
131
+
132
+ ipv6.format() » Formatting 0d::ffff:192.1.56.10/96...
133
+ ipv6.format() » Expanding '::' into zero series...
134
+ ipv6.format() » Adding leading zeros...
135
+
136
+ 000d:0000:0000:0000:0000:0000:ffff:192.1.56.10/96
137
+ */
138
+ ```
139
+
140
+ Available options:
141
+
142
+ Name | Type | Description | Default Value
143
+ ---------------|---------|--------------------------------------------------------------------------------|---------------
144
+ `verbose` | Integer | Show logging in console/terminal. | `true`
145
+ `leadingZeros` | Integer | Include leading zeros in hex pieces. | `false`
146
+ `doubleColon` | Integer | Replace series of zeros w/ `::` | `true`
147
+
148
+ <br>
149
+
150
+ <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
151
+
152
+ ## 💻 Command line usage
153
+
154
+ When installed [globally](#-installation), **generate-ip** can also be used from the command line. The basic command is:
155
+
156
+ ```
157
+ $ generate-ip
158
+ ```
159
+
160
+ Generated IP is logged to the console + copied to the clipboard.
161
+
162
+ #
163
+
164
+ ### Command line options
165
+
166
+ ```
167
+ Info commands:
168
+ -h, --help Display help screen.
169
+ -v, --version Show version number.
170
+ ```
171
+
172
+ <br>
173
+
174
+ <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
175
+
176
+ ## 🏛️ MIT License
177
+
178
+ **Copyright © 2024 [Adam Lui](https://github.com/adamlui)**
179
+
180
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
181
+
182
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
183
+
184
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
185
+
186
+ <br>
187
+
188
+ <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
189
+
190
+ <a href="https://github.com/adamlui/js-utils">**Home**</a> /
191
+ <a href="https://github.com/adamlui/js-utils/discussions">Discuss</a> /
192
+ <a href="#-generate-ip">Back to top ↑</a>
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ const n=require("crypto")["randomInt"],s=require("child_process")["execSync"],t={generate:function(e={}){(e={verbose:!0,...e}).verbose&&console.info("ipv4.generate() » Generating IPv4 address...");var o=[];for(let e=0;e<4;e++)o.push(n(0,256));var r=o.join(".");return e.verbose&&console.log("\n"+r),r},validate:function(e,o={}){(o={verbose:!0,...o}).verbose&&console.info("ipv4.validate() » "+`Validating ${e}...`);e=e.split("."),e=!(4!==e.length||e.some(e=>!/^\d+$/.test(e)||parseInt(e,10)<0||255<parseInt(e,10)));return o.verbose&&console.info("ipv4.validate() » "+`IP is ${e?"":"in"}valid IPv4 address!`),e}},e={generate:function(e={}){(e={verbose:!0,leadingZeros:!1,doubleColon:!0,...e}).verbose&&console.info("ipv6.generate() » Generating IPv6 address...");var r=[];for(let e=0;e<8;e++){let o="";for(let e=0;e<4;e++)o+=n(0,16).toString(16);r.push(o)}var o=this.format(r.join(":"),{...e,verbose:!1});return e.verbose&&console.log("\n"+o),o},format:function(e,o={}){var r,n;if(o={verbose:!0,leadingZeros:!1,doubleColon:!0,...o},!this.validate(e,{verbose:!1}))return console.error("ipv6.format() » "+`ERROR:
3
+ - ${e} is not a valid IPv6 address.`);o.verbose&&console.info(`ipv6.format() » Formatting ${e}...`);let s=e;if(s=o.doubleColon?(o.verbose&&console.info("ipv6.format() » Replacing zero series w/ '::'..."),s.replace(/:(?:0+:)+/,"::")):(o.verbose&&console.info("ipv6.format() » Expanding '::' into zero series..."),n=s.split(":").filter(Boolean).length,r=o.leadingZeros?"0000":"0",n=Array(8-n).fill(r).join(":"),s.replace("::",`:${n}:`)),o.leadingZeros){o.verbose&&console.info("ipv6.format() » Adding leading zeros...");var t=s.split(":");for(let e=0;e<t.length;e++)for(;t[e].length<4;)t[e]="0"+t[e];s=t.join(":")}else o.verbose&&console.info("ipv6.format() » Stripping leading zeros..."),s=e.replace(/(^|(?<=:))0+(?!:)/g,"$1");return o.verbose&&console.info("\n"+(s!==e?s:"Address cannot be formatted!")),s},validate:function(e,o={}){(o={verbose:!0,...o}).verbose&&console.info("ipv6.validate() » "+`Validating ${e}...`);const r=e.split(/::?/),n=r[r.length-1];e=!(e.includes("::")&&2<e.split("::").length||/:{3,}/g.test(e)||r.length<2||8<r.length||r.some(e=>!(/^[\dA-Fa-f]{1,4}$/.test(e)||e===n&&t.validate(n.replace(/\/(?:0|(?:[1-2]?\d)|32|96)$/,""),{verbose:!1}))));return o.verbose&&console.info("ipv6.validate() » "+`IP is ${e?"":"in"}valid IPv6 address!`),e}};if(require.main!==module)module.exports={ipv4:t,ipv6:e};else{const a="",c="",v="",p="",f={infoCmds:{help:/^--?h(?:elp)?$/,version:/^--?ve?r?s?i?o?n?$/}};let o=!1,r;for(const d of Object.keys(f))for(const g of Object.keys(f[d]))o||process.argv.forEach(e=>{e.startsWith("-")&&(r=f[d][g].test(e)?void(o=!0):e)});if(r&&(console.error(`
4
+ ${c}ERROR: Arg [${r}] not recognized.`+a),console.info(`
5
+ ${v}Valid arguments are below.`+a),i(["infoCmds"]),process.exit(1)),process.argv.some(e=>/^--?h(?:elp)?$/.test(e)))i();else if(process.argv.some(e=>/^--?ve?r?s?i?o?n?$/.test(e)))console.info("v"+require("./package.json").version);else{const m=t.generate({verbose:!1});l(m),console.log(p+m+a)}function i(e=["cmdFormat","formatOptions","infoCmds"]){const o={cmdFormat:[`
6
+ ${v}generate-ip [commands]`+a],infoCmds:["\nInfo commands:"," -h, --help Display help screen."," -v, --version Show version number."]};e.forEach(e=>{o[e]?.forEach(e=>{{const n=process.stdout.columns||80,s=[],o=e.match(/\S+|\s+/g);let r="";o.forEach(e=>{var o=n-(0===s.length?0:29);r.length+e.length>o&&(s.push(0===s.length?r:r.trimStart()),r=""),r+=e}),s.push(0===s.length?r:r.trimStart()),s.forEach((e,o)=>console.info(0===o?e:" ".repeat(29)+e))}})})}function l(e){e=e.replace(/\s+$/,"").replace(/"/g,'""'),"darwin"===process.platform?s(`printf "${e}" | pbcopy`):"linux"===process.platform?s(`printf "${e}" | xclip -selection clipboard`):"win32"===process.platform&&s(`Set-Clipboard -Value "${e}"`,{shell:"powershell"})}}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "generate-ip",
3
- "version": "1.0.0",
4
- "description": "Randomly generate fake IPv4 addresses.",
3
+ "version": "2.0.0",
4
+ "description": "Randomly generate IPv4 and IPv6 addresses.",
5
5
  "author": {
6
6
  "name": "Adam Lui",
7
7
  "email": "adam@kudoai.com",
@@ -9,13 +9,21 @@
9
9
  },
10
10
  "homepage": "https://github.com/adamlui/js-utils",
11
11
  "license": "MIT",
12
- "main": "generate-ip.js",
12
+ "main": "dist/generate-ip.min.js",
13
+ "files": [ "dist", "docs" ],
13
14
  "bin": {
14
- "generateip": "generate-ip.js",
15
- "generate-ip": "generate-ip.js"
15
+ "generateip": "dist/generate-ip.min.js",
16
+ "generate-ip": "dist/generate-ip.min.js"
16
17
  },
17
18
  "scripts": {
18
- "test": "echo \"Error: no test specified\" && exit 1"
19
+ "test": "echo \"Error: no test specified\" && exit 1",
20
+ "build": "minify-js generate-ip.js dist",
21
+ "bump:patch": "bash utils/bump.sh patch",
22
+ "bump:minor": "bash utils/bump.sh minor",
23
+ "bump:major": "bash utils/bump.sh major",
24
+ "publish:patch": "bash utils/bump.sh patch --publish",
25
+ "publish:minor": "bash utils/bump.sh minor --publish",
26
+ "publish:major": "bash utils/bump.sh major --publish"
19
27
  },
20
28
  "repository": {
21
29
  "type": "git",
@@ -39,5 +47,8 @@
39
47
  "funding": {
40
48
  "type": "github",
41
49
  "url": "https://github.com/sponsors/adamlui"
50
+ },
51
+ "devDependencies": {
52
+ "@adamlui/minify.js": "^1.4.5"
42
53
  }
43
54
  }
package/generate-ip.js DELETED
@@ -1,107 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- // Import crypto.randomInt() for secure RNG
4
- const { randomInt } = require('crypto');
5
-
6
- // Define MAIN functions
7
-
8
- function generateIPv4() {
9
- console.info('Generating IPv4 address...');
10
- const segments = [];
11
- for (let i = 0; i < 4; i++) segments.push(randomInt(0, 256));
12
- return segments.join('.');
13
- }
14
-
15
- function isValidIPv4(address) {
16
- console.info(`Validating IPv4 address ${ address }...`);
17
- const segments = address.split('.');
18
- if (segments.length !== 4) return false;
19
- for (const segment of segments) {
20
- const num = parseInt(segment);
21
- if (isNaN(num) || num < 0 || num > 255) return false;
22
- }
23
- return true;
24
- }
25
-
26
- // EXPORT functions if script was required
27
- if (require.main !== module) module.exports = { generateIPv4, isValidIPv4 };
28
-
29
- else { // run as CLI utility
30
-
31
- // Init UI colors
32
- const nc = '\x1b[0m', // no color
33
- br = '\x1b[1;91m', // bright red
34
- by = '\x1b[1;33m', // bright yellow
35
- bw = '\x1b[1;97m'; // bright white
36
-
37
- // Load settings from ARGS
38
- const argRegex = {
39
- infoCmds: {
40
- 'help': /^--?h(?:elp)?$/,
41
- 'version': /^--?ve?r?s?i?o?n?$/
42
- }
43
- };
44
- let matchedArg = false, unrecognizedArg = undefined;
45
- for (const argType of Object.keys(argRegex))
46
- for (const option of Object.keys(argRegex[argType]))
47
- if (!matchedArg) process.argv.forEach(arg => {
48
- if (!arg.startsWith('-')) return;
49
- if (argRegex[argType][option].test(arg)) {
50
- matchedArg = true; unrecognizedArg = undefined; return;
51
- } else unrecognizedArg = arg;
52
- });
53
- if (unrecognizedArg) {
54
- console.error(`\n${br}ERROR: Arg [${unrecognizedArg}] not recognized.${nc}`);
55
- console.info(`\n${by}Valid arguments are below.${nc}`);
56
- printHelpScreen(['infoCmds']);
57
- process.exit(1);
58
- }
59
-
60
- // Show HELP screen if -h or --help passed
61
- if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) printHelpScreen();
62
-
63
- // Show VERSION number if -v or --version passed
64
- else if (process.argv.some(arg => /^--?ve?r?s?i?o?n?$/.test(arg)))
65
- console.info('v' + require('./package.json').version);
66
-
67
- else // log RESULT
68
- console.log(bw + generateIPv4() + nc);
69
-
70
- function printHelpMsg(msg) { // wrap msg + indent 2nd+ lines (for --help screen)
71
- const terminalWidth = process.stdout.columns || 80,
72
- indentation = 29, lines = [], words = msg.match(/\S+|\s+/g);
73
-
74
- // Split msg into lines of appropriate lengths
75
- let currentLine = '';
76
- words.forEach(word => {
77
- const lineLength = terminalWidth - ( lines.length === 0 ? 0 : indentation );
78
- if (currentLine.length + word.length > lineLength) { // cap/store it
79
- lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
80
- currentLine = '';
81
- }
82
- currentLine += word;
83
- });
84
- lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
85
-
86
- // Print formatted msg
87
- lines.forEach((line, index) => console.info(
88
- index === 0 ? line // print 1st line unindented
89
- : ' '.repeat(indentation) + line // print subsequent lines indented
90
- ));
91
- }
92
-
93
- function printHelpScreen(includeSections = ['cmdFormat', 'formatOptions', 'infoCmds']) {
94
- const sections = {
95
- 'cmdFormat': [
96
- `\n${by}generate-ip [commands]${nc}`
97
- ],
98
- 'infoCmds': [
99
- '\nInfo commands:',
100
- ' -h, --help Display help screen.',
101
- ' -v, --version Show version number.'
102
- ]
103
- };
104
- includeSections.forEach(section => { // print valid arg elems
105
- sections[section]?.forEach(line => printHelpMsg(line)); });
106
- }
107
- }