Trying to understand the right way to have my sfdx plugin command just return a string as the default output, but so far every example shows the returning of an object.

Structure under the src folder that sfdx plugins:generate automatically scaffolds:

src [folder]
---commands [folder]
------base [folder]
---------topic1 [folder]
index.ts (which only has the line "export default {};")

My code so far:

import { SfdxCommand } from '@salesforce/command';
import { Messages } from '@salesforce/core';
import { AnyJson } from '@salesforce/ts-types';
// tslint:disable-next-line:no-var-requires
const { promisify } = require('util');
// tslint:disable-next-line:no-var-requires
const exec = promisify(require('child_process').exec);

// Initialize Messages with the current plugin directory

// Load the specific messages for this file. Messages from @salesforce/command, @salesforce/core,
// or any library that is using the messages framework can also be loaded this way.
const messages = Messages.loadMessages('topic1', 'org');

export default class Org extends SfdxCommand {
  public static description = messages.getMessage('latestCommitDescription');

  public static examples = [
  '$ sfdx base:topic1:command1'

  public static args = [{name: 'file'}];
  public static gitHashRegex = new RegExp('^[0-9A-Za-z]{7,40}$', 'g');

  public async getLatestCommitHash() {
    const data = exec('git rev-parse HEAD');
    return data;

  public async run(): Promise<AnyJson> {
    // tslint:disable-next-line:quotemark
    const latestCommit = await this.getLatestCommitHash();
    // using .trimEnd() because bash returns the string with a trailing \n

    if (Org.gitHashRegex.test(latestCommit.stdout.trimEnd()) === true) {
      // console.log('Data is ' + latestCommit.stdout);
      const latestHash = latestCommit.stdout.trimEnd();
      return latestHash;
    } else if (latestCommit.stderr.trimEnd() === 'fatal: not a git repository (or any of the parent directories): .git') {
      const latestHash = latestCommit.stderr.trimEnd();
      return {latestHash};
    } else {
      console.log('hit the ELSE block');

So if I link the plugin to a folder and inside that folder run sfdx base:topic1:command1, nothing is output to a console.

However if I run sfdx base:topic1:command1 --json, the data I want appears. Either way console.log() statements will show up if I uncomment them.

How can I get my command to just return the string of a SHA hash without having to specify the --json flag

1 Answer 1


--json will output what you return in your run method.

If you want to display something without the --json, you can use this.ux.log('Hello World!').
This output will not be visible using --json though, which is likely what you want, as in this case everything should be in your returned json object.

In case you would like to see something all the time, you can still use console.log().

  • Perfecto, thank you so much!
    – trebleCode
    Sep 25, 2019 at 20:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .