Skip to content

Commit 68baa44

Browse files
committed
feat: single component preview
1 parent d37f000 commit 68baa44

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

command-snapshot.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
"flags": ["device-id", "device-type", "flags-dir", "name", "target-org"],
88
"plugin": "@salesforce/plugin-lightning-dev"
99
},
10+
{
11+
"alias": [],
12+
"command": "lightning:dev:component",
13+
"flagAliases": [],
14+
"flagChars": ["a", "n", "o", "s"],
15+
"flags": ["attributes", "flags-dir", "json", "name", "namespace", "target-org"],
16+
"plugin": "@salesforce/plugin-lightning-dev"
17+
},
1018
{
1119
"alias": [],
1220
"command": "lightning:dev:site",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# summary
2+
3+
Preview a Lightning Web Component in isolation.
4+
5+
# description
6+
7+
Preview and develop a single Lightning Web Component in isolation using Local Dev.
8+
9+
# flags.name.summary
10+
11+
Name of the component to preview.
12+
13+
# flags.namespace.summary
14+
15+
Namespace of the component (defaults to 'c').
16+
17+
# flags.attributes.summary
18+
19+
JSON string of attributes to pass to the component.
20+
21+
# error.invalid.attributes
22+
23+
Invalid JSON format for attributes parameter.
24+
25+
# examples
26+
27+
- Preview a component named 'myComponent':
28+
<%= config.bin %> <%= command.id %> --name myComponent --target-org myOrg
29+
- Preview a component with attributes:
30+
<%= config.bin %> <%= command.id %> --name myComponent --target-org myOrg --attributes '{"label":"Hello","variant":"brand"}'
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2025, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
8+
import { Messages } from '@salesforce/core';
9+
import { cmpDev } from '@lwrjs/api';
10+
11+
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
12+
const messages = Messages.loadMessages('@salesforce/plugin-lightning-dev', 'lightning.dev.component');
13+
14+
export default class LightningDevComponent extends SfCommand<void> {
15+
public static readonly summary = messages.getMessage('summary');
16+
public static readonly description = messages.getMessage('description');
17+
public static readonly examples = messages.getMessages('examples');
18+
19+
public static readonly flags = {
20+
'target-org': Flags.optionalOrg(), // Don't necessarily require org unless we need to proxy
21+
name: Flags.string({
22+
char: 'n',
23+
summary: messages.getMessage('flags.name.summary'),
24+
required: true,
25+
}),
26+
namespace: Flags.string({
27+
char: 's',
28+
summary: messages.getMessage('flags.namespace.summary'),
29+
default: 'c',
30+
}),
31+
attributes: Flags.string({
32+
char: 'a',
33+
summary: messages.getMessage('flags.attributes.summary'),
34+
}),
35+
};
36+
37+
public async run(): Promise<void> {
38+
const { flags } = await this.parse(LightningDevComponent);
39+
40+
const connection = flags['target-org']?.getConnection();
41+
42+
let attributes = {};
43+
if (flags.attributes) {
44+
try {
45+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
46+
attributes = JSON.parse(flags.attributes);
47+
} catch (e) {
48+
throw new Error(messages.getMessage('error.invalid.attributes'));
49+
}
50+
}
51+
52+
await cmpDev({
53+
componentName: flags.name,
54+
namespace: flags.namespace,
55+
attributes,
56+
mode: 'dev',
57+
siteDir: '', // TODO fix
58+
port: 3000,
59+
open: true,
60+
logLevel: 'info',
61+
authToken: connection?.accessToken ?? '',
62+
});
63+
}
64+
}

0 commit comments

Comments
 (0)