diff --git a/packages/cli/package-lock.json b/packages/cli/package-lock.json index ebd71718..931eed02 100644 --- a/packages/cli/package-lock.json +++ b/packages/cli/package-lock.json @@ -42,12 +42,6 @@ "rxjs": "^6.4.0" } }, - "@types/jasmine": { - "version": "3.5.11", - "resolved": "https://registry.npmjs.org/@types/jasmine/-/jasmine-3.5.11.tgz", - "integrity": "sha512-fg1rOd/DehQTIJTifGqGVY6q92lDgnLfs7C6t1ccSwQrMyoTGSoH6wWzhJDZb6ezhsdwAX4EIBLe8w5fXWmEng==", - "dev": true - }, "@types/mime-types": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/@types/mime-types/-/mime-types-2.1.0.tgz", @@ -58,15 +52,6 @@ "resolved": "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-aaI6OtKcrwCX8G7aWbNh7i8GOfY=" }, - "@types/mock-fs": { - "version": "4.10.0", - "resolved": "https://registry.npmjs.org/@types/mock-fs/-/mock-fs-4.10.0.tgz", - "integrity": "sha512-FQ5alSzmHMmliqcL36JqIA4Yyn9jyJKvRSGV3mvPh108VFatX7naJDzSG4fnFQNZFq9dIx0Dzoe6ddflMB2Xkg==", - "dev": true, - "requires": { - "@types/node": "*" - } - }, "@types/node": { "version": "12.12.30", "resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.30.tgz", diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 1d0e6a11..87b3f56c 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -15,11 +15,11 @@ */ import {Cli} from './lib/Cli'; -import {Log} from '@bubblewrap/core'; +import {ConsoleLog} from '@bubblewrap/core'; module.exports = async (): Promise => { const cli = new Cli(); - const log = new Log('cli'); + const log = new ConsoleLog('cli'); const args = process.argv.slice(2); let success; diff --git a/packages/cli/src/lib/cmds/build.ts b/packages/cli/src/lib/cmds/build.ts index 660aaa7f..ea755280 100644 --- a/packages/cli/src/lib/cmds/build.ts +++ b/packages/cli/src/lib/cmds/build.ts @@ -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'; @@ -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); @@ -191,7 +191,7 @@ class Build { } export async function build(config: Config, args: ParsedArgs, - log = new Log('build'), prompt: Prompt = new InquirerPrompt()): Promise { + log: Log = new ConsoleLog('build'), prompt: Prompt = new InquirerPrompt()): Promise { const build = new Build(config, args, log, prompt); return build.build(); } diff --git a/packages/cli/src/lib/cmds/help.ts b/packages/cli/src/lib/cmds/help.ts index 09d4e952..c032b99e 100644 --- a/packages/cli/src/lib/cmds/help.ts +++ b/packages/cli/src/lib/cmds/help.ts @@ -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( @@ -87,7 +87,7 @@ const HELP_MESSAGES = new Map( ], ); -export async function help(args: ParsedArgs, log = new Log('help')): Promise { +export async function help(args: ParsedArgs, log: Log = new ConsoleLog('help')): Promise { // minimist uses an `_` object to store details. const command = args._[1]; const message = HELP_MESSAGES.get(command) || HELP_MESSAGES.get('main'); diff --git a/packages/cli/src/lib/cmds/install.ts b/packages/cli/src/lib/cmds/install.ts index c4582c89..eef4a4ef 100644 --- a/packages/cli/src/lib/cmds/install.ts +++ b/packages/cli/src/lib/cmds/install.ts @@ -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'; @@ -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 { + args: ParsedArgs, config: Config, log: Log = new ConsoleLog('install')): Promise { 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 diff --git a/packages/cli/src/lib/cmds/validate.ts b/packages/cli/src/lib/cmds/validate.ts index 71d4a16b..db91e407 100644 --- a/packages/cli/src/lib/cmds/validate.ts +++ b/packages/cli/src/lib/cmds/validate.ts @@ -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 diff --git a/packages/cli/src/lib/cmds/version.ts b/packages/cli/src/lib/cmds/version.ts index f3ed01f2..f741668f 100644 --- a/packages/cli/src/lib/cmds/version.ts +++ b/packages/cli/src/lib/cmds/version.ts @@ -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 { +export async function version(log: Log = new ConsoleLog('version')): Promise { const packageJsonFile = path.join(__dirname, '../../../package.json'); const packageJsonContents = await (await fs.promises.readFile(packageJsonFile)).toString(); const packageJson = JSON.parse(packageJsonContents); diff --git a/packages/cli/src/lib/config.ts b/packages/cli/src/lib/config.ts index 7a650bb3..367d3f7f 100644 --- a/packages/cli/src/lib/config.ts +++ b/packages/cli/src/lib/config.ts @@ -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'; @@ -44,7 +44,7 @@ async function createConfig(): Promise { return new Config(result.jdkPath, result.androidSdkPath); } -async function renameConfigIfNeeded(log = new Log('config')): Promise { +async function renameConfigIfNeeded(log: Log = new ConsoleLog('config')): Promise { if (existsSync(DEFAULT_CONFIG_FILE_PATH)) return; // No new named config file found. if (!existsSync(LEGACY_CONFIG_FILE_PATH)) return; diff --git a/packages/core/package-lock.json b/packages/core/package-lock.json index a8f7450e..355df312 100644 --- a/packages/core/package-lock.json +++ b/packages/core/package-lock.json @@ -368,11 +368,6 @@ "rxjs": "^6.4.0" } }, - "@types/jasmine": { - "version": "3.5.11", - "resolved": "https://registry.npmjs.org/@types/jasmine/-/jasmine-3.5.11.tgz", - "integrity": "sha512-fg1rOd/DehQTIJTifGqGVY6q92lDgnLfs7C6t1ccSwQrMyoTGSoH6wWzhJDZb6ezhsdwAX4EIBLe8w5fXWmEng==" - }, "@types/lodash": { "version": "4.14.157", "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.157.tgz", @@ -389,7 +384,8 @@ "@types/mime-types": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/@types/mime-types/-/mime-types-2.1.0.tgz", - "integrity": "sha1-nKUs2jY/aZxpRmwqbM2q2RPqenM=" + "integrity": "sha1-nKUs2jY/aZxpRmwqbM2q2RPqenM=", + "dev": true }, "@types/minipass": { "version": "2.2.0", diff --git a/packages/core/package.json b/packages/core/package.json index 41fcf9e9..85251060 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -48,4 +48,4 @@ "tar": "^6.0.2", "valid-url": "^1.0.9" } -} +} \ No newline at end of file diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 19d2c2f9..de9bff1c 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -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'; @@ -36,6 +36,7 @@ export {AndroidSdkTools, JdkHelper, KeyTool, Log, + ConsoleLog, TwaGenerator, TwaManifest, DisplayMode, diff --git a/packages/core/src/lib/Log.ts b/packages/core/src/lib/Log.ts index 796bc7ff..beef5037 100644 --- a/packages/core/src/lib/Log.ts +++ b/packages/core/src/lib/Log.ts @@ -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; @@ -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; @@ -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) { @@ -53,27 +88,27 @@ 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'); @@ -81,15 +116,23 @@ export default class Log { 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 { diff --git a/packages/core/src/lib/TwaGenerator.ts b/packages/core/src/lib/TwaGenerator.ts index 88463870..c1c92dd4 100644 --- a/packages/core/src/lib/TwaGenerator.ts +++ b/packages/core/src/lib/TwaGenerator.ts @@ -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 = [ @@ -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( diff --git a/packages/core/src/lib/TwaManifest.ts b/packages/core/src/lib/TwaManifest.ts index f527324c..cb8073b8 100644 --- a/packages/core/src/lib/TwaManifest.ts +++ b/packages/core/src/lib/TwaManifest.ts @@ -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'; @@ -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; diff --git a/packages/core/src/lib/androidSdk/AndroidSdkTools.ts b/packages/core/src/lib/androidSdk/AndroidSdkTools.ts index d843ae07..26219e52 100644 --- a/packages/core/src/lib/androidSdk/AndroidSdkTools.ts +++ b/packages/core/src/lib/androidSdk/AndroidSdkTools.ts @@ -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'; @@ -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}`); } diff --git a/packages/core/src/lib/jdk/KeyTool.ts b/packages/core/src/lib/jdk/KeyTool.ts index 4f85e715..1a1180f9 100644 --- a/packages/core/src/lib/jdk/KeyTool.ts +++ b/packages/core/src/lib/jdk/KeyTool.ts @@ -17,7 +17,7 @@ import {existsSync, promises} from 'fs'; import {execute} from '../util'; import {JdkHelper} from './JdkHelper'; -import Log from '../Log'; +import {Log, ConsoleLog} from '../Log'; export interface KeyInfo { fingerprints: Map; @@ -44,7 +44,7 @@ export class KeyTool { private jdkHelper: JdkHelper; private log: Log; - constructor(jdkHelper: JdkHelper, log = new Log('keytool')) { + constructor(jdkHelper: JdkHelper, log: Log = new ConsoleLog('keytool')) { this.jdkHelper = jdkHelper; this.log = log; } diff --git a/packages/core/src/lib/util.ts b/packages/core/src/lib/util.ts index a420aa62..c0853b5b 100644 --- a/packages/core/src/lib/util.ts +++ b/packages/core/src/lib/util.ts @@ -21,7 +21,7 @@ import {promisify} from 'util'; import {exec, execFile, spawn} from 'child_process'; import {x as extractTar} from 'tar'; import {WebManifestIcon} from './types/WebManifest'; -import Log from './Log'; +import {Log} from './Log'; import {lookup} from 'mime-types'; const execPromise = promisify(exec); diff --git a/packages/core/src/spec/lib/androidSdk/AndroidSdkToolsSpec.ts b/packages/core/src/spec/lib/androidSdk/AndroidSdkToolsSpec.ts index 9a4856c3..8fa507af 100644 --- a/packages/core/src/spec/lib/androidSdk/AndroidSdkToolsSpec.ts +++ b/packages/core/src/spec/lib/androidSdk/AndroidSdkToolsSpec.ts @@ -19,7 +19,7 @@ import {JdkHelper} from '../../../lib/jdk/JdkHelper'; import {AndroidSdkTools} from '../../../lib/androidSdk/AndroidSdkTools'; import util = require('../../../lib/util'); import * as fs from 'fs'; -import {Log} from '../../..'; +import {ConsoleLog} from '../../..'; function buildMockConfig(platform: string): Config { if (platform === 'linux' || platform == 'darwin') { @@ -179,7 +179,7 @@ describe('AndroidSdkTools', () => { const config = buildMockConfig(test.platform); const process = buildMockProcess(test.platform); const jdkHelper = new JdkHelper(process, config); - const log = new Log('test'); + const log = new ConsoleLog('test'); const androidSdkTools = new AndroidSdkTools(process, config, jdkHelper, log); spyOn(util, 'execute').and.stub(); await androidSdkTools.install('app-release-signed.apk'); @@ -244,7 +244,7 @@ describe('AndroidSdkTools', () => { const config = buildMockConfig(test.platform); const process = buildMockProcess(test.platform); const jdkHelper = new JdkHelper(process, config); - const log = new Log('test'); + const log = new ConsoleLog('test'); const androidSdkTools = new AndroidSdkTools(process, config, jdkHelper, log); spyOn(util, 'executeFile').and.stub(); await androidSdkTools.apksigner(