Skip to content

Commit

Permalink
Studio: Download the latest wp-cli and bundle it (#178)
Browse files Browse the repository at this point in the history
* Studio: Download the latest wp-cli and bundle it

* Studio: Remove copy and update functions
  • Loading branch information
kozer authored May 29, 2024
1 parent ac6e72a commit 547c532
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
15 changes: 13 additions & 2 deletions scripts/download-wp-server-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ const FILES_TO_DOWNLOAD = [
description: 'SQLite files',
url: 'https://downloads.wordpress.org/plugin/sqlite-database-integration.zip',
},
{
name: 'wp-cli',
description: 'WP-CLI phar file',
url: 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar',
destinationPath: path.join( WP_SERVER_FILES_PATH, 'wp-cli' ),
},
];

const downloadFile = async ( {
Expand Down Expand Up @@ -75,8 +81,13 @@ const downloadFile = async ( {
} );
} );

console.log( `[${ name }] Extracting files from zip ...` );
await extract( zipPath, { dir: extractedPath } );
if ( name === 'wp-cli' ) {
console.log( `[${ name }] Moving WP-CLI to destination ...` );
fs.moveSync( zipPath, path.join( extractedPath, 'wp-cli.phar' ), { overwrite: true } );
} else {
console.log( `[${ name }] Extracting files from zip ...` );
await extract( zipPath, { dir: extractedPath } );
}
console.log( `[${ name }] Files extracted` );
};

Expand Down
3 changes: 2 additions & 1 deletion src/setup-wp-server-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import path from 'path';
import fs from 'fs-extra';
import semver from 'semver';
import { SQLITE_FILENAME } from '../vendor/wp-now/src/constants';
import { getWordPressVersionPath } from '../vendor/wp-now/src/download';
import { downloadWPCLI, getWordPressVersionPath } from '../vendor/wp-now/src/download';
import getSqlitePath from '../vendor/wp-now/src/get-sqlite-path';
import getWpCliPath from '../vendor/wp-now/src/get-wp-cli-path';
import { recursiveCopyDirectory } from './lib/fs-utils';
import { updateLatestSqliteVersion } from './lib/sqlite-versions';
import {
Expand Down
6 changes: 4 additions & 2 deletions vendor/wp-now/src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ async function downloadFile({
url,
destinationFilePath,
itemName,
overwrite = false,
}): Promise<DownloadFileAndUnzipResult> {
let statusCode = 0;
try {
if (fs.existsSync(destinationFilePath)) {
if (fs.existsSync(destinationFilePath) && !overwrite ) {
return { downloaded: false, statusCode: 0 };
}
fs.ensureDirSync(path.dirname(destinationFilePath));
Expand Down Expand Up @@ -90,11 +91,12 @@ async function downloadFile({
}
}

export async function downloadWPCLI() {
export async function downloadWPCLI( overwrite = false ) {
return downloadFile({
url: WP_CLI_URL,
destinationFilePath: getWpCliPath(),
itemName: 'wp-cli',
overwrite,
});
}

Expand Down
4 changes: 2 additions & 2 deletions vendor/wp-now/src/get-wp-cli-path.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import path from 'path';
import getWpNowPath from './get-wp-now-path';
import getWpCliTmpPath from './get-wp-cli-tmp-path';
import { getResourcesPath } from '../../../src/storage/paths';

/**
* The path for wp-cli phar file within the WP Now folder.
*/
export default function getWpCliPath() {
if (process.env.NODE_ENV !== 'test') {
return path.join(getWpNowPath(), 'wp-cli.phar');
return path.join( getResourcesPath(), 'wp-files', 'wp-cli', 'wp-cli.phar' );
}
return path.join(getWpCliTmpPath(), 'wp-cli.phar');
}

0 comments on commit 547c532

Please sign in to comment.