Skip to content

Commit

Permalink
feat: add summary command (draft)
Browse files Browse the repository at this point in the history
  • Loading branch information
tony-go committed Dec 11, 2020
1 parent 3bdcc1a commit 604042b
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
105 changes: 104 additions & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require("make-promises-safe");
require("dotenv").config();

// Require Node.js Dependencies
const { writeFileSync, promises: { unlink, readdir } } = require("fs");
const { writeFileSync, promises: { unlink, readdir, readFile } } = require("fs");
const { join, extname, basename } = require("path");
const { once } = require("events");

Expand Down Expand Up @@ -109,6 +109,11 @@ prog
.option("-j, --json", i18n.getToken("cli.commands.verify.option_json"), false)
.action(verifyCmd);

prog
.command("summary")
.describe(i18n.getToken("cli.commands.summary.desc"))
.action(summaryCmd);

prog
.command("lang")
.describe(i18n.getToken("cli.commands.lang.desc"))
Expand Down Expand Up @@ -142,6 +147,104 @@ function locationToString(location) {
return `[${start}] - [${end}]`;
}

async function summaryCmd() {
const rawAnalysis = await readFile(join(__dirname, "../nsecure-result.json"), { encoding: "utf-8" });
const { rootDepencyName, dependencies } = JSON.parse(rawAnalysis);

// header
ui.div(
{ text: cyan().bold(`Package name: ${rootDepencyName}`), width: 50 }
);
ui.div({ text: yellow("-------------------------------------------------------------------"), width: 70 });

// body
const firstLevelPadding = "----";
if (dependencies) {
for (const dependencyIdentifier in dependencies) {
if (dependencyIdentifier) {
// name of the package
const dependencyData = dependencies[dependencyIdentifier];
ui.div(
{ text: cyan().bold(`${firstLevelPadding} Package`), width: 20 },
{ text: yellow().bold(dependencyIdentifier), width: 30 }
);
ui.div(
{ text: cyan().bold(`${firstLevelPadding} Stats:`), width: 20 }
);

displayDependency(dependencyData);

ui.div({ text: "\n", width: 70, align: "center" });
}
}
}
else {
ui.div(
{ text: cyan().bold("Error:"), width: 20 },
{ text: yellow().bold("No dependencies"), width: 30 }
);
}
ui.div({ text: yellow("-------------------------------------------------------------------"), width: 70 });
console.log(`${ui.toString()}`);
ui.resetOutput();

return void 0;
}

function displayDependency(dependencyData) {
const versions = dependencyData.versions;
const metadata = dependencyData.metadata;
for (const version of versions) {
if (version) {
const versionData = dependencyData[version];
ui.div(
{ text: "", width: 20 },
{ text: cyan().bold(`v${version}`), width: 20 }
);
ui.div(
{ text: "", width: 20 },
{ text: white().bold(`${i18n.getToken("ui.stats.total_packages")}: ${metadata.dependencyCount}`), width: 50 }
);
ui.div(
{ text: "", width: 20 },
{ text: white().bold(`${i18n.getToken("ui.stats.total_size")}: ${versionData.size}`), width: 50 }
);
// break line
ui.div({ text: "\n", width: 70, align: "center" });

// display extensions
ui.div(
{ text: "", width: 20 },
{ text: white().bold(`${i18n.getToken("ui.stats.extensions")}:`), width: 50 }
);
for (const extension of versionData.composition.extensions) {
if (extension) {
ui.div(
{ text: "", width: 20 },
{ text: white().bold(`---- ${extension}`), width: 50 }
);
}
}
// break line
ui.div({ text: "\n", width: 70, align: "center" });

// display licenses
ui.div(
{ text: "", width: 20 },
{ text: white().bold(`${i18n.getToken("ui.stats.licenses")}:`), width: 50 }
);
if (versionData.license.uniqueLicenseIds) {
for (const license of versionData.license.uniqueLicenseIds) {
ui.div(
{ text: "", width: 20 },
{ text: white().bold(`---- ${license}`), width: 50 }
);
}
}
}
}
}

async function verifyCmd(packageName = null, options) {
const payload = await verify(packageName);
if (options.json) {
Expand Down
3 changes: 3 additions & 0 deletions i18n/english.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ module.exports = {
desc: "Run a complete advanced analysis for a given npm package",
option_json: "Stdout the analysis payload"
},
summary: {
desc: "Display your analysis results"
},
lang: {
desc: "Configure the CLI default language",
question_text: "What language do you want to use?",
Expand Down
3 changes: 3 additions & 0 deletions i18n/french.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ module.exports = {
desc: "Démarre une analyse AST avancée pour un package npm donné",
option_json: "Affiche le résultat d'analyse dans la sortie standard"
},
summary: {
desc: "Afficher le résultat de votre analyse"
},
lang: {
desc: "Configure le langage par défaut du CLI",
question_text: "Quel langage souhaitez-vous utiliser ?",
Expand Down

0 comments on commit 604042b

Please sign in to comment.