Skip to content

Commit 7a207c7

Browse files
committed
refactor: address review issues
- $npm.install replaces $childProcess.exec("npm i") - extract prompts to separate functions - $childProcess.spawnFromEvent replaces $childProcess.exec - formatting - update tests
1 parent 9da66ac commit 7a207c7

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

lib/commands/plugin/create-plugin.ts

+37-23
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import { isInteractive } from "../../common/helpers";
44
export class CreatePluginCommand implements ICommand {
55
public allowedParameters: ICommandParameter[] = [];
66
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)";
88
constructor(private $options: IOptions,
99
private $errors: IErrors,
1010
private $terminalSpinnerService: ITerminalSpinnerService,
1111
private $logger: ILogger,
1212
private $pacoteService: IPacoteService,
1313
private $fs: IFileSystem,
1414
private $childProcess: IChildProcess,
15-
private $prompter: IPrompter) { }
15+
private $prompter: IPrompter,
16+
private $npm: INodePackageManager) { }
1617

1718
public async execute(args: string[]): Promise<void> {
1819
const pluginRepoName = args[0];
@@ -43,37 +44,26 @@ export class CreatePluginCommand implements ICommand {
4344
const cwd = path.join(projectDir, "src");
4445
try {
4546
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);
4749
} finally {
4850
spinner.stop();
4951
}
5052

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);
6855

6956
if (!isInteractive() && (!config.username || !config.pluginName)) {
7057
this.$logger.printMarkdown("Using default values for Github user and/or plugin name since your shell is not interactive.");
7158
}
7259

73-
const params = `gitHubUsername=${gitHubUsername} pluginName=${pluginNameSource} initGit=y`;
7460
// 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+
}
7767
}
7868

7969
private async downloadPackage(projectDir: string): Promise<void> {
@@ -95,6 +85,30 @@ export class CreatePluginCommand implements ICommand {
9585
spinner.stop();
9686
}
9787
}
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+
}
98112
}
99113

100114
$injector.registerCommand(["plugin|create"], CreatePluginCommand);

lib/options.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export class Options extends commonOptionsLibPath.OptionsBase {
4040
clean: { type: OptionType.Boolean },
4141
watch: { type: OptionType.Boolean, default: true },
4242
background: { type: OptionType.String },
43-
username: {type: OptionType.String},
44-
pluginName: {type: OptionType.String}
43+
username: { type: OptionType.String },
44+
pluginName: { type: OptionType.String }
4545
},
4646
$errors, $staticConfig, $settingsService);
4747

test/plugin-create.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function createTestInjector() {
1818
testInjector.register("childProcess", stubs.ChildProcessStub);
1919
testInjector.register("prompter", new stubs.PrompterStub());
2020
testInjector.register("fs", stubs.FileSystemStub);
21+
testInjector.register("npm", stubs.NpmInstallationManagerStub);
2122
testInjector.register("options", {
2223
username: undefined,
2324
pluginName: undefined

0 commit comments

Comments
 (0)