-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathrun-wp-cli-command.ts
More file actions
143 lines (121 loc) · 4.21 KB
/
run-wp-cli-command.ts
File metadata and controls
143 lines (121 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { spawn } from 'node:child_process';
import { rootCertificates } from 'node:tls';
import { loadNodeRuntime, createNodeFsMountHandler } from '@php-wasm/node';
import {
StreamedPHPResponse,
SupportedPHPVersion,
PHP,
setPhpIniEntries,
ProcessIdAllocator,
type SpawnHandler,
} from '@php-wasm/universal';
import { IS_JSPI_AVAILABLE } from '@studio/common/lib/jspi';
import { cleanupLegacyMuPlugins, getMuPlugins } from '@studio/common/lib/mu-plugins';
import { LatestSupportedPHPVersion } from '@studio/common/types/php-versions';
import { __ } from '@wordpress/i18n';
import { setupPlatformLevelMuPlugins } from '@wp-playground/wordpress';
import { getSqliteCommandPath, getWpCliPharPath } from 'cli/lib/server-files';
const processIdAllocator = new ProcessIdAllocator();
const PLAYGROUND_INTERNAL_SHARED_FOLDER = '/internal/shared';
function createNativeSpawnHandler(): SpawnHandler {
return spawn as unknown as SpawnHandler;
}
export interface RunWpCliCommandOptions {
siteUrl?: string;
}
interface DisposableWpCliResponse extends Disposable {
response: StreamedPHPResponse;
}
// Run a WP-CLI command in a PHP-WASM instance. This function can be used even if the targeted
// Studio site is already running, but it is typically faster to use the `sendWpCliCommand`
// function in that case.
export async function runWpCliCommand(
siteFolder: string,
phpVersion: SupportedPHPVersion,
args: string[]
): Promise< DisposableWpCliResponse > {
const id = await loadNodeRuntime( phpVersion, {
followSymlinks: true,
withRedis: IS_JSPI_AVAILABLE,
withMemcached: IS_JSPI_AVAILABLE,
emscriptenOptions: {
processId: processIdAllocator.claim(),
},
} );
const php = new PHP( id );
try {
await php.setSapiName( 'cli' );
php.defineConstant( 'WP_SQLITE_AST_DRIVER', true );
php.mkdir( '/wordpress' );
await php.mount( '/wordpress', createNodeFsMountHandler( siteFolder ) );
// Setup SSL certificates
php.writeFile( '/tmp/ca-bundle.crt', rootCertificates.join( '\n' ) );
await setPhpIniEntries( php, {
'openssl.cafile': '/tmp/ca-bundle.crt',
allow_url_fopen: 1,
} );
await php.setSpawnHandler( createNativeSpawnHandler() );
await cleanupLegacyMuPlugins( siteFolder );
// Mount mu-plugins
const [ studioMuPluginsHostPath, loaderMuPluginHostPath ] = await getMuPlugins( {
isWpAutoUpdating: false,
} );
await php.mount(
'/internal/studio/mu-plugins',
createNodeFsMountHandler( studioMuPluginsHostPath )
);
await php.mount(
PLAYGROUND_INTERNAL_SHARED_FOLDER + '/mu-plugins/99-studio-loader.php',
createNodeFsMountHandler( loaderMuPluginHostPath )
);
await php.mount( '/tmp/wp-cli.phar', createNodeFsMountHandler( getWpCliPharPath() ) );
await php.mount( '/tmp/sqlite-command', createNodeFsMountHandler( getSqliteCommandPath() ) );
await setupPlatformLevelMuPlugins( php );
const response = await php.cli( [ 'php', '/tmp/wp-cli.phar', '--path=/wordpress', ...args ] );
return {
response,
[ Symbol.dispose ]() {
php.exit();
},
};
} catch ( error ) {
php.exit();
throw new Error( __( 'An error occurred while running the WP-CLI command.' ) );
}
}
/**
* Run a global WP-CLI command without requiring a site.
* Useful for commands like --version that don't need a WordPress installation.
*/
export async function runGlobalWpCliCommand( args: string[] ): Promise< DisposableWpCliResponse > {
const id = await loadNodeRuntime( LatestSupportedPHPVersion, {
followSymlinks: true,
withRedis: false,
withMemcached: false,
emscriptenOptions: {
processId: processIdAllocator.claim(),
},
} );
const php = new PHP( id );
try {
await php.setSapiName( 'cli' );
// Setup SSL certificates
php.writeFile( '/tmp/ca-bundle.crt', rootCertificates.join( '\n' ) );
await setPhpIniEntries( php, {
'openssl.cafile': '/tmp/ca-bundle.crt',
allow_url_fopen: 1,
} );
await php.setSpawnHandler( createNativeSpawnHandler() );
await php.mount( '/tmp/wp-cli.phar', createNodeFsMountHandler( getWpCliPharPath() ) );
const response = await php.cli( [ 'php', '/tmp/wp-cli.phar', ...args ] );
return {
response,
[ Symbol.dispose ]() {
php.exit();
},
};
} catch ( error ) {
php.exit();
throw new Error( __( 'An error occurred while running the WP-CLI command.' ) );
}
}