Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add colors to console messages #324

Merged
merged 2 commits into from Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main.ts
Expand Up @@ -5,7 +5,7 @@ import { show } from './show';
import { search } from './search';
import { listPublishers, createPublisher, deletePublisher, loginPublisher, logoutPublisher } from './store';
import { getLatestVersion } from './npm';
import { CancellationToken, isCancelledError } from './util';
import { CancellationToken, isCancelledError, ERROR } from './util';
import * as semver from 'semver';
import { isatty } from 'tty';
const pkg = require('../package.json');
Expand All @@ -19,7 +19,7 @@ function fatal<T>(message: any, ...args: any[]): void {
}
}

console.error('Error:', message, ...args);
console.error(`${ERROR} `, message, ...args);

if (/Unauthorized\(401\)/.test(message)) {
console.error(`Be sure to use a Personal Access Token which has access to **all accessible accounts**.
Expand Down
4 changes: 2 additions & 2 deletions src/npm.ts
Expand Up @@ -3,7 +3,7 @@ import * as fs from 'fs';
import * as cp from 'child_process';
import * as parseSemver from 'parse-semver';
import * as _ from 'lodash';
import { CancellationToken } from './util';
import { CancellationToken, ERROR } from './util';

interface IOptions {
cwd?: string;
Expand Down Expand Up @@ -84,7 +84,7 @@ function asYarnDependency(prefix: string, tree: YarnTreeNode, prune: boolean): Y
name = parseResult.name;
version = parseResult.version;
} catch (err) {
console.error('Failed to parse dependency:', tree.name);
console.error(`${ERROR} Failed to parse dependency:`, tree.name);
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/package.ts
Expand Up @@ -234,7 +234,7 @@ class ManifestProcessor extends BaseProcessor {
}

if (!this.manifest.repository) {
console.warn(`A 'repository' field is missing from the 'package.json' manifest file.`);
console.warn(`${util.WARN} A 'repository' field is missing from the 'package.json' manifest file.`);

if (!/^y$/i.test(await util.read('Do you want to continue? [y/N] '))) {
throw new Error('Aborted');
Expand Down Expand Up @@ -854,7 +854,7 @@ export async function packageCommand(options: IPackageOptions = {}): Promise<any
unit = 'KB';
}

console.log(`Created: ${packagePath} (${files.length} files, ${size}${unit})`);
console.log(`${util.DONE} Packaged: ${packagePath} (${files.length} files, ${size}${unit})`);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/publish.ts
Expand Up @@ -3,7 +3,7 @@ import { ExtensionQueryFlags, PublishedExtension, ExtensionQueryFilterType, Pagi
import { pack, readManifest, IPackage } from './package';
import * as tmp from 'tmp';
import { getPublisher } from './store';
import { getGalleryAPI, read, getPublishedUrl } from './util';
import { getGalleryAPI, read, getPublishedUrl, DONE } from './util';
import { validatePublisher } from './validation';
import { Manifest } from './manifest';
import * as denodeify from 'denodeify';
Expand Down Expand Up @@ -74,7 +74,7 @@ function _publish(packagePath: string, pat: string, manifest: Manifest): Promise

return promise
.catch(err => Promise.reject(err.statusCode === 409 ? `${fullName} already exists.` : err))
.then(() => console.log(`Successfully published ${fullName}!\nYour extension will live at ${getPublishedUrl(name)} (might take a few seconds for it to show up).`));
.then(() => console.log(`${DONE} Published ${fullName}\nYour extension will live at ${getPublishedUrl(name)} (might take a few seconds for it to show up).`));
})
.catch(err => {
const message = err && err.message || '';
Expand Down Expand Up @@ -207,6 +207,6 @@ export function unpublish(options: IUnpublishOptions = {}): Promise<any> {
.then(() => pat)
.then(getGalleryAPI)
.then(api => api.deleteExtension(publisher, name))
.then(() => console.log(`Successfully deleted ${fullName}!`));
.then(() => console.log(`${DONE} Deleted extension: ${fullName}!`));
});
}
6 changes: 3 additions & 3 deletions src/show.ts
@@ -1,4 +1,4 @@
import { getPublicGalleryAPI } from './util';
import { getPublicGalleryAPI, ERROR } from './util';
import { ExtensionQueryFlags, PublishedExtension } from 'vso-node-api/interfaces/GalleryInterfaces';
import { ViewTable, formatDate, formatDateTime, ratingStars, tableView, indentRow, wordWrap, icons } from './viewutils';

Expand All @@ -25,7 +25,7 @@ export function show(extensionId: string, json: boolean = false): Promise<any> {
console.log(JSON.stringify(extension, undefined, '\t'));
} else {
if (extension === undefined) {
console.log(`Error: Extension "${extensionId}" not found.`);
console.log(`${ERROR} Extension "${extensionId}" not found.`);
} else {
showOverview(extension);
}
Expand Down Expand Up @@ -61,7 +61,7 @@ function showOverview({
averagerating = 0,
ratingcount = 0,
} = statistics
.reduce((map, { statisticName, value }) => ({ ...map, [statisticName]: value }), <ExtensionStatiticsMap>{});
.reduce((map, { statisticName, value }) => ({ ...map, [statisticName]: value }), <ExtensionStatiticsMap>{});

// Render
console.log([
Expand Down
6 changes: 3 additions & 3 deletions src/store.ts
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';
import { home } from 'osenv';
import { read, getGalleryAPI, getSecurityRolesAPI } from './util';
import { read, getGalleryAPI, getSecurityRolesAPI, DONE } from './util';
import { validatePublisher } from './validation';
import * as denodeify from 'denodeify';

Expand Down Expand Up @@ -133,7 +133,7 @@ export function createPublisher(publisherName: string): Promise<any> {
.then(publisher => load().then(store => addPublisherToStore(store, publisher)));
});
})
.then(() => console.log(`Successfully created publisher '${publisherName}'.`));
.then(() => console.log(`${DONE} Created publisher '${publisherName}'.`));
}

export function deletePublisher(publisherName: string): Promise<any> {
Expand All @@ -143,7 +143,7 @@ export function deletePublisher(publisherName: string): Promise<any> {
.then(() => getGalleryAPI(pat))
.then(api => api.deletePublisher(publisherName))
.then(() => load().then(store => removePublisherFromStore(store, publisherName)))
.then(() => console.log(`Successfully deleted publisher '${publisherName}'.`));
.then(() => console.log(`${DONE} Deleted publisher '${publisherName}'.`));
});
}

Expand Down
13 changes: 12 additions & 1 deletion src/util.ts
Expand Up @@ -95,4 +95,15 @@ export async function sequence(promiseFactories: { (): Promise<any> }[]): Promis
for (const factory of promiseFactories) {
await factory();
}
}
}

const ANSI_COLORS = {
reset: '\x1b[0m',
bgGreen: '\x1b[42m',
bgYellow: '\x1b[43m',
bgRed: '\x1b[41m',
fgBlack: '\x1b[30m',
};
export const DONE = `${ANSI_COLORS.bgGreen}${ANSI_COLORS.fgBlack} DONE ${ANSI_COLORS.reset}`;
export const WARN = `${ANSI_COLORS.bgYellow}${ANSI_COLORS.fgBlack} WARNING ${ANSI_COLORS.reset}`;
export const ERROR = `${ANSI_COLORS.bgRed}${ANSI_COLORS.fgBlack} ERROR ${ANSI_COLORS.reset}`;