From 2ff974eb96fd5a17f27217e9525592895f0c9d93 Mon Sep 17 00:00:00 2001 From: Jeremy Berglund Date: Thu, 24 Jun 2021 13:36:29 -0700 Subject: [PATCH 1/5] add support for creating plugins from urls and local directories --- cmd/plugin/init.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cmd/plugin/init.js b/cmd/plugin/init.js index 9ca6c21..cac1200 100644 --- a/cmd/plugin/init.js +++ b/cmd/plugin/init.js @@ -3,7 +3,17 @@ var git = require('simple-git/promise')(); var folderExists = require('../../tools/folderExists'); var isSdkDirectory = require('../../tools/isSdkDirectory'); var rmDir = require('../../tools/rmDir'); +var fs = require('fs'); +function isURL(str) { + var pattern = new RegExp(/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/) + return str && pattern.test(str); +} + +function isDirectory(str) { + var stats = fs.statSync(str); + return stats.isDirectory(); +} function initPlugin(args) { var cwd = process.cwd(); @@ -29,7 +39,10 @@ function initPlugin(args) { console.log('\x1b[32mCreating Plugin ' + args[2] + ' with tempalte ' + args[3] + '\x1b[0m'); - git.clone('https://github.com/BuildFire/' + args[3] + 'PluginTemplate.git', targetPath) + var gitUrl = isURL(args[3]) || isDirectory(args[3]) + ? args[3] + : `https://github.com/BuildFire/${args[3]}PluginTemplate.git`; + git.clone(gitUrl, targetPath) .then(function() { rmDir(path.join(targetPath, '.git')); From 814614c14ea8d6feae8808c265e1e63598e46dde Mon Sep 17 00:00:00 2001 From: Jeremy Berglund Date: Thu, 24 Jun 2021 13:36:47 -0700 Subject: [PATCH 2/5] fix spelling --- cmd/plugin/init.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/plugin/init.js b/cmd/plugin/init.js index cac1200..4e297ad 100644 --- a/cmd/plugin/init.js +++ b/cmd/plugin/init.js @@ -37,7 +37,7 @@ function initPlugin(args) { return console.log('\x1b[31mError: Plugin folder with that name already exists'); } - console.log('\x1b[32mCreating Plugin ' + args[2] + ' with tempalte ' + args[3] + '\x1b[0m'); + console.log('\x1b[32mCreating Plugin ' + args[2] + ' with template ' + args[3] + '\x1b[0m'); var gitUrl = isURL(args[3]) || isDirectory(args[3]) ? args[3] From 687e6a6b297c6a5aa16db8799b166adc23491c9c Mon Sep 17 00:00:00 2001 From: Jeremy Berglund Date: Thu, 24 Jun 2021 13:40:51 -0700 Subject: [PATCH 3/5] whitespace --- cmd/plugin/init.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/plugin/init.js b/cmd/plugin/init.js index 4e297ad..b2a4ccd 100644 --- a/cmd/plugin/init.js +++ b/cmd/plugin/init.js @@ -39,8 +39,8 @@ function initPlugin(args) { console.log('\x1b[32mCreating Plugin ' + args[2] + ' with template ' + args[3] + '\x1b[0m'); - var gitUrl = isURL(args[3]) || isDirectory(args[3]) - ? args[3] + var gitUrl = isURL(args[3]) || isDirectory(args[3]) + ? args[3] : `https://github.com/BuildFire/${args[3]}PluginTemplate.git`; git.clone(gitUrl, targetPath) .then(function() { From 03ff52cbd68193241bd992d01829e947aa358fc5 Mon Sep 17 00:00:00 2001 From: Jeremy Berglund Date: Thu, 24 Jun 2021 14:57:48 -0700 Subject: [PATCH 4/5] add support for git urls --- cmd/plugin/init.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cmd/plugin/init.js b/cmd/plugin/init.js index b2a4ccd..d23f1ce 100644 --- a/cmd/plugin/init.js +++ b/cmd/plugin/init.js @@ -5,11 +5,16 @@ var isSdkDirectory = require('../../tools/isSdkDirectory'); var rmDir = require('../../tools/rmDir'); var fs = require('fs'); -function isURL(str) { +function isUrl(str) { var pattern = new RegExp(/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/) return str && pattern.test(str); } +function isGitUrl(str) { + var pattern = new RegExp(/(?:git|ssh|https?|git@[-\w.]+):(\/\/)?(.*?)(\.git)(\/?|\#[-\d\w._]+?)$/); + return str && pattern.test(str); +} + function isDirectory(str) { var stats = fs.statSync(str); return stats.isDirectory(); @@ -39,7 +44,7 @@ function initPlugin(args) { console.log('\x1b[32mCreating Plugin ' + args[2] + ' with template ' + args[3] + '\x1b[0m'); - var gitUrl = isURL(args[3]) || isDirectory(args[3]) + var gitUrl = isUrl(args[3]) || isGitUrl(args[3]) || isDirectory(args[3]) ? args[3] : `https://github.com/BuildFire/${args[3]}PluginTemplate.git`; git.clone(gitUrl, targetPath) From db552a34cb5dc3eea18c5773de6e003683f61b97 Mon Sep 17 00:00:00 2001 From: Jeremy Berglund Date: Thu, 24 Jun 2021 15:10:17 -0700 Subject: [PATCH 5/5] add try-catch in isDirectory fn --- cmd/plugin/init.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cmd/plugin/init.js b/cmd/plugin/init.js index d23f1ce..3b38d4b 100644 --- a/cmd/plugin/init.js +++ b/cmd/plugin/init.js @@ -16,8 +16,12 @@ function isGitUrl(str) { } function isDirectory(str) { - var stats = fs.statSync(str); - return stats.isDirectory(); + try { + var stats = fs.statSync(str); + return stats.isDirectory(); + } catch(e) { + return false; + } } function initPlugin(args) {