Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions cli/commands/site/set-domain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { __, _n } from '@wordpress/i18n';
import { getDomainNameValidationError } from 'common/lib/domains';
import { arePathsEqual } from 'common/lib/fs-utils';
import { SiteCommandLoggerAction as LoggerAction } from 'common/logger-actions';
import { lockAppdata, readAppdata, saveAppdata, SiteData, unlockAppdata } from 'cli/lib/appdata';
import { removeDomainFromHosts } from 'cli/lib/hosts-file';
import { connect, disconnect } from 'cli/lib/pm2-manager';
import { setupCustomDomain } from 'cli/lib/site-utils';
import {
isServerRunning,
startWordPressServer,
stopWordPressServer,
} from 'cli/lib/wordpress-server-manager';
import { Logger, LoggerError } from 'cli/logger';
import { StudioArgv } from 'cli/types';

export async function runCommand( sitePath: string, domainName: string ): Promise< void > {
const logger = new Logger< LoggerAction >();

try {
let site: SiteData;
let oldDomainName: string | undefined;

try {
await lockAppdata();
const appdata = await readAppdata();

const foundSite = appdata.sites.find( ( site ) => arePathsEqual( site.path, sitePath ) );
if ( ! foundSite ) {
throw new LoggerError( __( 'The specified folder is not added to Studio.' ) );
}

site = foundSite;
const existingDomainNames = appdata.sites
.map( ( site ) => site.customDomain )
.filter( ( domain ): domain is string => Boolean( domain ) );
const domainError = getDomainNameValidationError( true, domainName, existingDomainNames );

if ( domainError ) {
throw new LoggerError( domainError );
}

oldDomainName = site.customDomain;

if ( oldDomainName === domainName ) {
throw new LoggerError( __( 'The specified domain is already set for this site.' ) );
}

site.customDomain = domainName;
await saveAppdata( appdata );
} finally {
await unlockAppdata();
}

if ( oldDomainName ) {
logger.reportStart(
LoggerAction.REMOVE_DOMAIN_FROM_HOSTS,
__( 'Removing domain from hosts file...' )
);
await removeDomainFromHosts( oldDomainName );
logger.reportSuccess( __( 'Domain removed from hosts file' ) );
}

logger.reportStart( LoggerAction.START_DAEMON, __( 'Starting process daemon...' ) );
await connect();
logger.reportSuccess( __( 'Process daemon started' ) );

const runningProcess = await isServerRunning( site.id );

if ( runningProcess ) {
await stopWordPressServer( site.id );
await setupCustomDomain( site, logger );
logger.reportStart( LoggerAction.START_SITE, __( 'Restarting site...' ) );
await startWordPressServer( site );
logger.reportSuccess( __( 'Site restarted' ) );
}
} catch ( error ) {
if ( error instanceof LoggerError ) {
logger.reportError( error );
} else {
const loggerError = new LoggerError( __( 'Failed to start site infrastructure' ), error );
logger.reportError( loggerError );
}
} finally {
disconnect();
}
}

export const registerCommand = ( yargs: StudioArgv ) => {
return yargs.command( {
command: 'set-domain <domain>',
describe: __( 'Set domain for a local site' ),
builder: ( yargs ) => {
return yargs.positional( 'domain', {
type: 'string',
description: __( 'Domain name' ),
demandOption: true,
} );
},
handler: async ( argv ) => {
await runCommand( argv.path, argv.domain );
},
} );
};
95 changes: 95 additions & 0 deletions cli/commands/site/set-https.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { __, _n } from '@wordpress/i18n';
import { arePathsEqual } from 'common/lib/fs-utils';
import { SiteCommandLoggerAction as LoggerAction } from 'common/logger-actions';
import {
getSiteByFolder,
lockAppdata,
readAppdata,
saveAppdata,
SiteData,
unlockAppdata,
} from 'cli/lib/appdata';
import { connect, disconnect } from 'cli/lib/pm2-manager';
import {
isServerRunning,
startWordPressServer,
stopWordPressServer,
} from 'cli/lib/wordpress-server-manager';
import { Logger, LoggerError } from 'cli/logger';
import { StudioArgv } from 'cli/types';

export async function runCommand( sitePath: string, enableHttps: boolean ): Promise< void > {
const logger = new Logger< LoggerAction >();

try {
let site: SiteData;

try {
await lockAppdata();
const appdata = await readAppdata();

const foundSite = appdata.sites.find( ( site ) => arePathsEqual( site.path, sitePath ) );
if ( ! foundSite ) {
throw new LoggerError( __( 'The specified folder is not added to Studio.' ) );
}

site = foundSite;

if ( ! site.customDomain ) {
throw new LoggerError( __( 'Site does not have a custom domain.' ) );
}

if ( site.enableHttps === enableHttps ) {
if ( enableHttps ) {
throw new LoggerError( __( 'HTTPS is already enabled for this site.' ) );
} else {
throw new LoggerError( __( 'HTTPS is already disabled for this site.' ) );
}
}

site.enableHttps = enableHttps;
await saveAppdata( appdata );
} finally {
await unlockAppdata();
}

logger.reportStart( LoggerAction.START_DAEMON, __( 'Starting process daemon...' ) );
await connect();
logger.reportSuccess( __( 'Process daemon started' ) );

const runningProcess = await isServerRunning( site.id );

if ( runningProcess ) {
logger.reportStart( LoggerAction.START_SITE, __( 'Restarting site...' ) );
await stopWordPressServer( site.id );
await startWordPressServer( site );
logger.reportSuccess( __( 'Site restarted' ) );
}
} catch ( error ) {
if ( error instanceof LoggerError ) {
logger.reportError( error );
} else {
const loggerError = new LoggerError( __( 'Failed to start site infrastructure' ), error );
logger.reportError( loggerError );
}
} finally {
disconnect();
}
}

export const registerCommand = ( yargs: StudioArgv ) => {
return yargs.command( {
command: 'set-https <enable>',
describe: __( 'Set HTTPS for a local site' ),
builder: ( yargs ) => {
return yargs.positional( 'enable', {
type: 'boolean',
description: __( 'Enable HTTPS' ),
demandOption: true,
} );
},
handler: async ( argv ) => {
await runCommand( argv.path, argv.enable );
},
} );
};
Loading