Skip to content

Commit

Permalink
Implement consola
Browse files Browse the repository at this point in the history
  • Loading branch information
AgustinSilverfin committed Oct 13, 2023
1 parent 8203312 commit 3c41278
Show file tree
Hide file tree
Showing 20 changed files with 242 additions and 186 deletions.
24 changes: 14 additions & 10 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ const devMode = require("../lib/cli/devMode");
const { firmCredentials } = require("../lib/api/firmCredentials");
const SF = require("../lib/api/sfApi");
const path = require("path");
const { consola } = require("consola");

let firmIdDefault = cliUtils.loadDefaultFirmId();
cliUtils.handleUncaughtErrors();

// Name & Version
program.name("silverfin");
if (pkg.version) {
program.version(pkg.version);
}
pkg.version ? program.version(pkg.version) : undefined;
// Verbose Option
program.option("-v, --verbose", "Verbose output");
program.on("option:verbose", () => {
consola.level = "debug"; // default: "info"
});

// READ reconciliations
program
Expand Down Expand Up @@ -459,7 +463,7 @@ program
);
} else {
if (options.previewOnly && !options.htmlInput && !options.htmlPreview) {
console.log(
consola.info(
`When using "--preview-only" you need to specify at least one of the following options: "--html-input", "--html-preview"`
);
process.exit(1);
Expand Down Expand Up @@ -545,22 +549,22 @@ program
if (options.setFirm) {
firmCredentials.setDefaultFirmId(options.setFirm);
const currentDirectory = path.basename(process.cwd());
console.log(`${currentDirectory}: firm id set to ${options.setFirm}`);
consola.success(`${currentDirectory}: firm id set to ${options.setFirm}`);
}
if (options.getFirm) {
const storedFirmId = firmCredentials.getDefaultFirmId();
if (storedFirmId) {
console.log(`Firm id previously stored: ${storedFirmId}`);
consola.info(`Firm id previously stored: ${storedFirmId}`);
} else {
console.log("There is no firm id previously stored");
consola.info("There is no firm id previously stored");
}
}
if (options.listAll) {
const firms = firmCredentials.listAuthorizedFirms() || [];
if (firms) {
console.log("List of authorized firms");
consola.info("List of authorized firms");
firms.forEach((element) =>
console.log(`- ${element[0]}${element[1] ? ` (${element[1]})` : ""}`)
consola.log(`- ${element[0]}${element[1] ? ` (${element[1]})` : ""}`)
);
}
}
Expand Down Expand Up @@ -710,5 +714,5 @@ if (pkg.repository && pkg.repository.url) {
(async function () {
// Check if there is a new version available
await cliUpdates.checkVersions();
program.parse();
await program.parseAsync();
})();
84 changes: 50 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ const { ReconciliationText } = require("./lib/reconciliationText");
const { SharedPart } = require("./lib/sharedPart");
const { firmCredentials } = require("./lib/api/firmCredentials");
const { ExportFile } = require("./lib/exportFile");
const { consola } = require("consola");

async function fetchReconciliationByHandle(firmId, handle) {
const template = await SF.findReconciliationTextByHandle(firmId, handle);
if (!template) {
throw `Reconciliation ${handle} wasn't found`;
consola.error(`Reconciliation "${handle}" wasn't found`);
process.exit(1);
}
ReconciliationText.save(firmId, template);
consola.success(`Reconciliation "${handle}" imported`);
}

async function fetchReconciliationById(firmId, id) {
Expand All @@ -29,7 +32,7 @@ async function fetchAllReconciliations(firmId, page = 1) {
const templates = await SF.readReconciliationTexts(firmId, page);
if (templates.length == 0) {
if (page == 1) {
console.log("No reconciliations found");
consola.error(`No reconciliations found in firm ${firmId}`);
}
return;
}
Expand All @@ -50,7 +53,7 @@ async function publishReconciliationByHandle(
errorUtils.missingReconciliationId(handle);
}
let templateId = templateConfig.id[firmId];
console.log(`Updating reconciliation ${handle}...`);
consola.debug(`Updating reconciliation ${handle}...`);
const template = await ReconciliationText.read(handle);
template.version_comment = message;
const response = await SF.updateReconciliationText(
Expand All @@ -59,10 +62,10 @@ async function publishReconciliationByHandle(
template
);
if (response && response.data && response.data.handle) {
console.log(`Reconciliation updated: ${response.data.handle}`);
consola.success(`Reconciliation updated: ${response.data.handle}`);
return true;
} else {
console.log(`Reconciliation update failed: ${handle}`);
consola.error(`Reconciliation update failed: ${handle}`);
return false;
}
} catch (error) {
Expand Down Expand Up @@ -92,8 +95,8 @@ async function newReconciliation(
handle
);
if (existingTemplate) {
console.log(
`Reconciliation ${handle} already exists. Skipping its creation`
consola.info(
`Reconciliation "${handle}" already exists. Skipping its creation`
);
return;
}
Expand All @@ -104,6 +107,7 @@ async function newReconciliation(
// Store new id
if (response && response.status == 201) {
ReconciliationText.updateTemplateId(firmId, handle, response.data.id);
consola.success(`Reconciliation "${handle}" created`);
}
} catch (error) {
errorUtils.errorHandler(error);
Expand All @@ -123,24 +127,28 @@ async function newAllReconciliations(
async function fetchExportFileByHandle(firmId, name) {
const template = await SF.findExportFileByName(firmId, name);
if (!template) {
throw `Export file ${name} wasn't found`;
consola.error(`Export file "${name}" wasn't found`);
process.exit(1);
}
ExportFile.save(firmId, template);
consola.success(`Export file "${name}" imported`);
}

async function fetchExportFileById(firmId, id) {
const template = await SF.readExportFileById(firmId, id);
if (!template) {
throw `Export file with id ${id} wasn't found`;
consola.error(`Export file with id ${id} wasn't found`);
process.exit(1);
}
ExportFile.save(firmId, template);
consola.success(`Export file "${template.name}" imported`);
}

async function fetchAllExportFiles(firmId, page = 1) {
const templates = await SF.readExportFiles(firmId, page);
if (templates.length == 0) {
if (page == 1) {
console.log("No export files found");
consola.error(`No export files found in firm ${firmId}`);
}
return;
}
Expand All @@ -161,15 +169,15 @@ async function publishExportFileByName(
errorUtils.missingExportFileId(name);
}
let templateId = templateConfig.id[firmId];
console.log(`Updating export file ${name}...`);
consola.debug(`Updating export file ${name}...`);
const template = await ExportFile.read(name);
template.version_comment = message;
const response = await SF.updateExportFile(firmId, templateId, template);
if (response && response.data && response.data.name) {
console.log(`Export file updated: ${response.data.name}`);
consola.success(`Export file updated: ${response.data.name}`);
return true;
} else {
console.log(`Export file update failed: ${name}`);
consola.error(`Export file update failed: ${name}`);
return false;
}
} catch (error) {
Expand All @@ -196,8 +204,8 @@ async function newExportFile(
try {
const existingTemplate = await SF.findExportFileByName(firmId, name);
if (existingTemplate) {
console.log(
`Reconciliation ${name} already exists. Skipping its creation`
consola.info(
`Export file "${name}" already exists. Skipping its creation`
);
return;
}
Expand All @@ -208,6 +216,7 @@ async function newExportFile(
// Store new id
if (response && response.status == 201) {
ExportFile.updateTemplateId(firmId, name, response.data.id);
consola.success(`Export file "${name}" created`);
}
} catch (error) {
errorUtils.errorHandler(error);
Expand All @@ -233,15 +242,17 @@ async function importExistingSharedPartById(firmId, id) {
const sharedPart = await SF.readSharedPartById(firmId, id);

if (!sharedPart) {
throw `Shared part ${id} wasn't found.`;
consola.error(`Shared part ${id} wasn't found.`);
process.exit(1);
}
await SharedPart.save(firmId, sharedPart.data);
consola.success(`Shared part "${sharedPart.data.name}" imported`);
}

async function fetchSharedPartByName(firmId, name) {
const sharedPartByName = await SF.findSharedPartByName(firmId, name);
if (!sharedPartByName) {
throw `Shared part with name ${name} wasn't found.`;
consola.error(`Shared part "${name}" wasn't found.`);
}
return importExistingSharedPartById(firmId, sharedPartByName.id);
}
Expand All @@ -251,7 +262,7 @@ async function fetchAllSharedParts(firmId, page = 1) {
const sharedParts = response.data;
if (sharedParts.length == 0) {
if (page == 1) {
console.log(`No shared parts found`);
consola.error(`No shared parts found in firm ${firmId}`);
}
return;
}
Expand All @@ -271,7 +282,7 @@ async function publishSharedPartByName(
if (!templateConfig || !templateConfig.id[firmId]) {
errorUtils.missingSharedPartId(name);
}
console.log(`Updating shared part ${name}...`);
consola.debug(`Updating shared part ${name}...`);
const template = await SharedPart.read(name);
template.version_comment = message;
const response = await SF.updateSharedPart(
Expand All @@ -280,10 +291,10 @@ async function publishSharedPartByName(
template
);
if (response && response.data && response.data.name) {
console.log(`Shared part updated: ${response.data.name}`);
consola.success(`Shared part updated: ${response.data.name}`);
return true;
} else {
console.log(`Shared part update failed: ${name}`);
consola.error(`Shared part update failed: ${name}`);
return false;
}
} catch (error) {
Expand All @@ -310,7 +321,9 @@ async function newSharedPart(
try {
const existingSharedPart = await SF.findSharedPartByName(firmId, name);
if (existingSharedPart) {
console.log(`Shared part ${name} already exists. Skipping its creation`);
consola.info(
`Shared part "${name}" already exists. Skipping its creation`
);
return;
}
const template = await SharedPart.read(name);
Expand All @@ -320,6 +333,7 @@ async function newSharedPart(
// Store new firm id
if (response && response.status == 201) {
SharedPart.updateTemplateId(firmId, name, response.data.id);
consola.success(`Shared part "${name}" created`);
}
} catch (error) {
errorUtils.errorHandler(error);
Expand Down Expand Up @@ -389,12 +403,12 @@ async function addSharedPart(

// Success or failure
if (!response || !response.status || !response.status === 201) {
console.log(
consola.warn(
`Adding shared part "${sharedPartName}" to "${templateHandle}" failed (${templateType}).`
);
return false;
}
console.log(
consola.success(
`Shared part "${sharedPartName}" added to "${templateHandle}" (${templateType}).`
);

Expand Down Expand Up @@ -444,13 +458,15 @@ async function addAllSharedParts(firmId) {
for await (let template of configSharedPart.used_in) {
template = SharedPart.checkReconciliationType(template);
if (!template.handle && !template.name) {
console.log(`Template has no handle or name. Skipping.`);
consola.warn(`Template has no handle or name. Skipping.`);
continue;
}
const folder = fsUtils.FOLDERS[template.type];
const handle = template.handle || template.name;
if (!fs.existsSync(`./${folder}/${handle}`)) {
console.log(`Template ${template.type} ${handle} not found. Skipping.`);
consola.warn(
`Template ${template.type} ${handle} not found. Skipping.`
);
continue;
}
addSharedPart(firmId, configSharedPart.name, handle, template.type);
Expand All @@ -468,13 +484,13 @@ async function removeSharedPart(
const configTemplate = fsUtils.readConfig(templateType, templateHandle);
const configSharedPart = fsUtils.readConfig("sharedPart", sharedPartHandle);
if (!configTemplate.id[firmId]) {
console.log(
consola.warn(
`Template id not found for ${templateHandle} (${templateType}). Skipping.`
);
return false;
}
if (!configSharedPart.id[firmId]) {
console.log(`Shared part id not found for ${templateHandle}. Skipping.`);
consola.warn(`Shared part id not found for ${templateHandle}. Skipping.`);
return false;
}
// Remove shared part from template
Expand All @@ -499,7 +515,7 @@ async function removeSharedPart(
);

if (response.status === 200) {
console.log(
consola.success(
`Shared part "${sharedPartHandle}" removed from template "${templateHandle}" (${templateType}).`
);
}
Expand Down Expand Up @@ -527,6 +543,7 @@ async function removeSharedPart(
// Look for the template in Silverfin with the handle/name and get it's ID
// Type has to be either "reconciliationText", "exportFile". "accountTemplate" or "sharedPart"
async function getTemplateId(firmId, type, handle) {
consola.debug(`Getting ID for ${handle}...`);
let templateText;
switch (type) {
case "reconciliationText":
Expand All @@ -543,7 +560,7 @@ async function getTemplateId(firmId, type, handle) {
break;
}
if (!templateText) {
console.log(`Template ${handle} wasn't found (${type})`);
consola.debug(`Template ${handle} wasn't found (${type})`);
return false;
}
const config = fsUtils.readConfig(type, handle);
Expand All @@ -552,7 +569,7 @@ async function getTemplateId(firmId, type, handle) {
}
config.id[firmId] = templateText.id;
fsUtils.writeConfig(type, handle, config);
console.log(`Template ${handle}: ID updated (${type})`);
consola.debug(`Template ${handle}: ID updated (${type})`);
return true;
}

Expand All @@ -566,7 +583,6 @@ async function getAllTemplatesId(firmId, type) {
if (!handle) {
continue;
}
console.log(`Getting ID for ${handle}...`);
await getTemplateId(firmId, type, handle);
}
} catch (error) {
Expand All @@ -578,11 +594,11 @@ async function updateFirmName(firmId) {
try {
const firmDetails = await SF.getFirmDetails(firmId);
if (!firmDetails) {
console.log(`Firm ${firmId} not found.`);
consola.warn(`Firm ${firmId} not found.`);
return false;
}
firmCredentials.storeFirmName(firmId, firmDetails.name);
console.log(`Firm ${firmId} name set to ${firmDetails.name}`);
consola.info(`Firm ${firmId} name set to ${firmDetails.name}`);
return true;
} catch (error) {
errorUtils.errorHandler(error);
Expand Down
5 changes: 4 additions & 1 deletion lib/api/firmCredentials.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require("fs");
const path = require("path");
const homedir = require("os").homedir();
const { consola } = require("consola");

/**
* Class to manage the credentials for the firms
Expand Down Expand Up @@ -39,7 +40,9 @@ class FirmCredentials {
"utf8",
(err) => {
if (err) {
console.log(`Error while writing credentials file: ${err}`);
consola.error(
new Error(`Error while writing credentials file: ${err}`)
);
}
}
);
Expand Down
Loading

0 comments on commit 3c41278

Please sign in to comment.