cli: minor cleanup

Signed-off-by: dzdidi <deniszalessky@gmail.com>
This commit is contained in:
dzdidi
2024-02-11 16:17:20 +00:00
parent 32f72eeb95
commit af65558870

View File

@@ -27,16 +27,15 @@ program
.option('-s, --share [branch]', 'share the repo as public, default false, default branch is current', '') .option('-s, --share [branch]', 'share the repo as public, default false, default branch is current', '')
.action(async (p, options) => { .action(async (p, options) => {
const fullPath = path.resolve(p) const fullPath = path.resolve(p)
if (!fs.existsSync(path.join(fullPath, '.git'))) { checkIfGitRepo(fullPath)
console.error('Not a git repo')
process.exit(1)
}
const name = fullPath.split(path.sep).pop() const name = fullPath.split(path.sep).pop()
if ((home.isInitialized(name))) { if ((home.isInitialized(name))) {
console.error(`${name} is already initialized`) console.error(`${name} is already initialized`)
try {
await git.addRemote(name) await git.addRemote(name)
console.log(`Added git remote for "${name}" as "pear"`) console.log(`Added git remote for "${name}" as "pear"`)
} catch (e) { }
process.exit(1) process.exit(1)
} }
@@ -58,12 +57,7 @@ program
branchToShare = options.share branchToShare = options.share
} }
if (options.share) { if (options.share) share(name, branchToShare)
try { home.shareAppFolder(name) } catch (e) { }
try { acl.setACL(name) } catch (e) { }
try { await git.push(branchToShare) } catch (e) { }
console.log(`Shared "${name}" project, ${branchToShare} branch`)
}
}) })
program program
@@ -74,10 +68,7 @@ program
.option('-p, --path [p]', 'path to the repo', '.') .option('-p, --path [p]', 'path to the repo', '.')
.action(async (options) => { .action(async (options) => {
const fullPath = path.resolve(options.path) const fullPath = path.resolve(options.path)
if (!fs.existsSync(path.join(fullPath, '.git'))) { checkIfGitRepo(fullPath)
console.error('Not a git repo')
process.exit(1)
}
const name = fullPath.split(path.sep).pop() const name = fullPath.split(path.sep).pop()
if (!home.isInitialized(name)) { if (!home.isInitialized(name)) {
@@ -87,11 +78,7 @@ program
const currentBranch = await git.getCurrentBranch() const currentBranch = await git.getCurrentBranch()
const branchToShare = options.branch || currentBranch const branchToShare = options.branch || currentBranch
try { home.shareAppFolder(name) } catch (e) { } share(name, branchToShare, options)
try { acl.setACL(name, { visibility: options.visibility }) } catch (e) { }
try { await git.push(branchToShare) } catch (e) { }
console.log(`Shared "${name}" project, ${branchToShare} branch, as ${options.visibility} repo`)
return
}) })
program program
@@ -106,7 +93,6 @@ program
} else { } else {
localBranchProtectionRules(a, b, p, options) localBranchProtectionRules(a, b, p, options)
} }
return
}) })
@@ -132,10 +118,7 @@ program
.addArgument(new commander.Argument('[p]', 'path to the repo').default('.')) .addArgument(new commander.Argument('[p]', 'path to the repo').default('.'))
.action((p, options) => { .action((p, options) => {
const fullPath = path.resolve(p) const fullPath = path.resolve(p)
if (!fs.existsSync(path.join(fullPath, '.git'))) { checkIfGitRepo(fullPath)
console.error('Not a git repo')
process.exit(1)
}
const name = fullPath.split(path.sep).pop() const name = fullPath.split(path.sep).pop()
if ((home.isInitialized(name))) { if ((home.isInitialized(name))) {
@@ -225,10 +208,7 @@ program
function localBranchProtectionRules(a, b, p, options) { function localBranchProtectionRules(a, b, p, options) {
const fullPath = path.resolve(p) const fullPath = path.resolve(p)
if (!fs.existsSync(path.join(fullPath, '.git'))) { checkIfGitRepo(fullPath)
console.error('Not a git repo')
process.exit(1)
}
const name = fullPath.split(path.sep).pop() const name = fullPath.split(path.sep).pop()
if (!home.isInitialized(name)) { if (!home.isInitialized(name)) {
@@ -251,10 +231,7 @@ function localBranchProtectionRules(a, b, p, options) {
function localACL(a, u, p, options) { function localACL(a, u, p, options) {
const fullPath = path.resolve(p) const fullPath = path.resolve(p)
if (!fs.existsSync(path.join(fullPath, '.git'))) { checkIfGitRepo(fullPath)
console.error('Not a git repo')
process.exit(1)
}
const name = fullPath.split(path.sep).pop() const name = fullPath.split(path.sep).pop()
if (!home.isInitialized(name)) { if (!home.isInitialized(name)) {
@@ -318,6 +295,27 @@ async function remoteBranchProtectionRules(a, b, p, options) {
async function remoteACL(a, b, p, options) { async function remoteACL(a, b, p, options) {
} }
function checkIfGitRepo(p) {
if (!fs.existsSync(path.join(p, '.git'))) {
console.error('Not a git repo')
process.exit(1)
}
}
function share(name, branchToShare, options) {
let aclOptions
let message = `Shared "${name}" project, ${branchToShare} branch`)
if (options?.visibility) {
aclOptions = { visibility: options.visibility }
message = `${message}, as ${options.visibility} repo`
}
try { home.shareAppFolder(name) } catch (e) { }
try { acl.setACL(name, aclOptions) } catch (e) { }
try { await git.push(branchToShare) } catch (e) { }
console.log(message)
}
function logBranches(name) { function logBranches(name) {
const repoACL = acl.getACL(name) const repoACL = acl.getACL(name)
console.log('Visibility:', '\t', repoACL.visibility) console.log('Visibility:', '\t', repoACL.visibility)