Skip to content

Instantly share code, notes, and snippets.

@vyuvalv
Last active November 5, 2019 20:34
Show Gist options
  • Save vyuvalv/cc337918de56521c0970286005947d5a to your computer and use it in GitHub Desktop.
Save vyuvalv/cc337918de56521c0970286005947d5a to your computer and use it in GitHub Desktop.
Will Initialize the generator with spinner, pull defaults and yosay image success
const Generator = require('yeoman-generator'); // this
const yosay = require('yosay'); // Yeoman Image
const chalk = require('chalk'); // color text
const shell = require('shelljs'); // shell commands
const spinner = require('ora'); // spinner
module.exports = class extends Generator {
initializing() {
// Start loading spinners
this.loading = new spinner(
{ spinner:'monkey',
color : 'yellow' }
);
// Silently get the available orgs as JSON
let orgsOutput = JSON.parse( shell.exec(' sfdx force:org:list --json', { silent: true } ).stdout );
// Parse all non Scratch orgs
const nonScratchOrgs = orgsOutput.result.nonScratchOrgs;
// Parse all Scratch orgs
const scratchOrgs = orgsOutput.result.scratchOrgs;
// sets Default DevHub
this.devHubOrg = nonScratchOrgs.find(org => org.isDevHub);
// sets Default Org
this.defaultOrg = scratchOrgs.find(org => org.isDefaultUsername);
this.defaultOrg.alias = this.defaultOrg.alias ? this.defaultOrg.alias : { alias : 'NONE'};
if(this.devHubOrg){
// tell yo to say something colourful
this.log(yosay( chalk.redBright( "SFDX Default Orgs ") +
chalk.yellow( "Default DevHub : ") + chalk.cyan(this.devHubOrg.alias) +
chalk.yellow( " Default Org : ") + chalk.cyan( this.defaultOrg.alias ) ));
// Stops Spinner and show success
this.loading.succeed('Pulled defaults successfully');
}
else {
// Stops Spinner and show failure
this.loading.fail('Failed to pull defaults');
// Tell yo to say you need to connect
this.log( yosay( chalk.redBright('NEED TO CONNECT DEVHUB') ));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment