custom-menu-cli 1.0.1 → 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 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
@@ -21,12 +21,33 @@ npm install -g custom-menu-cli
21
21
 
22
22
  ## Usage
23
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
+
24
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.
25
29
 
26
30
  ```bash
27
31
  custom-menu-cli [path/to/your/menu.json]
28
32
  ```
29
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
+
30
51
  ## JSON Structure
31
52
 
32
53
  The JSON file that defines the menu has the following structure:
package/index.js CHANGED
@@ -1,38 +1,24 @@
1
1
  #!/usr/bin/env node
2
- import chalk from 'chalk';
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
- (async () => {
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
- // Dynamic Header
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
- const topBorder = '╔' + '═'.repeat(boxWidth) + '╗';
22
- const bottomBorder = '╚' + '═'.repeat(boxWidth) + '╝';
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
- await showMenu(data);
38
- })();
22
+ if (process.argv[1] === fileURLToPath(import.meta.url)) {
23
+ runCli();
24
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "custom-menu-cli",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Menu interativo baseado em JSON para execução de comandos no terminal",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -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
- if (path && fs.existsSync(path)) {
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(path, 'utf-8'));
30
+ data = JSON.parse(fs.readFileSync(finalPath, 'utf-8'));
26
31
  } catch (error) {
27
- console.log(chalk.red(`Error parsing JSON file: ${path}`));
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 (path) {
32
- console.log(chalk.red(`File not found: ${path}`));
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
+ }