Skip to content
Prev Previous commit
Next Next commit
feat: Starting the app updates sqlite-database-integration installation
Ensure new sites receive the latest sqlite-database-integration fixes
and improvements.
  • Loading branch information
dcalhoun committed May 16, 2024
commit 3b203847094de9d7b0008561d6c03e2c0824aa25
52 changes: 52 additions & 0 deletions src/lib/sqlite-versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import path from 'path';
import fs from 'fs-extra';
import semver from 'semver';
import { downloadSqliteIntegrationPlugin } from '../../vendor/wp-now/src/download';
import getSqlitePath from '../../vendor/wp-now/src/get-sqlite-path';

export async function updateLatestSqliteVersion() {
let shouldOverwrite = false;
const installedPath = getSqlitePath();
const installedFiles = ( await fs.pathExists( installedPath ) )
? await fs.readdir( installedPath )
: [];
const latestVersion = await getLatestSqliteVersion();
if ( installedFiles.length !== 0 ) {
const installedVersion = getSqliteVersionFromInstallation( installedPath );
shouldOverwrite = !! installedVersion && !! latestVersion && installedVersion !== latestVersion;
}

await downloadSqliteIntegrationPlugin( latestVersion, { overwrite: shouldOverwrite } );
}

function getSqliteVersionFromInstallation( installationPath: string ): string {
let versionFileContent = '';
try {
versionFileContent = fs.readFileSync( path.join( installationPath, 'load.php' ), 'utf8' );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In general, we try to avoid using the sync version of FS functions to prevent blocking the main thread. I wonder if we could use the asynchronous one.

} catch ( err ) {
return '';
}
const matches = versionFileContent.match( /\s\*\sVersion:\s*([0-9a-zA-Z.-]+)/ );
return matches?.[ 1 ] || '';
}

async function getLatestSqliteVersion() {
const sqliteVersions = await fetchSqliteVersions();
return sqliteVersions.latest;
}

async function fetchSqliteVersions() {
try {
const response = await fetch(
'https://api.github.com/repos/WordPress/sqlite-database-integration/releases'
Comment thread
dcalhoun marked this conversation as resolved.
Outdated
);
const data: Record< string, string >[] = await response.json();
const versions = data.map( ( release ) => semver.coerce( release.tag_name ) );
return {
versions,
latest: versions[ 0 ]?.version || '',
};
} catch ( _error ) {
return { versions: [], latest: '' };
}
}
2 changes: 2 additions & 0 deletions src/setup-wp-server-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SQLITE_FILENAME } from '../vendor/wp-now/src/constants';
import { getWordPressVersionPath } from '../vendor/wp-now/src/download';
import getSqlitePath from '../vendor/wp-now/src/get-sqlite-path';
import { recursiveCopyDirectory } from './lib/fs-utils';
import { updateLatestSqliteVersion } from './lib/sqlite-versions';
import {
getWordPressVersionFromInstallation,
updateLatestWordPressVersion,
Expand Down Expand Up @@ -51,4 +52,5 @@ export default async function setupWPServerFiles() {
await copyBundledLatestWPVersion();
await copyBundledSqlite();
Comment thread
dcalhoun marked this conversation as resolved.
await updateLatestWordPressVersion();
await updateLatestSqliteVersion();
}
18 changes: 13 additions & 5 deletions vendor/wp-now/src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,26 @@ export async function downloadWordPress(
}
}

export async function downloadSqliteIntegrationPlugin() {
export async function downloadSqliteIntegrationPlugin(
version = '',
{overwrite}: {overwrite: boolean} = {overwrite: false}
) {
const finalFolder = getSqlitePath();
const tempFolder = path.join(os.tmpdir(), SQLITE_FILENAME);
const url = !! version
? `https://codeload.github.com/WordPress/sqlite-database-integration/zip/refs/tags/v${ version }`
: SQLITE_URL;
const { downloaded, statusCode } = await downloadFileAndUnzip({
url: SQLITE_URL,
url,
destinationFolder: tempFolder,
checkFinalPath: finalFolder,
itemName: 'SQLite',
overwrite,
});
if (downloaded) {
fs.ensureDirSync(path.dirname(finalFolder));
fs.moveSync(tempFolder, finalFolder, {
const nestedFolder = SQLITE_FILENAME.replace(/main$/, version);
Comment thread
dcalhoun marked this conversation as resolved.
Outdated
await fs.ensureDir(path.dirname(finalFolder));
await fs.move(path.join(tempFolder, nestedFolder), finalFolder, {
overwrite: true,
});
} else if(0 !== statusCode) {
Expand Down Expand Up @@ -318,4 +326,4 @@ set_error_handler(function($severity, $message, $file, $line) {

export function getWordPressVersionPath(wpVersion: string) {
return path.join(getWordpressVersionsPath(), wpVersion);
}
}