custom-menu-cli 1.0.0 → 1.0.3
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-pt.md +21 -0
- package/README.md +110 -1
- package/index.js +11 -25
- package/package.json +1 -1
- package/src/configLoader.js +13 -8
- package/src/header.js +25 -0
- package/README-en.md +0 -89
package/README-pt.md
CHANGED
@@ -19,12 +19,33 @@ npm install -g custom-menu-cli
|
|
19
19
|
|
20
20
|
## Uso
|
21
21
|
|
22
|
+
Esta ferramenta pode ser utilizada de duas formas: como uma ferramenta de Linha de Comando (CLI) ou programaticamente, importando sua função principal.
|
23
|
+
|
24
|
+
### Como Ferramenta CLI
|
25
|
+
|
22
26
|
Para usar o CLI, você pode executar o comando `custom-menu-cli`, passando opcionalmente o caminho para um arquivo JSON. Se nenhum caminho for fornecido, ele procurará um arquivo `menu.json` no diretório atual.
|
23
27
|
|
24
28
|
```bash
|
25
29
|
custom-menu-cli [caminho/para/seu/menu.json]
|
26
30
|
```
|
27
31
|
|
32
|
+
### Uso Programático
|
33
|
+
|
34
|
+
Você pode importar a função `runCli` do pacote e executá-la dentro de suas próprias aplicações Node.js. Isso permite integrar a funcionalidade do menu customizado em scripts ou sistemas maiores.
|
35
|
+
|
36
|
+
```javascript
|
37
|
+
import { runCli } from 'custom-menu-cli';
|
38
|
+
|
39
|
+
async function iniciarMeuMenuCustomizado() {
|
40
|
+
console.log("Iniciando menu customizado...");
|
41
|
+
// Opcionalmente, passe o caminho para o seu arquivo menu.json
|
42
|
+
await runCli('./caminho/para/seu/menu.json');
|
43
|
+
console.log("Menu customizado finalizado.");
|
44
|
+
}
|
45
|
+
|
46
|
+
iniciarMeuMenuCustomizado();
|
47
|
+
```
|
48
|
+
|
28
49
|
## Estrutura do JSON
|
29
50
|
|
30
51
|
O arquivo JSON que define o menu tem a seguinte estrutura:
|
package/README.md
CHANGED
@@ -1,3 +1,112 @@
|
|
1
1
|
# Custom Menu CLI
|
2
2
|
|
3
|
-
[
|
3
|
+
[Português (Brasil)](./README-pt.md)
|
4
|
+
|
5
|
+
This is a command-line interface (CLI) tool that creates an interactive menu based on a JSON file. It's designed to simplify the execution of frequent commands in a terminal.
|
6
|
+
|
7
|
+
## Features
|
8
|
+
|
9
|
+
- Interactive menu in the terminal.
|
10
|
+
- Menu structure defined by a JSON file.
|
11
|
+
- Easy to configure and use.
|
12
|
+
- Support for command execution with confirmation.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
To install this tool globally, run the following command:
|
17
|
+
|
18
|
+
```bash
|
19
|
+
npm install -g custom-menu-cli
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
This tool can be used in two ways: as a Command-Line Interface (CLI) tool or programmatically by importing its main function.
|
25
|
+
|
26
|
+
### As a CLI Tool
|
27
|
+
|
28
|
+
To use the CLI, you can run the `custom-menu-cli` command, optionally passing the path to a JSON file. If no path is provided, it will look for a `menu.json` file in the current directory.
|
29
|
+
|
30
|
+
```bash
|
31
|
+
custom-menu-cli [path/to/your/menu.json]
|
32
|
+
```
|
33
|
+
|
34
|
+
### Programmatic Usage
|
35
|
+
|
36
|
+
You can import the `runCli` function from the package and execute it within your own Node.js applications. This allows you to integrate the custom menu functionality into larger scripts or systems.
|
37
|
+
|
38
|
+
```javascript
|
39
|
+
import { runCli } from 'custom-menu-cli';
|
40
|
+
|
41
|
+
async function startMyCustomMenu() {
|
42
|
+
console.log("Starting custom menu...");
|
43
|
+
// Optionally, pass the path to your menu.json file
|
44
|
+
await runCli('./path/to/your/menu.json');
|
45
|
+
console.log("Custom menu finished.");
|
46
|
+
}
|
47
|
+
|
48
|
+
startMyCustomMenu();
|
49
|
+
```
|
50
|
+
|
51
|
+
## JSON Structure
|
52
|
+
|
53
|
+
The JSON file that defines the menu has the following structure:
|
54
|
+
|
55
|
+
```json
|
56
|
+
{
|
57
|
+
"name": "Deploy Menu",
|
58
|
+
"description": "Menu de navegação para deploys",
|
59
|
+
"options": [
|
60
|
+
{
|
61
|
+
"id": "1",
|
62
|
+
"name": "Projeto A",
|
63
|
+
"type": "navigation",
|
64
|
+
"options": [
|
65
|
+
{
|
66
|
+
"id": "1.1",
|
67
|
+
"name": "Down Service",
|
68
|
+
"type": "action",
|
69
|
+
"command": "echo 'Down A'",
|
70
|
+
"confirm": true
|
71
|
+
},
|
72
|
+
{
|
73
|
+
"id": "1.2",
|
74
|
+
"name": "Up Service",
|
75
|
+
"type": "action",
|
76
|
+
"command": "echo 'Up A'"
|
77
|
+
}
|
78
|
+
]
|
79
|
+
},
|
80
|
+
{
|
81
|
+
"id": "2",
|
82
|
+
"name": "Restart All",
|
83
|
+
"type": "custom-action",
|
84
|
+
"idList": ["1.1", "1.2"],
|
85
|
+
"confirm": true
|
86
|
+
}
|
87
|
+
]
|
88
|
+
}
|
89
|
+
```
|
90
|
+
|
91
|
+
### Fields
|
92
|
+
|
93
|
+
- `name`: The name of the menu.
|
94
|
+
- `description`: A brief description of the menu.
|
95
|
+
- `options`: An array of menu options.
|
96
|
+
- `id`: A unique identifier for the option.
|
97
|
+
- `name`: The text that will be displayed for the option.
|
98
|
+
- `type`: The type of option. It can be `action` (executes a command), `navigation` (opens a submenu) or `custom-action` (executes a list of commands from other actions).
|
99
|
+
- `command`: The command to be executed (if the type is `action`).
|
100
|
+
- `idList`: A list of ids from other actions to be executed (if the type is `custom-action`).
|
101
|
+
- `confirm`: A boolean that indicates whether a confirmation should be requested before executing the command.
|
102
|
+
- `options`: An array of sub-options (if the type is `navigation`).
|
103
|
+
|
104
|
+
## License
|
105
|
+
|
106
|
+
This project is licensed under the MIT License.
|
107
|
+
|
108
|
+
## Author
|
109
|
+
|
110
|
+
- **Mateus Medeiros**
|
111
|
+
- GitHub: [@mateusmed](https://github.com/mateusmed)
|
112
|
+
- LinkedIn: [Mateus Medeiros](https://www.linkedin.com/in/mateus-med/)
|
package/index.js
CHANGED
@@ -1,38 +1,24 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
|
-
|
2
|
+
|
3
3
|
import { loadMenuConfig } from './src/configLoader.js';
|
4
4
|
import { showMenu, buildIdMap } from './src/menu.js';
|
5
|
+
import { displayHeader } from './src/header.js';
|
6
|
+
import { fileURLToPath } from 'url';
|
5
7
|
|
6
|
-
|
7
|
-
const data = loadMenuConfig();
|
8
|
+
export async function runCli(menuPath = null) {
|
9
|
+
const data = loadMenuConfig(menuPath);
|
8
10
|
if (data.options) {
|
9
11
|
buildIdMap(data.options);
|
10
12
|
}
|
11
13
|
|
12
14
|
console.clear();
|
13
15
|
|
14
|
-
|
15
|
-
const name = data.name || 'Custom Menu';
|
16
|
-
const description = data.description || 'A CLI Menu';
|
17
|
-
const lines = [name, description];
|
18
|
-
const maxLength = Math.max(...lines.map(line => line.length));
|
19
|
-
const boxWidth = maxLength + 4;
|
16
|
+
displayHeader(data);
|
20
17
|
|
21
|
-
|
22
|
-
|
18
|
+
await showMenu(data);
|
19
|
+
}
|
23
20
|
|
24
|
-
console.log(chalk.bold.blueBright(topBorder));
|
25
|
-
lines.forEach(line => {
|
26
|
-
const paddingTotal = boxWidth - line.length;
|
27
|
-
const paddingLeft = Math.floor(paddingTotal / 2);
|
28
|
-
const paddingRight = Math.ceil(paddingTotal / 2);
|
29
|
-
const paddedLine = `║${' '.repeat(paddingLeft)}${line}${' '.repeat(paddingRight)}║`;
|
30
|
-
console.log(chalk.bold.blueBright(paddedLine));
|
31
|
-
});
|
32
|
-
console.log(chalk.bold.blueBright(bottomBorder));
|
33
|
-
console.log(''); // For spacing
|
34
|
-
console.log(chalk.gray(`Developed by Mateus Medeiros - GitHub: @mateusmed`));
|
35
|
-
console.log(''); // For spacing
|
36
21
|
|
37
|
-
|
38
|
-
|
22
|
+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
23
|
+
runCli();
|
24
|
+
}
|
package/package.json
CHANGED
package/src/configLoader.js
CHANGED
@@ -15,21 +15,26 @@ const defaultMenu = {
|
|
15
15
|
]
|
16
16
|
};
|
17
17
|
|
18
|
-
export function loadMenuConfig() {
|
19
|
-
const args = process.argv.slice(2);
|
20
|
-
const path = args[0];
|
18
|
+
export function loadMenuConfig(menuPath = null) {
|
21
19
|
let data;
|
20
|
+
let finalPath = menuPath;
|
22
21
|
|
23
|
-
|
22
|
+
// If menuPath is not provided, check command line arguments
|
23
|
+
if (!finalPath) {
|
24
|
+
const args = process.argv.slice(2);
|
25
|
+
finalPath = args[0];
|
26
|
+
}
|
27
|
+
|
28
|
+
if (finalPath && fs.existsSync(finalPath)) {
|
24
29
|
try {
|
25
|
-
data = JSON.parse(fs.readFileSync(
|
30
|
+
data = JSON.parse(fs.readFileSync(finalPath, 'utf-8'));
|
26
31
|
} catch (error) {
|
27
|
-
console.log(chalk.red(`Error parsing JSON file: ${
|
32
|
+
console.log(chalk.red(`Error parsing JSON file: ${finalPath}`));
|
28
33
|
console.error(error);
|
29
34
|
process.exit(1);
|
30
35
|
}
|
31
|
-
} else if (
|
32
|
-
console.log(chalk.red(`File not found: ${
|
36
|
+
} else if (finalPath) {
|
37
|
+
console.log(chalk.red(`File not found: ${finalPath}`));
|
33
38
|
process.exit(1);
|
34
39
|
} else if (fs.existsSync('./menu.json')) {
|
35
40
|
try {
|
package/src/header.js
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
import chalk from 'chalk';
|
2
|
+
|
3
|
+
export function displayHeader(data) {
|
4
|
+
const name = data.name || 'Custom Menu';
|
5
|
+
const description = data.description || 'A CLI Menu';
|
6
|
+
const lines = [name, description];
|
7
|
+
const maxLength = Math.max(...lines.map(line => line.length));
|
8
|
+
const boxWidth = maxLength + 4;
|
9
|
+
|
10
|
+
const topBorder = '╔' + '═'.repeat(boxWidth) + '╗';
|
11
|
+
const bottomBorder = '╚' + '═'.repeat(boxWidth) + '╝';
|
12
|
+
|
13
|
+
console.log(chalk.bold.blueBright(topBorder));
|
14
|
+
lines.forEach(line => {
|
15
|
+
const paddingTotal = boxWidth - line.length;
|
16
|
+
const paddingLeft = Math.floor(paddingTotal / 2);
|
17
|
+
const paddingRight = Math.ceil(paddingTotal / 2);
|
18
|
+
const paddedLine = `║${' '.repeat(paddingLeft)}${line}${' '.repeat(paddingRight)}║`;
|
19
|
+
console.log(chalk.bold.blueBright(paddedLine));
|
20
|
+
});
|
21
|
+
console.log(chalk.bold.blueBright(bottomBorder));
|
22
|
+
console.log('');
|
23
|
+
console.log(chalk.gray(`Developed by Mateus Medeiros - GitHub: @mateusmed`));
|
24
|
+
console.log('');
|
25
|
+
}
|
package/README-en.md
DELETED
@@ -1,89 +0,0 @@
|
|
1
|
-
# Custom Menu CLI
|
2
|
-
|
3
|
-
This is a command-line interface (CLI) tool that creates an interactive menu based on a JSON file. It's designed to simplify the execution of frequent commands in a terminal.
|
4
|
-
|
5
|
-
## Features
|
6
|
-
|
7
|
-
- Interactive menu in the terminal.
|
8
|
-
- Menu structure defined by a JSON file.
|
9
|
-
- Easy to configure and use.
|
10
|
-
- Support for command execution with confirmation.
|
11
|
-
|
12
|
-
## Installation
|
13
|
-
|
14
|
-
To install this tool globally, run the following command:
|
15
|
-
|
16
|
-
```bash
|
17
|
-
npm install -g custom-menu-cli
|
18
|
-
```
|
19
|
-
|
20
|
-
## Usage
|
21
|
-
|
22
|
-
To use the CLI, you can run the `custom-menu-cli` command, optionally passing the path to a JSON file. If no path is provided, it will look for a `menu.json` file in the current directory.
|
23
|
-
|
24
|
-
```bash
|
25
|
-
custom-menu-cli [path/to/your/menu.json]
|
26
|
-
```
|
27
|
-
|
28
|
-
## JSON Structure
|
29
|
-
|
30
|
-
The JSON file that defines the menu has the following structure:
|
31
|
-
|
32
|
-
```json
|
33
|
-
{
|
34
|
-
"name": "Deploy Menu",
|
35
|
-
"description": "Menu de navegação para deploys",
|
36
|
-
"options": [
|
37
|
-
{
|
38
|
-
"id": "1",
|
39
|
-
"name": "Projeto A",
|
40
|
-
"type": "navigation",
|
41
|
-
"options": [
|
42
|
-
{
|
43
|
-
"id": "1.1",
|
44
|
-
"name": "Down Service",
|
45
|
-
"type": "action",
|
46
|
-
"command": "echo 'Down A'",
|
47
|
-
"confirm": true
|
48
|
-
},
|
49
|
-
{
|
50
|
-
"id": "1.2",
|
51
|
-
"name": "Up Service",
|
52
|
-
"type": "action",
|
53
|
-
"command": "echo 'Up A'"
|
54
|
-
}
|
55
|
-
]
|
56
|
-
},
|
57
|
-
{
|
58
|
-
"id": "2",
|
59
|
-
"name": "Restart All",
|
60
|
-
"type": "custom-action",
|
61
|
-
"idList": ["1.1", "1.2"],
|
62
|
-
"confirm": true
|
63
|
-
}
|
64
|
-
]
|
65
|
-
}
|
66
|
-
```
|
67
|
-
|
68
|
-
### Fields
|
69
|
-
|
70
|
-
- `name`: The name of the menu.
|
71
|
-
- `description`: A brief description of the menu.
|
72
|
-
- `options`: An array of menu options.
|
73
|
-
- `id`: A unique identifier for the option.
|
74
|
-
- `name`: The text that will be displayed for the option.
|
75
|
-
- `type`: The type of option. It can be `action` (executes a command), `navigation` (opens a submenu) or `custom-action` (executes a list of commands from other actions).
|
76
|
-
- `command`: The command to be executed (if the type is `action`).
|
77
|
-
- `idList`: A list of ids from other actions to be executed (if the type is `custom-action`).
|
78
|
-
- `confirm`: A boolean that indicates whether a confirmation should be requested before executing the command.
|
79
|
-
- `options`: An array of sub-options (if the type is `navigation`).
|
80
|
-
|
81
|
-
## License
|
82
|
-
|
83
|
-
This project is licensed under the MIT License.
|
84
|
-
|
85
|
-
## Author
|
86
|
-
|
87
|
-
- **Mateus Medeiros**
|
88
|
-
- GitHub: [@mateusmed](https://github.com/mateusmed)
|
89
|
-
- LinkedIn: [Mateus Medeiros](https://www.linkedin.com/in/mateus-med/)
|