|
#!/bin/sh |
|
':' //; exec "$(command -v nodejs || command -v node)" "$0" "$@" |
|
// ^^^ Lovely polyglot script to permit usage via node _or_ via bash: see |
|
// http://unix.stackexchange.com/questions/65235/universal-node-js-shebang |
|
|
|
const { execSync } = require( 'child_process' ); |
|
const { readFileSync, writeFileSync } = require( 'fs' ); |
|
const { join } = require( 'path' ); |
|
|
|
console.log( '' ); |
|
|
|
// Ensure we have a valid target checkout type. |
|
// ============================================================================ |
|
|
|
const target = ( process.argv[2] || '' ).toLowerCase().trim(); |
|
|
|
if ( ! target || ! [ 'svn', 'git' ].includes( target ) ) { |
|
console.log( 'Missing checkout type argument! You must specify one of ' ); |
|
console.log( ' switch-wp-vm-checkout git' ); |
|
console.log( ' switch-wp-vm-checkout svn' ); |
|
console.log( '\nAborting...' ); |
|
process.exit( 1 ); |
|
} |
|
|
|
// Read in the VM configuration file. |
|
// ============================================================================ |
|
|
|
console.log( `Switching WordPress Core VM to ${ target } checkout...` ); |
|
|
|
const vmPath = '/wp/core-vm'; |
|
const vmConfigPath = join( vmPath, 'config.local.yaml' ); |
|
|
|
process.chdir( vmPath ); |
|
|
|
const config = readFileSync( vmConfigPath ).toString(); |
|
|
|
// Comment out the lines in the config referencing the undesired checkout type, |
|
// and un-comment those referencing the target checkout type. |
|
// ============================================================================ |
|
|
|
const wpPathTemplate = ( checkoutType, commented ) => |
|
`${ commented ? '#' : '' }wp: ../${ checkoutType }/build`; |
|
|
|
const testerTemplate = ( checkoutType, commented ) => |
|
`${ commented ? '#' : '' }../${ checkoutType }: /vagrant/extensions/tester/wpdevel`; |
|
|
|
const addComment = ( templateFn, target ) => ( { |
|
from: templateFn( target, false ), |
|
to: templateFn( target, true ), |
|
} ); |
|
const unComment = ( templateFn, target ) => ( { |
|
from: templateFn( target, true ), |
|
to: templateFn( target, false ), |
|
} ); |
|
|
|
const transformations = target => { |
|
const origin = target === 'git' ? 'svn' : 'git'; |
|
return [ |
|
// Swap the WP path |
|
unComment( wpPathTemplate, target ), |
|
addComment( wpPathTemplate, origin ), |
|
// Swap the test directory path |
|
unComment( testerTemplate, target ), |
|
addComment( testerTemplate, origin ), |
|
]; |
|
}; |
|
|
|
const updatedConfig = transformations( target ) |
|
.reduce( |
|
( result, transform ) => result.replace( transform.from, transform.to ), |
|
config |
|
) |
|
// Defend against replacements being made multiple times by mistake, which could |
|
// yield multiple comment `#`s and prevent a successful switch back to the other |
|
// VCS provider on a subsequent run. |
|
.replace( /(\n\s*)#+/g, '$1#' ); |
|
|
|
writeFileSync( vmConfigPath, updatedConfig ); |
|
|
|
console.log( `Virtual Machine configuration file updated to use ${ target } checkout.` ); |
|
|
|
// Reload the virtual machine. |
|
// ============================================================================ |
|
|
|
console.log( '\nReloading the Virtual Machine...' ); |
|
|
|
// execSync( 'vagrant reload' ); |
|
|
|
console.log( 'Done!' ); |
|
|
|
// Done! |
|
// ============================================================================ |
|
|
|
console.log( '\n\n!! Remember to run `grunt` when you apply a patch !!' ); |
|
console.log( ' Happy coding <3\n' ); |