@@ -4,15 +4,16 @@ import { isInteractive } from "../../common/helpers";
4
4
export class CreatePluginCommand implements ICommand {
5
5
public allowedParameters : ICommandParameter [ ] = [ ] ;
6
6
public userMessage = "What is your GitHub username?\n(will be used to update the Github URLs in the plugin's package.json)" ;
7
- public nameMessage = "" ;
7
+ public nameMessage = "What will be the name of your plugin?\n(use lowercase characters and dashes only) " ;
8
8
constructor ( private $options : IOptions ,
9
9
private $errors : IErrors ,
10
10
private $terminalSpinnerService : ITerminalSpinnerService ,
11
11
private $logger : ILogger ,
12
12
private $pacoteService : IPacoteService ,
13
13
private $fs : IFileSystem ,
14
14
private $childProcess : IChildProcess ,
15
- private $prompter : IPrompter ) { }
15
+ private $prompter : IPrompter ,
16
+ private $npm : INodePackageManager ) { }
16
17
17
18
public async execute ( args : string [ ] ) : Promise < void > {
18
19
const pluginRepoName = args [ 0 ] ;
@@ -43,37 +44,26 @@ export class CreatePluginCommand implements ICommand {
43
44
const cwd = path . join ( projectDir , "src" ) ;
44
45
try {
45
46
spinner . start ( ) ;
46
- await this . $childProcess . exec ( "npm i" , { cwd : cwd } ) ;
47
+ const npmOptions : any = { silent : true } ;
48
+ await this . $npm . install ( cwd , cwd , npmOptions ) ;
47
49
} finally {
48
50
spinner . stop ( ) ;
49
51
}
50
52
51
- let gitHubUsername = config . username ;
52
- if ( ! gitHubUsername ) {
53
- gitHubUsername = "NativeScriptDeveloper" ;
54
- if ( isInteractive ( ) ) {
55
- gitHubUsername = await this . $prompter . getString ( this . userMessage , { allowEmpty : false , defaultAction : ( ) => { return gitHubUsername ; } } ) ;
56
- }
57
- }
58
-
59
- let pluginNameSource = config . pluginName ;
60
- if ( ! pluginNameSource ) {
61
- // remove nativescript- prefix for naming plugin files
62
- const prefix = 'nativescript-' ;
63
- pluginNameSource = pluginRepoName . toLowerCase ( ) . startsWith ( prefix ) ? pluginRepoName . slice ( prefix . length , pluginRepoName . length ) : pluginRepoName ;
64
- if ( isInteractive ( ) ) {
65
- pluginNameSource = await this . $prompter . getString ( this . nameMessage , { allowEmpty : false , defaultAction : ( ) => { return pluginNameSource ; } } ) ;
66
- }
67
- }
53
+ const gitHubUsername = await this . getGitHubUsername ( config . username ) ;
54
+ const pluginNameSource = await this . getPluginNameSource ( config . pluginName , pluginRepoName ) ;
68
55
69
56
if ( ! isInteractive ( ) && ( ! config . username || ! config . pluginName ) ) {
70
57
this . $logger . printMarkdown ( "Using default values for Github user and/or plugin name since your shell is not interactive." ) ;
71
58
}
72
59
73
- const params = `gitHubUsername=${ gitHubUsername } pluginName=${ pluginNameSource } initGit=y` ;
74
60
// run postclone script manually and kill it if it takes more than 10 sec
75
- const outputScript = ( await this . $childProcess . exec ( `node scripts/postclone ${ params } ` , { cwd : cwd , timeout : 10000 } ) ) ;
76
- this . $logger . printMarkdown ( outputScript ) ;
61
+ const pathToPostCloneScript = path . join ( "scripts" , "postclone" ) ;
62
+ const params = [ pathToPostCloneScript , `gitHubUsername=${ gitHubUsername } ` , `pluginName=${ pluginNameSource } ` , "initGit=y" ] ;
63
+ const outputScript = ( await this . $childProcess . spawnFromEvent ( process . execPath , params , "close" , { cwd, timeout : 10000 } ) ) ;
64
+ if ( outputScript && outputScript . stdout ) {
65
+ this . $logger . printMarkdown ( outputScript . stdout ) ;
66
+ }
77
67
}
78
68
79
69
private async downloadPackage ( projectDir : string ) : Promise < void > {
@@ -95,6 +85,30 @@ export class CreatePluginCommand implements ICommand {
95
85
spinner . stop ( ) ;
96
86
}
97
87
}
88
+
89
+ private async getGitHubUsername ( gitHubUsername : string ) {
90
+ if ( ! gitHubUsername ) {
91
+ gitHubUsername = "NativeScriptDeveloper" ;
92
+ if ( isInteractive ( ) ) {
93
+ gitHubUsername = await this . $prompter . getString ( this . userMessage , { allowEmpty : false , defaultAction : ( ) => { return gitHubUsername ; } } ) ;
94
+ }
95
+ }
96
+
97
+ return gitHubUsername ;
98
+ }
99
+
100
+ private async getPluginNameSource ( pluginNameSource : string , pluginRepoName : string ) {
101
+ if ( ! pluginNameSource ) {
102
+ // remove nativescript- prefix for naming plugin files
103
+ const prefix = 'nativescript-' ;
104
+ pluginNameSource = pluginRepoName . toLowerCase ( ) . startsWith ( prefix ) ? pluginRepoName . slice ( prefix . length , pluginRepoName . length ) : pluginRepoName ;
105
+ if ( isInteractive ( ) ) {
106
+ pluginNameSource = await this . $prompter . getString ( this . nameMessage , { allowEmpty : false , defaultAction : ( ) => { return pluginNameSource ; } } ) ;
107
+ }
108
+ }
109
+
110
+ return pluginNameSource ;
111
+ }
98
112
}
99
113
100
114
$injector . registerCommand ( [ "plugin|create" ] , CreatePluginCommand ) ;
0 commit comments