Skip to content

Commit

Permalink
Now able to generate LWCs using template files.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbreese committed Nov 9, 2023
1 parent e9fa2f8 commit bc62e5e
Show file tree
Hide file tree
Showing 4 changed files with 375 additions and 9 deletions.
17 changes: 17 additions & 0 deletions resources/instructions/createSObjectLwcQuickActions.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,36 @@ <h1>Create sObject LWC Quick Actions</h1>
Click 'Generate LWC Quick Actions' to create the missing LWC Quick
Actions:
</label>

<button id="generateLwcQuickActionsButton">
Generate LWC Quick Actions
</button>

<button id="skipStepButton">
Skip This Step
</button>

<script>
const generateQuickActionsButtonElement = document.getElementById(
'generateLwcQuickActionsButton'
);

const skipStepButtonElement = document.getElementById(
'skipStepButton'
);

generateQuickActionsButtonElement.addEventListener('click', () => {
webviewMessaging.sendMessageRequest(
'generateLwcQuickActionsButton'
);
});

skipStepButtonElement.addEventListener('click', () => {
webviewMessaging.sendMessageRequest(
'skipStepButton'
);
});

// Wait until all scripts are loaded, before engaging with e.g.
// messaging functionality.
window.addEventListener('load', () => {
Expand Down
84 changes: 75 additions & 9 deletions src/commands/wizard/lwcGenerationCommand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

import { Uri, l10n } from 'vscode';
import { InstructionsWebviewProvider } from '../../webviews/instructions';
import * as fs from 'fs';
import { CodeBuilder } from '../../utils/codeBuilder';

export type QuickActionStatus = {
view: boolean;
Expand All @@ -15,27 +23,44 @@ export type SObjectQuickActionStatus = {
};

export class LwcGenerationCommand {
extensionUri: Uri;

constructor(extensionUri: Uri) {
this.extensionUri = extensionUri;
}

async createSObjectLwcQuickActions() {
static async createSObjectLwcQuickActions(extensionUri: Uri) {
return new Promise<void>((resolve) => {
new InstructionsWebviewProvider(
this.extensionUri
extensionUri
).showInstructionWebview(
l10n.t('Offline Starter Kit: Create sObject LWC Quick Actions'),
'resources/instructions/createSObjectLwcQuickActions.html',
[
{
type: 'generateLwcQuickActionsButton',
type: 'skipStepButton',
action: (panel) => {
panel.dispose();
return resolve();
}
},
{
type: 'generateLwcQuickActionsButton',
action: async (panel) => {
// TODO: Hook this up to function that parses landing_page.json.
const sobjects = [
'Account',
'Contact',
'Opportunity',
'SomeOther'
];
const quickActionStatus =
await LwcGenerationCommand.checkForExistingQuickActions(
sobjects
);

await this.generateMissingLwcsAndQuickActions(
extensionUri,
quickActionStatus
);
panel.dispose();
return resolve();
}
},
{
type: 'getQuickActionStatus',
action: async (_panel, _data, callback) => {
Expand Down Expand Up @@ -95,6 +120,47 @@ export class LwcGenerationCommand {
});
}

static async generateMissingLwcsAndQuickActions(
extensionUri: Uri,
quickActionStatus: SObjectQuickActionStatus
): Promise<SObjectQuickActionStatus> {
return new Promise<SObjectQuickActionStatus>(async (resolve) => {
for (const sobject in quickActionStatus.sobjects) {
const quickActions = quickActionStatus.sobjects[sobject];

if (
!quickActions.create ||
!quickActions.edit ||
!quickActions.view
) {
// at least 1 needs to be creaed
// TODO: Hook up to compact layout to obtain list of field names to use
const codeBuilder = new CodeBuilder(extensionUri, sobject, [
'Name',
'AccountId'
]);

if (!quickActions.view) {
await codeBuilder.generateView();
}

if (!quickActions.edit) {
await codeBuilder.generateEdit();
}

if (!quickActions.create) {
await codeBuilder.generateCreate();
}
}
}

// Just double check now that things have been created.
return await LwcGenerationCommand.checkForExistingQuickActions(
Object.keys(quickActionStatus.sobjects)
);
});
}

private static checkForExistingQuickAction(
sobject: string,
qaName: string
Expand Down
74 changes: 74 additions & 0 deletions src/test/suite/utils/codeBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

import * as assert from 'assert';
import * as sinon from 'sinon';
import { CodeBuilder } from '../../../utils/codeBuilder';
import { Uri } from 'vscode';
import { afterEach, beforeEach } from 'mocha';

suite('CodeBuilder Test Suite', () => {
var extensionUri = Uri.parse('file:///tmp/');

beforeEach(function () {});

afterEach(function () {
sinon.restore();
});

test('Template variables are populated in constructor', async () => {
const codeBuilder = new CodeBuilder(extensionUri, 'Account', [
'field1'
]);
const templateVars = codeBuilder.templateVariables;

assert.equal(
templateVars['TEMPLATE_CREATE_LWC_LABEL'],
'LWC for creating a/an Account instance.'
);
assert.equal(
templateVars['TEMPLATE_EDIT_LWC_LABEL'],
'LWC for editing a/an Account instance.'
);
assert.equal(
templateVars['TEMPLATE_VIEW_LWC_LABEL'],
'LWC for viewing a/an Account instance.'
);

assert.equal(templateVars['TEMPLATE_FIELDS'], 'FIELD1_FIELD, ');
assert.ok(
templateVars['TEMPLATE_IMPORTS'].includes(
'import FIELD1_FIELD from "@salesforce/schema/Account.field1";'
)
);
assert.ok(
templateVars[
'TEMPLATE_LIGHTNING_INPUT_CREATE_FIELDS_HTML'
].includes(
'<lightning-input-field field-name={field1Field} value={field1}></lightning-input-field>'
)
);

assert.ok(
templateVars['TEMPLATE_LIGHTNING_INPUT_EDIT_FIELDS_HTML'].includes(
'<lightning-input-field field-name={field1Field}></lightning-input-field>'
)
);

assert.ok(
templateVars['TEMPLATE_VARIABLES'].includes(
'field1Field = FIELD1_FIELD;'
)
);

assert.ok(
templateVars['TEMPLATE_VARIABLE_ASSIGNMENTS'].includes(
'field1 = "";'
)
);
});
});
Loading

0 comments on commit bc62e5e

Please sign in to comment.