Files
aurora-dockside/packages/aurora-module-wordpress/main/index.cjs
T

155 lines
5.7 KiB
JavaScript

'use strict'
function safeName(name) {
return (
name
.toLowerCase()
.replace(/[^a-z0-9_-]+/g, '-')
.replace(/^-+|-+$/g, '') || 'project'
)
}
function wpArgs(context, network) {
const uid = typeof process.getuid === 'function' ? process.getuid() : undefined
const gid = typeof process.getgid === 'function' ? process.getgid() : undefined
return [
'run',
'--rm',
...(uid === undefined ? [] : ['--user', `${uid}:${gid}`]),
'-e',
'HOME=/tmp',
'-e',
'WP_CLI_CACHE_DIR=/tmp/wp-cli-cache',
...(network ? ['--network', `aurora-${safeName(context.projectName)}_default`] : []),
'-v',
`${context.directory}:/app`,
'-w',
'/app',
'--entrypoint',
'php',
'wordpress:cli',
'-d',
'memory_limit=512M',
'/usr/local/bin/wp'
]
}
exports.projectCreate = async function projectCreate(context) {
const s = context.settings
const title = String(s.title || '').trim() || context.projectName
const native = context.environment.runtimeEngine === 'native'
const nativePhpPrefix = native ? context.native.phpPrefixArgs || [] : []
const nativePrefix = native ? context.native.wpPrefixArgs || [] : []
const base = native ? [...nativePrefix, `--path=${context.directory}`] : wpArgs(context, false)
const networkBase = native ? base : wpArgs(context, true)
const command = native ? context.native.wp : 'docker'
const siteUrl = native ? context.urls.http : context.urls.https
const wp = (label, args) =>
context.run(
label,
command,
[...networkBase, ...args],
native ? context.native.commandEnvironment : undefined
)
if (native) {
await context.native.start()
const port = Number(context.native.databasePort)
const bootstrap = `$db=new mysqli('127.0.0.1','root','',null,${port});if($db->connect_error)throw new Exception($db->connect_error);$db->query('CREATE DATABASE IF NOT EXISTS db');$db->query("CREATE USER IF NOT EXISTS 'db'@'127.0.0.1' IDENTIFIED BY 'db'");$db->query("GRANT ALL ON db.* TO 'db'@'127.0.0.1'");`
await context.run(
'database',
context.native.php,
[...nativePhpPrefix, '-r', bootstrap],
context.native.commandEnvironment
)
} else {
await context.ensureRouter()
await context.run('start', 'docker', [
'compose',
'-f',
`${context.directory}/.aurora/compose.yaml`,
'up',
'-d',
'--build',
'--remove-orphans'
])
}
if (native && nativePhpPrefix.length) {
const target = Buffer.from(context.directory, 'utf8').toString('base64')
const locale = encodeURIComponent(String(s.locale || 'en_US'))
const download = `$target=base64_decode('${target}');$work=$target.DIRECTORY_SEPARATOR.'.aurora'.DIRECTORY_SEPARATOR.'wordpress-download';$archive=$work.'.zip';@mkdir($work,0777,true);$api=json_decode(file_get_contents('https://api.wordpress.org/core/version-check/1.7/?locale=${locale}'),true);$offer=$api['offers'][0]??null;$url=$offer['packages']['full']??$offer['download']??null;if(!$url||!copy($url,$archive))throw new Exception('Unable to download WordPress.');$zip=new ZipArchive();if($zip->open($archive)!==true)throw new Exception('Unable to open WordPress archive.');if(!$zip->extractTo($work))throw new Exception('Unable to extract WordPress archive.');$zip->close();$source=$work.DIRECTORY_SEPARATOR.'wordpress';foreach(scandir($source) as $entry){if($entry==='.'||$entry==='..')continue;if(!rename($source.DIRECTORY_SEPARATOR.$entry,$target.DIRECTORY_SEPARATOR.$entry))throw new Exception('Unable to install WordPress file: '.$entry);}@rmdir($source);@rmdir($work);@unlink($archive);`
await context.run(
'download',
context.native.php,
[...nativePhpPrefix, '-r', download],
context.native.commandEnvironment
)
} else {
await context.run(
'download',
command,
[...base, 'core', 'download', `--locale=${s.locale || 'en_US'}`, '--force'],
native ? context.native.commandEnvironment : undefined
)
}
await context.run(
'config',
command,
[
...networkBase,
'config',
'create',
'--dbname=db',
'--dbuser=db',
'--dbpass=db',
`--dbhost=${native ? `127.0.0.1:${context.native.databasePort}` : 'db:3306'}`,
'--skip-check',
'--force'
],
native ? context.native.commandEnvironment : undefined
)
await wp('install', [
'core',
'install',
`--url=${siteUrl}`,
`--title=${title}`,
`--admin_user=${s.admin_user}`,
`--admin_password=${s.admin_password}`,
`--admin_email=${s.admin_email}`,
'--skip-email'
])
await wp('verify-install', ['core', 'is-installed'])
if (s.multisite !== 'none') {
await wp('multisite-convert', [
'core',
'multisite-convert',
`--title=${title}`,
...(s.multisite === 'subdomain' ? ['--subdomains'] : [])
])
await wp('verify-network', ['core', 'is-installed', '--network'])
await wp('verify-network-db', [
'db',
'query',
"SHOW TABLES LIKE 'wp_blogs';",
'--skip-column-names'
])
}
await context.setProjectMetadata({
multisite: String(s.multisite),
routingWildcard: s.multisite === 'subdomain'
})
if (native) {
await wp('permalinks', ['option', 'update', 'permalink_structure', '/%postname%/'])
} else {
await wp('permalinks', ['rewrite', 'structure', '/%postname%/', '--hard'])
}
await wp('debug', ['config', 'set', 'WP_DEBUG', String(Boolean(s.wp_debug)), '--raw'])
await wp('environment', ['config', 'set', 'WP_ENVIRONMENT_TYPE', 'local'])
await context.saveCredentials({
platform: context.moduleId,
adminUrl: `${siteUrl}/wp-admin/`,
username: String(s.admin_user),
password: String(s.admin_password),
email: String(s.admin_email)
})
}