Skip to content

Commit

Permalink
Remove repo add/remove command, add cosmiconfig, update all commands …
Browse files Browse the repository at this point in the history
…with new config
  • Loading branch information
srt4rulez committed Nov 19, 2021
1 parent 23a6a92 commit 6d9822d
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 142 deletions.
54 changes: 39 additions & 15 deletions bin/console.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,56 @@ import { makeRepoCommand } from './../src/repo.js';
import { makeBranchCommand } from './../src/branch.js';
import { makeTagCommand } from './../src/tag.js';
import { URL } from 'url';
import { cosmiconfigSync } from 'cosmiconfig';

const __dirname = new URL('.', import.meta.url).pathname;

const program = new Command();
const explorer = cosmiconfigSync('bmrm', {
searchPlaces: [
'package.json',
'.bmrm',
'.bmrm.json',
'.bmrm.yaml',
'.bmrm.yml',
'.bmrm.js',
'.bmrm.cjs',
'bmrm.config.js',
'bmrm.config.cjs',
],
});

const result = explorer.search() || {};

const config = result.config || {
repositories: [],
prereleaseIdentifier: undefined,
versionPrefix: 'v',
};

const configLocation = result.filepath || '';

const configFileLocation = path.resolve(os.homedir(), '.bitbucket-multi-repo-management.json');
const authConfigFileLocation = path.resolve(os.homedir(), '.bmrm-auth.json');

let config;
let authConfig;

try {
config = fs.readFileSync(configFileLocation);
config = JSON.parse(config);
authConfig = JSON.parse(String(fs.readFileSync(authConfigFileLocation)));
} catch (error) { // eslint-disable-line no-empty
}

const packageJson = JSON.parse(fs.readFileSync(path.resolve(__dirname, './../package.json')));
const packageJson = JSON.parse(String(fs.readFileSync(path.resolve(__dirname, './../package.json'))));

const program = new Command();

program
.name('bitbucket-multi-repo-management')
.name('bmrm')
.version(packageJson.version)
.addHelpText('before', '\nBitbucket Multiple Repository Management\n')
.addHelpText('after', result.config ? `\nLoaded configuration file: ${configLocation}` : '')
.description('A node.js console application that manages multiple git repositories in Bitbucket Cloud via the Bitbucket API.')
;

program.addCommand(makeInitCommand(config, configFileLocation));
program.addCommand(makeRepoCommand(config, configFileLocation));
program.addCommand(makeBranchCommand(config, configFileLocation));
program.addCommand(makeTagCommand(config, configFileLocation));

program.parse();
.addCommand(makeInitCommand(authConfig, authConfigFileLocation))
.addCommand(makeRepoCommand(config))
.addCommand(makeBranchCommand(authConfig, config))
.addCommand(makeTagCommand(authConfig, config))
.parse()
;
40 changes: 20 additions & 20 deletions src/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import inquirer from 'inquirer';

const getCreateBranchConfigLog = (props) => `
${chalk.blue('Repositories')}
${props.repos.join('\n')}
${props.repositories.join('\n')}
${chalk.blue('Branch Info')}
From Branch: ${chalk.magenta(props.fromBranch)}
Expand All @@ -26,7 +26,7 @@ ${chalk.white.bgRed('FAILURE')} ${chalk.yellow(props.repo)}

const getDeleteBranchConfigLog = (props) => `
${chalk.blue('Repositories')}
${props.repos.join('\n')}
${props.repositories.join('\n')}
${chalk.blue('Action Info')}
Branch to delete: ${chalk.red(props.branchName)}
Expand All @@ -38,7 +38,7 @@ ${chalk.black.bgGreenBright('SUCCESS')} ${chalk.yellow(props.repo)}

const getDeleteBranchErrorLog = getCreateBranchErrorLog; // same as create.

export const makeBranchCommand = (config) => {
export const makeBranchCommand = (authConfig, config) => {
const program = new Command();

program
Expand All @@ -52,20 +52,20 @@ export const makeBranchCommand = (config) => {
.argument('<fromBranch>', 'Branch to create from.')
.argument('<branchName>', 'Branch name to create.')
.action(async(fromBranch, branchName) => {
if (!config) {
console.log('Missing configuration file. Please run "init" command and try again.');
if (!authConfig) {
console.log('Missing auth configuration file. Please run "init" command and try again.');
return;
}

const repos = config.repos || [];
const repositories = config.repositories || [];

if (repos.length === 0) {
if (repositories.length === 0) {
console.log('No repositories setup yet. Please run "repo add" command and try again.');
return;
}

console.log('\n' + getCreateBranchConfigLog({
repos: repos,
repositories: repositories,
fromBranch: fromBranch,
branchName: branchName,
}) + '\n');
Expand All @@ -89,12 +89,12 @@ export const makeBranchCommand = (config) => {
'Content-Type': 'application/json',
},
auth: {
username: config.auth.username,
password: config.auth.appPassword,
username: authConfig.username,
password: authConfig.appPassword,
},
});

for (const repo of repos) {
for (const repo of repositories) {

const logProps = {
repo: repo,
Expand Down Expand Up @@ -149,20 +149,20 @@ export const makeBranchCommand = (config) => {
.description('Delete a branch on all configured repositories')
.argument('<branchName>', 'Branch name to delete.')
.action(async(branchName) => {
if (!config) {
console.log('Missing configuration file. Please run "init" command and try again.');
if (!authConfig) {
console.log('Missing auth configuration file. Please run "init" command and try again.');
return;
}

const repos = config.repos || [];
const repositories = config.repositories || [];

if (repos.length === 0) {
console.log('No repositories setup yet. Please run "repo add" command and try again.');
if (repositories.length === 0) {
console.log('No repositories setup yet.');
return;
}

console.log('\n' + getDeleteBranchConfigLog({
repos: repos,
repositories: repositories,
branchName: branchName,
}) + '\n');

Expand All @@ -185,12 +185,12 @@ export const makeBranchCommand = (config) => {
'Content-Type': 'application/json',
},
auth: {
username: config.auth.username,
password: config.auth.appPassword,
username: authConfig.username,
password: authConfig.appPassword,
},
});

for (const repo of repos) {
for (const repo of repositories) {

const logProps = {
repo: repo,
Expand Down
35 changes: 9 additions & 26 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ To setup a bitbucket app password, checkout the following documentation:
https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/
`.trim();

export const makeInitCommand = (config, configFileLocation) => {
export const makeInitCommand = (authConfig, authConfigFileLocation) => {
const program = new Command();

program
.name('init')
.description('Create the configuration file with your bitbucket username and app password.')
.action(async() => {

if (config) {
if (authConfig) {
const existingConfigAnswers = await inquirer.prompt([
{
type: 'confirm',
name: 'shouldOverwriteConfig',
message: `Existing configuration file at ${configFileLocation}. Would you like to overwrite it?`,
message: `Existing configuration file at ${authConfigFileLocation}. Would you like to overwrite it?`,
default: false,
},
]);
Expand Down Expand Up @@ -57,35 +57,18 @@ export const makeInitCommand = (config, configFileLocation) => {
return true;
},
},
{
type: 'input',
name: 'versionPrefix',
message: 'Version / tag prefix when creating tags:',
default: 'v',
},
{
type: 'input',
name: 'prereleaseIdentifier',
message: 'Pre-Release Identifier for tags (usually alpha, beta, rc):',
default: 'rc',
},
]);

if (!config) {
config = {};
if (!authConfig) {
authConfig = {};
}

config.auth = config.auth || {};

config.auth.username = answers.username.trim();
config.auth.appPassword = answers.appPassword.trim();

config.versionPrefix = answers.versionPrefix;
config.prereleaseIdentifier = answers.prereleaseIdentifier;
authConfig.username = answers.username.trim();
authConfig.appPassword = answers.appPassword.trim();

fs.writeFileSync(configFileLocation, JSON.stringify(config));
fs.writeFileSync(authConfigFileLocation, JSON.stringify(authConfig));

console.log(`Created ${configFileLocation}.`);
console.log(`Created ${authConfigFileLocation}.`);
})
;

Expand Down
67 changes: 6 additions & 61 deletions src/repo.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,25 @@
import { Command } from 'commander';
import fs from 'fs';

export const makeRepoCommand = (config, configFileLocation) => {
export const makeRepoCommand = (config) => {
const program = new Command();

program
.name('repo')
.description('Add, remove and list repositories.')
;

program
.command('add')
.description('Add a new repository to the list.')
.argument('<repository>', 'Repository name you\'d like to add. This should include the user/org.')
.action((repository) => {
if (!config) {
console.log('Missing configuration file. Please run "init" command and try again.');
return;
}

config.repos = config.repos || [];

const hasExistingRepo = config.repos.some((repo) => repo === repository);

if (hasExistingRepo) {
console.log(`Repository "${repository}" is already added, skipping.`);
return;
}

config.repos.push(repository);

fs.writeFileSync(configFileLocation, JSON.stringify(config));

console.log(`Repository "${repository}" successfully added to repository list.`);
})
;

program
.command('remove')
.description('Remove an existing repository from the list.')
.argument('<repository>', 'Repository name you\'d like to add. This should include the user/org.')
.action((repository) => {
if (!config) {
console.log('Missing configuration file. Please run "init" command and try again.');
return;
}

config.repos = config.repos || [];

const hasExistingRepo = config.repos.some((repo) => repo === repository);

if (!hasExistingRepo) {
console.log(`Repository "${repository}" doesn't exist, skipping.`);
return;
}

config.repos = config.repos.filter((repo) => repo !== repository);

fs.writeFileSync(configFileLocation, JSON.stringify(config));

console.log(`Repository "${repository}" successfully removed from repository list.`);
})
.description('List repositories.')
;

program
.command('list')
.description('List all existing repositories.')
.description('List all configured repositories.')
.action(() => {
const repos = (config.repos || []);
const repositories = (config.repositories || []);

if (repos.length === 0) {
if (repositories.length === 0) {
console.log('No repositories added yet. Try adding one with "repo add <repository>"');
return;
}

repos.forEach((repo) => {
repositories.forEach((repo) => {
console.log(repo);
});
})
Expand Down
Loading

0 comments on commit 6d9822d

Please sign in to comment.