Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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 {
getSiteByFolder,
lockAppdata,
readAppdata,
saveAppdata,
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 {
logger.reportStart( LoggerAction.LOAD_SITES, __( 'Loading site…' ) );
const site = await getSiteByFolder( sitePath, false );
logger.reportSuccess( __( 'Site loaded' ) );

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

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

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need both getSiteByFolder and this validation, I understand we want to search the site in appdata to make the changes, so we could remove getSiteByFolder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call 👍

const oldDomainName = site.customDomain;
site.customDomain = domainName;
await saveAppdata( appdata );

if ( oldDomainName ) {
logger.reportStart(
LoggerAction.REMOVE_DOMAIN_FROM_HOSTS,
__( 'Removing domain from hosts file...' )
);
await removeDomainFromHosts( oldDomainName );
logger.reportSuccess( __( 'Domain removed from hosts file' ) );
}
} 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 ) {
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 );
},
} );
};
92 changes: 92 additions & 0 deletions cli/commands/site/set-https.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
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,
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 {
logger.reportStart( LoggerAction.LOAD_SITES, __( 'Loading site…' ) );
const site = await getSiteByFolder( sitePath, false );
logger.reportSuccess( __( 'Site loaded' ) );

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.' ) );
}
}

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

try {
await lockAppdata();
const appdata = await readAppdata();
const site = appdata.sites.find( ( site ) => arePathsEqual( site.path, sitePath ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as my comment on set-domain

if ( ! site ) {
throw new LoggerError( __( 'The specified folder is not added to Studio.' ) );
}
site.enableHttps = enableHttps;
await saveAppdata( appdata );
} finally {
await unlockAppdata();
}

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