Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support global installation #31

Merged
merged 1 commit into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions bin/serve
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,64 @@ use SilverStripe\Core\Environment;

define('BASE_URL', '');

// Parse command-line options and env vars
$options = getopt('', ['host:', 'port:', 'hash:', 'bootstrap-file:', 'open', 'path:']);

// Autoload
$paths = [
__DIR__ . "/../autoload.php", // in ./vendor/bin
__DIR__ . "/../vendor/autoload.php", // in ./bin
__DIR__ . "/../../../autoload.php", // in ./vendor/silverstripe/serve/bin
];


if (!empty($options['path'])) {
$options['path'] = realpath($options['path']);
$dir = rtrim($options['path'], '/');

if (!is_dir($dir)) {
throw new Exception('--path is not a valid directory');
}

$paths[] = $dir . '/../autoload.php';
}

foreach ($paths as $path) {
if (file_exists($path)) {
require_once $path;
break;
}
}

// Parse command-line options and env vars
$options = getopt(null, [ 'host:', 'port:', 'hash:', 'bootstrap-file:', 'open' ]);

if (!empty($options['host'])) {
$host = $options['host'];
$host = $options['host'];
} elseif (Environment::getEnv('SERVE_HOST')) {
$host = Environment::getEnv('SERVE_HOST');
$host = Environment::getEnv('SERVE_HOST');
} else {
$host = '0.0.0.0';
$host = '0.0.0.0';
}

if (!empty($options['port'])) {
$port = $options['port'];
$port = $options['port'];
} elseif (Environment::getEnv('SERVE_PORT')) {
$port = Environment::getEnv('SERVE_PORT');
$port = Environment::getEnv('SERVE_PORT');
} else {
$port = '8080';
$port = '8080';
}

if (!empty($options['bootstrap-file'])) {
$bootstrapFile = $options['bootstrap-file'];
$bootstrapFile = $options['bootstrap-file'];
} elseif (Environment::getEnv('SERVE_BOOTSTRAP_FILE')) {
$bootstrapFile = Environment::getEnv('SERVE_BOOTSTRAP_FILE');
$bootstrapFile = Environment::getEnv('SERVE_BOOTSTRAP_FILE');
} else {
$bootstrapFile = null;
$bootstrapFile = null;
}

$path = defined('PUBLIC_PATH') ? PUBLIC_PATH : BASE_PATH;
if (!empty($options['path'])) {
$path = $options['path'];
} else {
$path = defined('PUBLIC_PATH') ? PUBLIC_PATH : BASE_PATH;
}

$factory = new SilverStripe\Serve\ServerFactory($path);

Expand All @@ -61,7 +78,7 @@ print "Server running at " . $server->getURL() . " for " . $path . "...\n";
// Check if the user wants to open the page in their browser
if (isset($options['open'])) {
// Simple function to detect if a command exists on *nix system
$command_exist = function($cmd) {
$command_exist = function ($cmd) {
$return = shell_exec(sprintf("which %s", escapeshellarg($cmd)));
return !empty($return);
};
Expand Down
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,16 @@ launchServer allows the following options to be passed to it:
* **preferredPort:** The preferred port. If this port isn't available, the next
highest one will be used
* **bootstrapFile:** The bootstrap file, as described above

## Using as global

```
composer global require silverstripe/serve
```

Then you can run `serve` with the `--path` argument

```
serve --path=.
```