002-console-homework 1.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.
@@ -0,0 +1,107 @@
1
+ #!/usr/bin/env node
2
+
3
+ const yargs = require("yargs/yargs");
4
+ const { hideBin } = require("yargs/helpers");
5
+
6
+ const currentDate = new Date();
7
+ yargs(hideBin(process.argv))
8
+ // Команда current
9
+ .command(
10
+ "current",
11
+ "Get current date",
12
+ yargs => {
13
+ return yargs
14
+ .option("year", {
15
+ alias: "y",
16
+ type: "boolean",
17
+ description: "Get current year"
18
+ })
19
+ .option("month", {
20
+ alias: "m",
21
+ type: "boolean",
22
+ description: "Get current month"
23
+ })
24
+ .option("date", {
25
+ alias: "d",
26
+ type: "boolean",
27
+ description: "Get current date"
28
+ });
29
+ },
30
+ argv => {
31
+ if ([argv.year, argv.month, argv.date].filter(Boolean).length > 1) {
32
+ console.log("Нельзя использовать больше 1 опции")
33
+ process.exit(0)
34
+ }
35
+ if (argv.year) {
36
+ console.log(currentDate.getFullYear());
37
+ } else if (argv.month) {
38
+ console.log(currentDate.getMonth() + 1)
39
+ } else if (argv.date) {
40
+ console.log(currentDate.getDate());
41
+ } else {
42
+ console.log(currentDate.toISOString());
43
+ }
44
+ }
45
+ )
46
+ // Команда add
47
+ .command(
48
+ "add",
49
+ "Add values to current date",
50
+ yargs => {
51
+ return yargs
52
+ .option("year", {
53
+ alias: "y",
54
+ type: "number",
55
+ description: "Years to add"
56
+ })
57
+ .option("month", {
58
+ alias: "m",
59
+ type: "number",
60
+ description: "Months to add"
61
+ })
62
+ .option("date", {
63
+ alias: "d",
64
+ type: "number",
65
+ description: "Days to add"
66
+ });
67
+ },
68
+ argv => {
69
+ currentDate.setFullYear(currentDate.getFullYear() + (argv.year || 0));
70
+ currentDate.setMonth(currentDate.getMonth() + (argv.month || 0));
71
+ currentDate.setDate(currentDate.getDate() + (argv.date || 0));
72
+
73
+ console.log(currentDate.toISOString());
74
+ }
75
+ )
76
+ // Команда sub
77
+ .command(
78
+ "sub",
79
+ "Subtract values from current date",
80
+ yargs => {
81
+ return yargs
82
+ .option("year", {
83
+ alias: "y",
84
+ type: "number",
85
+ description: "Years to subtract"
86
+ })
87
+ .option("month", {
88
+ alias: "m",
89
+ type: "number",
90
+ description: "Months to subtract"
91
+ })
92
+ .option("date", {
93
+ alias: "d",
94
+ type: "number",
95
+ description: "Days to subtract"
96
+ });
97
+ },
98
+ argv => {
99
+ currentDate.setFullYear(currentDate.getFullYear() - (argv.year || 0));
100
+ currentDate.setMonth(currentDate.getMonth() - (argv.month || 0));
101
+ currentDate.setDate(currentDate.getDate() - (argv.date || 0));
102
+
103
+ console.log(currentDate.toISOString());
104
+ }
105
+ )
106
+ .demandCommand(1, "You need to specify a command: current, add or sub")
107
+ .help().argv;
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env node
2
+ const { createInterface } = require("node:readline");
3
+
4
+ /*
5
+ Необходимо написать утилиту командной строки, которая играет в игру "Загадай число".
6
+ Программа загадывает число и выводит диапазон значений, в пределах которого число было загадано.
7
+ Пользователь набирает числа в стандартный поток ввода и получает ответ больше или меньше, чем загаданное.
8
+ */
9
+ const number = Math.round(Math.random() * 100);
10
+ const rl = createInterface({
11
+ input: process.stdin,
12
+ output: process.stdout,
13
+ prompt: "Загадано число в диапазоне от 0 до 100 \n"
14
+ });
15
+
16
+ rl.prompt();
17
+
18
+ rl
19
+ .on("line", line => {
20
+ const n = Number(line.trim());
21
+ if (isNaN(n) || line.trim().length === 0) {
22
+ console.log("Вводить можно только числа и сам ввод числа обязателен! \nПопробуй еще раз :)");
23
+ } else {
24
+ if (n > 100 || n < 0) {
25
+ console.log("Число должно быть от 0 до 100 ! Попробуй еще раз :)");
26
+ } else {
27
+ if (n < number) {
28
+ console.log("Больше");
29
+ } else if (n > number) {
30
+ console.log("Меньше");
31
+ } else if (n === number) {
32
+ console.log(`Отгадано число ${number}`);
33
+ process.exit(0);
34
+ }
35
+ }
36
+ }
37
+ })
38
+ .on("SIGINT", () => {
39
+ console.log("Жаль, что сдался так быстро");
40
+ process.exit(0);
41
+ })
42
+ .on("close", () => {
43
+ console.log("Жаль, что сдался так быстро");
44
+ process.exit(0);
45
+ });
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "002-console-homework",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "bin": {
6
+ "cmd": "./1 task/cli.js",
7
+ "game": "./2 task/readline.js"
8
+ },
9
+ "console": "false",
10
+ "homepage": "https://github.com/thalia-pawn/homeworks#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/thalia-pawn/homeworks/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/thalia-pawn/homeworks.git"
17
+ },
18
+ "license": "ISC",
19
+ "author": "A. Rashchupkin",
20
+ "type": "commonjs",
21
+ "main": "index.js",
22
+ "scripts": {
23
+ "test": "echo \"Error: no test specified\" && exit 1"
24
+ },
25
+ "dependencies": {
26
+ "yargs": "^17.7.2"
27
+ }
28
+ }