From a46386c87b36644176723a92db01398c95f1e3e5 Mon Sep 17 00:00:00 2001 From: Christoforos Sakellaris Date: Mon, 1 Aug 2022 11:21:50 +0300 Subject: [PATCH] label Creation Importing to JS files (AK, LZ) Sorting and reformating js and XML files --- src/commands/create-label.ts | 87 +++++++++++++++++++++++++++++++++ src/commands/create-metadata.ts | 5 +- src/commands/index.ts | 5 ++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 src/commands/create-label.ts diff --git a/src/commands/create-label.ts b/src/commands/create-label.ts new file mode 100644 index 0000000..4275b5b --- /dev/null +++ b/src/commands/create-label.ts @@ -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 { + 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 )(?\w*)(?: from '@salesforce\/label\/c.)(?.*)(?:')/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) + } +} diff --git a/src/commands/create-metadata.ts b/src/commands/create-metadata.ts index 8d24f0b..926225f 100644 --- a/src/commands/create-metadata.ts +++ b/src/commands/create-metadata.ts @@ -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 } @@ -20,7 +21,8 @@ async function chooseType (): Promise { { 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 } @@ -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 => { diff --git a/src/commands/index.ts b/src/commands/index.ts index 617235c..b70bae8 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -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' @@ -37,6 +38,10 @@ export default { reporter.sendEvent('createAuraDefinition') createAuraDefinition(docUri) }, + createLabel: () => { + reporter.sendEvent('createLabel') + createLabel() + }, createMeta: () => { reporter.sendEvent('createMeta') createMeta()