1+ import { z } from 'zod' ;
2+ import { zod } from '../../../../utils/zod.js' ;
3+ import { globalOptionsZod } from '../../../../Command.js' ;
4+ import { validation } from '../../../../utils/validation.js' ;
15import { cli } from '../../../../cli/cli.js' ;
26import { Logger } from '../../../../cli/Logger.js' ;
37import config from '../../../../config.js' ;
4- import GlobalOptions from '../../../../GlobalOptions.js' ;
58import request , { CliRequestOptions } from '../../../../request.js' ;
69import { ClientSvcResponse , ClientSvcResponseContents , spo } from '../../../../utils/spo.js' ;
710import SpoCommand from '../../../base/SpoCommand.js' ;
811import commands from '../../commands.js' ;
912
13+ const options = globalOptionsZod
14+ . extend ( {
15+ url : zod . alias ( 'u' , z . string ( )
16+ . refine ( url => validation . isValidSharePointUrl ( url ) === true , url => ( {
17+ message : `'${ url } ' is not a valid SharePoint Online site URL.`
18+ } ) ) . optional ( )
19+ ) ,
20+ force : zod . alias ( 'f' , z . boolean ( ) . optional ( ) )
21+ } )
22+ . strict ( ) ;
23+
24+ declare type Options = z . infer < typeof options > ;
1025interface CommandArgs {
1126 options : Options ;
1227}
1328
14- interface Options extends GlobalOptions {
15- force ?: boolean ;
16- }
17-
1829class SpoHomeSiteRemoveCommand extends SpoCommand {
1930 public get name ( ) : string {
2031 return commands . HOMESITE_REMOVE ;
2132 }
2233
2334 public get description ( ) : string {
24- return 'Removes the current Home Site' ;
25- }
26-
27- constructor ( ) {
28- super ( ) ;
29-
30- this . #initTelemetry( ) ;
31- this . #initOptions( ) ;
35+ return 'Removes a Home Site' ;
3236 }
3337
34- #initTelemetry( ) : void {
35- this . telemetry . push ( ( args : CommandArgs ) => {
36- Object . assign ( this . telemetryProperties , {
37- force : args . options . force || false
38- } ) ;
39- } ) ;
40- }
41-
42- #initOptions( ) : void {
43- this . options . unshift (
44- {
45- option : '-f, --force'
46- }
47- ) ;
38+ public get schema ( ) : z . ZodTypeAny {
39+ return options ;
4840 }
4941
5042 public async commandAction ( logger : Logger , args : CommandArgs ) : Promise < void > {
5143
5244 const removeHomeSite : ( ) => Promise < void > = async ( ) : Promise < void > => {
5345 try {
46+ if ( this . verbose ) {
47+ await logger . logToStderr ( `Removing ${ args . options . url ? `'${ args . options . url } ' as home site` : 'the current home site' } ...` ) ;
48+ }
49+
5450 const spoAdminUrl = await spo . getSpoAdminUrl ( logger , this . debug ) ;
5551 const reqDigest = await spo . getRequestDigest ( spoAdminUrl ) ;
5652
57- const requestOptions : CliRequestOptions = {
58- url : `${ spoAdminUrl } /_vti_bin/client.svc/ProcessQuery` ,
59- headers : {
60- 'X-RequestDigest' : reqDigest . FormDigestValue
61- } ,
62- data : `<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="${ config . applicationName } " xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009"><Actions><ObjectPath Id="28" ObjectPathId="27" /><Method Name="RemoveSPHSite" Id="29" ObjectPathId="27" /></Actions><ObjectPaths><Constructor Id="27" TypeId="{268004ae-ef6b-4e9b-8425-127220d84719}" /></ObjectPaths></Request>`
63- } ;
64-
65- const res = await request . post < string > ( requestOptions ) ;
66-
67- const json : ClientSvcResponse = JSON . parse ( res ) ;
68- const response : ClientSvcResponseContents = json [ 0 ] ;
69- if ( response . ErrorInfo ) {
70- throw response . ErrorInfo . ErrorMessage ;
53+ if ( args . options . url ) {
54+ await this . removeHomeSiteByUrl ( args . options . url , spoAdminUrl , logger ) ;
55+ await logger . log ( `${ args . options . url } has been removed as a Home Site. It may take some time for the change to apply. Check aka.ms/homesites for details.` ) ;
7156 }
7257 else {
73- await logger . log ( json [ json . length - 1 ] ) ;
58+ await this . warn ( logger , `The current way this command works is deprecated and will change in the next major release. The '--url' option will become required.` ) ;
59+
60+ const requestOptions : CliRequestOptions = {
61+ url : `${ spoAdminUrl } /_vti_bin/client.svc/ProcessQuery` ,
62+ headers : {
63+ 'X-RequestDigest' : reqDigest . FormDigestValue
64+ } ,
65+ data : `<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="${ config . applicationName } " xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009"><Actions><ObjectPath Id="28" ObjectPathId="27" /><Method Name="RemoveSPHSite" Id="29" ObjectPathId="27" /></Actions><ObjectPaths><Constructor Id="27" TypeId="{268004ae-ef6b-4e9b-8425-127220d84719}" /></ObjectPaths></Request>`
66+ } ;
67+
68+ const res = await request . post < string > ( requestOptions ) ;
69+
70+ const json : ClientSvcResponse = JSON . parse ( res ) ;
71+ const response : ClientSvcResponseContents = json [ 0 ] ;
72+ if ( response . ErrorInfo ) {
73+ throw response . ErrorInfo . ErrorMessage ;
74+ }
75+ else {
76+ await logger . log ( json [ json . length - 1 ] ) ;
77+ }
7478 }
7579 }
7680 catch ( err : any ) {
7781 this . handleRejectedODataJsonPromise ( err ) ;
7882 }
7983 } ;
8084
81-
8285 if ( args . options . force ) {
8386 await removeHomeSite ( ) ;
8487 }
8588 else {
86- const result = await cli . promptForConfirmation ( { message : `Are you sure you want to remove the Home Site?` } ) ;
89+ const result = await cli . promptForConfirmation ( {
90+ message : args . options . url
91+ ? `Are you sure you want to remove '${ args . options . url } ' as home site?`
92+ : `Are you sure you want to remove the current home site?`
93+ } ) ;
8794
8895 if ( result ) {
8996 await removeHomeSite ( ) ;
9097 }
9198 }
9299 }
100+
101+ private async removeHomeSiteByUrl ( siteUrl : string , spoAdminUrl : string , logger : Logger ) : Promise < void > {
102+ const siteAdminProperties = await spo . getSiteAdminPropertiesByUrl ( siteUrl , false , logger , this . verbose ) ;
103+
104+ const requestOptions : CliRequestOptions = {
105+ url : `${ spoAdminUrl } /_api/SPO.Tenant/RemoveTargetedSite` ,
106+ headers : {
107+ accept : 'application/json;odata=nometadata'
108+ } ,
109+ data : {
110+ siteId : siteAdminProperties . SiteId
111+ }
112+ } ;
113+
114+ await request . post ( requestOptions ) ;
115+ }
93116}
94117
95118export default new SpoHomeSiteRemoveCommand ( ) ;
0 commit comments