2020-04-02 13:41:06 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
const { spawnSync } = require( 'child_process' );
|
|
|
|
const program = require( 'commander' );
|
|
|
|
const path = require( 'path' );
|
|
|
|
const fs = require( 'fs' );
|
2021-01-19 15:02:08 +00:00
|
|
|
const { getAdminConfig, getAppBase, getAppRoot, getAppName, getTestConfig } = require( '../utils' );
|
2020-04-02 13:41:06 +00:00
|
|
|
|
|
|
|
const dockerArgs = [];
|
|
|
|
let command = '';
|
2020-10-21 13:53:00 +00:00
|
|
|
let customInitFile = '';
|
2020-04-02 13:41:06 +00:00
|
|
|
|
|
|
|
program
|
|
|
|
.command( 'up', 'Start and build the Docker container' )
|
|
|
|
.command( 'down', 'Stop the Docker container and remove volumes' )
|
|
|
|
.action( ( cmd, options ) => {
|
2020-10-21 13:53:00 +00:00
|
|
|
args = options.args ? options.args : options;
|
|
|
|
if ( 'up' === args[0] ) {
|
2020-04-02 13:41:06 +00:00
|
|
|
command = 'up';
|
|
|
|
dockerArgs.push( 'up', '--build', '-d' );
|
2020-10-21 13:53:00 +00:00
|
|
|
customInitFile = args[1] ? args[1] : '';
|
2020-04-02 13:41:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 13:53:00 +00:00
|
|
|
if ( 'down' === args[0] ) {
|
2020-04-02 13:41:06 +00:00
|
|
|
command = 'down';
|
|
|
|
dockerArgs.push( 'down', '-v' );
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
.parse( process.argv );
|
|
|
|
|
2020-07-29 18:13:22 +00:00
|
|
|
const appPath = getAppRoot();
|
2021-01-19 15:02:08 +00:00
|
|
|
const envVars = getAdminConfig();
|
2020-04-02 13:41:06 +00:00
|
|
|
|
|
|
|
if ( appPath ) {
|
|
|
|
if ( 'up' === command ) {
|
|
|
|
// Look for an initialization script in the dependent app.
|
2021-02-15 15:43:07 +00:00
|
|
|
if ( customInitFile && typeof customInitFile === 'string' ) {
|
2020-10-21 13:53:00 +00:00
|
|
|
const possibleInitFile = customInitFile;
|
|
|
|
customInitFile = path.resolve( possibleInitFile );
|
|
|
|
if ( ! fs.existsSync( customInitFile ) ) {
|
|
|
|
customInitFile = path.resolve( appPath, possibleInitFile );
|
|
|
|
}
|
|
|
|
if ( ! fs.existsSync( customInitFile ) ) {
|
|
|
|
customInitFile = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const appInitFile = customInitFile ? customInitFile : path.resolve( appPath, 'tests/e2e/docker/initialize.sh' );
|
2020-04-02 13:41:06 +00:00
|
|
|
// If found, copy it into the wp-cli Docker context so
|
|
|
|
// it gets picked up by the entrypoint script.
|
|
|
|
if ( fs.existsSync( appInitFile ) ) {
|
|
|
|
fs.copyFileSync(
|
|
|
|
appInitFile,
|
|
|
|
path.resolve( __dirname, '../docker/wp-cli/initialize.sh' )
|
|
|
|
);
|
2020-10-21 13:53:00 +00:00
|
|
|
console.log('Initializing ' + appInitFile );
|
2020-04-02 13:41:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provide an "app name" to use in Docker container names.
|
2020-08-10 17:54:10 +00:00
|
|
|
envVars.APP_NAME = getAppName();
|
2020-04-02 13:41:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 13:12:07 +00:00
|
|
|
// Load test configuration file into an object.
|
|
|
|
const testConfig = getTestConfig();
|
|
|
|
|
|
|
|
// Set some environment variables
|
2020-07-23 18:38:40 +00:00
|
|
|
if ( ! process.env.WC_E2E_FOLDER_MAPPING ) {
|
2021-01-13 11:27:11 +00:00
|
|
|
envVars.WC_E2E_FOLDER_MAPPING = '/var/www/html/wp-content/plugins/' + getAppBase();
|
2020-07-23 18:38:40 +00:00
|
|
|
}
|
2020-08-21 16:25:30 +00:00
|
|
|
if ( ! process.env.WORDPRESS_PORT ) {
|
|
|
|
process.env.WORDPRESS_PORT = testConfig.port;
|
2020-07-24 13:12:07 +00:00
|
|
|
}
|
2020-08-21 16:25:30 +00:00
|
|
|
if ( ! process.env.WORDPRESS_URL ) {
|
|
|
|
process.env.WORDPRESS_URL = testConfig.url;
|
2020-07-24 13:12:07 +00:00
|
|
|
}
|
2020-07-23 18:38:40 +00:00
|
|
|
|
2020-04-02 13:41:06 +00:00
|
|
|
// Ensure that the first Docker compose file loaded is from our local env.
|
|
|
|
dockerArgs.unshift( '-f', path.resolve( __dirname, '../docker-compose.yaml' ) );
|
|
|
|
|
|
|
|
const dockerProcess = spawnSync(
|
|
|
|
'docker-compose',
|
|
|
|
dockerArgs,
|
|
|
|
{
|
|
|
|
stdio: 'inherit',
|
|
|
|
env: Object.assign( {}, process.env, envVars ),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
console.log( 'Docker exit code: ' + dockerProcess.status );
|
|
|
|
|
|
|
|
// Pass Docker exit code to npm
|
|
|
|
process.exit( dockerProcess.status );
|