From 5eb15fcd0650327a5a977d354b1c90e89b4f710d Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sat, 14 Jul 2018 18:54:38 -0400 Subject: [PATCH] Handle String thrown differently --- bin/angel.dart | 8 ++++++-- lib/src/commands/init.dart | 4 ++-- lib/src/commands/rename.dart | 17 +++++++++++++++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/bin/angel.dart b/bin/angel.dart index cc11c968..41e6e57d 100644 --- a/bin/angel.dart +++ b/bin/angel.dart @@ -20,7 +20,11 @@ main(List args) async { ..addCommand(new MakeCommand()); return await runner.run(args).then((_) {}).catchError((exc) { - stderr.writeln("Oops, something went wrong: $exc"); - exitCode = 1; + if (exc is String) { + stdout.writeln(exc); + } else { + stderr.writeln("Oops, something went wrong: $exc"); + exitCode = 1; + } }); } diff --git a/lib/src/commands/init.dart b/lib/src/commands/init.dart index ac25ed0d..c6569baf 100644 --- a/lib/src/commands/init.dart +++ b/lib/src/commands/init.dart @@ -113,8 +113,8 @@ class InitCommand extends Command { var shouldDelete = prompts.getBool( "Directory '${projectDir.absolute.path}' already exists. Overwrite it?"); - if (shouldDelete) - throw new Exception("Chose not to overwrite existing directory."); + if (!shouldDelete) + throw "Chose not to overwrite existing directory."; else if (projectDir.absolute.uri.normalizePath().toFilePath() != Directory.current.absolute.uri.normalizePath().toFilePath()) await projectDir.delete(recursive: true); diff --git a/lib/src/commands/rename.dart b/lib/src/commands/rename.dart index 59b4c8d6..fee799eb 100644 --- a/lib/src/commands/rename.dart +++ b/lib/src/commands/rename.dart @@ -58,8 +58,21 @@ class RenameCommand extends Command { renamePubspec(Directory dir, String oldName, String newName) async { // var pubspec = await loadPubspec(dir); print(cyan.wrap('Renaming your project to `$newName.`')); - print(cyan - .wrap('Note that this does not actually modify your `pubspec.yaml`.')); + + var pubspecFile = new File.fromUri(dir.uri.resolve('pubspec.yaml')); + + if (await pubspecFile.exists()) { + var contents = await pubspecFile.readAsString(), oldContents = contents; + contents = + contents.replaceAll(new RegExp('name:\\s*$oldName'), 'name: $newName'); + + if (contents != oldContents) { + await pubspecFile.writeAsString(contents); + } + } + +// print(cyan +// .wrap('Note that this does not actually modify your `pubspec.yaml`.')); // TODO: https://github.com/dart-lang/pubspec_parse/issues/17 // var newPubspec = new Pubspec.fromJson(pubspec.toJson()..['name'] = newName); // await newPubspec.save(dir);