Skip to content

Commit

Permalink
Merge pull request #284 from chenlevy24/ConsoleLog
Browse files Browse the repository at this point in the history
Console log
  • Loading branch information
andreban authored Aug 18, 2020
2 parents ad139d1 + 9783686 commit 6245939
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 70 deletions.
15 changes: 0 additions & 15 deletions packages/cli/package-lock.json

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

4 changes: 2 additions & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/

import {Cli} from './lib/Cli';
import {Log} from '@bubblewrap/core';
import {ConsoleLog} from '@bubblewrap/core';

module.exports = async (): Promise<void> => {
const cli = new Cli();
const log = new Log('cli');
const log = new ConsoleLog('cli');
const args = process.argv.slice(2);

let success;
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/lib/cmds/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import {AndroidSdkTools, Config, DigitalAssetLinks, GradleWrapper, JdkHelper, KeyTool, Log,
TwaManifest, JarSigner, SigningKeyInfo, Result} from '@bubblewrap/core';
ConsoleLog, TwaManifest, JarSigner, SigningKeyInfo, Result} from '@bubblewrap/core';
import * as path from 'path';
import * as fs from 'fs';
import {enUS as messages} from '../strings';
Expand All @@ -38,7 +38,7 @@ class Build {
private jarSigner: JarSigner;

constructor(private config: Config, private args: ParsedArgs,
private log = new Log('build'), private prompt: Prompt = new InquirerPrompt()) {
private log: Log = new ConsoleLog('build'), private prompt: Prompt = new InquirerPrompt()) {
this.jdkHelper = new JdkHelper(process, this.config);
this.androidSdkTools = new AndroidSdkTools(process, this.config, this.jdkHelper, this.log);
this.keyTool = new KeyTool(this.jdkHelper, this.log);
Expand Down Expand Up @@ -191,7 +191,7 @@ class Build {
}

export async function build(config: Config, args: ParsedArgs,
log = new Log('build'), prompt: Prompt = new InquirerPrompt()): Promise<boolean> {
log: Log = new ConsoleLog('build'), prompt: Prompt = new InquirerPrompt()): Promise<boolean> {
const build = new Build(config, args, log, prompt);
return build.build();
}
4 changes: 2 additions & 2 deletions packages/cli/src/lib/cmds/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {Log} from '@bubblewrap/core';
import {Log, ConsoleLog} from '@bubblewrap/core';
import {ParsedArgs} from 'minimist';

const HELP_MESSAGES = new Map<string, string>(
Expand Down Expand Up @@ -87,7 +87,7 @@ const HELP_MESSAGES = new Map<string, string>(
],
);

export async function help(args: ParsedArgs, log = new Log('help')): Promise<boolean> {
export async function help(args: ParsedArgs, log: Log = new ConsoleLog('help')): Promise<boolean> {
// minimist uses an `_` object to store details.
const command = args._[1];
const message = HELP_MESSAGES.get(command) || HELP_MESSAGES.get('main');
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/lib/cmds/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {AndroidSdkTools, Config, JdkHelper, Log} from '@bubblewrap/core';
import {AndroidSdkTools, Config, JdkHelper, Log, ConsoleLog} from '@bubblewrap/core';
import {ParsedArgs} from 'minimist';

const APK_FILE_PARAM = '--apkFile';
Expand All @@ -23,12 +23,12 @@ const DEFAULT_APK_FILE = './app-release-signed.apk';
const PARAMETERS_TO_IGNORE = ['--verbose', '-r'];

export async function install(
args: ParsedArgs, config: Config, log = new Log('install')): Promise<boolean> {
args: ParsedArgs, config: Config, log: Log = new ConsoleLog('install')): Promise<boolean> {
const jdkHelper = new JdkHelper(process, config);
const androidSdkTools = new AndroidSdkTools(process, config, jdkHelper, log);
const apkFile = args.apkFile || DEFAULT_APK_FILE;
if (args.verbose) {
log.verbose = true;
log.setVerbose(true);
}

// parameter 0 would be the path to 'node', followed by `bubblewrap.js` at 1, then `install` at
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/lib/cmds/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/

import {PwaValidator} from '@bubblewrap/validator';
import {Log} from '@bubblewrap/core';
import {ConsoleLog} from '@bubblewrap/core';
import {ParsedArgs} from 'minimist';
import {printValidationResult} from '../pwaValidationHelper';

const log = new Log('validate');
const log = new ConsoleLog('validate');

/**
* Runs the PwaValidator to check a given URL agains the Quality criteria. More information on the
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/lib/cmds/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import * as fs from 'fs';
import * as path from 'path';
import {Log} from '@bubblewrap/core';
import {Log, ConsoleLog} from '@bubblewrap/core';

export async function version(log = new Log('version')): Promise<boolean> {
export async function version(log: Log = new ConsoleLog('version')): Promise<boolean> {
const packageJsonFile = path.join(__dirname, '../../../package.json');
const packageJsonContents = await (await fs.promises.readFile(packageJsonFile)).toString();
const packageJson = JSON.parse(packageJsonContents);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import {join} from 'path';
import {homedir} from 'os';
import {Config, Log} from '@bubblewrap/core';
import {Config, Log, ConsoleLog} from '@bubblewrap/core';
import * as inquirer from 'inquirer';
import {existsSync} from 'fs';
import {promises as fsPromises} from 'fs';
Expand All @@ -44,7 +44,7 @@ async function createConfig(): Promise<Config> {
return new Config(result.jdkPath, result.androidSdkPath);
}

async function renameConfigIfNeeded(log = new Log('config')): Promise<void> {
async function renameConfigIfNeeded(log: Log = new ConsoleLog('config')): Promise<void> {
if (existsSync(DEFAULT_CONFIG_FILE_PATH)) return;
// No new named config file found.
if (!existsSync(LEGACY_CONFIG_FILE_PATH)) return;
Expand Down
8 changes: 2 additions & 6 deletions packages/core/package-lock.json

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

2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@
"tar": "^6.0.2",
"valid-url": "^1.0.9"
}
}
}
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import {AndroidSdkTools} from './lib/androidSdk/AndroidSdkTools';
import {Config} from './lib/Config';
import {GradleWrapper} from './lib/GradleWrapper';
import Log from './lib/Log';
import {Log, ConsoleLog} from './lib/Log';
import {JarSigner} from './lib/jdk/JarSigner';
import {JdkHelper} from './lib/jdk/JdkHelper';
import {KeyTool} from './lib/jdk/KeyTool';
Expand All @@ -36,6 +36,7 @@ export {AndroidSdkTools,
JdkHelper,
KeyTool,
Log,
ConsoleLog,
TwaGenerator,
TwaManifest,
DisplayMode,
Expand Down
81 changes: 62 additions & 19 deletions packages/core/src/lib/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,45 @@
* limitations under the License.
*/

/**
* An interface for loggers.
*/
export interface Log {

/**
* Prints a debug message to the Log. Message is ignored if the Log is not set to verbose.
* @param message the message the be printed.
* @param args extra arguments for the console.
*/
debug(message: string, ...args: string[]): void;

/**
* Prints an info message to the Log. Message is ignored if the Log is not set to verbose.
* @param message The message the be printed.
* @param args Extra arguments for the console.
*/
info(message: string, ...args: string[]): void;

/**
* Prints an warning message to the Log. Message is ignored if the Log is not set to verbose.
* @param message The message the be printed.
* @param args Extra arguments for the console.
*/
warn(message: string, ...args: string[]): void;
/**
* Prints an error message to the Log. Message is ignored if the Log is not set to verbose.
* @param message The message the be printed.
* @param args Extra arguments for the console.
*/
error(message: string, ...args: string[]): void;

setVerbose(verbose: boolean): void;
};

/**
* An utility class to print nice Log messages.
*/
export default class Log {
export class ConsoleLog implements Log {
private tag: string;
private prefix: string;
private output: Console;
Expand All @@ -29,9 +64,9 @@ export default class Log {

/**
* Creates a new Log instance
* @param tag the tag used when logging. Printed at the beggining of a log message.
* @param verbose if the Log is verbose. Debug messages are only printed on verbose logs.
* @param output where to output the log messages.
* @param tag The tag used when logging. Printed at the beggining of a log message.
* @param verbose If the Log is verbose. Debug messages are only printed on verbose logs.
* @param output Where to output the log messages.
*/
constructor(tag = '', verbose = false, output = console) {
this.tag = tag;
Expand All @@ -41,9 +76,9 @@ export default class Log {
}

/**
* Prints a debug message to the Log. message is ignored if the Log is not set to verbose.
* @param message the message the be printed.
* @param args extra arguments for the console.
* Prints a debug message to the Log. Message is ignored if the Log is not set to verbose.
* @param message The message the be printed.
* @param args Extra arguments for the console.
*/
debug(message: string, ...args: string[]): void {
if (!this.verbose) {
Expand All @@ -53,43 +88,51 @@ export default class Log {
}

/**
* Prints an info message to the Log. message is ignored if the Log is not set to verbose.
* @param message the message the be printed.
* @param args extra arguments for the console.
* Prints an info message to the Log. Message is ignored if the Log is not set to verbose.
* @param message The message the be printed.
* @param args Extra arguments for the console.
*/
info(message: string, ...args: string[]): void {
this.log(this.output.log, message, ...args);
}

/**
* Prints an warning message to the Log. message is ignored if the Log is not set to verbose.
* @param message the message the be printed.
* @param args extra arguments for the console.
* Prints an warning message to the Log. Message is ignored if the Log is not set to verbose.
* @param message The message the be printed.
* @param args Extra arguments for the console.
*/
warn(message: string, ...args: string[]): void {
this.log(this.output.warn, this.yellow('WARNING ' + message), ...args);
}

/**
* Prints an error message to the Log. message is ignored if the Log is not set to verbose.
* @param message the message the be printed.
* @param args extra arguments for the console.
* Prints an error message to the Log. Message is ignored if the Log is not set to verbose.
* @param message The message the be printed.
* @param args Extra arguments for the console.
*/
error(message: string, ...args: string[]): void {
this.output.error('\n');
this.log(this.output.error, this.red('ERROR ' + message), ...args);
this.output.error('\n');
}

/**
* Sets the verbose.
* @param verbose The verbose value to set.
*/
setVerbose(verbose: boolean): void {
this.verbose = verbose;
}

/**
* Creates a new Log using the same output and verbositity of the current Log.
* @param newTag the tag the be used on the new Log instance.
* @param newTag The tag the be used on the new Log instance.
*/
newLog(newTag: string): Log {
newLog(newTag: string): ConsoleLog {
if (this.tag) {
newTag = this.tag + ' ' + newTag;
}
return new Log(newTag, this.verbose, this.output);
return new ConsoleLog(newTag, this.verbose, this.output);
}

private log(fn: Function, message: string, ...args: string[]): void {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/TwaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {template} from 'lodash';
import {promisify} from 'util';
import {TwaManifest} from './TwaManifest';
import {ShortcutInfo} from './ShortcutInfo';
import Log from './Log';
import {Log, ConsoleLog} from './Log';
import {ImageHelper, IconDefinition} from './ImageHelper';

const COPY_FILE_LIST = [
Expand Down Expand Up @@ -158,7 +158,7 @@ class Progress {
export class TwaGenerator {
private imageHelper = new ImageHelper();

constructor(private log = new Log('twa-generator')) {}
constructor(private log: Log = new ConsoleLog('twa-generator')) {}

// Ensures targetDir exists and copies a file from sourceDir to target dir.
private async copyStaticFile(
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/TwaManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import * as fs from 'fs';
import fetch from 'node-fetch';
import {findSuitableIcon, generatePackageId, validateNotEmpty} from './util';
import Color = require('color');
import Log from './Log';
import {ConsoleLog} from './Log';
import {WebManifestIcon, WebManifestJson} from './types/WebManifest';
import {ShortcutInfo} from './ShortcutInfo';

Expand Down Expand Up @@ -108,7 +108,7 @@ export class TwaManifest {
fallbackType: FallbackType;
enableSiteSettingsShortcut: boolean;

private static log: Log = new Log('twa-manifest');
private static log = new ConsoleLog('twa-manifest');

constructor(data: TwaManifestJson) {
this.packageId = data.packageId;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/androidSdk/AndroidSdkTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import * as path from 'path';
import util = require('../util');
import {Config} from '../Config';
import {JdkHelper} from '../jdk/JdkHelper';
import Log from '../../lib/Log';
import {Log, ConsoleLog} from '../../lib/Log';

const BUILD_TOOLS_VERSION = '29.0.2';

Expand All @@ -40,7 +40,7 @@ export class AndroidSdkTools {
* @param {jdkHelper} jdkHelper the JDK information to be used by the Android SDK
*/
constructor(process: NodeJS.Process, config: Config, jdkHelper: JdkHelper,
readonly log = new Log('AndroidSdkTools')) {
readonly log: Log = new ConsoleLog('AndroidSdkTools')) {
if (!fs.existsSync(config.androidSdkPath)) {
throw new Error(`androidSdkPath does not exist: ${config.androidSdkPath}`);
}
Expand Down
Loading

0 comments on commit 6245939

Please sign in to comment.