Skip to content

Commit

Permalink
Apply formatting to vendor/wp-now files (#547)
Browse files Browse the repository at this point in the history
* Use same Prettier configuration for `wp-now` files

* Apply formatting to all files under `vendor/wp-now`
  • Loading branch information
fluiddot authored Sep 18, 2024
1 parent 1928b54 commit 8e6816e
Show file tree
Hide file tree
Showing 37 changed files with 1,130 additions and 1,264 deletions.
3 changes: 0 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
src/translations

# wp-now
vendor/wp-now
3 changes: 0 additions & 3 deletions vendor/wp-now/.prettierignore

This file was deleted.

28 changes: 14 additions & 14 deletions vendor/wp-now/public/with-node-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fs from 'fs';
// Set the minimum required/supported version of node here.

// Check if `--blueprint=` is passed in proccess.argv
const hasBlueprint = process.argv.some((arg) => arg.startsWith('--blueprint='));
const hasBlueprint = process.argv.some( ( arg ) => arg.startsWith( '--blueprint=' ) );

const minimum = {
// `--blueprint=` requires node v20
Expand All @@ -17,38 +17,38 @@ const minimum = {

// Matches "v18.14.2", as an example.
const versionPattern = /^v(\d+)\.(\d+)\.(\d+)$/;
const [major, minor, patch] = versionPattern.exec(process.version).slice(1, 4);
const [ major, minor, patch ] = versionPattern.exec( process.version ).slice( 1, 4 );

function meetsMinimumVersion(minimum, [major, minor, patch]) {
if (major > minimum.major) {
function meetsMinimumVersion( minimum, [ major, minor, patch ] ) {
if ( major > minimum.major ) {
return true;
}

if (major < minimum.major) {
if ( major < minimum.major ) {
return false;
}

if (minor > minimum.minor) {
if ( minor > minimum.minor ) {
return true;
}

if (minor < minimum.minor) {
if ( minor < minimum.minor ) {
return false;
}

return patch >= minimum.patch;
}

if (!meetsMinimumVersion(minimum, [major, minor, patch])) {
if ( ! meetsMinimumVersion( minimum, [ major, minor, patch ] ) ) {
const extra = hasBlueprint ? ' when --blueprint=<file> is used' : '';
console.error(
`This script is requires node version v${minimum.major}.${minimum.minor}.${minimum.patch} or above${extra}; found ${process.version}`
`This script is requires node version v${ minimum.major }.${ minimum.minor }.${ minimum.patch } or above${ extra }; found ${ process.version }`
);
process.exit(1);
process.exit( 1 );
}

// Launch the wp-now process and pipe output through this wrappers streams.
const dir = path.dirname(fs.realpathSync(process.argv[1]));
child_process.spawn('node', [dir + '/cli.js', ...process.argv.slice(2)], {
stdio: ['inherit', 'inherit', 'inherit'],
});
const dir = path.dirname( fs.realpathSync( process.argv[ 1 ] ) );
child_process.spawn( 'node', [ dir + '/cli.js', ...process.argv.slice( 2 ) ], {
stdio: [ 'inherit', 'inherit', 'inherit' ],
} );
14 changes: 7 additions & 7 deletions vendor/wp-now/src/add-trailing-slash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* @param path - The path to add a trailing slash to. E.g. '/wp-admin'
* @returns - Returns a middleware function that may redirect adding a trailing slash to the given path. E.g. '/wp-admin/'
*/
export function addTrailingSlash(path) {
return (req, res, next) => {
const urlParts = req.url.split('?');
const url = urlParts[0];
const queryString = req.url.substr(url.length);
if (url === path) {
res.redirect(301, `${path}/${queryString}`);
export function addTrailingSlash( path ) {
return ( req, res, next ) => {
const urlParts = req.url.split( '?' );
const url = urlParts[ 0 ];
const queryString = req.url.substr( url.length );
if ( url === path ) {
res.redirect( 301, `${ path }/${ queryString }` );
} else {
next();
}
Expand Down
109 changes: 43 additions & 66 deletions vendor/wp-now/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
SupportedPHPVersion,
SupportedPHPVersionsList,
} from '@php-wasm/universal';
import { SupportedPHPVersion, SupportedPHPVersionsList } from '@php-wasm/universal';
import crypto from 'crypto';
import fs from 'fs-extra';
import path from 'path';
Expand Down Expand Up @@ -80,39 +77,32 @@ let absoluteUrlFromBlueprint = '';

async function getAbsoluteURL() {
const port = await portFinder.getOpenPort();
if (isGitHubCodespace) {
return getCodeSpaceURL(port);
if ( isGitHubCodespace ) {
return getCodeSpaceURL( port );
}

if (absoluteUrlFromBlueprint) {
if ( absoluteUrlFromBlueprint ) {
return absoluteUrlFromBlueprint;
}

const url = 'http://localhost';
if (port === 80) {
if ( port === 80 ) {
return url;
}
return `${url}:${port}`;
return `${ url }:${ port }`;
}

function getWpContentHomePath(projectPath: string, mode: string) {
const basename = path.basename(projectPath);
const directoryHash = crypto
.createHash('sha1')
.update(projectPath)
.digest('hex');
function getWpContentHomePath( projectPath: string, mode: string ) {
const basename = path.basename( projectPath );
const directoryHash = crypto.createHash( 'sha1' ).update( projectPath ).digest( 'hex' );
const projectDirectory =
mode === WPNowMode.PLAYGROUND
? 'playground'
: `${basename}-${directoryHash}`;
return path.join(getWpNowPath(), 'wp-content', projectDirectory);
mode === WPNowMode.PLAYGROUND ? 'playground' : `${ basename }-${ directoryHash }`;
return path.join( getWpNowPath(), 'wp-content', projectDirectory );
}

export default async function getWpNowConfig(
args: CliOptions
): Promise<WPNowOptions> {
if (args.port) {
portFinder.setPort(args.port);
export default async function getWpNowConfig( args: CliOptions ): Promise< WPNowOptions > {
if ( args.port ) {
portFinder.setPort( args.port );
}
const port = await portFinder.getOpenPort();
const optionsFromCli: WPNowOptions = {
Expand All @@ -125,81 +115,68 @@ export default async function getWpNowConfig(

const options: WPNowOptions = {} as WPNowOptions;

[optionsFromCli, DEFAULT_OPTIONS].forEach((config) => {
for (const key in config) {
if (!options[key]) {
options[key] = config[key];
[ optionsFromCli, DEFAULT_OPTIONS ].forEach( ( config ) => {
for ( const key in config ) {
if ( ! options[ key ] ) {
options[ key ] = config[ key ];
}
}
});
} );

if (!options.mode || options.mode === 'auto') {
options.mode = inferMode(options.projectPath);
if ( ! options.mode || options.mode === 'auto' ) {
options.mode = inferMode( options.projectPath );
}
if (!options.wpContentPath) {
options.wpContentPath = getWpContentHomePath(
options.projectPath,
options.mode
);
if ( ! options.wpContentPath ) {
options.wpContentPath = getWpContentHomePath( options.projectPath, options.mode );
}
if (!options.absoluteUrl) {
if ( ! options.absoluteUrl ) {
options.absoluteUrl = await getAbsoluteURL();
}
if (!isValidWordPressVersion(options.wordPressVersion)) {
if ( ! isValidWordPressVersion( options.wordPressVersion ) ) {
throw new Error(
'Unrecognized WordPress version. Please use "latest" or numeric versions such as "6.2", "6.0.1", "6.2-beta1", or "6.2-RC1"'
);
}
if (
options.phpVersion &&
!SupportedPHPVersionsList.includes(options.phpVersion)
) {
if ( options.phpVersion && ! SupportedPHPVersionsList.includes( options.phpVersion ) ) {
throw new Error(
`Unsupported PHP version: ${
options.phpVersion
}. Supported versions: ${SupportedPHPVersionsList.join(', ')}`
}. Supported versions: ${ SupportedPHPVersionsList.join( ', ' ) }`
);
}
if (args.blueprint) {
const blueprintPath = path.resolve(args.blueprint);
if (!fs.existsSync(blueprintPath)) {
throw new Error(`Blueprint file not found: ${blueprintPath}`);
if ( args.blueprint ) {
const blueprintPath = path.resolve( args.blueprint );
if ( ! fs.existsSync( blueprintPath ) ) {
throw new Error( `Blueprint file not found: ${ blueprintPath }` );
}
const blueprintObject = JSON.parse(
fs.readFileSync(blueprintPath, 'utf8')
);
const blueprintObject = JSON.parse( fs.readFileSync( blueprintPath, 'utf8' ) );

options.blueprintObject = blueprintObject;
const siteUrl = extractSiteUrlFromBlueprint(blueprintObject);
if (siteUrl) {
const siteUrl = extractSiteUrlFromBlueprint( blueprintObject );
if ( siteUrl ) {
options.absoluteUrl = siteUrl;
absoluteUrlFromBlueprint = siteUrl;
}
}
if (args.adminPassword) {
if ( args.adminPassword ) {
options.adminPassword = args.adminPassword;
}
if (args.siteTitle) {
if ( args.siteTitle ) {
options.siteTitle = args.siteTitle;
}
return options;
}

function extractSiteUrlFromBlueprint(
blueprintObject: Blueprint
): string | false {
for (const step of blueprintObject.steps) {
if (typeof step !== 'object') {
function extractSiteUrlFromBlueprint( blueprintObject: Blueprint ): string | false {
for ( const step of blueprintObject.steps ) {
if ( typeof step !== 'object' ) {
return false;
}

if (step.step === 'defineSiteUrl') {
return `${step.siteUrl}`;
} else if (
step.step === 'defineWpConfigConsts' &&
step.consts.WP_SITEURL
) {
return `${step.consts.WP_SITEURL}`;
if ( step.step === 'defineSiteUrl' ) {
return `${ step.siteUrl }`;
} else if ( step.step === 'defineWpConfigConsts' && step.consts.WP_SITEURL ) {
return `${ step.consts.WP_SITEURL }`;
}
}
return false;
Expand Down
3 changes: 1 addition & 2 deletions vendor/wp-now/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ export const SQLITE_FILENAME = 'sqlite-database-integration';
/**
* The URL for downloading the "SQLite database integration" WordPress Plugin.
*/
export const SQLITE_URL =
'https://downloads.wordpress.org/plugin/sqlite-database-integration.zip';
export const SQLITE_URL = 'https://downloads.wordpress.org/plugin/sqlite-database-integration.zip';

/**
* The default starting port for running the WP Now server.
Expand Down
Loading

0 comments on commit 8e6816e

Please sign in to comment.