import { flags , SfdxCommand } from '@salesforce/command' ; import { Messages , SfdxError } from '@salesforce/core' ; import * as fs from 'fs' ; import * as path from 'path' ; const util = require ( 'util' ) ; const xml2js = require ( 'xml2js' ) ; Messages . importMessagesDirectory ( __dirname ) ; const defaultCustomLabelsFolder = 'force-app/main/default/labels' ; const customLabelsFileName = 'CustomLabels.labels-meta.xml' ; const messages = Messages . loadMessages ( 'texei-sfdx-plugin' , 'source-customlabel-replace' ) ; export default class Replace extends SfdxCommand { public static description = messages . getMessage ( 'commandDescription' ) ; public static examples = [ '$ texei:source:customlabel:replace --label GreatSalesforceBlog --value https://blog.texei.com' ] ; protected static flagsConfig = { path : flags . string ( { char : 'p' , required : false , description : `path to custom label` } ) , label : flags . string ( { char : 'l' , required : true , description : `custom label to replace` } ) , value : flags . string ( { char : 'v' , required : true , description : `new custom label` } ) , } ; protected static requiresUsername = false ; protected static requiresDevhubUsername = false ; protected static requiresProject = true ; public async run ( ) { let updatedCustomLabels = [ ] ; const readFile = util . promisify ( fs . readFile ) ; const writeFile = util . promisify ( fs . writeFile ) ; const pathToFile = this . flags . path ? this . flags . path : defaultCustomLabelsFolder ; const filePath = path . join ( process . cwd ( ) , pathToFile , customLabelsFileName ) ; const customLabelFile = await readFile ( filePath , 'utf8' ) ; var parser = new xml2js . Parser ( ) ; const parseString = util . promisify ( parser . parseString ) ; const customLabelJson = await parseString ( customLabelFile ) ; let labelUpdated = false ; const labelIndex = customLabelJson . CustomLabels . labels . findIndex ( label => label . fullName == this . flags . label ) ; if ( labelIndex != - 1 ) { customLabelJson . CustomLabels . labels [ labelIndex ] . value = this . flags . value ; labelUpdated = true ; } else { throw new SfdxError ( `Custom Label ${ this . flags . label } not found.` ) ; } if ( labelUpdated ) { const builder = new xml2js . Builder ( ) ; var xmlFile = builder . buildObject ( customLabelJson ) ; await writeFile ( filePath , xmlFile , 'utf8' ) ; updatedCustomLabels . push ( this . flags . label ) ; this . ux . log ( `Custom Label ${ this . flags . label } has been replaced with value ${ this . flags . value }` ) ; } return { updatelabels : updatedCustomLabels } ; } }