-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcreateConfig.js
More file actions
206 lines (167 loc) · 6.22 KB
/
createConfig.js
File metadata and controls
206 lines (167 loc) · 6.22 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* eslint-disable global-require, import/no-dynamic-require */
const fs = require('fs-extra');
const { execSync } = require('child_process');
const path = require('path');
const glob = require('glob');
const { get, defaults } = require('lodash');
const Ajv = require('ajv');
const createDefaultSeeds = require('./createDefaultSeeds');
const getThemeAndPluginData = require('./getThemeAndPluginData');
const renderTemplate = require('./renderTemplate');
const getValidVersions = require('./getValidVersions');
const getEnv = require('./getEnv');
const getConfigFile = require('../utils/get-config-file');
// Initialize the validation object.
const validation = new Ajv({ allErrors: true, jsonPointers: true });
require('ajv-errors')(validation);
// Constants.
const INVALID_WP_CONTENT_PATH_ERROR = 'Path to wp-content is not a directory.';
const WP_CONTENT_EXCLUDE_PATHS = ['uploads', 'upgrade', 'node_modules', 'cypress'];
/**
* Generate wp-cypress config, Dockerfile and docker-compose.yml.
*
* @param {string} packageDir
* @param {boolean} hasVolumeSupport
*
* @return {object}
*/
const createConfig = async (packageDir, hasVolumeSupport = true) => {
const CWD = process.cwd();
const configFile = getConfigFile();
const { wp = {}, integrationFolder } = configFile;
const workingDir = getEnv('WORKING_DIR');
const config = defaults(wp, {
seedsPath: 'cypress/seeds',
wpContent: false,
multisite: false,
url: false,
phpMemoryLimit: '128M',
adminUsername: 'admin',
});
const schema = await fs.readJSON(`${packageDir}/lib/schemas/config-validation.json`);
if (!validation.validate(schema, config)) {
throw new Error(
[
validation.errors[0].message,
...[get(validation.errors[0], 'params.errors[0].message') || ''],
].join(', '),
);
}
if (!config.url) {
config.url = config.port ? `http://localhost:${config.port}` : 'http://localhost';
}
let volumes = [];
let activePlugins = [];
let activeTheme = false;
let pluginsActivatedAfterTheme = [];
if (config.wpContent) {
const pathToWPContent = path.resolve(config.wpContent.path);
if (!fs.lstatSync(pathToWPContent).isDirectory()) {
throw new Error(INVALID_WP_CONTENT_PATH_ERROR);
}
const ignore = WP_CONTENT_EXCLUDE_PATHS.map((exclude) => `${pathToWPContent}/${exclude}`);
if (config.muPlugins) {
ignore.push(`${pathToWPContent}/mu-plugins`);
}
glob.sync(`${pathToWPContent}/*`, { ignore }).forEach((location) => {
const destination = path.relative(pathToWPContent, location);
volumes.push(`${location}:${workingDir}/html/wp-content/${destination}`);
});
if (config.wpContent.activePlugins) {
activePlugins = config.wpContent.activePlugins;
}
if (config.wpContent.activeTheme) {
activeTheme = config.wpContent.activeTheme;
}
if (config.wpContent.pluginsActivatedAfterTheme) {
pluginsActivatedAfterTheme = config.wpContent.pluginsActivatedAfterTheme;
}
} else {
const { plugins, themes } = getThemeAndPluginData(config.plugins, config.themes);
if (plugins.length > 0) {
activePlugins = plugins.map((x) => x.name);
}
if (themes.length > 0) {
activeTheme = themes[0].name;
}
volumes = [...volumes, ...plugins.map((x) => x.volume), ...themes.map((x) => x.volume)];
}
const seedsDir = `${CWD}/${config.seedsPath}`;
createDefaultSeeds(seedsDir, packageDir);
volumes.push(
`${seedsDir}:${workingDir}/html/seeds`,
`${packageDir}/plugin:${workingDir}/html/wp-content/plugins/wp-cypress`,
);
if (config.configFile) {
volumes.push(`${path.resolve(config.configFile)}:${workingDir}/html/wp-cypress-config.php`);
}
if (config.customDatabase) {
volumes.push(`${path.resolve(config.customDatabase)}:/var/www/html/custom-database.sql`);
}
if (config.muPlugins && config.muPlugins.path) {
const muPluginsLocation = path.resolve(config.muPlugins.path);
const muPluginsDestination = config.muPlugins.vip ? 'client-mu-plugins' : 'mu-plugins';
volumes.push(`${muPluginsLocation}:${workingDir}/html/wp-content/${muPluginsDestination}`);
}
const version = Array.isArray(config.version) ? config.version : [config.version];
const validVersions = await getValidVersions(version);
const composerWordpress = path.resolve( CWD, version[0] );
let usableVersions;
if ( fs.existsSync( composerWordpress ) ) {
if ( ! hasVolumeSupport ) {
const vendorSync = `${packageDir}/wordpress`;
if ( !fs.existsSync( vendorSync ) ) {
fs.mkdirSync( vendorSync );
}
execSync(`rsync -r --delete ${composerWordpress}/ ${vendorSync}/`);
} else {
volumes.push( `${composerWordpress}:/var/www/wordpress` );
}
usableVersions = [ 'wordpress' ];
} else {
usableVersions = validVersions;
}
await renderTemplate(
`${packageDir}/lib/templates/docker-compose.ejs`,
`${packageDir}/docker-compose.yml`,
{
port: config.port || 80,
dbPort: config.dbPort || 3306,
volumes: hasVolumeSupport ? volumes : false,
env: config.env || {},
},
);
let htaccessFile = '.htaccess';
if (config.multisite === 'subdomain') {
htaccessFile = `${htaccessFile}-subdomain`;
} else if (config.multisite) {
htaccessFile = `${htaccessFile}-subfolder`;
}
await renderTemplate(`${packageDir}/lib/templates/dockerfile.ejs`, `${packageDir}/Dockerfile`, {
isWpContent: config.wpContent,
versions: usableVersions,
vip: config.muPlugins ? config.muPlugins.vip : false,
phpVersion: config.phpVersion || 7.4,
phpMemoryLimit: config.phpMemoryLimit || '128M',
htaccessFile,
workingDir,
volumes,
});
const wpCypressConfig = {
...config,
version,
validVersions: usableVersions,
volumes,
activePlugins,
activeTheme,
pluginsActivatedAfterTheme,
// We need to store the user integration folder because we replace cypress's
// configuration path to the integration folder with a path to the
// temp integration folder, but we still need to be aware of the original.
userIntegrationFolder: integrationFolder || `${CWD}/cypress/integration`,
htaccessFile,
};
fs.writeJSONSync(`${packageDir}/config.json`, wpCypressConfig);
return wpCypressConfig;
};
module.exports = createConfig;