@tolinax/ayoune-cli 2024.2.16 → 2024.2.17

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.
@@ -1,43 +1,43 @@
1
- import yaml from "js-yaml";
2
- export function handleResponseFormatOptions(opts, res) {
3
- let plainResult;
4
- let result;
5
- let meta = {};
6
- let content;
7
- if (opts.responseFormat && opts.responseFormat === "yaml") {
8
- plainResult = yaml.load(res);
9
- result = plainResult.payload;
10
- meta = plainResult.meta;
11
- content = yaml.dump(result);
12
- console.log("\n");
13
- console.log(content);
14
- console.log("\n");
15
- }
16
- if (opts.responseFormat && opts.responseFormat === "csv") {
17
- plainResult = res;
18
- result = res.payload;
19
- meta = res.meta;
20
- content = result;
21
- console.log("\n");
22
- console.log(content);
23
- console.log("\n");
24
- }
25
- if (opts.responseFormat && opts.responseFormat === "table") {
26
- plainResult = res;
27
- result = res.payload;
28
- meta = res.meta;
29
- content = result;
30
- console.log("\n");
31
- console.log(res);
32
- console.log("\n");
33
- }
34
- if (opts.responseFormat && opts.responseFormat === "json") {
35
- result = res.payload;
36
- meta = res.meta;
37
- content = JSON.stringify(result, null, 4);
38
- console.log("\n");
39
- console.table(result);
40
- console.log("\n");
41
- }
42
- return { plainResult, result, meta, content };
43
- }
1
+ import yaml from "js-yaml";
2
+ export function handleResponseFormatOptions(opts, res) {
3
+ let plainResult;
4
+ let result;
5
+ let meta = {};
6
+ let content;
7
+ if (opts.responseFormat && opts.responseFormat === "yaml") {
8
+ plainResult = yaml.load(res);
9
+ result = plainResult.payload;
10
+ meta = plainResult.meta;
11
+ content = yaml.dump(result);
12
+ console.log("\n");
13
+ console.log(content);
14
+ console.log("\n");
15
+ }
16
+ if (opts.responseFormat && opts.responseFormat === "csv") {
17
+ plainResult = res;
18
+ result = res.payload;
19
+ meta = res.meta;
20
+ content = result;
21
+ console.log("\n");
22
+ console.log(content);
23
+ console.log("\n");
24
+ }
25
+ if (opts.responseFormat && opts.responseFormat === "table") {
26
+ plainResult = res;
27
+ result = res.payload;
28
+ meta = res.meta;
29
+ content = result;
30
+ console.log("\n");
31
+ console.log(res);
32
+ console.log("\n");
33
+ }
34
+ if (opts.responseFormat && opts.responseFormat === "json") {
35
+ result = res.payload;
36
+ meta = res.meta;
37
+ content = JSON.stringify(result, null, 4);
38
+ console.log("\n");
39
+ console.table(result);
40
+ console.log("\n");
41
+ }
42
+ return { plainResult, result, meta, content };
43
+ }
@@ -0,0 +1,27 @@
1
+ import { randomBytes } from "crypto";
2
+ /** Sync */
3
+ const randomString = (length, chars) => {
4
+ if (!chars) {
5
+ throw new Error("Argument 'chars' is undefined");
6
+ }
7
+ const charsLength = chars.length;
8
+ if (charsLength > 256) {
9
+ throw new Error("Argument 'chars' should not have more than 256 characters" +
10
+ ", otherwise unpredictability will be broken");
11
+ }
12
+ const _randomBytes = randomBytes(length);
13
+ let result = new Array(length);
14
+ let cursor = 0;
15
+ for (let i = 0; i < length; i++) {
16
+ cursor += _randomBytes[i];
17
+ result[i] = chars[cursor % charsLength];
18
+ }
19
+ return result.join("");
20
+ };
21
+ /** Sync */
22
+ export const randomAsciiString = (length = 32, alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") => {
23
+ if (typeof length === "undefined") {
24
+ length = 32;
25
+ }
26
+ return randomString(length, alphabet);
27
+ };
@@ -1,4 +1,4 @@
1
- import { modelsAndRights } from "@tolinax/ayoune-models/data/modelsAndRights.js";
1
+ import { modelsAndRights } from "../../data/modelsAndRights.js";
2
2
  import { addSpacesToCamelCase } from "../helpers/addSpacesToCamelCase.js";
3
3
  const getCollections = () => {
4
4
  return modelsAndRights
@@ -1,6 +1,6 @@
1
1
  import _ from "lodash";
2
2
  import { addSpacesToCamelCase } from "../helpers/addSpacesToCamelCase.js";
3
- import { modelsAndRights } from "@tolinax/ayoune-models/data/modelsAndRights.js";
3
+ import { modelsAndRights } from "../../data/modelsAndRights.js";
4
4
  const getModelsInModules = (module) => {
5
5
  const m = _.filter(modelsAndRights, { module });
6
6
  return m.map((el) => {
@@ -1,5 +1,5 @@
1
1
  import _ from "lodash";
2
- import { modelsAndRights } from "@tolinax/ayoune-models/data/modelsAndRights.js";
2
+ import { modelsAndRights } from "../../data/modelsAndRights.js";
3
3
  const getModuleFromCollection = (collection) => {
4
4
  const m = _.find(modelsAndRights, (el) => el.plural.toLowerCase() === collection);
5
5
  return m;
@@ -1,68 +1,68 @@
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
- import { mkdirp } from "mkdirp";
13
- export async function handleCollectionOperation(module, collection, entry, opts) {
14
- spinner.start({ text: "", color: "magenta" });
15
- let res;
16
- let editContent;
17
- const action = await promptDefaultAction();
18
- switch (action) {
19
- case "edit":
20
- res = await apiCallHandler(`${module}/${collection.toLowerCase()}/${entry}?responseFormat=table`);
21
- spinner.success({
22
- text: `Got entry ${res.payload.name} - Took ${res.meta.responseTime} ms`,
23
- });
24
- spinner.stop();
25
- editContent = res.payload;
26
- await handleEditOperation(module, collection, editContent);
27
- break;
28
- case "edit raw":
29
- res = await apiCallHandler(`${module}/${collection.toLowerCase()}/${entry}?responseFormat=${opts.responseFormat}`);
30
- spinner.success({
31
- text: `Got entry ${res.payload.name} - Took ${res.meta.responseTime} ms`,
32
- });
33
- spinner.stop();
34
- editContent = res.payload;
35
- await handleEditRawOperation(module, collection, editContent);
36
- break;
37
- case "describe":
38
- res = await apiCallHandler(`${module}/${collection.toLowerCase()}/${entry}?responseFormat=yaml}`);
39
- spinner.success({
40
- text: `Got entry ${res.payload.name} - Took ${res.meta.responseTime} ms`,
41
- });
42
- spinner.stop();
43
- editContent = res.payload;
44
- console.log(yaml.dump(editContent));
45
- break;
46
- case "download":
47
- res = await apiCallHandler(`${module}/${collection.toLowerCase()}/${entry}?responseFormat=${opts.responseFormat}}`);
48
- spinner.success({
49
- text: `Got entry ${res.payload.name} - Took ${res.meta.responseTime} ms`,
50
- });
51
- spinner.stop();
52
- editContent = res.payload;
53
- const folder = await promptFilePath(collection);
54
- await mkdirp(folder);
55
- const fileName = await promptFileName(`${collection}_${editContent._id}.${opts.responseFormat}`);
56
- const contentToWrite = opts.responseFormat === "yaml"
57
- ? yaml.dump(editContent)
58
- : opts.responseFormat === "csv"
59
- ? editContent
60
- : JSON.stringify(editContent, null, 4);
61
- await writeFile(path.join(folder, fileName), contentToWrite, {
62
- encoding: "utf8",
63
- });
64
- break;
65
- default:
66
- break;
67
- }
68
- }
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
+ import { mkdirp } from "mkdirp";
13
+ export async function handleCollectionOperation(module, collection, entry, opts) {
14
+ spinner.start({ text: "", color: "magenta" });
15
+ let res;
16
+ let editContent;
17
+ const action = await promptDefaultAction();
18
+ switch (action) {
19
+ case "edit":
20
+ res = await apiCallHandler(`${module}/${collection.toLowerCase()}/${entry}?responseFormat=table`);
21
+ spinner.success({
22
+ text: `Got entry ${res.payload.name} - Took ${res.meta.responseTime} ms`,
23
+ });
24
+ spinner.stop();
25
+ editContent = res.payload;
26
+ await handleEditOperation(module, collection, editContent);
27
+ break;
28
+ case "edit raw":
29
+ res = await apiCallHandler(`${module}/${collection.toLowerCase()}/${entry}?responseFormat=${opts.responseFormat}`);
30
+ spinner.success({
31
+ text: `Got entry ${res.payload.name} - Took ${res.meta.responseTime} ms`,
32
+ });
33
+ spinner.stop();
34
+ editContent = res.payload;
35
+ await handleEditRawOperation(module, collection, editContent);
36
+ break;
37
+ case "describe":
38
+ res = await apiCallHandler(`${module}/${collection.toLowerCase()}/${entry}?responseFormat=yaml}`);
39
+ spinner.success({
40
+ text: `Got entry ${res.payload.name} - Took ${res.meta.responseTime} ms`,
41
+ });
42
+ spinner.stop();
43
+ editContent = res.payload;
44
+ console.log(yaml.dump(editContent));
45
+ break;
46
+ case "download":
47
+ res = await apiCallHandler(`${module}/${collection.toLowerCase()}/${entry}?responseFormat=${opts.responseFormat}}`);
48
+ spinner.success({
49
+ text: `Got entry ${res.payload.name} - Took ${res.meta.responseTime} ms`,
50
+ });
51
+ spinner.stop();
52
+ editContent = res.payload;
53
+ const folder = await promptFilePath(collection);
54
+ await mkdirp(folder);
55
+ const fileName = await promptFileName(`${collection}_${editContent._id}.${opts.responseFormat}`);
56
+ const contentToWrite = opts.responseFormat === "yaml"
57
+ ? yaml.dump(editContent)
58
+ : opts.responseFormat === "csv"
59
+ ? editContent
60
+ : JSON.stringify(editContent, null, 4);
61
+ await writeFile(path.join(folder, fileName), contentToWrite, {
62
+ encoding: "utf8",
63
+ });
64
+ break;
65
+ default:
66
+ break;
67
+ }
68
+ }
@@ -1,27 +1,27 @@
1
- // Operation handling functions
2
- import { apiCallHandler } from "../api/apiCallHandler.js";
3
- import { spinner } from "../../index.js";
4
- import { handleResponseFormatOptions } from "../helpers/handleResponseFormatOptions.js";
5
- import { localStorage } from "../helpers/localStorage.js";
6
- export async function handleCreateSingleOperation(module, collection, name, opts) {
7
- spinner.start({
8
- text: `Creating entry in [${collection}]`,
9
- color: "magenta",
10
- });
11
- let res = await apiCallHandler(`${module}/${collection.toLowerCase()}`, "post", {
12
- name,
13
- title: name,
14
- subject: name,
15
- summary: name,
16
- }, {
17
- responseFormat: opts.responseFormat,
18
- verbosity: opts.verbosity,
19
- });
20
- let { plainResult, result, meta, content } = handleResponseFormatOptions({ ...opts }, res);
21
- spinner.success({
22
- text: `Created entry [${result._id}] in ${collection}`,
23
- });
24
- spinner.stop();
25
- localStorage.setItem("lastId", result._id);
26
- return { data: plainResult, content, result, meta };
27
- }
1
+ // Operation handling functions
2
+ import { apiCallHandler } from "../api/apiCallHandler.js";
3
+ import { spinner } from "../../index.js";
4
+ import { handleResponseFormatOptions } from "../helpers/handleResponseFormatOptions.js";
5
+ import { localStorage } from "../helpers/localStorage.js";
6
+ export async function handleCreateSingleOperation(module, collection, name, opts) {
7
+ spinner.start({
8
+ text: `Creating entry in [${collection}]`,
9
+ color: "magenta",
10
+ });
11
+ let res = await apiCallHandler(`${module}/${collection.toLowerCase()}`, "post", {
12
+ name,
13
+ title: name,
14
+ subject: name,
15
+ summary: name,
16
+ }, {
17
+ responseFormat: opts.responseFormat,
18
+ verbosity: opts.verbosity,
19
+ });
20
+ let { plainResult, result, meta, content } = handleResponseFormatOptions({ ...opts }, res);
21
+ spinner.success({
22
+ text: `Created entry [${result._id}] in ${collection}`,
23
+ });
24
+ spinner.stop();
25
+ localStorage.setItem("lastId", result._id);
26
+ return { data: plainResult, content, result, meta };
27
+ }
@@ -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
+ }
@@ -1,25 +1,25 @@
1
- // Operation handling functions
2
- import { apiCallHandler } from "../api/apiCallHandler.js";
3
- import { spinner } from "../../index.js";
4
- import { handleResponseFormatOptions } from "../helpers/handleResponseFormatOptions.js";
5
- export async function handleGetOperation(module, collection, opts) {
6
- spinner.start({
7
- text: `Getting entries in [${collection}]`,
8
- color: "magenta",
9
- });
10
- let res = await apiCallHandler(`${module}/${collection.toLowerCase()}`, "get", null, {
11
- page: opts.page,
12
- responseFormat: opts.responseFormat,
13
- limit: opts.limit,
14
- from: opts.from,
15
- fields: opts.fields,
16
- hideMeta: opts.hideMeta,
17
- verbosity: opts.verbosity,
18
- });
19
- let { plainResult, result, meta, content } = handleResponseFormatOptions(opts, res);
20
- spinner.success({
21
- text: `Got ${opts.limit} entries of ${meta.pageInfo.totalEntries} : Page ${meta.pageInfo.page} of ${meta.pageInfo.totalPages}`,
22
- });
23
- spinner.stop();
24
- return { data: plainResult, content, result, meta };
25
- }
1
+ // Operation handling functions
2
+ import { apiCallHandler } from "../api/apiCallHandler.js";
3
+ import { spinner } from "../../index.js";
4
+ import { handleResponseFormatOptions } from "../helpers/handleResponseFormatOptions.js";
5
+ export async function handleGetOperation(module, collection, opts) {
6
+ spinner.start({
7
+ text: `Getting entries in [${collection}]`,
8
+ color: "magenta",
9
+ });
10
+ let res = await apiCallHandler(`${module}/${collection.toLowerCase()}`, "get", null, {
11
+ page: opts.page,
12
+ responseFormat: opts.responseFormat,
13
+ limit: opts.limit,
14
+ from: opts.from,
15
+ fields: opts.fields,
16
+ hideMeta: opts.hideMeta,
17
+ verbosity: opts.verbosity,
18
+ });
19
+ let { plainResult, result, meta, content } = handleResponseFormatOptions(opts, res);
20
+ spinner.success({
21
+ text: `Got ${opts.limit} entries of ${meta.pageInfo.totalEntries} : Page ${meta.pageInfo.page} of ${meta.pageInfo.totalPages}`,
22
+ });
23
+ spinner.stop();
24
+ return { data: plainResult, content, result, meta };
25
+ }
@@ -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
+ }
@@ -1,12 +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
- }
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
+ }
@@ -1,18 +1,18 @@
1
- import inquirer from "inquirer";
2
- import os from "os";
3
- import path from "path";
4
- export async function promptFilePath(fp) {
5
- const { filePath } = await inquirer.prompt([
6
- {
7
- type: "fuzzypath",
8
- name: "filePath",
9
- message: "Select a target directory:",
10
- itemType: "directory",
11
- default: fp,
12
- rootPath: path.join(os.homedir(), "aYOUne"),
13
- suggestOnly: false,
14
- depthLimit: 8,
15
- },
16
- ]);
17
- return filePath;
18
- }
1
+ import inquirer from "inquirer";
2
+ import os from "os";
3
+ import path from "path";
4
+ export async function promptFilePath(fp) {
5
+ const { filePath } = await inquirer.prompt([
6
+ {
7
+ type: "fuzzypath",
8
+ name: "filePath",
9
+ message: "Select a target directory:",
10
+ itemType: "directory",
11
+ default: fp,
12
+ rootPath: path.join(os.homedir(), "aYOUne"),
13
+ suggestOnly: false,
14
+ depthLimit: 8,
15
+ },
16
+ ]);
17
+ return filePath;
18
+ }
@@ -1,6 +1,6 @@
1
1
  // Prompt functions
2
2
  import inquirer from "inquirer";
3
- import { aYOUneModules } from "@tolinax/ayoune-models/data/modules.js";
3
+ import { aYOUneModules } from "../../data/modules.js";
4
4
  export async function promptModule() {
5
5
  const { module } = await inquirer.prompt([
6
6
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tolinax/ayoune-cli",
3
- "version": "2024.2.16",
3
+ "version": "2024.2.17",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -102,7 +102,6 @@
102
102
  }
103
103
  },
104
104
  "dependencies": {
105
- "@tolinax/ayoune-models": "^2024.2.29",
106
105
  "axios": "^1.6.7",
107
106
  "axios-retry": "^4.0.0",
108
107
  "chalk": "^5.3.0",
@@ -1,11 +0,0 @@
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
- }