Skip to content

Commit

Permalink
Add user-friendly option values for spo site commsite enable. Closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Saurabh7019 committed Aug 1, 2023
1 parent 1d7c557 commit 76e2476
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 7 deletions.
13 changes: 11 additions & 2 deletions docs/docs/cmd/spo/site/site-commsite-enable.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ m365 spo site commsite enable [options]
: The URL of the site to enable communication site features on

`-i, --designPackageId [designPackageId]`
: The ID of the site design to apply when enabling communication site features. Allowed values are `96c933ac-3698-44c7-9f4a-5fd17d71af9e` (Topic = default), `6142d2a0-63a5-4ba0-aede-d9fefca2c767` (Showcase), `f6cc5403-0d63-442e-96c0-285923709ffc` (Blank)
: The ID of the site design to apply when enabling communication site features. Specify designPackageId or designPackage but not both.

`--designPackage [designPackage]`
: The site design to apply when enabling communication site features. Valid values are: Topic, Showcase, or Blank. Defaults to Topic. Specify designPackageId or designPackage but not both.
```

<Global />
Expand All @@ -38,12 +41,18 @@ Enable communication site features on an existing site
m365 spo site commsite enable --url https://contoso.sharepoint.com
```

Enable communication site features on an existing site and apply Showcase design package
Enable communication site features on an existing site and apply the Showcase design package using its ID.

```sh
m365 spo site commsite enable --url https://contoso.sharepoint.com --designPackageId 6142d2a0-63a5-4ba0-aede-d9fefca2c767
```

Enable communication site features on an existing site and apply the Showcase design package using its friendly name.

```sh
m365 spo site commsite enable --url https://contoso.sharepoint.com --designPackage Showcase
```

## Response

The command won't return a response on success.
45 changes: 44 additions & 1 deletion src/m365/spo/commands/site/site-commsite-enable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,51 @@ describe(commands.SITE_COMMSITE_ENABLE, () => {

it('passes validation when valid design package ID specified', async () => {
const actual = await command.validate({
options: { url: 'https://contoso.sharepoint.com', designPackageId: '18eefaa9-ca7b-4ca4-802c-db6d254c533d' }
options: { url: 'https://contoso.sharepoint.com', designPackageId: '96c933ac-3698-44c7-9f4a-5fd17d71af9e' }
}, commandInfo);
assert.strictEqual(actual, true);
});

it('fails validation when invalid design package specified', async () => {
const actual = await command.validate({
options: { url: 'https://contoso.sharepoint.com', designPackage: 'invalid' }
}, commandInfo);
assert.notStrictEqual(actual, true);
});

it('passes validation when valid design package specified', async () => {
const actual = await command.validate({
options: { url: 'https://contoso.sharepoint.com', designPackage: 'Topic' }
}, commandInfo);
assert.strictEqual(actual, true);
});

it('fails validation when designPackage and designPackageId specified (multiple options)', async () => {
const actual = await command.validate({
options: { url: 'https://contoso.sharepoint.com', designPackage: 'Topic', designPackageId: '96c933ac-3698-44c7-9f4a-5fd17d71af9e' }
}, commandInfo);
assert.notStrictEqual(actual, true);
});

it('enables communication site features with design package on the specified site', async () => {
sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === `https://contoso.sharepoint.com/_api/sitepages/communicationsite/enable`) {
return;
}

throw 'Invalid request';
});
await command.action(logger, { options: { designPackage: 'Topic', url: 'https://contoso.sharepoint.com' } } as any);
});

it('enables communication site features with design package ID on the specified site', async () => {
sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === `https://contoso.sharepoint.com/_api/sitepages/communicationsite/enable`) {
return;
}

throw 'Invalid request';
});
await command.action(logger, { options: { designPackageId: '96c933ac-3698-44c7-9f4a-5fd17d71af9e', url: 'https://contoso.sharepoint.com' } } as any);
});
});
34 changes: 30 additions & 4 deletions src/m365/spo/commands/site/site-commsite-enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ interface CommandArgs {

interface Options extends GlobalOptions {
designPackageId?: string;
designPackage?: string;
url: string;
}

enum DesignPackage {
Topic = '96c933ac-3698-44c7-9f4a-5fd17d71af9e',
Showcase = '6142d2a0-63a5-4ba0-aede-d9fefca2c767',
Blank = 'f6cc5403-0d63-442e-96c0-285923709ffc'
}

class SpoSiteCommSiteEnableCommand extends SpoCommand {
public get name(): string {
return commands.SITE_COMMSITE_ENABLE;
Expand Down Expand Up @@ -46,6 +53,9 @@ class SpoSiteCommSiteEnableCommand extends SpoCommand {
},
{
option: '-i, --designPackageId [designPackageId]'
},
{
option: '--designPackage [designPackage]'
}
);
}
Expand All @@ -54,20 +64,36 @@ class SpoSiteCommSiteEnableCommand extends SpoCommand {
this.validators.push(
async (args: CommandArgs) => {
if (args.options.designPackageId &&
!validation.isValidGuid(args.options.designPackageId)) {
return `${args.options.designPackageId} is not a valid GUID`;
!this.isValidDesignPackageId(args.options.designPackageId)) {
return `${args.options.designPackageId} is not a valid designPackageId. Allowed values are: ${Object.values(DesignPackage).join(', ')}.`;
}

if (args.options.designPackage) {
const designPackage = args.options.designPackage as keyof typeof DesignPackage;
if (!(designPackage in DesignPackage)) {
return `${designPackage} is not a valid designPackage. Allowed values are: ${Object.keys(DesignPackage).join(', ')}.`;
}
}

if (args.options.designPackageId && args.options.designPackage) {
return 'Specify designPackageId or designPackage but not both.';
}

return validation.isValidSharePointUrl(args.options.url);
}
);
}

private isValidDesignPackageId(designPackageId: string): boolean {
return Object.values(DesignPackage).includes(designPackageId as DesignPackage);
}

public async commandAction(logger: Logger, args: CommandArgs): Promise<void> {
const designPackageId: string = args.options.designPackageId || '96c933ac-3698-44c7-9f4a-5fd17d71af9e';
const designPackageId = args.options.designPackageId ?? DesignPackage[args.options.designPackage as keyof typeof DesignPackage] ?? DesignPackage.Topic;

if (this.verbose) {
logger.logToStderr(`Enabling communication site at ${args.options.url}...`);
const selectedDesignPackage = Object.keys(DesignPackage).find(key => DesignPackage[key as keyof typeof DesignPackage] === designPackageId);
logger.logToStderr(`Enabling communication site with design package '${selectedDesignPackage}' at '${args.options.url}'...`);
}

try {
Expand Down

0 comments on commit 76e2476

Please sign in to comment.