cleanup chalk usage

This commit is contained in:
Joao Moreno 2019-01-16 11:59:25 +01:00
parent a86fc109b2
commit e12b77022f
10 changed files with 4848 additions and 38 deletions

4786
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -53,7 +53,6 @@
"yazl": "^2.2.2"
},
"devDependencies": {
"@types/chalk": "^2.2.0",
"@types/cheerio": "^0.22.1",
"@types/markdown-it": "0.0.2",
"@types/node": "^8",

View file

@ -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, ERROR } from './util';
import { CancellationToken, isCancelledError, log } from './util';
import * as semver from 'semver';
import { isatty } from 'tty';
const pkg = require('../package.json');
@ -19,10 +19,10 @@ function fatal<T>(message: any, ...args: any[]): void {
}
}
console.error(`${ERROR} `, message, ...args);
log.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**.
log.error(`Be sure to use a Personal Access Token which has access to **all accessible accounts**.
See https://code.visualstudio.com/api/working-with-extensions/publishing-extension#publishing-extensions for more information.`);
}
@ -37,14 +37,14 @@ function main<T>(task: Promise<any>): void {
if (isatty(1)) {
getLatestVersion(pkg.name, token)
.then(version => latestVersion = version)
.catch(err => !isCancelledError(err) && console.error(err));
.catch(err => !isCancelledError(err) && log.error(err));
}
task
.catch(fatal)
.then(() => {
if (latestVersion && semver.gt(latestVersion, pkg.version)) {
console.log(`\nThe latest version of ${pkg.name} is ${latestVersion} and you have ${pkg.version}.\nUpdate it now: npm install -g ${pkg.name}`);
log.warn(`\nThe latest version of ${pkg.name} is ${latestVersion} and you have ${pkg.version}.\nUpdate it now: npm install -g ${pkg.name}`);
} else {
token.cancel();
}

View file

@ -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, ERROR } from './util';
import { CancellationToken, log } from './util';
interface IOptions {
cwd?: string;
@ -84,7 +84,7 @@ function asYarnDependency(prefix: string, tree: YarnTreeNode, prune: boolean): Y
name = parseResult.name;
version = parseResult.version;
} catch (err) {
console.error(`${ERROR} Failed to parse dependency:`, tree.name);
log.error('Failed to parse dependency:', tree.name);
return null;
}

View file

@ -234,7 +234,7 @@ class ManifestProcessor extends BaseProcessor {
}
if (!this.manifest.repository) {
console.warn(`${util.WARN} A 'repository' field is missing from the 'package.json' manifest file.`);
util.log.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');
@ -854,7 +854,7 @@ export async function packageCommand(options: IPackageOptions = {}): Promise<any
unit = 'KB';
}
console.log(`${util.DONE} Packaged: ${packagePath} (${files.length} files, ${size}${unit})`);
util.log.done(`Packaged: ${packagePath} (${files.length} files, ${size}${unit})`);
}
/**

View file

@ -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, DONE } from './util';
import { getGalleryAPI, read, getPublishedUrl, log } from './util';
import { validatePublisher } from './validation';
import { Manifest } from './manifest';
import * as denodeify from 'denodeify';
@ -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(`${DONE} Published ${fullName}\nYour extension will live at ${getPublishedUrl(name)} (might take a few seconds for it to show up).`));
.then(() => 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 || '';
@ -207,6 +207,6 @@ export function unpublish(options: IUnpublishOptions = {}): Promise<any> {
.then(() => pat)
.then(getGalleryAPI)
.then(api => api.deleteExtension(publisher, name))
.then(() => console.log(`${DONE} Deleted extension: ${fullName}!`));
.then(() => log.done(`Deleted extension: ${fullName}!`));
});
}

View file

@ -1,4 +1,4 @@
import { getPublicGalleryAPI, ERROR } from './util';
import { getPublicGalleryAPI, log } from './util';
import { ExtensionQueryFlags, PublishedExtension } from 'vso-node-api/interfaces/GalleryInterfaces';
import { ViewTable, formatDate, formatDateTime, ratingStars, tableView, indentRow, wordWrap, icons } from './viewutils';
@ -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.`);
log.error(`Extension "${extensionId}" not found.`);
} else {
showOverview(extension);
}

View file

@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';
import { home } from 'osenv';
import { read, getGalleryAPI, getSecurityRolesAPI, DONE } from './util';
import { read, getGalleryAPI, getSecurityRolesAPI, log } from './util';
import { validatePublisher } from './validation';
import * as denodeify from 'denodeify';
@ -133,7 +133,7 @@ export function createPublisher(publisherName: string): Promise<any> {
.then(publisher => load().then(store => addPublisherToStore(store, publisher)));
});
})
.then(() => console.log(`${DONE} Created publisher '${publisherName}'.`));
.then(() => log.done(`Created publisher '${publisherName}'.`));
}
export function deletePublisher(publisherName: string): Promise<any> {
@ -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(`${DONE} Deleted publisher '${publisherName}'.`));
.then(() => log.done(`Deleted publisher '${publisherName}'.`));
});
}

View file

@ -98,7 +98,39 @@ export async function sequence(promiseFactories: { (): Promise<any> }[]): Promis
}
}
export const DONE = chalk.bgGreen.black(' DONE ');
export const INFO = chalk.bgBlueBright.black(' INFO ');
export const WARN = chalk.bgYellow.black(' WARNING ');
export const ERROR = chalk.bgRed.black(' ERROR ');
enum LogMessageType {
DONE,
INFO,
WARNING,
ERROR
}
const LogPrefix = {
[LogMessageType.DONE]: chalk.bgGreen.black(' DONE '),
[LogMessageType.INFO]: chalk.bgBlueBright.black(' INFO '),
[LogMessageType.WARNING]: chalk.bgYellow.black(' WARNING '),
[LogMessageType.ERROR]: chalk.bgRed.black(' ERROR '),
};
function _log(type: LogMessageType, msg: any, ...args: any[]): void {
args = [LogPrefix[type], msg, ...args];
if (type === LogMessageType.WARNING) {
console.warn(...args);
} else if (type === LogMessageType.ERROR) {
console.error(...args);
} else {
console.log(...args);
}
}
export interface LogFn {
(msg: any, ...args: any[]): void;
}
export const log = {
done: _log.bind(null, LogMessageType.DONE) as LogFn,
info: _log.bind(null, LogMessageType.INFO) as LogFn,
warn: _log.bind(null, LogMessageType.WARNING) as LogFn,
error: _log.bind(null, LogMessageType.ERROR) as LogFn
}

View file

@ -21,13 +21,6 @@
normalize-path "^2.0.1"
through2 "^2.0.3"
"@types/chalk@^2.2.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@types/chalk/-/chalk-2.2.0.tgz#b7f6e446f4511029ee8e3f43075fb5b73fbaa0ba"
integrity sha512-1zzPV9FDe1I/WHhRkf9SNgqtRJWZqrBWgu7JGveuHmmyR9CnAPCie2N/x+iHrgnpYBIcCJWHBoMRv2TRWktsvw==
dependencies:
chalk "*"
"@types/cheerio@^0.22.1":
version "0.22.10"
resolved "https://registry.yarnpkg.com/@types/cheerio/-/cheerio-0.22.10.tgz#780d552467824be4a241b29510a7873a7432c4a6"
@ -315,15 +308,6 @@ callsites@^0.2.0:
resolved "https://registry.yarnpkg.com/callsites/-/callsites-0.2.0.tgz#afab96262910a7f33c19a5775825c69f34e350ca"
integrity sha1-r6uWJikQp/M8GaV3WCXGnzTjUMo=
chalk@*, chalk@^2.4.2:
version "2.4.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==
dependencies:
ansi-styles "^3.2.1"
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"
chalk@^1.0.0, chalk@^1.1.1, chalk@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98"
@ -335,6 +319,15 @@ chalk@^1.0.0, chalk@^1.1.1, chalk@^1.1.3:
strip-ansi "^3.0.0"
supports-color "^2.0.0"
chalk@^2.4.2:
version "2.4.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==
dependencies:
ansi-styles "^3.2.1"
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"
cheerio@^1.0.0-rc.1:
version "1.0.0-rc.2"
resolved "https://registry.yarnpkg.com/cheerio/-/cheerio-1.0.0-rc.2.tgz#4b9f53a81b27e4d5dac31c0ffd0cfa03cc6830db"