Skip to content

Commit

Permalink
chore: refactoring out 'enable verbose mode' into a class method
Browse files Browse the repository at this point in the history
  • Loading branch information
rpl committed Jan 23, 2018
1 parent 8f8e17c commit 5fa17c8
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class Program {
yargs: any;
commands: { [key: string]: Function };
shouldExitProgram: boolean;
verboseEnabled: boolean;
options: Object;

constructor(
Expand All @@ -69,6 +70,7 @@ export class Program {
// config (See web-ext#469 for rationale).
const yargsInstance = yargs(argv, absolutePackageDir);

this.verboseEnabled = false;
this.shouldExitProgram = true;
this.yargs = yargsInstance;
this.yargs.strict();
Expand Down Expand Up @@ -121,6 +123,19 @@ export class Program {
return this;
}

enableVerboseMode(
logStream: typeof defaultLogStream,
version: string
): void {
if (this.verboseEnabled) {
return;
}

logStream.makeVerbose();
log.info('Version:', version);
this.verboseEnabled = true;
}

async execute(
absolutePackageDir: string,
{
Expand All @@ -135,21 +150,17 @@ export class Program {
globalEnv = WEBEXT_BUILD_ENV,
}: ExecuteOptions = {}
): Promise<void> {

this.shouldExitProgram = shouldExitProgram;
this.yargs.exitProcess(this.shouldExitProgram);

const argv = this.yargs.argv;
const cmd = argv._[0];

const version = getVersion(absolutePackageDir);
const runCommand = this.commands[cmd];

let versionLogged = false;

if (argv.verbose) {
logStream.makeVerbose();
log.info('Version:', getVersion(absolutePackageDir));
versionLogged = true;
this.enableVerboseMode(logStream, version);
}

let adjustedArgv = {...argv};
Expand Down Expand Up @@ -206,11 +217,8 @@ export class Program {
});

if (adjustedArgv.verbose) {
logStream.makeVerbose();

if (!versionLogged) {
log.info('Version:', getVersion(absolutePackageDir));
}
// Ensure that the verbose is enabled when specified in a config file.
this.enableVerboseMode(logStream, version);
}

await runCommand(adjustedArgv, {shouldExitProgram});
Expand Down

0 comments on commit 5fa17c8

Please sign in to comment.