Skip to content

Commit

Permalink
wp now: add codespaces support (#41)
Browse files Browse the repository at this point in the history
Allow running `wp-now` in GitHub codespaces
  • Loading branch information
sejas authored Jun 1, 2023
1 parent fa50062 commit f2f21da
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
9 changes: 8 additions & 1 deletion packages/wp-now/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
SupportedPHPVersionsList,
} from '@php-wasm/universal';
import crypto from 'crypto';
import { getCodeSpaceURL, isGitHubCodespace } from './github-codespaces';
import { inferMode } from './wp-now';
import { portFinder } from './port-finder';
import { isValidWordPressVersion } from './wp-playground-wordpress';
Expand Down Expand Up @@ -64,6 +65,9 @@ export interface WPEnvOptions {

async function getAbsoluteURL() {
const port = await portFinder.getOpenPort();
if (isGitHubCodespace) {
return getCodeSpaceURL(port);
}
return `http://localhost:${port}`;
}

Expand All @@ -83,7 +87,10 @@ function getWpContentHomePath(projectPath: string, mode: string) {
export default async function getWpNowConfig(
args: CliOptions
): Promise<WPNowOptions> {
const port = args.port || (await portFinder.getOpenPort());
if (args.port) {
portFinder.setPort(args.port);
}
const port = await portFinder.getOpenPort();
const optionsFromCli: WPNowOptions = {
phpVersion: args.php as SupportedPHPVersion,
projectPath: args.path as string,
Expand Down
15 changes: 15 additions & 0 deletions packages/wp-now/src/github-codespaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* True if the current environment is a GitHub Codespace
*/
export const isGitHubCodespace = Boolean(
process.env.CODESPACE_NAME &&
process.env.GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN
);

/*
* Returns the URL in the current GitHub Codespace
* e.g: https://sejas-fluffy-space-eureka-wrj7r95qhvq4x-8881.preview.app.github.dev
*/
export function getCodeSpaceURL(port: number): string {
return `https://${process.env.CODESPACE_NAME}-${port}.${process.env.GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}`;
}
8 changes: 7 additions & 1 deletion packages/wp-now/src/run-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import getWpNowConfig, { CliOptions } from './config';
import { spawn, SpawnOptionsWithoutStdio } from 'child_process';
import { executePHP } from './execute-php';
import { output } from './output';
import { isGitHubCodespace } from './github-codespaces';

function startSpinner(message: string) {
process.stdout.write(`${message}...\n`);
Expand Down Expand Up @@ -123,6 +124,9 @@ export async function runCli() {
}

function openInDefaultBrowser(url: string) {
if (isGitHubCodespace) {
return;
}
let cmd: string, args: string[] | SpawnOptionsWithoutStdio;
switch (process.platform) {
case 'darwin':
Expand All @@ -141,5 +145,7 @@ function openInDefaultBrowser(url: string) {
output?.log(`Platform '${process.platform}' not supported`);
return;
}
spawn(cmd, args);
spawn(cmd, args).on('error', function (err) {
console.error(err.message);
});
}
3 changes: 1 addition & 2 deletions packages/wp-now/src/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ export async function startServer(
output?.trace(e);
}
});

const url = `http://localhost:${port}/`;
const url = options.absoluteUrl;
app.listen(port, () => {
output?.log(`Server running at ${url}`);
});
Expand Down
35 changes: 35 additions & 0 deletions packages/wp-now/src/tests/github-codespaces.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import getWpNowConfig from '../config';
import * as codespaces from '../github-codespaces';

describe('Test GitHub Codespaces', () => {
let processEnv;
beforeAll(() => {
processEnv = process.env;
process.env = {};
});

afterAll(() => {
process.env = processEnv;
});

test('getAbsoluteURL returns the localhost URL', async () => {
vi.spyOn(codespaces, 'isGitHubCodespace', 'get').mockReturnValue(false);
const options = await getWpNowConfig({ port: 7777 });

expect(options.absoluteUrl).toBe('http://localhost:7777');
vi.resetAllMocks();
});

test('getAbsoluteURL returns the codespace URL', async () => {
vi.spyOn(codespaces, 'isGitHubCodespace', 'get').mockReturnValue(true);
process.env.CODESPACE_NAME = 'my-codespace-name';
process.env.GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN =
'preview.app.github.dev';
const options = await getWpNowConfig({ port: 7777 });

expect(options.absoluteUrl).toBe(
'https://my-codespace-name-7777.preview.app.github.dev'
);
vi.resetAllMocks();
});
});

0 comments on commit f2f21da

Please sign in to comment.