|
1 | 1 | import { StepHandler } from '.'; |
2 | 2 | import { InstallAssetOptions, installAsset } from './install-asset'; |
3 | 3 | import { activatePlugin } from './activate-plugin'; |
| 4 | +import { writeFile } from './write-file'; |
4 | 5 | import { zipNameToHumanName } from '../utils/zip-name-to-human-name'; |
5 | 6 | import { Directory } from '../resources'; |
6 | 7 | import { joinPaths } from '@php-wasm/util'; |
@@ -51,8 +52,8 @@ export interface InstallPluginStep<FileResource, DirectoryResource> |
51 | 52 | */ |
52 | 53 | step: 'installPlugin'; |
53 | 54 | /** |
54 | | - * The plugin files to install. It can be either a plugin zip file, or a |
55 | | - * directory containing all the plugin files at its root. |
| 55 | + * The plugin files to install. It can be a plugin zip file, a single PHP |
| 56 | + * file, or a directory containing all the plugin files at its root. |
56 | 57 | */ |
57 | 58 | pluginData: FileResource | DirectoryResource; |
58 | 59 |
|
@@ -99,31 +100,52 @@ export const installPlugin: StepHandler< |
99 | 100 | ); |
100 | 101 | } |
101 | 102 |
|
102 | | - const targetFolderName = 'targetFolderName' in options ? options.targetFolderName : ''; |
| 103 | + const pluginsDirectoryPath = joinPaths( |
| 104 | + await playground.documentRoot, |
| 105 | + 'wp-content', |
| 106 | + 'plugins' |
| 107 | + ); |
| 108 | + const targetFolderName = |
| 109 | + 'targetFolderName' in options ? options.targetFolderName : ''; |
103 | 110 | let assetFolderPath = ''; |
104 | 111 | let assetNiceName = ''; |
105 | 112 | if (pluginData instanceof File) { |
106 | | - // @TODO: Consider validating whether this is a zip file? |
107 | | - const zipFileName = pluginData.name.split('/').pop() || 'plugin.zip'; |
108 | | - assetNiceName = zipNameToHumanName(zipFileName); |
| 113 | + if (pluginData.name.endsWith('.php')) { |
| 114 | + const destinationFilePath = joinPaths( |
| 115 | + pluginsDirectoryPath, |
| 116 | + pluginData.name |
| 117 | + ); |
| 118 | + await writeFile(playground, { |
| 119 | + path: destinationFilePath, |
| 120 | + data: pluginData, |
| 121 | + }); |
| 122 | + assetFolderPath = pluginsDirectoryPath; |
| 123 | + assetNiceName = pluginData.name; |
| 124 | + } else { |
| 125 | + // Assume any other file is a zip file |
| 126 | + // @TODO: Consider validating whether this is a zip file? |
| 127 | + const zipFileName = |
| 128 | + pluginData.name.split('/').pop() || 'plugin.zip'; |
| 129 | + assetNiceName = zipNameToHumanName(zipFileName); |
109 | 130 |
|
110 | | - progress?.tracker.setCaption(`Installing the ${assetNiceName} plugin`); |
111 | | - const assetResult = await installAsset(playground, { |
112 | | - ifAlreadyInstalled, |
113 | | - zipFile: pluginData, |
114 | | - targetPath: `${await playground.documentRoot}/wp-content/plugins`, |
115 | | - targetFolderName: targetFolderName |
116 | | - }); |
117 | | - assetFolderPath = assetResult.assetFolderPath; |
118 | | - assetNiceName = assetResult.assetFolderName; |
| 131 | + progress?.tracker.setCaption( |
| 132 | + `Installing the ${assetNiceName} plugin` |
| 133 | + ); |
| 134 | + const assetResult = await installAsset(playground, { |
| 135 | + ifAlreadyInstalled, |
| 136 | + zipFile: pluginData, |
| 137 | + targetPath: `${await playground.documentRoot}/wp-content/plugins`, |
| 138 | + targetFolderName: targetFolderName, |
| 139 | + }); |
| 140 | + assetFolderPath = assetResult.assetFolderPath; |
| 141 | + assetNiceName = assetResult.assetFolderName; |
| 142 | + } |
119 | 143 | } else if (pluginData) { |
120 | 144 | assetNiceName = pluginData.name; |
121 | 145 | progress?.tracker.setCaption(`Installing the ${assetNiceName} plugin`); |
122 | 146 |
|
123 | 147 | const pluginDirectoryPath = joinPaths( |
124 | | - await playground.documentRoot, |
125 | | - 'wp-content', |
126 | | - 'plugins', |
| 148 | + pluginsDirectoryPath, |
127 | 149 | targetFolderName || pluginData.name |
128 | 150 | ); |
129 | 151 | await writeFiles(playground, pluginDirectoryPath, pluginData.files, { |
|
0 commit comments