Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wp-now: Mount SQLite database directory from wp-content stored in .wp-now #320

Merged
merged 4 commits into from
May 12, 2023
Merged
Changes from all 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
36 changes: 23 additions & 13 deletions packages/wp-now/src/wp-now.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ async function runWpContentMode(

php.mount(projectPath, `${documentRoot}/wp-content`);

mountSqlitePlugin(php, documentRoot);
mountSqliteDatabaseDirectory(php, documentRoot, wpContentPath);
mountMuPlugins(php, documentRoot);
mountSqlite(php, documentRoot);
}

async function runWordPressDevelopMode(
Expand All @@ -214,14 +215,15 @@ async function runWordPressDevelopMode(

async function runWordPressMode(
php: NodePHP,
{ documentRoot, projectPath, absoluteUrl }: WPNowOptions
{ documentRoot, wpContentPath, projectPath, absoluteUrl }: WPNowOptions
) {
php.mount(projectPath, documentRoot);
if (!php.fileExists(`${documentRoot}/wp-config.php`)) {
await initWordPress(php, 'user-provided', documentRoot, absoluteUrl);
}
mountSqlitePlugin(php, documentRoot);
mountSqliteDatabaseDirectory(php, documentRoot, wpContentPath);
mountMuPlugins(php, documentRoot);
copySqlite(projectPath);
}

async function runPluginOrThemeMode(
Expand Down Expand Up @@ -252,8 +254,8 @@ async function runPluginOrThemeMode(
projectPath,
`${documentRoot}/wp-content/${directoryName}/${pluginName}`
);
mountSqlitePlugin(php, documentRoot);
mountMuPlugins(php, documentRoot);
mountSqlite(php, documentRoot);
}

async function initWordPress(
Expand Down Expand Up @@ -283,7 +285,7 @@ function mountMuPlugins(php: NodePHP, vfsDocumentRoot: string) {
);
}

function mountSqlite(php: NodePHP, vfsDocumentRoot: string) {
function mountSqlitePlugin(php: NodePHP, vfsDocumentRoot: string) {
const sqlitePluginPath = `${vfsDocumentRoot}/wp-content/plugins/${SQLITE_FILENAME}`;
if (php.listFiles(sqlitePluginPath).length === 0) {
php.mount(SQLITE_PATH, sqlitePluginPath);
Expand All @@ -294,14 +296,22 @@ function mountSqlite(php: NodePHP, vfsDocumentRoot: string) {
}
}

function copySqlite(localWordPressPath: string) {
const targetPath = `${localWordPressPath}/wp-content/plugins/${SQLITE_FILENAME}`;
if (!fs.existsSync(targetPath)) {
fs.copySync(SQLITE_PATH, targetPath);
}
fs.copySync(
`${SQLITE_PATH}/db.copy`,
`${localWordPressPath}/wp-content/db.php`
/**
* Create SQLite database directory in hidden utility directory and mount it to the document root
*
* @param php
* @param vfsDocumentRoot
* @param wpContentPath
*/
function mountSqliteDatabaseDirectory(
php: NodePHP,
vfsDocumentRoot: string,
wpContentPath: string
) {
fs.ensureDirSync(path.join(wpContentPath, 'database'));
php.mount(
path.join(wpContentPath, 'database'),
path.join(vfsDocumentRoot, 'wp-content', 'database')
);
}

Expand Down