From 64a2ee2d4048f678d8fbfa71a542d770e86f1105 Mon Sep 17 00:00:00 2001 From: Greg Smith Date: Thu, 27 May 2021 11:47:59 -0600 Subject: [PATCH] feat: add analytics dashboard lwc templates (#325) * feat: support aura/lwc component templates @W-9191596@ * feat: analytics dashboard lwc templates @W-9191596@ * fix: update to union type on LightningComponentOptions and also use a hard-coded list on the options flag config for --template to match for consistency. --- .../plugin-templates/messages/messages.json | 1 + .../force/lightning/component/create.ts | 11 +- .../force/lightning/component/create.test.ts | 184 +++++++++++++++++- .../generators/lightningComponentGenerator.ts | 146 ++++++++------ packages/templates/src/i18n/i18n.ts | 2 + .../default.auradoc} | 0 .../default.cmp} | 0 .../default.cmp-meta.xml} | 0 .../default.css} | 0 .../default.design} | 0 .../default.svg} | 0 .../defaultController.js} | 0 .../defaultHelper.js} | 0 .../defaultRenderer.js} | 0 .../analyticsDashboard.html} | 0 .../analyticsDashboard/analyticsDashboard.js | 6 + .../analyticsDashboard.js-meta.xml | 13 ++ .../analyticsDashboardWithStep.html | 3 + .../analyticsDashboardWithStep.js | 11 ++ .../analyticsDashboardWithStep.js-meta.xml | 13 ++ .../lwc/default/default.html | 3 + .../default.js} | 0 .../default.js-meta.xml} | 0 packages/templates/src/utils/createUtil.ts | 29 +++ packages/templates/src/utils/types.ts | 10 +- .../templates/test/utils/createUtil.test.ts | 78 ++++++++ 26 files changed, 434 insertions(+), 76 deletions(-) rename packages/templates/src/templates/lightningcomponent/aura/{DefaultLightningAuradoc.auradoc => default/default.auradoc} (100%) rename packages/templates/src/templates/lightningcomponent/aura/{DefaultLightningCmp.cmp => default/default.cmp} (100%) rename packages/templates/src/templates/lightningcomponent/aura/{_auradefinitionbundle.cmp-meta.xml => default/default.cmp-meta.xml} (100%) rename packages/templates/src/templates/lightningcomponent/aura/{DefaultLightningCss.css => default/default.css} (100%) rename packages/templates/src/templates/lightningcomponent/aura/{DefaultLightningDesign.design => default/default.design} (100%) rename packages/templates/src/templates/lightningcomponent/aura/{DefaultLightningSVG.svg => default/default.svg} (100%) rename packages/templates/src/templates/lightningcomponent/aura/{DefaultLightningController.js => default/defaultController.js} (100%) rename packages/templates/src/templates/lightningcomponent/aura/{DefaultLightningHelper.js => default/defaultHelper.js} (100%) rename packages/templates/src/templates/lightningcomponent/aura/{DefaultLightningRenderer.js => default/defaultRenderer.js} (100%) rename packages/templates/src/templates/lightningcomponent/lwc/{_.html => analyticsDashboard/analyticsDashboard.html} (100%) create mode 100644 packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboard/analyticsDashboard.js create mode 100644 packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboard/analyticsDashboard.js-meta.xml create mode 100644 packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboardWithStep/analyticsDashboardWithStep.html create mode 100644 packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboardWithStep/analyticsDashboardWithStep.js create mode 100644 packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboardWithStep/analyticsDashboardWithStep.js-meta.xml create mode 100644 packages/templates/src/templates/lightningcomponent/lwc/default/default.html rename packages/templates/src/templates/lightningcomponent/lwc/{DefaultLightningLWC.js => default/default.js} (100%) rename packages/templates/src/templates/lightningcomponent/lwc/{_js-meta.xml => default/default.js-meta.xml} (100%) diff --git a/packages/plugin-templates/messages/messages.json b/packages/plugin-templates/messages/messages.json index 5f18fc0f..e0b4bf8d 100644 --- a/packages/plugin-templates/messages/messages.json +++ b/packages/plugin-templates/messages/messages.json @@ -64,6 +64,7 @@ "MissingAuraFolder": "Lightning bundles must have a parent folder named 'aura'.", "MissingAppname": "Missing required flag:\n -n, --appname APPNAME name of the generated Lightning app\nSee more help with --help\n", "MissingComponentName": "Missing required flag:\n -n, --componentname COMPONENTNAME name of the generated Lightning component\nSee more help with --help\n", + "MissingLightningComponentTemplate": "Template %s not available for component type %s.", "MissingLWCFolder": "Lightning bundles must have a parent folder named 'lwc'.", "MissingEventname": "Missing required flag:\n -n, --eventname EVENTNAME name of the generated Lightning event\nSee more help with --help\n", "MissingInterfacename": "Missing required flag:\n -n, --interfacename INTERFACENAME name of the generated Lightning interface\nSee more help with --help\n", diff --git a/packages/plugin-templates/src/commands/force/lightning/component/create.ts b/packages/plugin-templates/src/commands/force/lightning/component/create.ts index ef01865f..2441b55f 100644 --- a/packages/plugin-templates/src/commands/force/lightning/component/create.ts +++ b/packages/plugin-templates/src/commands/force/lightning/component/create.ts @@ -9,11 +9,9 @@ import { flags } from '@salesforce/command'; import LightningComponentGenerator from '@salesforce/templates/lib/generators/lightningComponentGenerator'; -import { CreateUtil } from '@salesforce/templates/lib/utils'; import { AnyJson } from '@salesforce/ts-types'; import { MessageUtil, TemplateCommand } from '../../../../utils'; -const lightningComponentFileSuffix = /.cmp$/; const BUNDLE_TYPE = MessageUtil.get('Component'); export default class LightningComponent extends TemplateCommand { @@ -53,11 +51,10 @@ export default class LightningComponent extends TemplateCommand { char: 't', description: MessageUtil.get('TemplateFlagDescription'), longDescription: MessageUtil.get('TemplateFlagLongDescription'), - default: 'DefaultLightningCmp', - options: CreateUtil.getCommandTemplatesForFiletype( - lightningComponentFileSuffix, - 'lightningcomponent' - ) + default: 'default', + // Note: keep this list here and LightningComponentOptions#template in-sync with the + // templates/lightningcomponents/[aura|lwc]/* folders + options: ['default', 'analyticsDashboard', 'analyticsDashboardWithStep'] }), outputdir: flags.string({ char: 'd', diff --git a/packages/plugin-templates/test/commands/force/lightning/component/create.test.ts b/packages/plugin-templates/test/commands/force/lightning/component/create.test.ts index cbe46df7..81649784 100644 --- a/packages/plugin-templates/test/commands/force/lightning/component/create.test.ts +++ b/packages/plugin-templates/test/commands/force/lightning/component/create.test.ts @@ -59,6 +59,31 @@ describe('Lightning component creation tests:', () => { ); } ); + + test + .withOrg() + .withProject() + .stdout() + .command([ + 'force:lightning:component:create', + '--componentname', + 'foo', + '--outputdir', + 'aura', + '--template', + 'default' + ]) + .it( + 'should create lightning aura component files from default template in the aura output directory', + ctx => { + assert.file(AuraLightningTestFormatter.fileformatter('foo', 'foo')); + assert.file(path.join('aura', 'foo', 'foo.cmp-meta.xml')); + assert.fileContent( + path.join('aura', 'foo', 'foo.cmp-meta.xml'), + '' + ); + } + ); }); describe('Check lightning aura components creation without -meta.xml file', () => { @@ -112,12 +137,12 @@ describe('Lightning component creation tests:', () => { .it( 'should create lightning web component files in the lwc output directory with the internal flag for disabling -meta.xml files', ctx => { - assert.file( - path.join('lwc', 'internallwctest', 'internallwctest.html') - ); assert.noFile( path.join('lwc', 'internallwctest', 'internallwctest.js-meta.xml') ); + assert.file( + path.join('lwc', 'internallwctest', 'internallwctest.html') + ); assert.file( path.join('lwc', 'internallwctest', 'internallwctest.js') ); @@ -144,9 +169,119 @@ describe('Lightning component creation tests:', () => { 'lwc' ]) .it( - 'should create lightning web component files in the lwc output directory with the internal flag for disabling -meta.xml files', + 'should create lightning web component files in the lwc output directory', ctx => { assert.file(path.join('lwc', 'foo', 'foo.js-meta.xml')); + assert.file(path.join('lwc', 'foo', 'foo.html')); + assert.file(path.join('lwc', 'foo', 'foo.js')); + assert.fileContent( + path.join('lwc', 'foo', 'foo.js'), + 'export default class Foo extends LightningElement {}' + ); + } + ); + + test + .withOrg() + .withProject() + .stdout() + .command([ + 'force:lightning:component:create', + '--componentname', + 'foo', + '--outputdir', + 'lwc', + '--type', + 'lwc', + '--template', + 'default' + ]) + .it( + 'should create lightning web component files from default template in the lwc output directory', + ctx => { + assert.file(path.join('lwc', 'foo', 'foo.js-meta.xml')); + assert.file(path.join('lwc', 'foo', 'foo.html')); + assert.file(path.join('lwc', 'foo', 'foo.js')); + assert.fileContent( + path.join('lwc', 'foo', 'foo.js'), + 'export default class Foo extends LightningElement {}' + ); + } + ); + }); + + describe('Check analytics dashboard lwc creation', () => { + test + .withOrg() + .withProject() + .stdout() + .command([ + 'force:lightning:component:create', + '--componentname', + 'foo', + '--outputdir', + 'lwc', + '--type', + 'lwc', + '--template', + 'analyticsDashboard' + ]) + .it( + 'should create analyticsDashboard lwc files in the lwc output directory', + ctx => { + const jsFile = path.join('lwc', 'foo', 'foo.js'); + const metaFile = path.join('lwc', 'foo', 'foo.js-meta.xml'); + assert.file(metaFile); + assert.file(path.join('lwc', 'foo', 'foo.html')); + assert.file(jsFile); + assert.fileContent(metaFile, 'analytics__Dashboard'); + assert.fileContent(metaFile, 'targets="analytics__Dashboard"'); + assert.fileContent(metaFile, 'false'); + assert.fileContent( + jsFile, + 'export default class Foo extends LightningElement {' + ); + assert.fileContent(jsFile, '@api getState;'); + assert.fileContent(jsFile, '@api setState;'); + } + ); + test + .withOrg() + .withProject() + .stdout() + .command([ + 'force:lightning:component:create', + '--componentname', + 'foo', + '--outputdir', + 'lwc', + '--type', + 'lwc', + '--template', + 'analyticsDashboardWithStep' + ]) + .it( + 'should create analyticsDashboardWithStep lwc files in the lwc output directory', + ctx => { + const jsFile = path.join('lwc', 'foo', 'foo.js'); + const metaFile = path.join('lwc', 'foo', 'foo.js-meta.xml'); + assert.file(metaFile); + assert.file(path.join('lwc', 'foo', 'foo.html')); + assert.file(jsFile); + assert.fileContent(metaFile, 'analytics__Dashboard'); + assert.fileContent(metaFile, 'targets="analytics__Dashboard"'); + assert.fileContent(metaFile, 'true'); + assert.fileContent( + jsFile, + 'export default class Foo extends LightningElement {' + ); + assert.fileContent(jsFile, '@api getState;'); + assert.fileContent(jsFile, '@api setState;'); + assert.fileContent(jsFile, '@api results;'); + assert.fileContent(jsFile, '@api metadata;'); + assert.fileContent(jsFile, '@api selection;'); + assert.fileContent(jsFile, '@api setSelection;'); + assert.fileContent(jsFile, '@api selectMode;'); } ); }); @@ -184,5 +319,46 @@ describe('Lightning component creation tests:', () => { .it('should throw missing lwc parent folder error', ctx => { expect(ctx.stderr).to.contain(messages.getMessage('MissingLWCFolder')); }); + test + .withOrg() + .withProject() + .stderr() + .command([ + 'force:lightning:component:create', + '--outputdir', + 'lwc', + '--componentname', + 'foo', + '--type', + 'lwc', + '--template', + 'foo' + ]) + .it('should throw invalid template error', ctx => { + expect(ctx.stderr).to.contain(messages.getMessage('InvalidTemplate')); + }); + test + .withOrg() + .withProject() + .stderr() + .command([ + 'force:lightning:component:create', + '--outputdir', + 'aura', + '--componentname', + 'foo', + '--type', + 'aura', + '--template', + 'analyticsDashboard' + ]) + .it('should throw missing template error', ctx => { + expect(ctx.stderr).to.contain( + messages.getMessage('MissingLightningComponentTemplate', [ + 'analyticsDashboard', + 'aura' + ]) + ); + }); }); }); diff --git a/packages/templates/src/generators/lightningComponentGenerator.ts b/packages/templates/src/generators/lightningComponentGenerator.ts index 65c651f8..f1e0e874 100644 --- a/packages/templates/src/generators/lightningComponentGenerator.ts +++ b/packages/templates/src/generators/lightningComponentGenerator.ts @@ -30,6 +30,19 @@ export default class LightningComponentGenerator extends SfdxGenerator< throw new Error(nls.localize('MissingAuraDir')); } } + + if ( + CreateUtil.getCommandTemplatesInSubdirs('lightningcomponent', { + subdir: this.options.type + }).indexOf(this.options.template) < 0 + ) { + throw new Error( + nls.localize('MissingLightningComponentTemplate', [ + this.options.template, + this.options.type + ]) + ); + } } public writing() { @@ -41,14 +54,21 @@ export default class LightningComponentGenerator extends SfdxGenerator< type, internal } = this.options; - // tslint:disable-next-line:no-unused-expression + if (type === 'aura') { this.sourceRoot( - path.join(__dirname, '..', 'templates', 'lightningcomponent', 'aura') + path.join( + __dirname, + '..', + 'templates', + 'lightningcomponent', + 'aura', + template + ) ); if (!internal) { this.fs.copyTpl( - this.templatePath('_auradefinitionbundle.cmp-meta.xml'), + this.templatePath(`${template}.cmp-meta.xml`), this.destinationPath( path.join(outputdir, componentname, `${componentname}.cmp-meta.xml`) ), @@ -60,63 +80,62 @@ export default class LightningComponentGenerator extends SfdxGenerator< ); } this.fs.copyTpl( - this.templatePath('DefaultLightningAuradoc.auradoc'), + this.templatePath(`${template}.auradoc`), this.destinationPath( path.join(outputdir, componentname, `${componentname}.auradoc`) ), {} - ), - this.fs.copyTpl( - this.templatePath(`${template}.cmp`), - this.destinationPath( - path.join(outputdir, componentname, `${componentname}.cmp`) - ), - {} + ); + this.fs.copyTpl( + this.templatePath(`${template}.cmp`), + this.destinationPath( + path.join(outputdir, componentname, `${componentname}.cmp`) ), - this.fs.copyTpl( - this.templatePath('DefaultLightningCss.css'), - this.destinationPath( - path.join(outputdir, componentname, `${componentname}.css`) - ), - {} + {} + ); + this.fs.copyTpl( + this.templatePath(`${template}.css`), + this.destinationPath( + path.join(outputdir, componentname, `${componentname}.css`) ), - this.fs.copyTpl( - this.templatePath('DefaultLightningDesign.design'), - this.destinationPath( - path.join(outputdir, componentname, `${componentname}.design`) - ), - {} + {} + ); + this.fs.copyTpl( + this.templatePath(`${template}.design`), + this.destinationPath( + path.join(outputdir, componentname, `${componentname}.design`) ), - this.fs.copyTpl( - this.templatePath('DefaultLightningSVG.svg'), - this.destinationPath( - path.join(outputdir, componentname, `${componentname}.svg`) - ), - {} + {} + ); + this.fs.copyTpl( + this.templatePath(`${template}.svg`), + this.destinationPath( + path.join(outputdir, componentname, `${componentname}.svg`) ), - this.fs.copyTpl( - this.templatePath('DefaultLightningController.js'), - this.destinationPath( - path.join(outputdir, componentname, `${componentname}Controller.js`) - ), - {} + {} + ); + this.fs.copyTpl( + this.templatePath(`${template}Controller.js`), + this.destinationPath( + path.join(outputdir, componentname, `${componentname}Controller.js`) ), - this.fs.copyTpl( - this.templatePath('DefaultLightningHelper.js'), - this.destinationPath( - path.join(outputdir, componentname, `${componentname}Helper.js`) - ), - {} + {} + ); + this.fs.copyTpl( + this.templatePath(`${template}Helper.js`), + this.destinationPath( + path.join(outputdir, componentname, `${componentname}Helper.js`) ), - this.fs.copyTpl( - this.templatePath('DefaultLightningRenderer.js'), - this.destinationPath( - path.join(outputdir, componentname, `${componentname}Renderer.js`) - ), - {} - ); + {} + ); + this.fs.copyTpl( + this.templatePath(`${template}Renderer.js`), + this.destinationPath( + path.join(outputdir, componentname, `${componentname}Renderer.js`) + ), + {} + ); } - // tslint:disable-next-line:no-unused-expression if (type === 'lwc') { // lwc requires first letter of filename to be lowercase const fileName = `${componentname @@ -129,23 +148,30 @@ export default class LightningComponentGenerator extends SfdxGenerator< .toUpperCase()}${componentname.substring(1)}`; this.sourceRoot( - path.join(__dirname, '..', 'templates', 'lightningcomponent', 'lwc') + path.join( + __dirname, + '..', + 'templates', + 'lightningcomponent', + 'lwc', + template + ) ); this.fs.copyTpl( - this.templatePath('DefaultLightningLWC.js'), + this.templatePath(`${template}.js`), this.destinationPath(path.join(outputdir, fileName, `${fileName}.js`)), { className } - ), - this.fs.copyTpl( - this.templatePath('_.html'), - this.destinationPath( - path.join(outputdir, fileName, `${fileName}.html`) - ), - {} - ); + ); + this.fs.copyTpl( + this.templatePath(`${template}.html`), + this.destinationPath( + path.join(outputdir, fileName, `${fileName}.html`) + ), + {} + ); if (!internal) { this.fs.copyTpl( - this.templatePath('_js-meta.xml'), + this.templatePath(`${template}.js-meta.xml`), this.destinationPath( path.join(outputdir, fileName, `${fileName}.js-meta.xml`) ), diff --git a/packages/templates/src/i18n/i18n.ts b/packages/templates/src/i18n/i18n.ts index 59058bff..93e92209 100644 --- a/packages/templates/src/i18n/i18n.ts +++ b/packages/templates/src/i18n/i18n.ts @@ -26,6 +26,8 @@ export const messages = { "Analytics templates must have a parent folder named 'waveTemplates'.", MissingAuraDir: "Lightning bundles must have a parent folder named 'aura'.", MissingLWCDir: "Lightning bundles must have a parent folder named 'lwc'.", + MissingLightningComponentTemplate: + 'Template %s not available for component type %s.', LightningAppBundle: 'A Lightning Application Bundle', LightningComponentBundle: 'A Lightning Component Bundle', diff --git a/packages/templates/src/templates/lightningcomponent/aura/DefaultLightningAuradoc.auradoc b/packages/templates/src/templates/lightningcomponent/aura/default/default.auradoc similarity index 100% rename from packages/templates/src/templates/lightningcomponent/aura/DefaultLightningAuradoc.auradoc rename to packages/templates/src/templates/lightningcomponent/aura/default/default.auradoc diff --git a/packages/templates/src/templates/lightningcomponent/aura/DefaultLightningCmp.cmp b/packages/templates/src/templates/lightningcomponent/aura/default/default.cmp similarity index 100% rename from packages/templates/src/templates/lightningcomponent/aura/DefaultLightningCmp.cmp rename to packages/templates/src/templates/lightningcomponent/aura/default/default.cmp diff --git a/packages/templates/src/templates/lightningcomponent/aura/_auradefinitionbundle.cmp-meta.xml b/packages/templates/src/templates/lightningcomponent/aura/default/default.cmp-meta.xml similarity index 100% rename from packages/templates/src/templates/lightningcomponent/aura/_auradefinitionbundle.cmp-meta.xml rename to packages/templates/src/templates/lightningcomponent/aura/default/default.cmp-meta.xml diff --git a/packages/templates/src/templates/lightningcomponent/aura/DefaultLightningCss.css b/packages/templates/src/templates/lightningcomponent/aura/default/default.css similarity index 100% rename from packages/templates/src/templates/lightningcomponent/aura/DefaultLightningCss.css rename to packages/templates/src/templates/lightningcomponent/aura/default/default.css diff --git a/packages/templates/src/templates/lightningcomponent/aura/DefaultLightningDesign.design b/packages/templates/src/templates/lightningcomponent/aura/default/default.design similarity index 100% rename from packages/templates/src/templates/lightningcomponent/aura/DefaultLightningDesign.design rename to packages/templates/src/templates/lightningcomponent/aura/default/default.design diff --git a/packages/templates/src/templates/lightningcomponent/aura/DefaultLightningSVG.svg b/packages/templates/src/templates/lightningcomponent/aura/default/default.svg similarity index 100% rename from packages/templates/src/templates/lightningcomponent/aura/DefaultLightningSVG.svg rename to packages/templates/src/templates/lightningcomponent/aura/default/default.svg diff --git a/packages/templates/src/templates/lightningcomponent/aura/DefaultLightningController.js b/packages/templates/src/templates/lightningcomponent/aura/default/defaultController.js similarity index 100% rename from packages/templates/src/templates/lightningcomponent/aura/DefaultLightningController.js rename to packages/templates/src/templates/lightningcomponent/aura/default/defaultController.js diff --git a/packages/templates/src/templates/lightningcomponent/aura/DefaultLightningHelper.js b/packages/templates/src/templates/lightningcomponent/aura/default/defaultHelper.js similarity index 100% rename from packages/templates/src/templates/lightningcomponent/aura/DefaultLightningHelper.js rename to packages/templates/src/templates/lightningcomponent/aura/default/defaultHelper.js diff --git a/packages/templates/src/templates/lightningcomponent/aura/DefaultLightningRenderer.js b/packages/templates/src/templates/lightningcomponent/aura/default/defaultRenderer.js similarity index 100% rename from packages/templates/src/templates/lightningcomponent/aura/DefaultLightningRenderer.js rename to packages/templates/src/templates/lightningcomponent/aura/default/defaultRenderer.js diff --git a/packages/templates/src/templates/lightningcomponent/lwc/_.html b/packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboard/analyticsDashboard.html similarity index 100% rename from packages/templates/src/templates/lightningcomponent/lwc/_.html rename to packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboard/analyticsDashboard.html diff --git a/packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboard/analyticsDashboard.js b/packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboard/analyticsDashboard.js new file mode 100644 index 00000000..2dbe7656 --- /dev/null +++ b/packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboard/analyticsDashboard.js @@ -0,0 +1,6 @@ +import { LightningElement, api } from 'lwc'; + +export default class <%= className %> extends LightningElement { + @api getState; + @api setState; +} \ No newline at end of file diff --git a/packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboard/analyticsDashboard.js-meta.xml b/packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboard/analyticsDashboard.js-meta.xml new file mode 100644 index 00000000..efdc8304 --- /dev/null +++ b/packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboard/analyticsDashboard.js-meta.xml @@ -0,0 +1,13 @@ + + + <%= apiVersion %> + true + + analytics__Dashboard + + + + false + + + \ No newline at end of file diff --git a/packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboardWithStep/analyticsDashboardWithStep.html b/packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboardWithStep/analyticsDashboardWithStep.html new file mode 100644 index 00000000..27e0f695 --- /dev/null +++ b/packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboardWithStep/analyticsDashboardWithStep.html @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboardWithStep/analyticsDashboardWithStep.js b/packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboardWithStep/analyticsDashboardWithStep.js new file mode 100644 index 00000000..4d9450f4 --- /dev/null +++ b/packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboardWithStep/analyticsDashboardWithStep.js @@ -0,0 +1,11 @@ +import { LightningElement, api } from 'lwc'; + +export default class <%= className %> extends LightningElement { + @api results; + @api metadata; + @api selection; + @api setSelection; + @api selectMode; + @api getState; + @api setState; +} \ No newline at end of file diff --git a/packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboardWithStep/analyticsDashboardWithStep.js-meta.xml b/packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboardWithStep/analyticsDashboardWithStep.js-meta.xml new file mode 100644 index 00000000..8bfcdfcf --- /dev/null +++ b/packages/templates/src/templates/lightningcomponent/lwc/analyticsDashboardWithStep/analyticsDashboardWithStep.js-meta.xml @@ -0,0 +1,13 @@ + + + <%= apiVersion %> + true + + analytics__Dashboard + + + + true + + + \ No newline at end of file diff --git a/packages/templates/src/templates/lightningcomponent/lwc/default/default.html b/packages/templates/src/templates/lightningcomponent/lwc/default/default.html new file mode 100644 index 00000000..27e0f695 --- /dev/null +++ b/packages/templates/src/templates/lightningcomponent/lwc/default/default.html @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/packages/templates/src/templates/lightningcomponent/lwc/DefaultLightningLWC.js b/packages/templates/src/templates/lightningcomponent/lwc/default/default.js similarity index 100% rename from packages/templates/src/templates/lightningcomponent/lwc/DefaultLightningLWC.js rename to packages/templates/src/templates/lightningcomponent/lwc/default/default.js diff --git a/packages/templates/src/templates/lightningcomponent/lwc/_js-meta.xml b/packages/templates/src/templates/lightningcomponent/lwc/default/default.js-meta.xml similarity index 100% rename from packages/templates/src/templates/lightningcomponent/lwc/_js-meta.xml rename to packages/templates/src/templates/lightningcomponent/lwc/default/default.js-meta.xml diff --git a/packages/templates/src/utils/createUtil.ts b/packages/templates/src/utils/createUtil.ts index 13dd051e..b8bda24a 100644 --- a/packages/templates/src/utils/createUtil.ts +++ b/packages/templates/src/utils/createUtil.ts @@ -48,4 +48,33 @@ export class CreateUtil { }); return files; } + + /** Get the names of directories that contain matching template files. + * This will look in directories under the command/subdir folder. + * @param command the command name + * @param filetype optional file name pattern to match on in the subdirectories + * @param subdir optional subdirectory under `templates/${command}` + * @return the set of template names + */ + public static getCommandTemplatesInSubdirs( + command: string, + { filetype, subdir }: { filetype?: RegExp; subdir?: string } = {} + ): string[] { + let basedir = path.resolve(__dirname, '..', 'templates', command); + if (subdir) { + basedir = path.join(basedir, subdir); + } + const subdirs = fs + .readdirSync(basedir, { withFileTypes: true }) + .filter(ent => ent.isDirectory()) + .map(ent => ent.name); + if (filetype) { + return subdirs.filter(dir => + fs + .readdirSync(path.join(basedir, dir), { withFileTypes: true }) + .some(ent => ent.isFile() && filetype.test(ent.name)) + ); + } + return subdirs; + } } diff --git a/packages/templates/src/utils/types.ts b/packages/templates/src/utils/types.ts index 9afa833b..513df681 100644 --- a/packages/templates/src/utils/types.ts +++ b/packages/templates/src/utils/types.ts @@ -50,10 +50,10 @@ export interface AnalyticsTemplateOptions extends TemplateOptions { export interface ApexClassOptions extends TemplateOptions { template: - | 'DefaultApexClass' - | 'ApexUnitTest' - | 'ApexException' - | 'InboundEmailService'; + | 'DefaultApexClass' + | 'ApexUnitTest' + | 'ApexException' + | 'InboundEmailService'; classname: string; } @@ -81,7 +81,7 @@ export interface LightningAppOptions extends TemplateOptions { export interface LightningComponentOptions extends TemplateOptions { componentname: string; - template: 'DefaultLightningCmp'; + template: 'default' | 'analyticsDashboard' | 'analyticsDashboardWithStep'; type: 'aura' | 'lwc'; internal: boolean; } diff --git a/packages/templates/test/utils/createUtil.test.ts b/packages/templates/test/utils/createUtil.test.ts index e4fac586..b649b3cd 100644 --- a/packages/templates/test/utils/createUtil.test.ts +++ b/packages/templates/test/utils/createUtil.test.ts @@ -86,4 +86,82 @@ describe('CreateUtil', () => { expect(templates).to.eql(names); }; }); + + describe('getCommandTemplatesInSubdirs', () => { + const templateType = 'lightningcomponent'; + const templatePath = path.resolve( + __dirname, + '../../src/templates', + templateType + ); + const auraPath = path.join(templatePath, 'aura'); + + function dirent(name: string, isDirectory: boolean): fs.Dirent { + const ent = new fs.Dirent(); + ent.name = name; + ent.isDirectory = () => isDirectory; + ent.isFile = () => !isDirectory; + return ent; + } + + let readdirStub: SinonStub; + + beforeEach(() => { + readdirStub = stub(fs, 'readdirSync'); + }); + + afterEach(() => readdirStub.restore()); + + it('should get template names', () => { + readdirStub + .withArgs(templatePath, { withFileTypes: true }) + .returns([ + dirent('Template1', true), + dirent('Template2', true), + dirent('afile.txt', false) + ]); + + const templates = CreateUtil.getCommandTemplatesInSubdirs(templateType); + expect(templates).to.eql(['Template1', 'Template2']); + }); + + it('should get template names for given subdir', () => { + readdirStub + .withArgs(auraPath, { withFileTypes: true }) + .returns([dirent('Template1', true), dirent('Template2', true)]); + + const templates = CreateUtil.getCommandTemplatesInSubdirs(templateType, { + subdir: 'aura' + }); + expect(templates).to.eql(['Template1', 'Template2']); + }); + + it('should ignore subdirs that do not have the given file suffix', () => { + readdirStub + .withArgs(auraPath, { withFileTypes: true }) + .returns([ + dirent('Template1', true), + dirent('Template2', true), + dirent('Template3', true) + ]); + readdirStub + .withArgs(path.join(auraPath, 'Template1'), { withFileTypes: true }) + .returns([ + dirent('Template1.cmp', false), + dirent('Template1Controller.js', false) + ]); + readdirStub + .withArgs(path.join(auraPath, 'Template2'), { withFileTypes: true }) + .returns([dirent('randomfile.html', false)]); + readdirStub + .withArgs(path.join(auraPath, 'Template3'), { withFileTypes: true }) + .returns([]); + + const templates = CreateUtil.getCommandTemplatesInSubdirs(templateType, { + subdir: 'aura', + filetype: /\.cmp$/ + }); + expect(templates).to.eql(['Template1']); + }); + }); });