-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
117 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2024, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
import path from 'node:path'; | ||
import fs from 'node:fs'; | ||
import { SfProject } from '@salesforce/core'; | ||
import { glob } from 'glob'; | ||
import { parseStringPromise } from 'xml2js'; | ||
|
||
export type LwcMetadata = { | ||
LightningComponentBundle: { | ||
description?: string; | ||
masterLabel?: string; | ||
}; | ||
}; | ||
|
||
export class ComponentUtils { | ||
public static componentNameToTitleCase(componentName: string): string { | ||
if (!componentName) { | ||
return ''; | ||
} | ||
|
||
return componentName.replace(/([A-Z])/g, ' $1').replace(/^./, (str) => str.toUpperCase()); | ||
} | ||
|
||
public static async getComponentPaths(project: SfProject): Promise<string[]> { | ||
const packageDirs = project.getPackageDirectories(); | ||
const namespacePaths = ( | ||
await Promise.all(packageDirs.map((dir) => glob(`${dir.fullPath}/**/lwc`, { absolute: true }))) | ||
).flat(); | ||
|
||
return ( | ||
await Promise.all( | ||
namespacePaths.map(async (namespacePath): Promise<string[]> => { | ||
const children = await fs.promises.readdir(namespacePath, { withFileTypes: true }); | ||
|
||
return children | ||
.filter((child) => child.isDirectory()) | ||
.map((child) => path.join(child.parentPath, child.name)); | ||
}) | ||
) | ||
).flat(); | ||
} | ||
|
||
public static async getComponentMetadata(dirname: string): Promise<LwcMetadata | undefined> { | ||
const componentName = path.basename(dirname); | ||
const metaXmlPath: string = path.join(dirname, `${componentName}.js-meta.xml`); | ||
if (!fs.existsSync(metaXmlPath)) { | ||
return undefined; | ||
} | ||
|
||
const xmlContent: string = fs.readFileSync(metaXmlPath, 'utf8'); | ||
const parsedData = (await parseStringPromise(xmlContent)) as LwcMetadata; | ||
if (!this.isLwcMetadata(parsedData)) { | ||
return undefined; | ||
} | ||
|
||
return parsedData; | ||
} | ||
|
||
private static isLwcMetadata(obj: unknown): obj is LwcMetadata { | ||
return (obj && typeof obj === 'object' && 'LightningComponentBundle' in obj) === true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright (c) 2024, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import { expect } from 'chai'; | ||
import { ComponentUtils } from '../../src/shared/componentUtils.js'; | ||
|
||
describe('componentUtils', () => { | ||
it('converts camel case component name to title case', () => { | ||
expect(ComponentUtils.componentNameToTitleCase('myButton')).to.equal('My Button'); | ||
expect(ComponentUtils.componentNameToTitleCase('myButtonGroup')).to.equal('My Button Group'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters