Skip to content

Commit

Permalink
feat: add requiresProject property
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Feb 16, 2022
1 parent 2ce81b3 commit 5ff0cf3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions messages/messages.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# warning.security

This command will expose sensitive information that allows for subsequent activity using your current authenticated session. Sharing this information is equivalent to logging someone in under the current credential, resulting in unintended access and escalation of privilege. For additional information, please review the authorization section of the https://developer.salesforce.com/docs/atlas.en-us.234.0.sfdx_dev.meta/sfdx_dev/sfdx_dev_auth_web_flow.htm

# errors.RequiresProject

This command is required to run from within a Salesforce project directory.
26 changes: 25 additions & 1 deletion src/sfCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { CliUx, Command, Config, HelpSection, Interfaces } from '@oclif/core';
import { Messages } from '@salesforce/core';
import { Messages, SfdxProject } from '@salesforce/core';
import { AnyJson } from '@salesforce/ts-types';
import { Progress, Prompter, Spinner, Ux } from './ux';

Expand Down Expand Up @@ -33,14 +33,20 @@ export abstract class SfCommand<T> extends Command {
public static envVariablesSection?: HelpSection;
public static errorCodes?: HelpSection;
public static tableFlags = CliUx.ux.table.flags;
public static requiresProject: boolean;

public spinner: Spinner;
public progress: Progress;
public project!: SfdxProject;

private warnings: SfCommand.Warning[] = [];
private ux: Ux;
private prompter: Prompter;

protected get statics(): typeof SfCommand {
return this.constructor as typeof SfCommand;
}

public constructor(argv: string[], config: Config) {
super(argv, config);
const outputEnabled = !this.jsonEnabled();
Expand Down Expand Up @@ -114,6 +120,13 @@ export abstract class SfCommand<T> extends Command {
return this.prompter.prompt(questions, initialAnswers);
}

public async _run<R>(): Promise<R | undefined> {
if (this.statics.requiresProject) {
this.project = await this.assignProject();
}
return super._run<R>();
}

/**
* Wrap the command result into the standardized JSON structure.
*/
Expand All @@ -138,6 +151,17 @@ export abstract class SfCommand<T> extends Command {
};
}

protected async assignProject(): Promise<SfdxProject> {
try {
return await SfdxProject.resolve();
} catch (err) {
if (err instanceof Error && err.name === 'InvalidProjectWorkspaceError') {
throw messages.createError('errors.RequiresProject');
}
throw err;
}
}

public abstract run(): Promise<T>;
}

Expand Down

0 comments on commit 5ff0cf3

Please sign in to comment.