Browse Source

Merge pull request #8412 from polarathene/fix/improve-createproject-ux

Fix: Improve the create-project UX
Jordi Boggiano 5 years ago
parent
commit
92cc5a821f
1 changed files with 18 additions and 5 deletions
  1. 18 5
      src/Composer/Command/CreateProjectCommand.php

+ 18 - 5
src/Composer/Command/CreateProjectCommand.php

@@ -279,6 +279,24 @@ EOT
             $packageVersion = $requirements[0]['version'];
         }
 
+        // if no directory was specified, use the 2nd part of the package name
+        if (null === $directory) {
+            $parts = explode("/", $name, 2);
+            $directory = array_pop($parts);
+        }
+
+        $directory = getcwd() . DIRECTORY_SEPARATOR . $directory;
+        $io->writeError('<info>Creating a "' . $packageName . '" project at "' . $directory . '"</info>');
+
+        $fs = new Filesystem();
+        if (file_exists($directory)) {
+            if (!is_dir($directory)) {
+                throw new \InvalidArgumentException('Cannot create project directory at "'.$directory.'", it exists as a file.');
+            } elseif (!$fs->isDirEmpty($directory)) {
+                throw new \InvalidArgumentException('Project directory "'.$directory.'" is not empty.');
+            }
+        }
+
         if (null === $stability) {
             if (preg_match('{^[^,\s]*?@('.implode('|', array_keys(BasePackage::$stabilities)).')$}i', $packageVersion, $match)) {
                 $stability = $match[1];
@@ -320,11 +338,6 @@ EOT
             throw new \InvalidArgumentException($errorMessage .'.');
         }
 
-        if (null === $directory) {
-            $parts = explode("/", $name, 2);
-            $directory = getcwd() . DIRECTORY_SEPARATOR . array_pop($parts);
-        }
-
         // handler Ctrl+C for unix-like systems
         if (function_exists('pcntl_async_signals')) {
             @mkdir($directory, 0777, true);