generate-ip 2.8.2 → 2.9.1
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 +31 -29
- package/bin/gen-ip +6 -0
- package/bin/gen-ipv4 +6 -0
- package/bin/gen-ipv6 +6 -0
- package/bin/gen-mac +6 -0
- package/bin/genip +6 -0
- package/bin/genipv4 +6 -0
- package/bin/genipv6 +6 -0
- package/bin/genmac +6 -0
- package/dist/cli/index.js +8 -9
- package/dist/cli/lib/color.js +31 -0
- package/dist/cli/lib/data.js +1 -1
- package/dist/cli/lib/init.js +51 -44
- package/dist/cli/lib/jsdelivr.js +10 -10
- package/dist/cli/lib/language.js +21 -15
- package/dist/cli/lib/log.js +85 -29
- package/dist/cli/lib/pkg.js +78 -20
- package/dist/cli/lib/settings.js +43 -16
- package/dist/cli/lib/string.js +3 -0
- package/dist/data/messages.json +17 -7
- package/dist/data/package-data.json +3 -2
- package/dist/generate-ip.min.js +1 -1
- package/docs/README.md +31 -29
- package/docs/SECURITY.md +1 -1
- package/package.json +6 -3
- package/dist/cli/.cache/locales.json +0 -102
package/README.md
CHANGED
|
@@ -2,30 +2,30 @@
|
|
|
2
2
|
|
|
3
3
|
<div align="center">
|
|
4
4
|
|
|
5
|
-
<picture><source media="(prefers-color-scheme: dark)" srcset="https://
|
|
5
|
+
<picture><source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d3424a/generate-ip/assets/images/logos/generate-ip/white-logo.png"><img width=700 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d3424a/generate-ip/assets/images/logos/generate-ip/black-logo.png"></picture>
|
|
6
6
|
|
|
7
|
-
### <picture><source media="(prefers-color-scheme: dark)" srcset="https://
|
|
7
|
+
### <picture><source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/adamlui/js-utils@5c34563/generate-ip/assets/images/icons/node-graph/white/icon55x49.png"><img width=23 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@5c34563/generate-ip/assets/images/icons/node-graph/black/icon55x49.png"></picture> Randomly generate, format, and validate IPv4 + IPv6 + MAC addresses.
|
|
8
8
|
|
|
9
9
|
<br>
|
|
10
10
|
|
|
11
|
+
<a href="https://npmstar.com/compare/generate-ip">
|
|
12
|
+
<img height=31 src="https://img.shields.io/npm/dm/generate-ip?logo=npm&color=af68ff&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
11
13
|
<a href="#%EF%B8%8F-mit-license">
|
|
12
14
|
<img height=31 src="https://img.shields.io/badge/License-MIT-orange.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
13
|
-
<a href="https://github.com/adamlui/js-utils/releases/tag/generate-ip-2.
|
|
14
|
-
<img height=31 src="https://img.shields.io/badge/Latest_Build-2.
|
|
15
|
+
<a href="https://github.com/adamlui/js-utils/releases/tag/generate-ip-2.9.1">
|
|
16
|
+
<img height=31 src="https://img.shields.io/badge/Latest_Build-2.9.1-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
15
17
|
<a href="https://www.npmjs.com/package/generate-ip?activeTab=code">
|
|
16
18
|
<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>
|
|
17
|
-
<a href="https://github.com/adamlui/js-utils/blob/generate-ip-2.8.2/generate-ip/dist/generate-ip.min.js">
|
|
18
|
-
<img height=31 src="https://img.shields.io/github/size/adamlui/js-utils/generate-ip/dist/generate-ip.min.js?branch=generate-ip-2.8.2&label=Minified%20Size&logo=databricks&logoColor=white&labelColor=464646&color=ff69b4&style=for-the-badge"></a>
|
|
19
19
|
<a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:generate-ip/src/generate-ip.js">
|
|
20
20
|
<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%2Fsrc%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>
|
|
21
|
-
<a href="https://github.com/toolleeo/cli-apps
|
|
21
|
+
<a href="https://github.com/toolleeo/cli-apps/#networking">
|
|
22
22
|
<img height=31 src="https://img.shields.io/badge/Mentioned_in-Awesome-ff69b4?logo=awesomelists&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
23
23
|
<a href="https://www.jsdelivr.com/package/npm/generate-ip?tab=stats">
|
|
24
24
|
<img height=31 src="https://img.shields.io/jsdelivr/npm/hm/generate-ip?style=for-the-badge&logo=jsdelivr&logoColor=white&label=jsDelivr%20Hits&labelColor=464646&color=49c5d8"></a>
|
|
25
25
|
|
|
26
26
|
</div>
|
|
27
27
|
|
|
28
|
-
<img height=6px width="100%" src="https://
|
|
28
|
+
<img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/separators/aqua-gradient.png">
|
|
29
29
|
|
|
30
30
|
## 💡 About
|
|
31
31
|
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
- **Multi-environment support —** Use in Node.js or the web browser
|
|
37
37
|
- **Command line usable —** Just type `generate-ip`, that's it
|
|
38
38
|
|
|
39
|
-
<img height=6px width="100%" src="https://
|
|
39
|
+
<img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/separators/aqua-gradient.png">
|
|
40
40
|
|
|
41
41
|
## ⚡ Installation
|
|
42
42
|
|
|
@@ -60,13 +60,13 @@ $ npm install generate-ip
|
|
|
60
60
|
|
|
61
61
|
<br>
|
|
62
62
|
|
|
63
|
-
<a href="https://github.com/sponsors/adamlui"><img src="https://
|
|
63
|
+
<a href="https://github.com/sponsors/adamlui"><img src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/banners/sponsor/$10/banner1660x260.png"></a>
|
|
64
64
|
|
|
65
|
-
<img height=6px width="100%" src="https://
|
|
65
|
+
<img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/separators/aqua-gradient.png">
|
|
66
66
|
|
|
67
67
|
## 🔌 Importing the APIs
|
|
68
68
|
|
|
69
|
-
### <img height=18 src="https://
|
|
69
|
+
### <img height=18 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/icons/platforms/node.js/icon25x28.png"> Node.js
|
|
70
70
|
|
|
71
71
|
#### ECMAScript*:
|
|
72
72
|
|
|
@@ -82,28 +82,28 @@ const { ipv4, ipv6, mac } = require('generate-ip')
|
|
|
82
82
|
|
|
83
83
|
###### _*Node.js version 14 or higher required_
|
|
84
84
|
|
|
85
|
-
### <picture><source media="(prefers-color-scheme: dark)" srcset="https://
|
|
85
|
+
### <picture><source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/icons/platforms/web/light/icon25.png"><img width=16 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/icons/platforms/web/dark/icon25.png"></picture> Web
|
|
86
86
|
|
|
87
87
|
#### <> HTML script tag:
|
|
88
88
|
|
|
89
89
|
```html
|
|
90
|
-
<script src="https://cdn.jsdelivr.net/npm/generate-ip@2.
|
|
90
|
+
<script src="https://cdn.jsdelivr.net/npm/generate-ip@2.9.1/dist/generate-ip.min.js"></script>
|
|
91
91
|
```
|
|
92
92
|
|
|
93
93
|
#### ES6:
|
|
94
94
|
|
|
95
95
|
```js
|
|
96
96
|
(async () => {
|
|
97
|
-
await import('https://cdn.jsdelivr.net/npm/generate-ip@2.
|
|
97
|
+
await import('https://cdn.jsdelivr.net/npm/generate-ip@2.9.1/dist/generate-ip.min.js')
|
|
98
98
|
// Your code here...
|
|
99
99
|
})()
|
|
100
100
|
```
|
|
101
101
|
|
|
102
|
-
### <img height=17 src="https://
|
|
102
|
+
### <img height=17 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/icons/platforms/tampermonkey/icon28.png"><img height=17.5 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/icons/platforms/violentmonkey/icon25.png"> Greasemonkey
|
|
103
103
|
|
|
104
104
|
```js
|
|
105
105
|
...
|
|
106
|
-
// @require https://cdn.jsdelivr.net/npm/generate-ip@2.
|
|
106
|
+
// @require https://cdn.jsdelivr.net/npm/generate-ip@2.9.1/dist/generate-ip.min.js
|
|
107
107
|
// ==/UserScript==
|
|
108
108
|
|
|
109
109
|
// Your code here...
|
|
@@ -111,11 +111,11 @@ const { ipv4, ipv6, mac } = require('generate-ip')
|
|
|
111
111
|
|
|
112
112
|
<br>
|
|
113
113
|
|
|
114
|
-
📝 **Note:** To always import the latest version (not recommended in production!) remove the `@2.
|
|
114
|
+
📝 **Note:** To always import the latest version (not recommended in production!) remove the `@2.9.1` version tag from the jsDelivr URL: `https://cdn.jsdelivr.net/npm/generate-ip/dist/generate-ip.min.js`
|
|
115
115
|
|
|
116
116
|
<br>
|
|
117
117
|
|
|
118
|
-
<img height=6px width="100%" src="https://
|
|
118
|
+
<img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/separators/aqua-gradient.png">
|
|
119
119
|
|
|
120
120
|
## 📋 API usage
|
|
121
121
|
|
|
@@ -156,7 +156,7 @@ Name | Type | Description
|
|
|
156
156
|
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
157
157
|
`qty` | Integer | Number of IP addresses to generate. | `1`
|
|
158
158
|
`sequential` | Boolean | Generate addresses in sequence. | `false`
|
|
159
|
-
`network` | String |
|
|
159
|
+
`network` | String | Starting network address (required for sequential mode). | `null`
|
|
160
160
|
|
|
161
161
|
#
|
|
162
162
|
|
|
@@ -352,11 +352,11 @@ Name | Type | Description | Default Value
|
|
|
352
352
|
|
|
353
353
|
<br>
|
|
354
354
|
|
|
355
|
-
<img height=6px width="100%" src="https://
|
|
355
|
+
<img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/separators/aqua-gradient.png">
|
|
356
356
|
|
|
357
357
|
## 💻 Command line usage
|
|
358
358
|
|
|
359
|
-
|
|
359
|
+
**generate-ip** can also be used directly from the command line. The basic command is:
|
|
360
360
|
|
|
361
361
|
```
|
|
362
362
|
$ generate-ip
|
|
@@ -364,7 +364,7 @@ $ generate-ip
|
|
|
364
364
|
|
|
365
365
|
Sample output:
|
|
366
366
|
|
|
367
|
-
<img src="https://
|
|
367
|
+
<img src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@5c34563/generate-ip/assets/images/screenshots/cli/generate-ip-cmd-output.png">
|
|
368
368
|
|
|
369
369
|
📝 **Note:** To generate other address types, type `generate-ipv6` or `generate-mac`
|
|
370
370
|
|
|
@@ -389,6 +389,8 @@ Commands:
|
|
|
389
389
|
-i, --init Create config file (in project root).
|
|
390
390
|
-h, --help Display help screen.
|
|
391
391
|
-v, --version Show version number.
|
|
392
|
+
--stats Show npm stats.
|
|
393
|
+
--debug [targetKey] Show debug logs.
|
|
392
394
|
```
|
|
393
395
|
|
|
394
396
|
#
|
|
@@ -412,7 +414,7 @@ export default {
|
|
|
412
414
|
|
|
413
415
|
<br>
|
|
414
416
|
|
|
415
|
-
<img height=6px width="100%" src="https://
|
|
417
|
+
<img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/separators/aqua-gradient.png">
|
|
416
418
|
|
|
417
419
|
## 🏛️ MIT License
|
|
418
420
|
|
|
@@ -426,11 +428,11 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
|
|
426
428
|
|
|
427
429
|
<br>
|
|
428
430
|
|
|
429
|
-
<img height=6px width="100%" src="https://
|
|
431
|
+
<img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/separators/aqua-gradient.png">
|
|
430
432
|
|
|
431
433
|
## 🛠️ Related utilities
|
|
432
434
|
|
|
433
|
-
### [🔒 generate-pw](../generate-pw) <a href="https://github.com/toolleeo/cli-apps
|
|
435
|
+
### [🔒 generate-pw](../generate-pw) <a href="https://github.com/toolleeo/cli-apps/#password-managers"><img height=18 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/badges/awesome/badge.svg"></a>
|
|
434
436
|
|
|
435
437
|
> Randomly generate, strengthen, and validate cryptographically-secure passwords.
|
|
436
438
|
<br>[Install](https://docs.generatepw.org/#-installation) /
|
|
@@ -439,7 +441,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
|
|
439
441
|
[CLI usage](https://docs.generatepw.org/#-command-line-usage) /
|
|
440
442
|
[Discuss](https://github.com/adamlui/js-utils/discussions)
|
|
441
443
|
|
|
442
|
-
### <picture><source media="(prefers-color-scheme: dark)" srcset="https://
|
|
444
|
+
### <picture><source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d3424a/geolocate/assets/images/icons/wire-globe/white/icon32.png"><img height=22 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d3424a/geolocate/assets/images/icons/wire-globe/black/icon32.png"></picture> [geolocate](https://js-utils.org/geolocate) <a href="https://github.com/toolleeo/cli-apps/#networking"><img height=18 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/badges/awesome/badge.svg"></a>
|
|
443
445
|
|
|
444
446
|
> Fetch IP geolocation data from the CLI.
|
|
445
447
|
<br>[Install](https://docs.geolocatejs.org/#-installation) /
|
|
@@ -450,8 +452,8 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
|
|
450
452
|
|
|
451
453
|
<br>
|
|
452
454
|
|
|
453
|
-
<img height=6px width="100%" src="https://
|
|
455
|
+
<img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/separators/aqua-gradient.png">
|
|
454
456
|
|
|
455
|
-
<picture><source media="(prefers-color-scheme: dark)" srcset="https://
|
|
457
|
+
<picture><source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/icons/home/white/icon32x27.png"><img height=13 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@0d36e26/assets/images/icons/home/dark-gray/icon32x27.png"></picture> <a href="https://js-utils.org">**More JavaScript utilities**</a> /
|
|
456
458
|
<a href="https://github.com/adamlui/js-utils/discussions">Discuss</a> /
|
|
457
459
|
<a href="#top">Back to top ↑</a>
|
package/bin/gen-ip
ADDED
package/bin/gen-ipv4
ADDED
package/bin/gen-ipv6
ADDED
package/bin/gen-mac
ADDED
package/bin/genip
ADDED
package/bin/genipv4
ADDED
package/bin/genipv6
ADDED
package/bin/genmac
ADDED
package/dist/cli/index.js
CHANGED
|
@@ -3,17 +3,14 @@
|
|
|
3
3
|
(async () => {
|
|
4
4
|
'use strict'
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
env.debugMode = env.args.some(arg => /^--?debug(?:-?mode)?$/.test(arg))
|
|
6
|
+
// Init ENV
|
|
7
|
+
const init = require('./lib/init')
|
|
8
|
+
init.env()
|
|
11
9
|
|
|
12
10
|
// Import LIBS
|
|
11
|
+
globalThis.log = require('./lib/log')
|
|
13
12
|
const clipboardy = require('node-clipboardy'),
|
|
14
|
-
generateIP = require(`../generate-ip${ env.
|
|
15
|
-
init = require('./lib/init'),
|
|
16
|
-
log = require('./lib/log')
|
|
13
|
+
generateIP = require(`../generate-ip${ env.modes.dev ? '' : '.min' }.js`)
|
|
17
14
|
|
|
18
15
|
await init.cli()
|
|
19
16
|
|
|
@@ -21,6 +18,7 @@
|
|
|
21
18
|
if (cli.config.init) return init.configFile()
|
|
22
19
|
else if (cli.config.help) return log.help()
|
|
23
20
|
else if (cli.config.version) return log.version()
|
|
21
|
+
else if (cli.config.stats) return log.stats()
|
|
24
22
|
|
|
25
23
|
// Log/copy random IP(s)
|
|
26
24
|
const genOptions = {
|
|
@@ -29,7 +27,8 @@
|
|
|
29
27
|
network: cli.config.network,
|
|
30
28
|
verbose: !cli.config.quietMode
|
|
31
29
|
}
|
|
30
|
+
log.break()
|
|
32
31
|
clipboardy.writeSync([].concat(generateIP[cli.config.mode || 'ipv4'].generate(genOptions)).join('\n'))
|
|
33
|
-
log.ifNotQuiet(
|
|
32
|
+
log.ifNotQuiet(`${cli.msgs.info_copyingToClip}...`)
|
|
34
33
|
|
|
35
34
|
})()
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const color = module.exports = {
|
|
2
|
+
nc: '\x1b[0m',
|
|
3
|
+
hex: {
|
|
4
|
+
br: '#ff0000', by: '#ffff00', bo: '#ffa500', bg: '#00ff00',
|
|
5
|
+
bw: '#ffffff', gry: '#808080', blk: '#000000', tlBG: '#008080'
|
|
6
|
+
},
|
|
7
|
+
schemes: {
|
|
8
|
+
get default() {
|
|
9
|
+
return [
|
|
10
|
+
'#00e5bc', '#18c8ae', '#30ac9f', '#488f91', '#607383',
|
|
11
|
+
'#775674', '#8f3966', '#a71d57', '#bf0049', '#9a1b5e'
|
|
12
|
+
].map(color.hexToANSI)
|
|
13
|
+
},
|
|
14
|
+
get rainbow() {
|
|
15
|
+
return [
|
|
16
|
+
'#e41a1c', '#ff7f00', '#ffff33', '#4daf4a', '#377eb8',
|
|
17
|
+
'#984ea3', '#f781bf', '#999999', '#a65628', '#d95f02'
|
|
18
|
+
].map(color.hexToANSI)
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
hexToANSI(hex) {
|
|
23
|
+
const r = parseInt(hex.slice(1,3), 16),
|
|
24
|
+
g = parseInt(hex.slice(3,5), 16),
|
|
25
|
+
b = parseInt(hex.slice(5,7), 16)
|
|
26
|
+
return `\x1b[38;2;${r};${g};${b}m`
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
for (const hexKey of Object.keys(color.hex)) // add color[hexKey] getters that return ANSI
|
|
31
|
+
Object.defineProperty(color, hexKey, { get: () => color.hexToANSI(color.hex[hexKey]) })
|
package/dist/cli/lib/data.js
CHANGED
|
@@ -15,7 +15,7 @@ module.exports = {
|
|
|
15
15
|
require(protocol).get(url, resp => {
|
|
16
16
|
let rawData = ''
|
|
17
17
|
resp.on('data', chunk => rawData += chunk)
|
|
18
|
-
resp.on('end', () => resolve({ json: () => JSON.parse(rawData) }))
|
|
18
|
+
resp.on('end', () => resolve({ json: () => JSON.parse(rawData), text: () => rawData }))
|
|
19
19
|
}).on('error', reject)
|
|
20
20
|
}) : fetch(url) // using Node.js fetch()
|
|
21
21
|
},
|
package/dist/cli/lib/init.js
CHANGED
|
@@ -1,44 +1,51 @@
|
|
|
1
|
-
const language = require('./language'),
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
cli.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
1
|
+
const language = require('./language'),
|
|
2
|
+
settings = require('./settings')
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
|
|
6
|
+
async cli() {
|
|
7
|
+
Object.assign(globalThis.cli ??= {}, require(`${env.paths.libData}/package-data.json`))
|
|
8
|
+
cli.msgs = await language.getMsgs('en')
|
|
9
|
+
cli.msgs = await language.getMsgs(cli.lang = settings.load('uiLang') || (
|
|
10
|
+
env.modes.debug ? language.generateRandomLang({ excludes: ['en'] }) : language.getSysLang() ))
|
|
11
|
+
cli.urls.cliDocs ||= `${cli.urls.docs}/#-command-line-usage`
|
|
12
|
+
settings.load() // all keys to cli.config
|
|
13
|
+
},
|
|
14
|
+
|
|
15
|
+
async configFile(filename = settings.configFilename) {
|
|
16
|
+
const fs = require('fs'),
|
|
17
|
+
path = require('path'),
|
|
18
|
+
paths = { target: path.resolve(process.cwd(), filename) }
|
|
19
|
+
|
|
20
|
+
if (fs.existsSync(paths.target)) // use existing config file
|
|
21
|
+
return log.warn(`${cli.msgs.warn_configFileExists}:`, paths.target)
|
|
22
|
+
if (fs.existsSync(paths.src = path.resolve(__dirname, `${env.paths.libData}/${filename}`)))
|
|
23
|
+
fs.copyFileSync(paths.src, paths.target) // use found template
|
|
24
|
+
|
|
25
|
+
else { // use jsDelivr copy
|
|
26
|
+
const jsdURL = `${require('./jsdelivr').getPkgVerURL()}/${filename}`
|
|
27
|
+
log.data(`${cli.msgs.info_fetchingRemoteConfigFrom} ${jsdURL}...`)
|
|
28
|
+
try {
|
|
29
|
+
const data = require('./data'),
|
|
30
|
+
resp = await data.fetch(jsdURL)
|
|
31
|
+
if (resp.ok) data.atomicWrite(paths.target, await resp.text())
|
|
32
|
+
else return log.warn(`${cli.msgs.warn_remoteConfigNotFound}: ${jsdURL} (${resp.status})`)
|
|
33
|
+
} catch (err) {
|
|
34
|
+
return log.warn(`${cli.msgs.warn_remoteConfigFailed}: ${jsdURL} ${err.message}`) }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
log.success(`${cli.msgs.info_configFileCreated}: ${paths.target}\n`)
|
|
38
|
+
log.tip(`${cli.msgs.tip_editToSetDefaults}.`)
|
|
39
|
+
log.tip(`${cli.msgs.tip_cliArgsPrioritized}.`)
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
env() {
|
|
43
|
+
Object.assign(globalThis.env ??= {}, {
|
|
44
|
+
args: process.argv.slice(2),
|
|
45
|
+
modes: { dev: /[\\/]src(?:[\\/]|$)/i.test(__dirname) },
|
|
46
|
+
supports: { unicode: require('is-unicode-supported').default() }
|
|
47
|
+
})
|
|
48
|
+
env.modes.debug = env.args.some(arg => /^--?(?:V|debug(?:[-_]?mode)?)$/.test(arg))
|
|
49
|
+
env.paths = { libData: `../../${ env.modes.dev ? '..' : 'data' }` }
|
|
50
|
+
}
|
|
51
|
+
}
|
package/dist/cli/lib/jsdelivr.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
version ||= cli.version ||= require('./pkg').getVer('local') || 'none'
|
|
5
|
-
const verTag = !/^\d+\.\d+\.\d+$/.test(version) ? 'latest' : `${cli.name}-${version}`
|
|
6
|
-
return `${cli.urls.jsdelivr}@${verTag}/${cli.name}`
|
|
7
|
-
},
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
1
|
+
module.exports = {
|
|
2
|
+
|
|
3
|
+
getPkgVerURL(version) {
|
|
4
|
+
version ||= cli.version ||= require('./pkg').getVer('local') || 'none'
|
|
5
|
+
const verTag = !/^\d+\.\d+\.\d+$/.test(version) ? 'latest' : `${cli.name}-${version}`
|
|
6
|
+
return `${cli.urls.jsdelivr}@${verTag}/${cli.name}`
|
|
7
|
+
},
|
|
8
|
+
|
|
9
|
+
getCommitURL(hash = 'latest') { return `${cli.urls.jsdelivr}@${hash}/${cli.name}` }
|
|
10
|
+
}
|
package/dist/cli/lib/language.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
const data = require('./data')
|
|
2
|
-
log = require('./log')
|
|
1
|
+
const data = require('./data')
|
|
3
2
|
|
|
4
3
|
module.exports = {
|
|
5
4
|
|
|
@@ -14,13 +13,13 @@ module.exports = {
|
|
|
14
13
|
let locales = includes.length ? includes : (() => {
|
|
15
14
|
|
|
16
15
|
// Read cache if found
|
|
17
|
-
const cacheDir = path.join(__dirname, '
|
|
16
|
+
const cacheDir = path.join(__dirname, '../../.cache'),
|
|
18
17
|
localeCache = path.join(cacheDir, 'locales.json')
|
|
19
18
|
if (fs.existsSync(localeCache))
|
|
20
19
|
try { return JSON.parse(fs.readFileSync(localeCache, 'utf8')) } catch (err) {}
|
|
21
20
|
|
|
22
21
|
// Discover pkg _locales
|
|
23
|
-
const localesDir = path.resolve(
|
|
22
|
+
const localesDir = path.resolve(__dirname, '../../../_locales')
|
|
24
23
|
if (!fs.existsSync(localesDir)) return ['en']
|
|
25
24
|
const locales = fs.readdirSync(localesDir, { withFileTypes: true })
|
|
26
25
|
.filter(entry => entry.isDirectory()).map(entry => entry.name)
|
|
@@ -38,10 +37,8 @@ module.exports = {
|
|
|
38
37
|
locales = locales.filter(locale => !excludeSet.has(locale))
|
|
39
38
|
|
|
40
39
|
// Get random language
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
randomLang = locales[Math.floor(Math.random() * locales.length)]
|
|
44
|
-
log.debug(`Random language: ${randomLang}\n`)
|
|
40
|
+
const randomLang = locales.length ? locales[Math.floor(Math.random() * locales.length)] : 'en'
|
|
41
|
+
log.debug(`Random language: ${randomLang}`)
|
|
45
42
|
|
|
46
43
|
return randomLang
|
|
47
44
|
},
|
|
@@ -51,20 +48,29 @@ module.exports = {
|
|
|
51
48
|
if (env.msgs && langCode == cli.lang) return env.msgs // don't re-fetch same msgs
|
|
52
49
|
|
|
53
50
|
let msgs = data.flatten( // local ones
|
|
54
|
-
require(`../../${ env.
|
|
51
|
+
require(`../../${ env.modes.dev ? '../_locales/en' : 'data' }/messages.json`))
|
|
55
52
|
|
|
56
53
|
if (!langCode.startsWith('en')) { // fetch non-English msgs from jsDelivr
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
try { // check if terminal supports non-Latin scripts
|
|
55
|
+
const nonLatinLocales = await (await data.fetch(
|
|
56
|
+
`${cli.urls.jsdelivr}@${cli.commitHashes.data}/assets/data/non-latin-locales.json`
|
|
57
|
+
)).json()
|
|
58
|
+
if (nonLatinLocales.includes(langCode.split('-')[0]) && !env.supports.unicode)
|
|
59
|
+
return msgs // en ones
|
|
60
|
+
} catch (err) {
|
|
61
|
+
log.debug(`Failed to fetch non-Latin locales: ${err}`)
|
|
62
|
+
}
|
|
63
|
+
const msgBaseURL = `${require('./jsdelivr').getCommitURL(cli.commitHashes.locales)}/_locales`
|
|
64
|
+
let msgURL = `${msgBaseURL}/${langCode}/messages.json`, msgFetchesTried = 0
|
|
59
65
|
while (msgFetchesTried < 3)
|
|
60
66
|
try { // fetch remote msgs
|
|
61
|
-
msgs = data.flatten(await (await data.fetch(
|
|
67
|
+
msgs = data.flatten(await (await data.fetch(msgURL)).json())
|
|
62
68
|
break
|
|
63
69
|
} catch (err) { // retry up to 2X (region-stripped + EN)
|
|
64
70
|
msgFetchesTried++ ; if (msgFetchesTried >= 3) break
|
|
65
|
-
log.debug(
|
|
66
|
-
|
|
67
|
-
: `${
|
|
71
|
+
log.debug(msgURL = langCode.includes('-') && msgFetchesTried == 1 ?
|
|
72
|
+
msgURL.replace(/([^_]*)_[^/]*(\/.*)/, '$1$2') // strip region before retrying
|
|
73
|
+
: `${msgBaseURL}/en/messages.json` // else use EN msgs
|
|
68
74
|
)
|
|
69
75
|
}
|
|
70
76
|
}
|