-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathinstall-plugin.ts
170 lines (160 loc) · 4.26 KB
/
install-plugin.ts
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { StepHandler } from '.';
import { InstallAssetOptions, installAsset } from './install-asset';
import { activatePlugin } from './activate-plugin';
import { writeFile } from './write-file';
import { zipNameToHumanName } from '../utils/zip-name-to-human-name';
import { Directory } from '../resources';
import { joinPaths } from '@php-wasm/util';
import { writeFiles } from '@php-wasm/universal';
import { logger } from '@php-wasm/logger';
/**
* @inheritDoc installPlugin
* @hasRunnableExample
* @needsLogin
* @landingPage /wp-admin/plugins.php
* @example
*
* <code>
* {
* "step": "installPlugin",
* "pluginData": {
* "resource": "wordpress.org/plugins",
* "slug": "gutenberg"
* },
* "options": {
* "activate": true
* }
* }
* </code>
*
* @example
*
* <code>
* {
* "step": "installPlugin",
* "pluginData": {
* "resource": "git:directory",
* "url": "https://github.com/wordpress/wordpress-playground.git",
* "ref": "HEAD",
* "path": "wp-content/plugins/hello-dolly"
* },
* "options": {
* "activate": true
* }
* }
* </code>
*/
export interface InstallPluginStep<FileResource, DirectoryResource>
extends Pick<InstallAssetOptions, 'ifAlreadyInstalled'> {
/**
* The step identifier.
*/
step: 'installPlugin';
/**
* The plugin files to install. It can be a plugin zip file, a single PHP
* file, or a directory containing all the plugin files at its root.
*/
pluginData: FileResource | DirectoryResource;
/**
* @deprecated. Use `pluginData` instead.
*/
pluginZipFile?: FileResource;
/**
* Optional installation options.
*/
options?: InstallPluginOptions;
}
export interface InstallPluginOptions {
/**
* Whether to activate the plugin after installing it.
*/
activate?: boolean;
/**
* The name of the folder to install the plugin to. Defaults to guessing from pluginData
*/
targetFolderName?: string;
}
/**
* Installs a WordPress plugin in the Playground.
*
* @param playground The playground client.
* @param pluginData The plugin zip file.
* @param options Optional. Set `activate` to false if you don't want to activate the plugin.
*/
export const installPlugin: StepHandler<
InstallPluginStep<File, Directory>
> = async (
playground,
{ pluginData, pluginZipFile, ifAlreadyInstalled, options = {} },
progress?
) => {
if (pluginZipFile) {
pluginData = pluginZipFile;
logger.warn(
'The "pluginZipFile" option is deprecated. Use "pluginData" instead.'
);
}
const pluginsDirectoryPath = joinPaths(
await playground.documentRoot,
'wp-content',
'plugins'
);
const targetFolderName =
'targetFolderName' in options ? options.targetFolderName : '';
let assetFolderPath = '';
let assetNiceName = '';
if (pluginData instanceof File) {
if (pluginData.name.endsWith('.php')) {
const destinationFilePath = joinPaths(
pluginsDirectoryPath,
pluginData.name
);
await writeFile(playground, {
path: destinationFilePath,
data: pluginData,
});
assetFolderPath = pluginsDirectoryPath;
assetNiceName = pluginData.name;
} else {
// Assume any other file is a zip file
// @TODO: Consider validating whether this is a zip file?
const zipFileName =
pluginData.name.split('/').pop() || 'plugin.zip';
assetNiceName = zipNameToHumanName(zipFileName);
progress?.tracker.setCaption(
`Installing the ${assetNiceName} plugin`
);
const assetResult = await installAsset(playground, {
ifAlreadyInstalled,
zipFile: pluginData,
targetPath: `${await playground.documentRoot}/wp-content/plugins`,
targetFolderName: targetFolderName,
});
assetFolderPath = assetResult.assetFolderPath;
assetNiceName = assetResult.assetFolderName;
}
} else if (pluginData) {
assetNiceName = pluginData.name;
progress?.tracker.setCaption(`Installing the ${assetNiceName} plugin`);
const pluginDirectoryPath = joinPaths(
pluginsDirectoryPath,
targetFolderName || pluginData.name
);
await writeFiles(playground, pluginDirectoryPath, pluginData.files, {
rmRoot: true,
});
assetFolderPath = pluginDirectoryPath;
}
// Activate
const activate = 'activate' in options ? options.activate : true;
if (activate) {
await activatePlugin(
playground,
{
pluginPath: assetFolderPath,
pluginName: assetNiceName,
},
progress
);
}
};