feat: run WordPress projects on native runtime
This commit is contained in:
@@ -1,35 +1,125 @@
|
||||
'use strict'
|
||||
|
||||
function safeName(name) {
|
||||
return name.toLowerCase().replace(/[^a-z0-9_-]+/g, '-').replace(/^-+|-+$/g, '') || 'project'
|
||||
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']
|
||||
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 base = wpArgs(context, false)
|
||||
const networkBase = wpArgs(context, true)
|
||||
const siteUrl = context.urls.https
|
||||
await context.ensureRouter()
|
||||
await context.run('start', 'docker', ['compose', '-f', `${context.directory}/.aurora/compose.yaml`, 'up', '-d', '--build', '--remove-orphans'])
|
||||
await context.run('download', 'docker', [...base, 'core', 'download', `--locale=${s.locale || 'en_US'}`, '--force'])
|
||||
await context.run('config', 'docker', [...networkBase, 'config', 'create', '--dbname=db', '--dbuser=db', '--dbpass=db', '--dbhost=db:3306', '--skip-check', '--force'])
|
||||
await context.run('install', 'docker', [...networkBase, 'core', 'install', `--url=${siteUrl}`, `--title=${title}`, `--admin_user=${s.admin_user}`, `--admin_password=${s.admin_password}`, `--admin_email=${s.admin_email}`, '--skip-email'])
|
||||
await context.run('verify-install', 'docker', [...networkBase, 'core', 'is-installed'])
|
||||
if (s.multisite !== 'none') {
|
||||
await context.run('multisite-convert', 'docker', [...networkBase, 'core', 'multisite-convert', `--title=${title}`, ...(s.multisite === 'subdomain' ? ['--subdomains'] : [])])
|
||||
await context.run('verify-network', 'docker', [...networkBase, 'core', 'is-installed', '--network'])
|
||||
await context.run('verify-network-db', 'docker', [...networkBase, 'db', 'query', "SHOW TABLES LIKE 'wp_blogs';", '--skip-column-names'])
|
||||
const native = context.environment.runtimeEngine === 'native'
|
||||
const base = native ? [`--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, [...(native ? [] : networkBase), ...args])
|
||||
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, ['-r', bootstrap])
|
||||
} else {
|
||||
await context.ensureRouter()
|
||||
await context.run('start', 'docker', [
|
||||
'compose',
|
||||
'-f',
|
||||
`${context.directory}/.aurora/compose.yaml`,
|
||||
'up',
|
||||
'-d',
|
||||
'--build',
|
||||
'--remove-orphans'
|
||||
])
|
||||
}
|
||||
await context.setProjectMetadata({ multisite: String(s.multisite), routingWildcard: s.multisite === 'subdomain' })
|
||||
await context.run('permalinks', 'docker', [...networkBase, 'rewrite', 'structure', '/%postname%/', '--hard'])
|
||||
await context.run('debug', 'docker', [...networkBase, 'config', 'set', 'WP_DEBUG', String(Boolean(s.wp_debug)), '--raw'])
|
||||
await context.run('environment', 'docker', [...networkBase, '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) })
|
||||
await context.run('download', command, [
|
||||
...base,
|
||||
'core',
|
||||
'download',
|
||||
`--locale=${s.locale || 'en_US'}`,
|
||||
'--force'
|
||||
])
|
||||
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'
|
||||
])
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user