@mondaycom/apps-cli
Version:
A cli tool to manage apps (and monday-code projects) in monday.com
113 lines (112 loc) • 4.82 kB
JavaScript
import { Parser } from '@json2csv/plainjs';
import { Flags } from '@oclif/core';
import chalk from 'chalk';
import { format } from 'date-fns';
import { AuthenticatedCommand } from '../../commands-base/authenticated-command.js';
import { VAR_UNKNOWN } from '../../consts/messages.js';
import { DynamicChoicesService } from '../../services/dynamic-choices-service.js';
import { PromptService } from '../../services/prompt-service.js';
import { getStorageItemsExport } from '../../services/storage-service.js';
import { HttpError } from '../../types/errors/index.js';
import { FSError, saveToFile } from '../../utils/file-system.js';
import logger from '../../utils/logger.js';
const clientAccountNumberMessage = 'Client account number';
const fileFormatDescription = 'Optional, file format "CSV" or "JSON" (the default value is "JSON")';
const fileDirectoryDescription = 'Optional, file path';
const saveToCSV = async (itemsFound, csvPath) => {
const parser = new Parser({
fields: [
{
value: 'key',
label: 'Key name',
},
{
value: 'backendOnly',
label: 'can be used only for backend',
},
{
value: 'value',
label: 'Value',
},
],
});
const result = parser.parse(itemsFound.records);
await saveToFile(csvPath, result);
};
const saveToJSON = async (itemsFound, jsonPath) => {
await saveToFile(jsonPath, JSON.stringify(itemsFound.records));
};
export default class Search extends AuthenticatedCommand {
static description = 'Export all keys and values stored on monday for a specific customer account.';
static examples = [
'<%= config.bin %> <%= command.id %> -a APP_ID -c CLIENT_ACCOUNT_ID -d FILE_FULL_PATH -f FILE_FORMAT ',
];
static flags = Search.serializeFlags({
appId: Flags.integer({
char: 'a',
description: 'Select the app that you wish to retrieve the key for',
}),
clientAccountId: Flags.integer({
char: 'c',
description: `${clientAccountNumberMessage}.`,
}),
fileFormat: Flags.string({
char: 'f',
description: `${fileFormatDescription}.`,
}),
fileDirectory: Flags.string({
char: 'd',
description: `${fileDirectoryDescription}.`,
}),
});
async run() {
const { flags } = await this.parse(Search);
let { appId, clientAccountId, fileFormat, fileDirectory } = flags;
try {
if (!appId) {
appId = await DynamicChoicesService.chooseApp();
}
if (!clientAccountId) {
clientAccountId = await PromptService.promptInputNumber(`${clientAccountNumberMessage}:`, true);
}
const itemsFound = await getStorageItemsExport(appId, clientAccountId);
if (itemsFound && itemsFound.records) {
if (!fileFormat) {
fileFormat = 'json';
}
if (!['csv', 'json'].includes(fileFormat.toLowerCase())) {
throw new FSError(`file format must be "CSV" or "JSON".`);
}
if (!fileDirectory) {
fileDirectory = `${process.cwd()}/${format(new Date(), 'yyyyMMddHHmmss')}.${fileFormat.toLowerCase()}`;
}
let fileWasExportedSuccessfully = false;
if (fileFormat.toLowerCase() === 'csv') {
await saveToCSV(itemsFound, fileDirectory);
fileWasExportedSuccessfully = true;
}
if (fileFormat.toLowerCase() === 'json') {
await saveToJSON(itemsFound, fileDirectory);
fileWasExportedSuccessfully = true;
}
if (fileWasExportedSuccessfully) {
logger.log(chalk.blue(`Exported ${itemsFound.records.length} record(s) to file "${chalk.bold(fileDirectory)}" in the format of "${chalk.bold(fileFormat.toUpperCase())}".`));
}
}
else {
throw new Error('Unexpected error occurred or retrieved data in a bad format.');
}
this.preparePrintCommand(this, { appId, clientAccountId });
}
catch (error) {
logger.debug(error);
if (error instanceof HttpError || error instanceof FSError) {
logger.error(`\n ${chalk.italic(chalk.red(error.message))}`);
}
else {
logger.error(`An unknown error happened while fetching storage items status for app id - "${appId || VAR_UNKNOWN}"`);
}
process.exit(1);
}
}
}