Skip to content

Commit a525f7d

Browse files
authored
Merge pull request #102 from GeoJunkie/fix/99-overwrite-option
Add --force option to overwrite
2 parents 1d0adc4 + 4b979a8 commit a525f7d

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

features/dist-archive.feature

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,3 +494,19 @@ Feature: Generate a distribution archive of a project
494494
"""
495495
And the {RUN_DIR}/subdir/hello-world-dist.zip file should exist
496496
And the return code should be 0
497+
498+
Scenario: Do not ask for confirmation if archive file exists when using --force
499+
Given a WP install
500+
501+
When I run `wp scaffold plugin hello-world`
502+
Then the wp-content/plugins/hello-world directory should exist
503+
504+
When I run `wp dist-archive wp-content/plugins/hello-world ./hello-world-dist.zip`
505+
Then STDERR should be empty
506+
507+
When I try `wp dist-archive wp-content/plugins/hello-world ./hello-world-dist.zip --force`
508+
Then STDERR should be empty
509+
And STDOUT should contain:
510+
"""
511+
Success: Created hello-world-dist.zip
512+
"""

src/Dist_Archive_Command.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class Dist_Archive_Command {
4646
* [--create-target-dir]
4747
* : Automatically create the target directory as needed.
4848
*
49+
* [--force]
50+
* : Forces overwriting of the archive file if it already exists.
51+
*
4952
* [--plugin-dirname=<plugin-slug>]
5053
* : Set the archive extract directory name. Defaults to project directory name.
5154
*
@@ -100,20 +103,23 @@ public function __invoke( $args, $assoc_args ) {
100103
$archive_absolute_filepath = "{$destination_dir_path}/{$archive_file_name}";
101104

102105
if ( file_exists( $archive_absolute_filepath ) ) {
103-
WP_CLI::warning( 'Archive file already exists' );
104-
WP_CLI::log( $archive_absolute_filepath );
105-
$answer = \cli\prompt(
106-
'Do you want to skip or replace it with a new archive?',
107-
$default = false,
108-
$marker = ' [s/r]: '
109-
);
110-
$should_overwrite = 'r' === strtolower( $answer );
106+
$should_overwrite = Utils\get_flag_value( $assoc_args, 'force' );
107+
if ( ! $should_overwrite ) {
108+
WP_CLI::warning( 'Archive file already exists' );
109+
WP_CLI::log( $archive_absolute_filepath );
110+
$answer = \cli\prompt(
111+
'Do you want to skip or replace it with a new archive?',
112+
$default = false,
113+
$marker = ' [s/r]: '
114+
);
115+
$should_overwrite = 'r' === strtolower( $answer );
116+
}
111117
if ( ! $should_overwrite ) {
112118
WP_CLI::log( 'Skipping' . PHP_EOL );
113119
WP_CLI::log( 'Archive generation skipped.' );
114120
exit( 0 );
115121
}
116-
WP_CLI::log( 'Replacing' . PHP_EOL );
122+
WP_CLI::log( "Replacing $archive_absolute_filepath" . PHP_EOL );
117123
}
118124

119125
chdir( dirname( $source_path ) );

0 commit comments

Comments
 (0)