Skip to content

Commit 9a3b5e7

Browse files
committed
feat: Allow new command in non-empty dirs #14
1 parent 779c646 commit 9a3b5e7

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/Commands/Common/NewCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class NewCommand extends PWConnector {
6666
'userpass' => '',
6767
'userpass_confirm' => '',
6868
'useremail' => '',
69-
'color' => 'classic',
69+
'color' => 'classic'
7070
);
7171

7272
/**
@@ -82,6 +82,7 @@ protected function configure() {
8282
->setName('new')
8383
->setDescription('Creates a new ProcessWire project')
8484
->addArgument('directory', InputArgument::OPTIONAL, 'Directory where the new project will be created')
85+
->addOption('force', null, InputOption::VALUE_NONE, 'Force installation in an non empty directory')
8586
->addOption('dbUser', null, InputOption::VALUE_REQUIRED, 'Database user')
8687
->addOption('dbPass', null, InputOption::VALUE_OPTIONAL, 'Database password')
8788
->addOption('dbName', null, InputOption::VALUE_REQUIRED, 'Database name')
@@ -410,9 +411,10 @@ private function download($branch) {
410411
* @throws \RuntimeException if the downloaded archive could not be extracted
411412
*/
412413
private function extract() {
414+
$forceInstall = $this->input->getOption('force');
413415
$this->tools->writeBlockBasic('Preparing project...');
414416
$cfp = $this->src ? $this->src : $this->compressedFilePath;
415-
$this->downloader->extract($cfp, $this->projectDir, $this->getName());
417+
$this->downloader->extract($cfp, $this->projectDir, $this->getName(), $forceInstall);
416418

417419
return $this;
418420
}

src/Helpers/Downloader.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function download($uri, $prefix = 'pw') {
154154
* @param string $to
155155
* @param string $name
156156
*/
157-
public function extract($from, $to, $name = '') {
157+
public function extract($from, $to, $name = '', $force = false) {
158158
//var_dump($from, $to, $name);exit;
159159
$source = $name ? 'ProcessWire' : 'the module';
160160

@@ -164,12 +164,18 @@ public function extract($from, $to, $name = '') {
164164
}
165165

166166
// check for empty directory
167-
$filesInsideDir = new \FilesystemIterator($to, \FilesystemIterator::SKIP_DOTS);
168-
if (iterator_count($filesInsideDir) > 1) {
169-
throw new \RuntimeException(sprintf(
170-
"ProcessWire can't be installed because the target folder `%s` is not empty.\n" .
171-
"Use an empty directory or provide an argument where the new project will be created like `wire-cli new <dirname>`",
172-
$to));
167+
if (!$force) {
168+
$filesInsideDir = new \FilesystemIterator($to, \FilesystemIterator::SKIP_DOTS);
169+
if (iterator_count($filesInsideDir) > 1) {
170+
throw new \RuntimeException(sprintf(
171+
"ProcessWire can't be installed because the target folder `%s` is not empty.\n" .
172+
"Use an empty directory or provide an argument where the new project will be created like `wire-cli new <dirname>`.\n" .
173+
"Use option --force to proceed the installation in a non empty directory.",
174+
$to));
175+
}
176+
}
177+
else {
178+
$this->tools->writeWarning("ProcessWire will be installed in a non-empty dir (--force found on command-line).\n");
173179
}
174180

175181
try {

0 commit comments

Comments
 (0)