@@ -22,8 +22,17 @@ export class CreatePluginCommand implements ICommand {
2222 const selectedPath = path . resolve ( pathToProject || "." ) ;
2323 const projectDir = path . join ( selectedPath , pluginRepoName ) ;
2424
25- await this . downloadPackage ( selectedTemplate , projectDir ) ;
26- await this . setupSeed ( projectDir , pluginRepoName ) ;
25+ // Must be out of try catch block, because will throw error if folder alredy exists and we don't want to delete it.
26+ this . ensurePackageDir ( projectDir ) ;
27+
28+ try {
29+ await this . downloadPackage ( selectedTemplate , projectDir ) ;
30+ await this . setupSeed ( projectDir , pluginRepoName ) ;
31+ } catch ( err ) {
32+ // The call to this.ensurePackageDir() above will throw error if folder alredy exists, so it is safe to delete here.
33+ this . $fs . deleteDirectory ( projectDir ) ;
34+ throw err ;
35+ }
2736
2837 this . $logger . printMarkdown ( "Solution for `%s` was successfully created." , pluginRepoName ) ;
2938 }
@@ -66,13 +75,15 @@ export class CreatePluginCommand implements ICommand {
6675 }
6776 }
6877
69- private async downloadPackage ( selectedTemplate : string , projectDir : string ) : Promise < void > {
78+ private ensurePackageDir ( projectDir : string ) : void {
7079 this . $fs . createDirectory ( projectDir ) ;
7180
7281 if ( this . $fs . exists ( projectDir ) && ! this . $fs . isEmptyDir ( projectDir ) ) {
7382 this . $errors . fail ( "Path already exists and is not empty %s" , projectDir ) ;
7483 }
84+ }
7585
86+ private async downloadPackage ( selectedTemplate : string , projectDir : string ) : Promise < void > {
7687 if ( selectedTemplate ) {
7788 this . $logger . printMarkdown ( "Make sure your custom template is compatible with the Plugin Seed at https://github.com/NativeScript/nativescript-plugin-seed/" ) ;
7889 } else {
@@ -84,9 +95,6 @@ export class CreatePluginCommand implements ICommand {
8495 try {
8596 spinner . start ( ) ;
8697 await this . $pacoteService . extractPackage ( packageToInstall , projectDir ) ;
87- } catch ( err ) {
88- this . $fs . deleteDirectory ( projectDir ) ;
89- throw err ;
9098 } finally {
9199 spinner . stop ( ) ;
92100 }
0 commit comments