run method

  1. @override
Future<int> run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<int> run() async {
  try {
    final config = GlacierConfig.loadFromFile();
    final destinationDir = path.join(
      Directory.current.absolute.path,
      Utils.stripRelativePath(config.destinationDirectory),
    );

    if (!Directory(destinationDir).existsSync()) {
      throw GlacierException("Run `glacier generate` before serving the content");
    }

    final port = int.tryParse(argResults!["port"] as String)!;
    final host = argResults!["host"] as String;

    final handler = shelf_static.createStaticHandler(
      destinationDir,
      defaultDocument: "index.html",
    );

    await shelf_io.serve(handler, host, port).then((_) => print("Running at http://$host:$port/"));

    return 0;
  } on GlacierException catch (e) {
    print("Error: ${e.message}");

    return 1;
  }
}