Skip to content

Commit

Permalink
feat: add typescript template for lwc (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsjtu authored Jul 18, 2024
1 parent 9fd88f7 commit 5dc9ae4
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 7 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,8 @@
"@typescript-eslint/no-non-null-assertion": ["off"],
"@typescript-eslint/no-var-requires": ["off"]
},
"ignorePatterns": ["*.js"]
"ignorePatterns": [
"*.js",
"src/templates/lightningcomponent/lwc/typeScript/*"
]
}
19 changes: 15 additions & 4 deletions src/generators/lightningComponentGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,18 @@ export default class LightningComponentGenerator extends BaseGenerator<Lightning
)!
.join('-')
.toLowerCase();
const ext = template === 'typeScript' ? 'ts' : 'js';

this.sourceRootWithPartialPath(
path.join('lightningcomponent', 'lwc', template)
);
await this.render(
this.templatePath(`${template}.js`),
this.templatePath(`${template}.${ext}`),
this.destinationPath(
path.join(
this.outputdir,
camelCaseComponentName,
`${camelCaseComponentName}.js`
`${camelCaseComponentName}.${ext}`
)
),
{ pascalCaseComponentName }
Expand All @@ -177,13 +178,13 @@ export default class LightningComponentGenerator extends BaseGenerator<Lightning
);

await this.render(
this.templatePath(path.join(`__tests__`, `${template}.test.js`)),
this.templatePath(path.join(`__tests__`, `${template}.test.${ext}`)),
this.destinationPath(
path.join(
this.outputdir,
camelCaseComponentName,
`__tests__`,
`${camelCaseComponentName}.test.js`
`${camelCaseComponentName}.test.${ext}`
)
),
{
Expand All @@ -193,6 +194,16 @@ export default class LightningComponentGenerator extends BaseGenerator<Lightning
}
);

if (template === 'typeScript') {
await this.render(
this.templatePath('gitignore'),
this.destinationPath(
path.join(this.outputdir, camelCaseComponentName, '.gitignore')
),
{}
);
}

if (!internal) {
const masterLabel = camelCaseToTitleCase(componentname)
.replace(/</g, '&lt;')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createElement } from 'lwc';
import <%= pascalCaseComponentName %> from 'c/<%= camelCaseComponentName %>';

describe('c-<%= kebabCaseComponentName %>', () => {
afterEach(() => {
// The jsdom instance is shared across test cases in a single file so reset the DOM
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
});

it('TODO: test case generated by CLI command, please fill in test logic', () => {
// Arrange
const element = createElement('c-<%= kebabCaseComponentName %>', {
is: <%= pascalCaseComponentName %>
});

// Act
document.body.appendChild(element);

// Assert
// const div = element.shadowRoot.querySelector('div');
expect(1).toBe(1);
});
});
1 change: 1 addition & 0 deletions src/templates/lightningcomponent/lwc/typeScript/gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>

</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion><%= apiVersion %></apiVersion>
<isExposed>false</isExposed>
</LightningComponentBundle>
3 changes: 3 additions & 0 deletions src/templates/lightningcomponent/lwc/typeScript/typeScript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LightningElement } from 'lwc';

export default class <%= pascalCaseComponentName %> extends LightningElement {}
6 changes: 5 additions & 1 deletion src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ export interface LightningAppOptions extends TemplateOptions {

export interface LightningComponentOptions extends TemplateOptions {
componentname: string;
template: 'default' | 'analyticsDashboard' | 'analyticsDashboardWithStep';
template:
| 'default'
| 'analyticsDashboard'
| 'analyticsDashboardWithStep'
| 'typeScript';
type: 'aura' | 'lwc';
internal: boolean;
}
Expand Down
28 changes: 28 additions & 0 deletions test/service/templateService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,34 @@ describe('TemplateService', () => {
);
});

it('should create LightningComponent (lwc) with TypeScript', async () => {
await remove(path.join('testsoutput', 'libraryCreate', 'lwc'));
const templateService = TemplateService.getInstance();
const result = await templateService.create(
TemplateType.LightningComponent,
{
componentname: 'LibraryCreateComponent',
outputdir: path.join('testsoutput', 'libraryCreate', 'lwc'),
template: 'typeScript',
type: 'lwc',
}
);

chai
.expect(result.created.sort())
.to.deep.equal(
[
'testsoutput/libraryCreate/lwc/libraryCreateComponent/libraryCreateComponent.ts',
'testsoutput/libraryCreate/lwc/libraryCreateComponent/libraryCreateComponent.html',
'testsoutput/libraryCreate/lwc/libraryCreateComponent/__tests__/libraryCreateComponent.test.ts',
'testsoutput/libraryCreate/lwc/libraryCreateComponent/libraryCreateComponent.js-meta.xml',
'testsoutput/libraryCreate/lwc/libraryCreateComponent/.gitignore',
]
.map((p) => path.normalize(p))
.sort()
);
});

it('should create LightningEvent', async () => {
await remove(path.join('testsoutput', 'libraryCreate', 'aura'));
const templateService = TemplateService.getInstance();
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
},
"include": ["./src/**/*"]
"include": ["./src/**/*"],
"exclude": ["./src/templates/lightningcomponent/lwc/typeScript/*"]
}

0 comments on commit 5dc9ae4

Please sign in to comment.