Skip to content

Commit

Permalink
feat: color output
Browse files Browse the repository at this point in the history
  • Loading branch information
favoyang committed Aug 8, 2020
1 parent 09bd329 commit aac4ce9
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 18 deletions.
3 changes: 2 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ program
.option("-c, --chdir <path>", "change the working directory")
.option("-r, --registry <url>", "specify registry url")
.option("-v, --verbose", "output extra debugging")
.option("--no-upstream", "don't use upstream unity registry");
.option("--no-upstream", "don't use upstream unity registry")
.option("--no-color", "disable color");

program
.command("add <pkg> [otherPkgs...]")
Expand Down
35 changes: 25 additions & 10 deletions lib/cmd-view.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const chalk = require("chalk");

const { log } = require("./logger");
const {
env,
Expand Down Expand Up @@ -30,7 +32,6 @@ const view = async function(pkg, options) {
};

const printInfo = function(pkg) {
console.log();
const versionCount = Object.keys(pkg.versions).length;
const ver = getLatestVersion(pkg);
const verInfo = pkg.versions[ver];
Expand All @@ -42,33 +43,47 @@ const printInfo = function(pkg) {
const dist = verInfo.dist;
const dependencies = verInfo.dependencies;
const time = (pkg.time.modified || "").split("T")[0];
const latest = pkg["dist-tags"].latest;

console.log(`${pkg.name}@${ver} | ${license} | versions: ${versionCount}`);
console.log();
if (displayName) console.log(`display name: ${displayName}`);
if (description) console.log(`description: ${description}`);
console.log(
chalk.greenBright(pkg.name) +
"@" +
chalk.greenBright(ver) +
" | " +
chalk.green(license) +
" | versions: " +
chalk.yellow(versionCount)
);
console.log();
if (displayName) console.log(chalk.greenBright(displayName));
if (description) console.log(description);
if (description && description.includes("\n")) console.log();
if (homepage) console.log(`homepage: ${homepage}`);
if (homepage) console.log(chalk.cyan(homepage));
if (keywords) console.log(`keywords: ${keywords.join(", ")}`);

if (dist) {
console.log();
console.log("dist");
console.log(`.tarball: ${dist.tarball}`);
if (dist.shasum) console.log(`.shasum: ${dist.shasum}`);
if (dist.integrity) console.log(`.integrity: ${dist.integrity}`);
console.log(".tarball: " + chalk.cyan(dist.tarball));
if (dist.shasum) console.log(".shasum: " + chalk.yellow(dist.shasum));
if (dist.integrity)
console.log(".integrity: " + chalk.yellow(dist.integrity));
}

if (dependencies && Object.keys(dependencies).length > 0) {
console.log();
console.log("dependencies");
Object.keys(dependencies)
.sort()
.forEach(n => console.log(`${n} ${dependencies[n]}`));
.forEach(n => console.log(chalk.yellow(n) + ` ${dependencies[n]}`));
}

console.log();
console.log(`published at ${time}`);
console.log("latest: " + chalk.greenBright(latest));

console.log();
console.log("published at " + chalk.yellow(time));
};

module.exports = view;
9 changes: 9 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const _ = require("lodash");
const chalk = require("chalk");
const fs = require("fs");
const keyFileStorage = require("key-file-storage").default;
const isIp = require("is-ip");
Expand All @@ -21,9 +22,17 @@ const parseEnv = function(options, { checkPath }) {
env.cwd = "";
env.manifestPath = "";
env.upstream = true;
env.color = true;
env.upstreamRegistry = "https://packages.unity.com";
// log level
log.level = options.parent.verbose ? "verbose" : "notice";
// color
if (options.parent.color === false) env.color = false;
if (process.env.NODE_ENV == "test") env.color = false;
if (!env.color) {
chalk.level = 0;
log.disableColor();
}
// upstream
if (options.parent.upstream === false) env.upstream = false;
// registry
Expand Down
134 changes: 127 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"dependencies": {
"@iarna/toml": "^2.2.5",
"chalk": "^4.1.0",
"cli-table": "^0.3.1",
"commander": "^5.1.0",
"is-ip": "^3.1.0",
Expand Down

0 comments on commit aac4ce9

Please sign in to comment.