@tolinax/ayoune-cli 2024.2.6 → 2024.2.8

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/index.js CHANGED
@@ -1,16 +1,11 @@
1
- #! /usr/bin/env node
2
- import { Command } from "commander";
3
- import { createSpinner } from "nanospinner";
4
- import { initializeSettings } from "./lib/helpers/initializeSettings.js";
5
- import { createProgram } from "./lib/commands/createProgram.js";
6
- import path from "path";
7
- import { readFile } from "fs/promises";
8
- import * as process from "process";
9
- //Create new command instance
10
- //@ts-ignore
11
- const pkg = JSON.parse(await readFile(path.join(process.cwd(), "package.json"), "utf8"));
12
- const program = new Command();
13
- initializeSettings();
14
- //Setup spinner
15
- export const spinner = createSpinner("Getting data...");
16
- createProgram(program);
1
+ #! /usr/bin/env node
2
+ import { Command } from "commander";
3
+ import { createSpinner } from "nanospinner";
4
+ import { initializeSettings } from "./lib/helpers/initializeSettings.js";
5
+ import { createProgram } from "./lib/commands/createProgram.js";
6
+ //Create new command instance
7
+ const program = new Command();
8
+ initializeSettings();
9
+ //Setup spinner
10
+ export const spinner = createSpinner("Getting data...");
11
+ createProgram(program);
@@ -1,35 +1,35 @@
1
- import { parseInteger } from "../helpers/parseInt.js";
2
- import { promptCollectionWithModule } from "../prompts/promptCollectionWithModule.js";
3
- import { getModuleFromCollection } from "../models/getModuleFromCollection.js";
4
- import { handleListOperation } from "../operations/handleListOperation.js";
5
- import { saveFile } from "../helpers/saveFile.js";
6
- import { localStorage } from "../helpers/localStorage.js";
7
- export function createListCommand(program) {
8
- program
9
- .command("list [collection]")
10
- .alias("l")
11
- .description("List entries in collection")
12
- .option("-p, --page <number>", "Page", parseInteger, 1)
13
- .option("-l, --limit <number>", "Limit", parseInteger, 20)
14
- .option("-f, --from <date>", "From date")
15
- .action(async (collection, options) => {
16
- try {
17
- const opts = { ...program.opts(), ...options };
18
- if (!collection) {
19
- collection = await promptCollectionWithModule();
20
- }
21
- const module = getModuleFromCollection(collection);
22
- localStorage.setItem("lastModule", module.module);
23
- localStorage.setItem("lastCollection", collection);
24
- let result = {};
25
- let entry = "";
26
- result = await handleListOperation(module.module, collection, opts);
27
- if (opts.save) {
28
- await saveFile("list", opts, result);
29
- }
30
- }
31
- catch (e) {
32
- console.error(e);
33
- }
34
- });
35
- }
1
+ import { parseInteger } from "../helpers/parseInt.js";
2
+ import { promptCollectionWithModule } from "../prompts/promptCollectionWithModule.js";
3
+ import { getModuleFromCollection } from "../models/getModuleFromCollection.js";
4
+ import { handleListOperation } from "../operations/handleListOperation.js";
5
+ import { saveFile } from "../helpers/saveFile.js";
6
+ import { localStorage } from "../helpers/localStorage.js";
7
+ export function createListCommand(program) {
8
+ program
9
+ .command("list [collection]")
10
+ .alias("l")
11
+ .description("List entries in collection")
12
+ .option("-p, --page <number>", "Page", parseInteger, 1)
13
+ .option("-l, --limit <number>", "Limit", parseInteger, 20)
14
+ .option("-f, --from <date>", "From date")
15
+ .action(async (collection, options) => {
16
+ try {
17
+ const opts = { ...program.opts(), ...options };
18
+ if (!collection) {
19
+ collection = await promptCollectionWithModule();
20
+ }
21
+ const module = getModuleFromCollection(collection);
22
+ localStorage.setItem("lastModule", module.module);
23
+ localStorage.setItem("lastCollection", collection);
24
+ let result = {};
25
+ let entry = "";
26
+ result = await handleListOperation(module.module, collection, opts);
27
+ if (opts.save) {
28
+ await saveFile("list", opts, result);
29
+ }
30
+ }
31
+ catch (e) {
32
+ console.error(e);
33
+ }
34
+ });
35
+ }
@@ -1,33 +1,41 @@
1
- import { promptModule } from "../prompts/promptModule.js";
2
- import { promptCollectionInModule } from "../prompts/promptCollectionInModule.js";
3
- import { promptOperation } from "../prompts/promptOperation.js";
4
- import { handleListOperation } from "../operations/handleListOperation.js";
5
- import { promptEntry } from "../prompts/promptEntry.js";
6
- import { handleCollectionOperation } from "../operations/handleCollectionOperation.js";
7
- import { localStorage } from "../helpers/localStorage.js";
8
- export function createModulesCommand(program) {
9
- program
10
- .command("modules")
11
- .alias("m")
12
- .description("List modules")
13
- .action(async (message) => {
14
- try {
15
- console.log(message);
16
- const module = await promptModule();
17
- const collection = await promptCollectionInModule(module);
18
- const operation = await promptOperation();
19
- localStorage.setItem("lastModule", module);
20
- localStorage.setItem("lastCollection", collection);
21
- let result = {};
22
- let entry = "";
23
- if (operation === "list") {
24
- result = await handleListOperation(module, collection);
25
- entry = await promptEntry(result);
26
- }
27
- await handleCollectionOperation(module, collection, entry, operation);
28
- }
29
- catch (e) {
30
- console.error(e);
31
- }
32
- });
33
- }
1
+ import { promptModule } from "../prompts/promptModule.js";
2
+ import { promptCollectionInModule } from "../prompts/promptCollectionInModule.js";
3
+ import { promptOperation } from "../prompts/promptOperation.js";
4
+ import { handleListOperation } from "../operations/handleListOperation.js";
5
+ import { promptEntry } from "../prompts/promptEntry.js";
6
+ import { handleCollectionOperation } from "../operations/handleCollectionOperation.js";
7
+ import { localStorage } from "../helpers/localStorage.js";
8
+ import { parseInteger } from "../helpers/parseInt.js";
9
+ import { handleGetOperation } from "../operations/handleGetOperation";
10
+ export function createModulesCommand(program) {
11
+ program
12
+ .command("modules")
13
+ .alias("m")
14
+ .description("List modules")
15
+ .option("-p, --page <number>", "Page", parseInteger, 1)
16
+ .option("-l, --limit <number>", "Limit", parseInteger, 20)
17
+ .option("-f, --from <date>", "From date")
18
+ .action(async (options) => {
19
+ try {
20
+ const opts = { ...program.opts(), ...options };
21
+ const module = await promptModule();
22
+ const collection = await promptCollectionInModule(module);
23
+ const operation = await promptOperation();
24
+ localStorage.setItem("lastModule", module);
25
+ localStorage.setItem("lastCollection", collection);
26
+ let entry = "";
27
+ if (operation === "list") {
28
+ const { result } = await handleListOperation(module, collection, opts);
29
+ entry = await promptEntry(result);
30
+ }
31
+ if (operation === "get") {
32
+ const { result } = await handleGetOperation(module, collection, opts);
33
+ entry = await promptEntry(result);
34
+ }
35
+ await handleCollectionOperation(module, collection, entry, opts);
36
+ }
37
+ catch (e) {
38
+ console.error(e);
39
+ }
40
+ });
41
+ }
@@ -1,27 +1,42 @@
1
- // All the remaining operations are classified here
2
- import { apiCallHandler } from "../api/apiCallHandler.js";
3
- import { handleEditOperation } from "./handleEditOperation.js";
4
- import { handleEditRawOperation } from "./handleEditRawOperation.js";
5
- import { spinner } from "../../index.js";
6
- import { promptDefaultAction } from "../prompts/promptDefaultAction.js";
7
- export async function handleCollectionOperation(module, collection, entry, operation) {
8
- spinner.start({ text: "", color: "magenta" });
9
- const res = await apiCallHandler(`${module}/${collection.toLowerCase()}/${entry}?responseFormat=table`);
10
- const editContent = res.payload;
11
- console.log(editContent);
12
- spinner.success({
13
- text: `Got entry ${res.payload.name} - Took ${res.meta.responseTime} ms`,
14
- });
15
- spinner.stop();
16
- const action = await promptDefaultAction();
17
- switch (action) {
18
- case "edit":
19
- await handleEditOperation(module, collection, editContent);
20
- break;
21
- case "edit raw":
22
- await handleEditRawOperation(module, collection, editContent);
23
- break;
24
- default:
25
- break;
26
- }
27
- }
1
+ // All the remaining operations are classified here
2
+ import { apiCallHandler } from "../api/apiCallHandler.js";
3
+ import { handleEditOperation } from "./handleEditOperation.js";
4
+ import { handleEditRawOperation } from "./handleEditRawOperation.js";
5
+ import { spinner } from "../../index.js";
6
+ import { promptDefaultAction } from "../prompts/promptDefaultAction.js";
7
+ import yaml from "js-yaml";
8
+ import { promptFilePath } from "../prompts/promptFilePath.js";
9
+ import { promptFileName } from "../prompts/promptFileName.js";
10
+ import path from "path";
11
+ import { writeFile } from "fs/promises";
12
+ export async function handleCollectionOperation(module, collection, entry, opts) {
13
+ spinner.start({ text: "", color: "magenta" });
14
+ const res = await apiCallHandler(`${module}/${collection.toLowerCase()}/${entry}?responseFormat=table`);
15
+ const editContent = res.payload;
16
+ console.log(editContent);
17
+ spinner.success({
18
+ text: `Got entry ${res.payload.name} - Took ${res.meta.responseTime} ms`,
19
+ });
20
+ spinner.stop();
21
+ const action = await promptDefaultAction();
22
+ switch (action) {
23
+ case "edit":
24
+ await handleEditOperation(module, collection, editContent);
25
+ break;
26
+ case "edit raw":
27
+ await handleEditRawOperation(module, collection, editContent);
28
+ break;
29
+ case "describe":
30
+ console.log(yaml.dump(editContent));
31
+ break;
32
+ case "download":
33
+ const folder = await promptFilePath();
34
+ const fileName = await promptFileName(`${collection}_${editContent._id}.${opts.responseFormat}`);
35
+ await writeFile(path.join(folder, fileName), editContent, {
36
+ encoding: "utf8",
37
+ });
38
+ break;
39
+ default:
40
+ break;
41
+ }
42
+ }
@@ -1,35 +1,35 @@
1
- import inquirer from "inquirer";
2
- import { apiCallHandler } from "../api/apiCallHandler.js";
3
- import { spinner } from "../../index.js";
4
- export async function handleEditRawOperation(module, collection, result) {
5
- const { content } = await inquirer.prompt([
6
- {
7
- type: "editor",
8
- name: "content",
9
- postfix: ".json",
10
- message: "Edit",
11
- default: JSON.stringify(result, null, 4),
12
- },
13
- ]);
14
- const contentToSave = JSON.parse(content);
15
- const { save } = await inquirer.prompt([
16
- {
17
- type: "confirm",
18
- name: "save",
19
- message: "Save changes",
20
- },
21
- ]);
22
- if (save) {
23
- spinner.start({
24
- text: `Attempting to save [${contentToSave.name}]`,
25
- color: "magenta",
26
- });
27
- const res = await apiCallHandler(`${module}/${collection.toLowerCase()}`, "put", JSON.parse(content));
28
- result = res.payload;
29
- console.log(result);
30
- spinner.success({
31
- text: `Saved [${contentToSave.name}] - Took ${res.meta.responseTime} ms`,
32
- });
33
- spinner.stop();
34
- }
35
- }
1
+ import inquirer from "inquirer";
2
+ import { apiCallHandler } from "../api/apiCallHandler.js";
3
+ import { spinner } from "../../index.js";
4
+ export async function handleEditRawOperation(module, collection, result) {
5
+ const { content } = await inquirer.prompt([
6
+ {
7
+ type: "editor",
8
+ name: "content",
9
+ postfix: ".json",
10
+ message: "Edit",
11
+ default: JSON.stringify(result, null, 4),
12
+ },
13
+ ]);
14
+ const contentToSave = JSON.parse(content);
15
+ const { save } = await inquirer.prompt([
16
+ {
17
+ type: "confirm",
18
+ name: "save",
19
+ message: "Save changes",
20
+ },
21
+ ]);
22
+ if (save) {
23
+ spinner.start({
24
+ text: `Attempting to save [${contentToSave.name}]`,
25
+ color: "magenta",
26
+ });
27
+ const res = await apiCallHandler(`${module}/${collection.toLowerCase()}`, "put", JSON.parse(content));
28
+ result = res.payload;
29
+ console.log(result);
30
+ spinner.success({
31
+ text: `Saved [${contentToSave.name}] - Took ${res.meta.responseTime} ms`,
32
+ });
33
+ spinner.stop();
34
+ }
35
+ }
@@ -0,0 +1,11 @@
1
+ import inquirer from "inquirer";
2
+ export async function promptFilePath() {
3
+ const { path } = await inquirer.prompt([
4
+ {
5
+ type: "fuzzypath",
6
+ name: "path",
7
+ message: "Select a target directory:",
8
+ },
9
+ ]);
10
+ return path;
11
+ }
@@ -1,15 +1,15 @@
1
- import inquirer from "inquirer";
2
- export async function promptAudits(result) {
3
- const { audit } = await inquirer.prompt([
4
- {
5
- type: "list",
6
- name: "audit",
7
- message: "Choose a audit:",
8
- choices: result.map((el) => ({
9
- name: `${el.action} at ${el.ts} by ${el._userID.first_name} ${el._userID.last_name}`,
10
- value: el._id,
11
- })),
12
- },
13
- ]);
14
- return audit;
15
- }
1
+ import inquirer from "inquirer";
2
+ export async function promptAudits(result) {
3
+ const { audit } = await inquirer.prompt([
4
+ {
5
+ type: "list",
6
+ name: "audit",
7
+ message: "Choose a audit:",
8
+ choices: result.map((el) => ({
9
+ name: `${el.action} at ${el.ts} by ${el._userID.first_name} ${el._userID.last_name}`,
10
+ value: el._id,
11
+ })),
12
+ },
13
+ ]);
14
+ return audit;
15
+ }
@@ -0,0 +1,12 @@
1
+ import inquirer from "inquirer";
2
+ export async function promptFileName(name) {
3
+ const { fileName } = await inquirer.prompt([
4
+ {
5
+ type: "text",
6
+ name: "fileName",
7
+ message: "Specify a file name",
8
+ default: name,
9
+ },
10
+ ]);
11
+ return fileName;
12
+ }
@@ -0,0 +1,11 @@
1
+ import inquirer from "inquirer";
2
+ export async function promptFilePath() {
3
+ const { path } = await inquirer.prompt([
4
+ {
5
+ type: "fuzzypath",
6
+ name: "path",
7
+ message: "Select a target directory:",
8
+ },
9
+ ]);
10
+ return path;
11
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tolinax/ayoune-cli",
3
- "version": "2024.2.6",
3
+ "version": "2024.2.8",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./index.js",