This commit is contained in:
thosakwe 2016-12-23 23:42:55 -05:00
parent 4195927822
commit 6b36b314b7
6 changed files with 112 additions and 15 deletions

View file

@ -13,6 +13,7 @@ main(List<String> args) {
runner runner
..addCommand(new DoctorCommand()) ..addCommand(new DoctorCommand())
..addCommand(new KeyCommand())
..addCommand(new ServiceCommand()) ..addCommand(new ServiceCommand())
..addCommand(new InitCommand()) ..addCommand(new InitCommand())
..addCommand(new TestCommand()) ..addCommand(new TestCommand())

View file

@ -1,6 +1,7 @@
library angel_cli.commands; library angel_cli.commands;
export "doctor.dart"; export "doctor.dart";
export "key.dart";
export "init.dart"; export "init.dart";
export "plugin.dart"; export "plugin.dart";
export "service.dart"; export "service.dart";

View file

@ -2,8 +2,11 @@ import "dart:convert";
import "dart:io"; import "dart:io";
import "package:args/command_runner.dart"; import "package:args/command_runner.dart";
import "package:console/console.dart"; import "package:console/console.dart";
import 'package:random_string/random_string.dart' as rs;
import 'key.dart';
class InitCommand extends Command { class InitCommand extends Command {
final KeyCommand _key = new KeyCommand();
final TextPen _pen = new TextPen(); final TextPen _pen = new TextPen();
@override @override
@ -20,10 +23,20 @@ class InitCommand extends Command {
Directory projectDir = new Directory( Directory projectDir = new Directory(
argResults.arguments.isEmpty ? "." : argResults.arguments[0]); argResults.arguments.isEmpty ? "." : argResults.arguments[0]);
print("Creating new Angel project in ${projectDir.absolute.path}..."); print("Creating new Angel project in ${projectDir.absolute.path}...");
await _cloneRepo(projectDir);_pen.green(); await _cloneRepo(projectDir);
_pen("${Icon.CHECKMARK} Successfully initialized Angel project. Now running pub get..."); _pen.green();
_pen(
"${Icon.CHECKMARK} Successfully initialized Angel project. Now running pub get...");
_pen(); _pen();
await _pubGet(projectDir); await _pubGet(projectDir);
var secret = rs.randomAlphaNumeric(32);
print('Generated new JWT secret: $secret');
await _key.changeSecret(
new File.fromUri(projectDir.uri.resolve('config/default.yaml')),
secret);
await _key.changeSecret(
new File.fromUri(projectDir.uri.resolve('config/production.yaml')),
secret);
} }
_cloneRepo(Directory projectDir) async { _cloneRepo(Directory projectDir) async {
@ -55,8 +68,7 @@ class InitCommand extends Command {
var gitDir = new Directory.fromUri(projectDir.uri.resolve(".git")); var gitDir = new Directory.fromUri(projectDir.uri.resolve(".git"));
if (await gitDir.exists()) if (await gitDir.exists()) await gitDir.delete(recursive: true);
await gitDir.delete(recursive: true);
} catch (e) { } catch (e) {
print(e); print(e);
_pen.red(); _pen.red();
@ -67,7 +79,8 @@ class InitCommand extends Command {
} }
_pubGet(Directory projectDir) async { _pubGet(Directory projectDir) async {
var pub = await Process.start("pub", ["get"], workingDirectory: projectDir.absolute.path); var pub = await Process.start("pub", ["get"],
workingDirectory: projectDir.absolute.path);
pub.stdout.pipe(stdout); pub.stdout.pipe(stdout);
pub.stderr.pipe(stderr); pub.stderr.pipe(stderr);
var code = await pub.exitCode; var code = await pub.exitCode;

37
lib/src/commands/key.dart Normal file
View file

@ -0,0 +1,37 @@
import 'dart:convert';
import 'dart:io';
import 'package:args/command_runner.dart';
import 'package:random_string/random_string.dart' as rs;
class KeyCommand extends Command {
@override
String get name => 'key';
@override
String get description => 'Generates a new `angel_auth`key.';
@override
run() async {
var secret = rs.randomAlphaNumeric(32);
print('Generated new JWT secret: $secret');
await changeSecret(new File('config/default.yaml'), secret);
await changeSecret(new File('config/production.yaml'), secret);
}
changeSecret(File file, String secret) async {
if (await file.exists()) {
var sink = await file.openWrite();
await for (var chunk
in await file.openRead().transform(UTF8.decoder)) {
var lines = chunk.split('\n');
for (String line in lines) {
if (line.contains('jwt_secret:')) {
sink.writeln('jwt_secret: $secret');
} else sink.writeln(line);
}
}
}
}
}

View file

@ -1,7 +1,11 @@
import 'dart:io'; import 'dart:io';
import 'package:args/command_runner.dart'; import 'package:args/command_runner.dart';
import 'package:watcher/watcher.dart';
import 'package:yaml/yaml.dart'; import 'package:yaml/yaml.dart';
Process server;
bool watching = false;
class StartCommand extends Command { class StartCommand extends Command {
@override @override
String get name => 'start'; String get name => 'start';
@ -11,14 +15,38 @@ class StartCommand extends Command {
'Runs any `start` scripts, and then runs the server.'; 'Runs any `start` scripts, and then runs the server.';
StartCommand() : super() { StartCommand() : super() {
argParser.addFlag('production', argParser
..addFlag('production',
help: 'Starts the server in production mode.', help: 'Starts the server in production mode.',
negatable: false, negatable: false,
defaultsTo: false); defaultsTo: false)
..addFlag('watch',
abbr: 'w',
help: 'Restart the server on file changes.',
defaultsTo: true);
} }
@override @override
run() async { run() async {
if (argResults['watch']) {
new DirectoryWatcher('bin').events.listen((_) async => start());
new DirectoryWatcher('config').events.listen((_) async => start());
new DirectoryWatcher('lib').events.listen((_) async => start());
}
return await start();
}
start() async {
bool isNew = true;
if (server != null) {
isNew = false;
if (!server.kill()) {
throw new Exception('Could not kill existing server process.');
}
}
final pubspec = new File('pubspec.yaml'); final pubspec = new File('pubspec.yaml');
if (await pubspec.exists()) { if (await pubspec.exists()) {
@ -43,16 +71,30 @@ class StartCommand extends Command {
} }
} }
if (isNew)
print('Starting server...'); print('Starting server...');
else
print('Changes detected - restarting server...');
final env = {}; final env = {};
if (argResults['production']) env['ANGEL_ENV'] = 'production'; if (argResults['production']) env['ANGEL_ENV'] = 'production';
final server = await Process.start(Platform.executable, ['bin/server.dart'], server = await Process.start(Platform.executable, ['bin/server.dart'],
environment: env); environment: env);
try {
if (isNew) {
server.stdout.pipe(stdout); server.stdout.pipe(stdout);
server.stderr.pipe(stderr); server.stderr.pipe(stderr);
}
} catch (e) {
print(e);
}
if (!isNew) {
print('Successfully restarted server.');
}
exitCode = await server.exitCode; exitCode = await server.exitCode;
} }

View file

@ -1,5 +1,5 @@
name: angel_cli name: angel_cli
version: 1.0.0-dev+11 version: 1.0.0-dev+12
description: Command-line tools for the Angel framework. description: Command-line tools for the Angel framework.
environment: environment:
sdk: ">=1.19.0" sdk: ">=1.19.0"
@ -9,6 +9,9 @@ executables:
angel: angel angel: angel
dependencies: dependencies:
args: ^0.13.7 args: ^0.13.7
id: ^1.0.0
console: ^2.2.3 console: ^2.2.3
glob: ^1.1.0
id: ^1.0.0
random_string: ^0.0.1
watcher: ^0.9.7
yaml: ^2.0.0 yaml: ^2.0.0