-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
env.js
367 lines (317 loc) · 9.54 KB
/
env.js
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
'use strict';
/**
* External dependencies
*/
const util = require( 'util' );
const path = require( 'path' );
const fs = require( 'fs' ).promises;
const dockerCompose = require( 'docker-compose' );
const yaml = require( 'js-yaml' );
/**
* Promisified dependencies
*/
const copyDir = util.promisify( require( 'copy-dir' ) );
const sleep = util.promisify( setTimeout );
/**
* Internal dependencies
*/
const { ValidationError, readConfig } = require( './config' );
const downloadSource = require( './download-source' );
const buildDockerComposeConfig = require( './build-docker-compose-config' );
/**
* @typedef {import('./config').Config} Config
*/
module.exports = {
/**
* Starts the development server.
*
* @param {Object} options
* @param {Object} options.spinner A CLI spinner which indicates progress.
*/
async start( { spinner } ) {
const config = await initConfig();
spinner.text = 'Downloading WordPress.';
const progresses = {};
const getProgressSetter = ( id ) => ( progress ) => {
progresses[ id ] = progress;
spinner.text =
'Downloading WordPress.\n' +
Object.entries( progresses )
.map(
( [ key, value ] ) =>
` - ${ key }: ${ ( value * 100 ).toFixed(
0
) }/100%`
)
.join( '\n' );
};
await Promise.all( [
// Preemptively start the database while we wait for sources to download.
dockerCompose.upOne( 'mysql', {
config: config.dockerComposeConfigPath,
} ),
( async () => {
if ( config.coreSource ) {
await downloadSource( config.coreSource, {
onProgress: getProgressSetter( 'core' ),
} );
await copyCoreFiles(
config.coreSource.path,
config.coreSource.testsPath
);
}
} )(),
...config.pluginSources.map( ( source ) =>
downloadSource( source, {
onProgress: getProgressSetter( source.basename ),
} )
),
...config.themeSources.map( ( source ) =>
downloadSource( source, {
onProgress: getProgressSetter( source.basename ),
} )
),
] );
spinner.text = 'Starting WordPress.';
await dockerCompose.upMany( [ 'wordpress', 'tests-wordpress' ], {
config: config.dockerComposeConfigPath,
} );
try {
await checkDatabaseConnection( config );
} catch ( error ) {
// Wait 30 seconds for MySQL to accept connections.
await retry( () => checkDatabaseConnection( config ), {
times: 30,
delay: 1000,
} );
// It takes 3-4 seconds for MySQL to be ready after it starts accepting connections.
await sleep( 4000 );
}
// Retry WordPress installation in case MySQL *still* wasn't ready.
await Promise.all( [
retry( () => configureWordPress( 'development', config ), {
times: 2,
} ),
retry( () => configureWordPress( 'tests', config ), { times: 2 } ),
] );
spinner.text = 'WordPress started.';
},
/**
* Stops the development server.
*
* @param {Object} options
* @param {Object} options.spinner A CLI spinner which indicates progress.
*/
async stop( { spinner } ) {
const { dockerComposeConfigPath } = await initConfig();
spinner.text = 'Stopping WordPress.';
await dockerCompose.down( { config: dockerComposeConfigPath } );
spinner.text = 'Stopped WordPress.';
},
/**
* Wipes the development server's database, the tests server's database, or both.
*
* @param {Object} options
* @param {string} options.environment The environment to clean. Either 'development', 'tests', or 'all'.
* @param {Object} options.spinner A CLI spinner which indicates progress.
*/
async clean( { environment, spinner } ) {
const config = await initConfig();
const description = `${ environment } environment${
environment === 'all' ? 's' : ''
}`;
spinner.text = `Cleaning ${ description }.`;
const tasks = [];
if ( environment === 'all' || environment === 'development' ) {
tasks.push(
resetDatabase( 'development', config )
.then( () => configureWordPress( 'development', config ) )
.catch( () => {} )
);
}
if ( environment === 'all' || environment === 'tests' ) {
tasks.push(
resetDatabase( 'tests', config )
.then( () => configureWordPress( 'tests', config ) )
.catch( () => {} )
);
}
await Promise.all( tasks );
spinner.text = `Cleaned ${ description }.`;
},
/**
* Runs an arbitrary command on the given Docker container.
*
* @param {Object} options
* @param {Object} options.container The Docker container to run the command on.
* @param {Object} options.command The command to run.
* @param {Object} options.spinner A CLI spinner which indicates progress.
*/
async run( { container, command, spinner } ) {
const config = await initConfig();
command = command.join( ' ' );
spinner.text = `Running \`${ command }\` in '${ container }'.`;
const result = await dockerCompose.run( container, command, {
config: config.dockerComposeConfigPath,
commandOptions: [ '--rm' ],
} );
if ( result.out ) {
// eslint-disable-next-line no-console
console.log(
process.stdout.isTTY ? `\n\n${ result.out }\n\n` : result.out
);
} else if ( result.err ) {
// eslint-disable-next-line no-console
console.error(
process.stdout.isTTY ? `\n\n${ result.err }\n\n` : result.err
);
throw result.err;
}
spinner.text = `Ran \`${ command }\` in '${ container }'.`;
},
ValidationError,
};
/**
* Initializes the local environment so that Docker commands can be run. Reads
* ./.wp-env.json, creates ~/.wp-env, and creates ~/.wp-env/docker-compose.yml.
*
* @return {Config} The-env config object.
*/
async function initConfig() {
const configPath = path.resolve( '.wp-env.json' );
const config = await readConfig( configPath );
await fs.mkdir( config.workDirectoryPath, { recursive: true } );
await fs.writeFile(
config.dockerComposeConfigPath,
yaml.dump( buildDockerComposeConfig( config ) )
);
return config;
}
/**
* Copies a WordPress installation, taking care to ignore large directories
* (.git, node_modules) and configuration files (wp-config.php).
*
* @param {string} fromPath Path to the WordPress directory to copy.
* @param {string} toPath Destination path.
*/
async function copyCoreFiles( fromPath, toPath ) {
await copyDir( fromPath, toPath, {
filter( stat, filepath, filename ) {
if ( stat === 'symbolicLink' ) {
return false;
}
if ( stat === 'directory' && filename === '.git' ) {
return false;
}
if ( stat === 'directory' && filename === 'node_modules' ) {
return false;
}
if ( stat === 'file' && filename === 'wp-config.php' ) {
return false;
}
return true;
},
} );
}
/**
* Performs the given action again and again until it does not throw an error.
*
* @param {Function} action The action to perform.
* @param {Object} options
* @param {number} options.times How many times to try before giving up.
* @param {number} [options.delay=5000] How long, in milliseconds, to wait between each try.
*/
async function retry( action, { times, delay = 5000 } ) {
let tries = 0;
while ( true ) {
try {
return await action();
} catch ( error ) {
if ( ++tries >= times ) {
throw error;
}
await sleep( delay );
}
}
}
/**
* Checks a WordPress database connection. An error is thrown if the test is
* unsuccessful.
*
* @param {Config} config The wp-env config object.
*/
async function checkDatabaseConnection( { dockerComposeConfigPath } ) {
await dockerCompose.run( 'cli', 'wp db check', {
config: dockerComposeConfigPath,
commandOptions: [ '--rm' ],
} );
}
/**
* Configures WordPress for the given environment by installing WordPress,
* activating all plugins, and activating the first theme. These steps are
* performed sequentially so as to not overload the WordPress instance.
*
* @param {string} environment The environment to configure. Either 'development' or 'tests'.
* @param {Config} config The wp-env config object.
*/
async function configureWordPress( environment, config ) {
const options = {
config: config.dockerComposeConfigPath,
commandOptions: [ '--rm' ],
};
// Install WordPress.
await dockerCompose.run(
environment === 'development' ? 'cli' : 'tests-cli',
`wp core install
--url=localhost:${
environment === 'development'
? process.env.WP_ENV_PORT || '8888'
: process.env.WP_ENV_TESTS_PORT || '8889'
}
--title='${ config.name }'
--admin_user=admin
--admin_password=password
--skip-email`,
options
);
// Activate all plugins.
for ( const pluginSource of config.pluginSources ) {
await dockerCompose.run(
environment === 'development' ? 'cli' : 'tests-cli',
`wp plugin activate ${ pluginSource.basename }`,
options
);
}
// Activate the first theme.
const [ themeSource ] = config.themeSources;
if ( themeSource ) {
await dockerCompose.run(
environment === 'development' ? 'cli' : 'tests-cli',
`wp theme activate ${ themeSource.basename }`,
options
);
}
}
/**
* Resets the development server's database, the tests server's database, or both.
*
* @param {string} environment The environment to clean. Either 'development', 'tests', or 'all'.
* @param {Config} config The wp-env config object.
*/
async function resetDatabase( environment, { dockerComposeConfigPath } ) {
const options = {
config: dockerComposeConfigPath,
commandOptions: [ '--rm' ],
};
const tasks = [];
if ( environment === 'all' || environment === 'development' ) {
tasks.push( dockerCompose.run( 'cli', 'wp db reset --yes', options ) );
}
if ( environment === 'all' || environment === 'tests' ) {
tasks.push(
dockerCompose.run( 'tests-cli', 'wp db reset --yes', options )
);
}
await Promise.all( tasks );
}