|
| 1 | +import * as path from "path"; |
| 2 | +import { isInteractive } from "../../common/helpers"; |
| 3 | + |
| 4 | +export class CreatePluginCommand implements ICommand { |
| 5 | + public allowedParameters: ICommandParameter[] = []; |
| 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 = "What will be the name of your plugin?\n(use lowercase characters and dashes only)"; |
| 8 | + constructor(private $options: IOptions, |
| 9 | + private $errors: IErrors, |
| 10 | + private $terminalSpinnerService: ITerminalSpinnerService, |
| 11 | + private $logger: ILogger, |
| 12 | + private $pacoteService: IPacoteService, |
| 13 | + private $fs: IFileSystem, |
| 14 | + private $childProcess: IChildProcess, |
| 15 | + private $prompter: IPrompter, |
| 16 | + private $npm: INodePackageManager) { } |
| 17 | + |
| 18 | + public async execute(args: string[]): Promise<void> { |
| 19 | + const pluginRepoName = args[0]; |
| 20 | + const pathToProject = this.$options.path; |
| 21 | + const selectedPath = path.resolve(pathToProject || "."); |
| 22 | + const projectDir = path.join(selectedPath, pluginRepoName); |
| 23 | + |
| 24 | + this.$logger.printMarkdown("Downloading the latest version of NativeScript Plugin Seed..."); |
| 25 | + await this.downloadPackage(projectDir); |
| 26 | + |
| 27 | + this.$logger.printMarkdown("Executing initial plugin configuration script..."); |
| 28 | + await this.setupSeed(projectDir, pluginRepoName); |
| 29 | + |
| 30 | + this.$logger.printMarkdown("Solution for `%s` was successfully created.", pluginRepoName); |
| 31 | + } |
| 32 | + |
| 33 | + public async canExecute(args: string[]): Promise<boolean> { |
| 34 | + if (!args[0]) { |
| 35 | + this.$errors.fail("You must specify the plugin repository name."); |
| 36 | + } |
| 37 | + |
| 38 | + return true; |
| 39 | + } |
| 40 | + |
| 41 | + private async setupSeed(projectDir: string, pluginRepoName: string): Promise<void> { |
| 42 | + const config = this.$options; |
| 43 | + const spinner = this.$terminalSpinnerService.createSpinner(); |
| 44 | + const cwd = path.join(projectDir, "src"); |
| 45 | + try { |
| 46 | + spinner.start(); |
| 47 | + const npmOptions: any = { silent: true }; |
| 48 | + await this.$npm.install(cwd, cwd, npmOptions); |
| 49 | + } finally { |
| 50 | + spinner.stop(); |
| 51 | + } |
| 52 | + |
| 53 | + const gitHubUsername = await this.getGitHubUsername(config.username); |
| 54 | + const pluginNameSource = await this.getPluginNameSource(config.pluginName, pluginRepoName); |
| 55 | + |
| 56 | + if (!isInteractive() && (!config.username || !config.pluginName)) { |
| 57 | + this.$logger.printMarkdown("Using default values for Github user and/or plugin name since your shell is not interactive."); |
| 58 | + } |
| 59 | + |
| 60 | + // run postclone script manually and kill it if it takes more than 10 sec |
| 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 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private async downloadPackage(projectDir: string): Promise<void> { |
| 70 | + this.$fs.createDirectory(projectDir); |
| 71 | + |
| 72 | + if (this.$fs.exists(projectDir) && !this.$fs.isEmptyDir(projectDir)) { |
| 73 | + this.$errors.fail("Path already exists and is not empty %s", projectDir); |
| 74 | + } |
| 75 | + |
| 76 | + const spinner = this.$terminalSpinnerService.createSpinner(); |
| 77 | + const packageToInstall = "https://github.com/NativeScript/nativescript-plugin-seed/archive/master.tar.gz"; |
| 78 | + try { |
| 79 | + spinner.start(); |
| 80 | + await this.$pacoteService.extractPackage(packageToInstall, projectDir); |
| 81 | + } catch (err) { |
| 82 | + this.$fs.deleteDirectory(projectDir); |
| 83 | + throw err; |
| 84 | + } finally { |
| 85 | + spinner.stop(); |
| 86 | + } |
| 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 | + } |
| 112 | +} |
| 113 | + |
| 114 | +$injector.registerCommand(["plugin|create"], CreatePluginCommand); |
0 commit comments