-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
init-config.js
118 lines (104 loc) · 3.9 KB
/
init-config.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
/**
* External dependencies
*/
const path = require( 'path' );
const { writeFile, mkdir } = require( 'fs' ).promises;
const { existsSync } = require( 'fs' );
const yaml = require( 'js-yaml' );
const os = require( 'os' );
/**
* Internal dependencies
*/
const { readConfig } = require( './config' );
const buildDockerComposeConfig = require( './build-docker-compose-config' );
/**
* @typedef {import('./config').WPConfig} WPConfig
*/
/**
* 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.
*
* @param {Object} options
* @param {Object} options.spinner A CLI spinner which indicates progress.
* @param {boolean} options.debug True if debug mode is enabled.
* @param {string} options.xdebug The Xdebug mode to set. Defaults to "off".
* @param {boolean} options.writeChanges If true, writes the parsed config to the
* required docker files like docker-compose
* and Dockerfile. By default, this is false
* and only the `start` command writes any
* changes.
* @return {WPConfig} The-env config object.
*/
module.exports = async function initConfig( {
spinner,
debug,
xdebug = 'off',
writeChanges = false,
} ) {
const configPath = path.resolve( '.wp-env.json' );
const config = await readConfig( configPath );
config.debug = debug;
// Adding this to the config allows the start command to understand that the
// config has changed when only the xdebug param has changed. This is needed
// so that Docker will rebuild the image whenever the xdebug flag changes.
config.xdebug = xdebug;
const dockerComposeConfig = buildDockerComposeConfig( config );
if ( config.debug ) {
spinner.info(
`Config:\n${ JSON.stringify(
config,
null,
4
) }\n\nDocker Compose Config:\n${ JSON.stringify(
dockerComposeConfig,
null,
4
) }`
);
spinner.start();
}
/**
* We avoid writing changes most of the time so that we can better pass params
* to the start command. For example, say you start wp-env with Xdebug enabled.
* If you then run another command, like opening bash in the wp instance, it
* would turn off Xdebug in the Dockerfile because it wouldn't have the --xdebug
* arg. This basically makes it such that wp-env start is the only command
* which updates any of the Docker configuration.
*/
if ( writeChanges ) {
await mkdir( config.workDirectoryPath, { recursive: true } );
await writeFile(
config.dockerComposeConfigPath,
yaml.dump( dockerComposeConfig )
);
await writeFile(
path.resolve( config.workDirectoryPath, 'Dockerfile' ),
dockerFileContents(
dockerComposeConfig.services.wordpress.image,
xdebug
)
);
} else if ( ! existsSync( config.workDirectoryPath ) ) {
spinner.fail(
'wp-env has not yet been initalized. Please run `wp-env start` to install the WordPress instance before using any other commands. This is only necessary to set up the environment for the first time; it is typically not necessary for the instance to be running after that in order to use other commands.'
);
process.exit( 1 );
}
return config;
};
function dockerFileContents( image, xdebugMode ) {
const isLinux = os.type() === 'Linux';
// Discover client host does not appear to work on macOS with Docker.
const clientDetectSettings = isLinux
? 'xdebug.discover_client_host=true'
: 'xdebug.client_host="host.docker.internal"';
return `FROM ${ image }
RUN apt-get -qy install $PHPIZE_DEPS \\
&& pecl install xdebug \\
&& docker-php-ext-enable xdebug
RUN touch /usr/local/etc/php/php.ini
RUN echo 'xdebug.start_with_request=yes' >> /usr/local/etc/php/php.ini
RUN echo 'xdebug.mode=${ xdebugMode }' >> /usr/local/etc/php/php.ini
RUN echo '${ clientDetectSettings }' >> /usr/local/etc/php/php.ini
`;
}