Skip to content

Commit 2c460cd

Browse files
committed
Switch logic to all docker commands run in the container, except for up, down, stop, clean
1 parent f3ba0e7 commit 2c460cd

File tree

1 file changed

+130
-113
lines changed
  • projects/js-packages/jetpack-cli/bin

1 file changed

+130
-113
lines changed

projects/js-packages/jetpack-cli/bin/jp.js

+130-113
Original file line numberDiff line numberDiff line change
@@ -136,131 +136,148 @@ const main = async () => {
136136
throw new Error( 'Monorepo not found' );
137137
}
138138

139-
// Handle docker commands on the host
139+
// Handle docker commands that must run on the host machine
140140
if ( args[ 0 ] === 'docker' ) {
141-
// Commands that should run in the container
142-
const containerCommands = [ 'build-image', 'install' ];
143-
if ( containerCommands.includes( args[ 1 ] ) ) {
144-
const result = spawnSync(
145-
resolve( monorepoRoot, 'tools/docker/bin/monorepo' ),
146-
[ 'pnpm', 'jetpack', ...args ],
147-
{
148-
stdio: 'inherit',
149-
shell: true,
150-
cwd: monorepoRoot,
151-
}
152-
);
153-
154-
if ( result.status !== 0 ) {
155-
throw new Error( `Command failed with status ${ result.status }` );
156-
}
157-
return;
158-
}
159-
160-
// Run config generation first if this is an 'up' command
161-
if ( args[ 1 ] === 'up' ) {
162-
// Create required directories
163-
fs.mkdirSync( resolve( monorepoRoot, 'tools/docker/data/jetpack_dev_mysql' ), {
164-
recursive: true,
165-
} );
166-
fs.mkdirSync( resolve( monorepoRoot, 'tools/docker/data/ssh.keys' ), { recursive: true } );
167-
fs.mkdirSync( resolve( monorepoRoot, 'tools/docker/wordpress' ), { recursive: true } );
168-
169-
const images = [
170-
{ name: 'mariadb:lts' },
171-
{ name: 'automattic/jetpack-wordpress-dev:latest' },
172-
{ name: 'phpmyadmin/phpmyadmin:latest', platform: 'linux/amd64' },
173-
{ name: 'maildev/maildev', platform: 'linux/amd64' },
174-
{ name: 'atmoz/sftp', platform: 'linux/amd64' },
175-
];
176-
177-
for ( const image of images ) {
178-
const inspect = spawnSync( 'docker', [ 'image', 'inspect', image.name ], {
179-
stdio: 'ignore',
141+
const hostCommands = [ 'up', 'down', 'stop', 'clean' ];
142+
if ( hostCommands.includes( args[ 1 ] ) ) {
143+
// Handle command-specific setup/cleanup
144+
if ( args[ 1 ] === 'up' ) {
145+
// Create required directories
146+
fs.mkdirSync( resolve( monorepoRoot, 'tools/docker/data/jetpack_dev_mysql' ), {
147+
recursive: true,
180148
} );
181-
if ( inspect.status !== 0 ) {
182-
console.log( chalk.blue( `Pulling ${ image.name }...` ) );
183-
const pullArgs = [ 'pull', image.name ];
184-
if ( image.platform ) {
185-
pullArgs.splice( 1, 0, '--platform', image.platform );
149+
fs.mkdirSync( resolve( monorepoRoot, 'tools/docker/data/ssh.keys' ), {
150+
recursive: true,
151+
} );
152+
fs.mkdirSync( resolve( monorepoRoot, 'tools/docker/wordpress' ), { recursive: true } );
153+
154+
// Create empty .env file
155+
fs.writeFileSync( resolve( monorepoRoot, 'tools/docker/.env' ), '' );
156+
157+
const images = [
158+
{ name: 'mariadb:lts' },
159+
{ name: 'automattic/jetpack-wordpress-dev:latest' },
160+
{ name: 'phpmyadmin/phpmyadmin:latest', platform: 'linux/amd64' },
161+
{ name: 'maildev/maildev', platform: 'linux/amd64' },
162+
{ name: 'atmoz/sftp', platform: 'linux/amd64' },
163+
];
164+
165+
for ( const image of images ) {
166+
const inspect = spawnSync( 'docker', [ 'image', 'inspect', image.name ], {
167+
stdio: 'ignore',
168+
} );
169+
if ( inspect.status !== 0 ) {
170+
console.log( chalk.blue( `Pulling ${ image.name }...` ) );
171+
const pullArgs = [ 'pull', image.name ];
172+
if ( image.platform ) {
173+
pullArgs.splice( 1, 0, '--platform', image.platform );
174+
}
175+
const pull = spawnSync( 'docker', pullArgs, { stdio: 'inherit' } );
176+
if ( pull.status !== 0 ) {
177+
throw new Error( `Failed to pull ${ image.name }` );
178+
}
186179
}
187-
const pull = spawnSync( 'docker', pullArgs, { stdio: 'inherit' } );
188-
if ( pull.status !== 0 ) {
189-
throw new Error( `Failed to pull ${ image.name }` );
180+
}
181+
182+
const configResult = spawnSync(
183+
resolve( monorepoRoot, 'tools/docker/bin/monorepo' ),
184+
[ 'pnpm', 'jetpack', 'docker', 'config' ],
185+
{
186+
stdio: 'inherit',
187+
shell: true,
188+
cwd: monorepoRoot,
190189
}
190+
);
191+
192+
if ( configResult.status !== 0 ) {
193+
throw new Error( 'Failed to generate Docker config' );
191194
}
195+
} else if ( args[ 1 ] === 'clean' ) {
196+
// After docker-compose down -v, also remove local files
197+
const projectName = args.includes( '--type=e2e' ) ? 'jetpack_e2e' : 'jetpack_dev';
198+
const cleanupPaths = [
199+
resolve( monorepoRoot, 'tools/docker/wordpress/' ),
200+
resolve( monorepoRoot, 'tools/docker/wordpress-develop/*' ),
201+
resolve( monorepoRoot, 'tools/docker/logs/', projectName ),
202+
resolve( monorepoRoot, 'tools/docker/data/', `${ projectName }_mysql` ),
203+
];
204+
205+
// Function to clean up after docker-compose down
206+
const cleanupFiles = () => {
207+
for ( const path of cleanupPaths ) {
208+
try {
209+
fs.rmSync( path, { recursive: true, force: true } );
210+
} catch ( error ) {
211+
console.warn(
212+
chalk.yellow( `Warning: Could not remove ${ path }: ${ error.message }` )
213+
);
214+
}
215+
}
216+
};
217+
218+
// Add cleanup to process events to ensure it runs after docker-compose
219+
process.once( 'beforeExit', cleanupFiles );
220+
221+
// Replace 'clean' with 'down -v' in the arguments
222+
args.splice( 1, 1, 'down', '-v' );
192223
}
193224

194-
const configResult = spawnSync(
195-
resolve( monorepoRoot, 'tools/docker/bin/monorepo' ),
196-
[ 'pnpm', 'jetpack', 'docker', 'config' ],
197-
{
198-
stdio: 'inherit',
199-
shell: true,
200-
cwd: monorepoRoot,
225+
// Get project name (from docker.js)
226+
const projectName = args.includes( '--type=e2e' ) ? 'jetpack_e2e' : 'jetpack_dev';
227+
228+
// Load versions from .github/versions.sh
229+
const versionsPath = resolve( monorepoRoot, '.github/versions.sh' );
230+
const versions = fs.readFileSync( versionsPath, 'utf8' );
231+
const versionVars = {};
232+
versions.split( '\n' ).forEach( line => {
233+
const match = line.match( /^([A-Z_]+)=(.+)$/ );
234+
if ( match ) {
235+
versionVars[ match[ 1 ] ] = match[ 2 ].replace( /['"]/g, '' );
201236
}
202-
);
237+
} );
203238

204-
if ( configResult.status !== 0 ) {
205-
throw new Error( 'Failed to generate Docker config' );
206-
}
207-
}
239+
// Build environment variables (from docker.js)
240+
const envVars = {
241+
...process.env,
242+
// Load from default.env
243+
...( fs.existsSync( resolve( monorepoRoot, 'tools/docker/default.env' ) )
244+
? dotenv.parse( fs.readFileSync( resolve( monorepoRoot, 'tools/docker/default.env' ) ) )
245+
: {} ),
246+
// Load from .env if it exists
247+
...( fs.existsSync( resolve( monorepoRoot, 'tools/docker/.env' ) )
248+
? dotenv.parse( fs.readFileSync( resolve( monorepoRoot, 'tools/docker/.env' ) ) )
249+
: {} ),
250+
HOST_CWD: monorepoRoot,
251+
PHP_VERSION: versionVars.PHP_VERSION,
252+
COMPOSER_VERSION: versionVars.COMPOSER_VERSION,
253+
NODE_VERSION: versionVars.NODE_VERSION,
254+
PNPM_VERSION: versionVars.PNPM_VERSION,
255+
COMPOSE_PROJECT_NAME: projectName,
256+
PORT_WORDPRESS: args.includes( '--type=e2e' ) ? '8889' : '80',
257+
};
258+
259+
// Build the list of compose files to use
260+
const composeFiles = [
261+
'-f',
262+
resolve( monorepoRoot, 'tools/docker/docker-compose.yml' ),
263+
'-f',
264+
resolve( monorepoRoot, 'tools/docker/compose-mappings.built.yml' ),
265+
'-f',
266+
resolve( monorepoRoot, 'tools/docker/compose-extras.built.yml' ),
267+
];
208268

209-
// Get project name (from docker.js)
210-
const projectName = args.includes( '--type=e2e' ) ? 'jetpack_e2e' : 'jetpack_dev';
211-
212-
// Load versions from .github/versions.sh
213-
const versionsPath = resolve( monorepoRoot, '.github/versions.sh' );
214-
const versions = fs.readFileSync( versionsPath, 'utf8' );
215-
const versionVars = {};
216-
versions.split( '\n' ).forEach( line => {
217-
const match = line.match( /^([A-Z_]+)=(.+)$/ );
218-
if ( match ) {
219-
versionVars[ match[ 1 ] ] = match[ 2 ].replace( /['"]/g, '' );
220-
}
221-
} );
222-
223-
// Build environment variables (from docker.js)
224-
const envVars = {
225-
...process.env,
226-
// Load from default.env
227-
...( fs.existsSync( resolve( monorepoRoot, 'tools/docker/default.env' ) )
228-
? dotenv.parse( fs.readFileSync( resolve( monorepoRoot, 'tools/docker/default.env' ) ) )
229-
: {} ),
230-
// Load from .env if it exists
231-
...( fs.existsSync( resolve( monorepoRoot, 'tools/docker/.env' ) )
232-
? dotenv.parse( fs.readFileSync( resolve( monorepoRoot, 'tools/docker/.env' ) ) )
233-
: {} ),
234-
HOST_CWD: monorepoRoot,
235-
PHP_VERSION: versionVars.PHP_VERSION,
236-
COMPOSER_VERSION: versionVars.COMPOSER_VERSION,
237-
NODE_VERSION: versionVars.NODE_VERSION,
238-
PNPM_VERSION: versionVars.PNPM_VERSION,
239-
COMPOSE_PROJECT_NAME: projectName,
240-
PORT_WORDPRESS: args.includes( '--type=e2e' ) ? '8889' : '80',
241-
};
242-
243-
// Build the list of compose files to use
244-
const composeFiles = [
245-
'-f',
246-
resolve( monorepoRoot, 'tools/docker/docker-compose.yml' ),
247-
'-f',
248-
resolve( monorepoRoot, 'tools/docker/compose-mappings.built.yml' ),
249-
'-f',
250-
resolve( monorepoRoot, 'tools/docker/compose-extras.built.yml' ),
251-
];
252-
253-
const result = spawnSync( 'docker', [ 'compose', ...composeFiles, ...args.slice( 1 ) ], {
254-
stdio: 'inherit',
255-
shell: true,
256-
cwd: resolve( monorepoRoot, 'tools/docker' ),
257-
env: envVars,
258-
} );
269+
const result = spawnSync( 'docker', [ 'compose', ...composeFiles, ...args.slice( 1 ) ], {
270+
stdio: 'inherit',
271+
shell: true,
272+
cwd: resolve( monorepoRoot, 'tools/docker' ),
273+
env: envVars,
274+
} );
259275

260-
if ( result.status !== 0 ) {
261-
throw new Error( `Docker command failed with status ${ result.status }` );
276+
if ( result.status !== 0 ) {
277+
throw new Error( `Docker command failed with status ${ result.status }` );
278+
}
279+
return;
262280
}
263-
return;
264281
}
265282

266283
// Run the monorepo script with the original arguments

0 commit comments

Comments
 (0)