@k03mad/dice 1.0.0 → 1.4.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/app/run.js CHANGED
@@ -1,30 +1,24 @@
1
1
  #!/usr/bin/env node
2
2
 
3
+ import fs from 'node:fs/promises';
4
+ import path from 'node:path';
3
5
  import {emitKeypressEvents} from 'node:readline';
4
6
 
5
7
  import {log} from '@k03mad/simple-log';
6
- import chalk from 'chalk';
7
- import {globby} from 'globby';
8
- import _ from 'lodash';
9
8
  import image from 'terminal-image';
10
9
 
11
- const {green, dim} = chalk;
10
+ import {
11
+ DICE_DEFAULT_COUNT,
12
+ DICE_HEIGHT,
13
+ DICE_PICTURES_PATH,
14
+ EXIT_CTRL_MODIFIER,
15
+ EXIT_EXTRA_KEY,
16
+ texts,
17
+ } from './utils/config.js';
18
+ import {getRandomArrElem} from './utils/helpers.js';
12
19
 
13
- const DICE_PICTURES_PATH = 'app/png';
14
- const DICE_HEIGHT = '30%';
15
-
16
- const EXIT_KEY = 'q';
17
-
18
- const texts = {
19
- message: green([
20
- '— enter any digit to generate this count of dices',
21
- '— press any key to repeat previous generating',
22
- `— press "CTRL+C" or enter "${EXIT_KEY}" to exit`,
23
- ].join('\n')),
24
- delimiter: dim('——————————————\n'),
25
- };
26
-
27
- const imgs = await globby(DICE_PICTURES_PATH);
20
+ const imgFiles = await fs.readdir(DICE_PICTURES_PATH);
21
+ const imgPaths = imgFiles.map(img => path.join(DICE_PICTURES_PATH, img));
28
22
 
29
23
  emitKeypressEvents(process.stdin);
30
24
 
@@ -32,28 +26,27 @@ if (process.stdin.isTTY) {
32
26
  process.stdin.setRawMode(true);
33
27
  }
34
28
 
29
+ let diceCurrentCount = DICE_DEFAULT_COUNT;
35
30
  log(texts.message);
36
31
 
37
- let count = 1;
38
-
39
- process.stdin.on('keypress', async (str, key) => {
32
+ process.stdin.on('keypress', async (char, key) => {
40
33
  if (
41
- (key.ctrl === true && key.name === 'c')
42
- || str === EXIT_KEY
34
+ (key.ctrl === true && key.name === EXIT_CTRL_MODIFIER)
35
+ || char === EXIT_EXTRA_KEY
43
36
  ) {
44
37
  process.exit();
38
+ } else {
39
+ log(texts.delimiter);
45
40
  }
46
41
 
47
- const inputNum = Number(str);
42
+ diceCurrentCount = Number(char) || diceCurrentCount;
48
43
 
49
- if (inputNum) {
50
- count = inputNum;
51
- }
52
-
53
- log(texts.delimiter);
44
+ const output = await Promise.all(
45
+ Array.from({length: diceCurrentCount}, () => {
46
+ const diceImg = getRandomArrElem(imgPaths);
47
+ return image.file(diceImg, {height: DICE_HEIGHT});
48
+ }),
49
+ );
54
50
 
55
- for (let i = 0; i < count; i++) {
56
- const img = await image.file(_.sample(imgs), {height: DICE_HEIGHT});
57
- log(img);
58
- }
51
+ log(output);
59
52
  });
@@ -0,0 +1,19 @@
1
+ import chalk from 'chalk';
2
+
3
+ const {green, dim, bold} = chalk;
4
+
5
+ export const DICE_DEFAULT_COUNT = 1;
6
+ export const DICE_PICTURES_PATH = './app/png';
7
+ export const DICE_HEIGHT = '30%';
8
+
9
+ export const EXIT_CTRL_MODIFIER = 'c';
10
+ export const EXIT_EXTRA_KEY = 'q';
11
+
12
+ export const texts = {
13
+ message: green([
14
+ '— enter any digit to generate this count of dices',
15
+ '— press any key to repeat previous count generating',
16
+ bold(`— press "CTRL+C" or enter "${EXIT_EXTRA_KEY}" to exit`),
17
+ ].join('\n')),
18
+ delimiter: dim('——————————————\n'),
19
+ };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @param {Array} arr
3
+ */
4
+ export const getRandomArrElem = arr => arr[
5
+ Math.floor(Math.random() * arr.length)
6
+ ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@k03mad/dice",
3
- "version": "1.0.0",
3
+ "version": "1.4.0",
4
4
  "description": "Dice",
5
5
  "maintainers": [
6
6
  "Kirill Molchanov <k03.mad@gmail.com"
@@ -20,8 +20,6 @@
20
20
  "dependencies": {
21
21
  "@k03mad/simple-log": "2.3.0",
22
22
  "chalk": "5.3.0",
23
- "globby": "14.0.2",
24
- "lodash": "4.17.21",
25
23
  "terminal-image": "3.0.0"
26
24
  },
27
25
  "devDependencies": {