platform/lib/src/commands/start.dart

60 lines
1.5 KiB
Dart
Raw Normal View History

2016-12-13 17:50:53 +00:00
import 'dart:io';
import 'package:args/command_runner.dart';
import 'package:yaml/yaml.dart';
class StartCommand extends Command {
@override
String get name => 'start';
@override
String get description =>
'Runs any `start` scripts, and then runs the server.';
StartCommand() : super() {
argParser.addFlag('production',
help: 'Starts the server in production mode.',
negatable: false,
defaultsTo: false);
}
@override
run() async {
final pubspec = new File('pubspec.yaml');
if (await pubspec.exists()) {
// Run start scripts
final doc = loadYamlDocument(await pubspec.readAsString());
final scriptsNode = doc.contents['scripts'];
if (scriptsNode != null && scriptsNode.containsKey('start')) {
2016-12-21 17:34:15 +00:00
try {
var scripts =
await Process.start('pub', ['global', 'run', 'scripts', 'start']);
scripts.stdout.pipe(stdout);
scripts.stderr.pipe(stderr);
int code = await scripts.exitCode;
2016-12-13 17:50:53 +00:00
if (code != 0) {
2016-12-21 17:34:15 +00:00
throw new Exception('`scripts start` failed with exit code $code.');
2016-12-13 17:50:53 +00:00
}
2016-12-21 17:34:15 +00:00
} catch (e) {
// No scripts? No problem...
2016-12-13 17:50:53 +00:00
}
}
}
print('Starting server...');
final env = {};
if (argResults['production']) env['ANGEL_ENV'] = 'production';
final server = await Process.start(Platform.executable, ['bin/server.dart'],
environment: env);
server.stdout.pipe(stdout);
server.stderr.pipe(stderr);
exitCode = await server.exitCode;
}
}