Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#!/usr/bin/env node | ||
|
||
const { Command } = require('commander'); | ||
const packageJson = require('../package.json'); | ||
const fs = require('fs'); | ||
const inquirer = require('inquirer'); | ||
const os = require('os'); | ||
const path = require('path'); | ||
|
||
const program = new Command(); | ||
|
||
const configFileLocation = path.resolve(os.homedir(), '.bitbucket-multi-repo-management.json'); | ||
|
||
let config; | ||
let hasExistingConfig; | ||
|
||
try { | ||
config = fs.readFileSync(configFileLocation); | ||
config = JSON.parse(config); | ||
hasExistingConfig = true; | ||
} catch (e) { | ||
config = {}; | ||
hasExistingConfig = false; | ||
} | ||
|
||
console.log(config); | ||
|
||
program | ||
.name(packageJson.name) | ||
.version(packageJson.version) | ||
.description('A node.js console application that manages multiple git repositories in Bitbucket Cloud via the Bitbucket API.') | ||
|
||
.command('init') | ||
.action(async() => { | ||
|
||
if (hasExistingConfig) { | ||
const existingConfigAnswers = await inquirer.prompt([ | ||
{ | ||
type: 'confirm', | ||
name: 'shouldOverwriteConfig', | ||
message: `Existing configuration file at ${configFileLocation}. Would you like to overwrite it?`, | ||
default: false, | ||
}, | ||
]); | ||
|
||
if (!existingConfigAnswers.shouldOverwriteConfig) { | ||
return; | ||
} | ||
} | ||
|
||
console.log(` | ||
Note: Do not use your own bitbucket password. Create an app password with repositories read + write access. | ||
To setup a bitbucket app password, checkout the following documentation: | ||
https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/ | ||
`); | ||
|
||
const answers = await inquirer.prompt([ | ||
{ | ||
type: 'input', | ||
name: 'username', | ||
message: 'Bitbucket Username:', | ||
validate: (input) => { | ||
if (!input.trim()) { | ||
return 'Username cannot be empty.'; | ||
} | ||
return true; | ||
}, | ||
}, | ||
{ | ||
type: 'password', | ||
name: 'appPassword', | ||
message: 'App Password:', | ||
validate: (input) => { | ||
if (!input.trim()) { | ||
return 'App password cannot be empty.'; | ||
} | ||
return true; | ||
}, | ||
}, | ||
]); | ||
|
||
config.auth = config.auth || {}; | ||
|
||
config.auth.username = answers.username.trim(); | ||
config.auth.appPassword = answers.appPassword.trim(); | ||
|
||
fs.writeFileSync(configFileLocation, JSON.stringify(config)); | ||
|
||
if (!hasExistingConfig) { | ||
console.log(`Created ${configFileLocation}.`); | ||
} else { | ||
console.log(`Updated ${configFileLocation}.`); | ||
} | ||
}) | ||
; | ||
|
||
program.parse(); |