Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

label Creation #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/commands/create-label.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import * as vscode from 'vscode'
import utils from '../utils/utils'
import * as path from 'upath'
import { readFileSync, writeFileSync } from 'fs'

interface Label { fullName: string[]; categories: string[]|null; language: string[]|null, protected: string[]|null, shortDescription: string[]|null, value: string[] }
async function chooseType (options:string[]): Promise<string|undefined> {
return new Promise((resolve) => {
const quickPick = vscode.window.createQuickPick()
const choices = options.map(choice => ({ label: choice }))
quickPick.items = choices
quickPick.title = 'Category'
quickPick.onDidChangeValue(() => {
if (!options.includes(quickPick.value)) quickPick.items = [{ label: quickPick.value }, ...choices]
})
quickPick.onDidAccept(() => {
const selection = quickPick.activeItems[0]
resolve(selection.label)
quickPick.hide()
})
quickPick.show()
})
}

export default async function createLabel () {
try {
const customLabelsUri = path.join(utils.getWorkspaceFolder(), 'src/labels/CustomLabels.labels')
const labelsXml = await utils.parseXml(readFileSync(customLabelsUri))
const labelApiNames = new Set(labelsXml.CustomLabels.labels.map((l:Label) => l.fullName[0]))

const labelName = await utils.inputText('enter label ApiName', '', {
validateInput: v => {
if (!/[A-Z][a-zA-Z0-9]*$/.test(v)) {
return `Wrong Label Api Name
Should begin with a capital Letter and contain only alphanumeric characters`
}
if (labelApiNames.has(v)) {
return 'ApiName already exists'
}
return null
}
})
if (!labelName) return
const value = await utils.inputText('enter label Value', '')
if (!value) return
const options:string[] = Array.from(new Set(labelsXml.CustomLabels.labels.map((l:Label) => { return l.categories ? l.categories[0] : null }))).filter((l) => !!l)
const cat = await chooseType(options)
const desc = await utils.inputText('enter label Description', '')
const lang = await utils.inputText('enter label Language', 'en-US')
if (!lang) return
const importLabel = await vscode.window.showQuickPick([{ label: 'Yes', value: true }, { label: 'No' }], { ignoreFocusOut: true, title: `Do you want to import label to labels${labelName[0].toLowerCase() <= 'k' ? 'AK' : 'LZ'}.js` })
if (!importLabel) return

labelsXml.CustomLabels.labels.push({
fullName: [labelName],
categories: [cat] || [],
language: [lang],
protected: ['false'],
shortDescription: [desc],
value: [value]
})
labelsXml.CustomLabels.labels.sort((a:Label, b:Label) => (a.fullName[0].toLowerCase() < b.fullName[0].toLowerCase() ? -1 : 1))
writeFileSync(customLabelsUri, utils.buildXml(labelsXml))

if (importLabel?.value) {
const filename = `labels${labelName[0].toLowerCase() <= 'k' ? 'AK' : 'LZ'}`
const filepath = path.join(utils.getWorkspaceFolder(), `src/lwc/${filename}/${filename}.js`)
const fileData = String.fromCharCode(...readFileSync(filepath))
const jsLabels = [...fileData.matchAll(/(?:import )(?<name>\w*)(?: from '@salesforce\/label\/c.)(?<object>.*)(?:')/g)].map(l => l.groups)
jsLabels.push({ name: labelName, object: labelName })
jsLabels.sort((a, b) => { return (a?.name.toLowerCase() || '') < (b?.name.toLowerCase() || '') ? -1 : 1 })
let newData = ''
jsLabels.forEach(l => {
newData += `import ${l?.name} from '@salesforce/label/c.${l?.object}'\n`
})
newData += '\nexport default {\n'
jsLabels.forEach((l, idx) => {
newData += ` ${l?.name}${idx < jsLabels.length - 1 ? ',' : ''}\n`
})
newData += '}'
await writeFileSync(filepath, newData)
}
vscode.window.showInformationMessage(`Label ${labelName} has been Created`)
} catch (e) {
vscode.window.showErrorMessage('An Error occured: \n' + e.message)
}
}
5 changes: 4 additions & 1 deletion src/commands/create-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import sfdcConnector from '../sfdc-connector'
import packageService from '../services/package-service'
import { buildXml } from 'sfdy/src/utils/xml-utils'
import createField from './create-field'
import createLabel from './create-label'

interface DocType { label: string; toolingType: string; folder?: string; extension?: string }

Expand All @@ -20,7 +21,8 @@ async function chooseType (): Promise<DocType | undefined> {
{ label: 'Apex trigger', toolingType: 'ApexTriggerMember', folder: 'triggers', extension: '.trigger' },
{ label: 'Lightning component', toolingType: 'AuraDefinitionBundle', folder: 'aura', extension: '.cmp' },
{ label: 'Lightning web component', toolingType: 'LightningComponentBundle', folder: 'lwc', extension: '.js' },
{ label: 'Field', toolingType: 'CustomField' }
{ label: 'Field', toolingType: 'CustomField' },
{ label: 'Label', toolingType: 'CustomLabel' }
], { ignoreFocusOut: true })
return res
}
Expand Down Expand Up @@ -159,6 +161,7 @@ export default async function createMeta () {
if (!docType) return

if (docType.toolingType === 'CustomField') return createField()
if (docType.toolingType === 'CustomLabel') return createLabel()

const docName = await utils.inputText(`enter ${docType.label.toLowerCase()} name`, '', {
validateInput: v => {
Expand Down
5 changes: 5 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import manageCredentials from './manage-credentials'
import removeCredentials from './remove-credentials'
import compile from './compile'
import createAuraDefinition from './create-aura-definition'
import createLabel from './create-label'
import createMeta from './create-metadata'
import credentials from './credentials'
import deploy from './deploy'
Expand Down Expand Up @@ -37,6 +38,10 @@ export default {
reporter.sendEvent('createAuraDefinition')
createAuraDefinition(docUri)
},
createLabel: () => {
reporter.sendEvent('createLabel')
createLabel()
},
createMeta: () => {
reporter.sendEvent('createMeta')
createMeta()
Expand Down