UNPKG

sync-postman

Version:

CLI tool to sync Postman collections with GitHub for free

134 lines (106 loc) 4.48 kB
#!/usr/bin/env node const { program } = require('commander'); const { loadConfig, pushOnGithub, hardPullPostmanCollections, pullFromGithub, getOwnerName } = require('../lib/helper'); const fs = require('fs'); const os = require('os'); const path = require('path'); const configPath = path.join(os.homedir(), '.sync-postman-config.json'); program .name('sync-postman') .description('CLI tool to sync Postman collections with GitHub') .version('1.2.0'); program .command('setup') .description('Set up or reset your configuration for Postman and GitHub') .option('--reset <key>', 'Reset a specific configuration key') .action(async (options) => { const prompt = require('prompt-sync')(); let config = {}; if (fs.existsSync(configPath)) { config = JSON.parse(fs.readFileSync(configPath, 'utf-8')); } if (options.reset) { const key = options.reset.toUpperCase(); if (config[key] !== undefined) { config[key] = prompt(`Enter new value for ${key}: `); console.log(`${key} updated successfully!`); } else { console.error(`Key '${key}' not found in the existing configuration.`); } } else { config.POSTMAN_API_KEY = prompt('Enter your Postman API Key: '); config.GITHUB_REPO = prompt('Enter your GitHub Repository URL: '); config.GITHUB_TOKEN = prompt('Enter your GitHub Token: '); console.log('Configuration updated successfully!'); } if (config.GITHUB_REPO) { const ownerName = getOwnerName(config.GITHUB_REPO); config.GITHUB_USERNAME = ownerName ?? ""; } fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); }); program .command('push') .description('Push Postman collections to GitHub') .option('--branchName <branchName>', 'Specify the branch name for the push (default: autogenerated branch name)') .option('--sourceBranch <sourceBranch>', 'Specify the source branch name for the origin (default: main)') .action(async (options) => { const config = loadConfig(); if (!config) { console.error('Please run "sync-postman setup" to configure the application.'); return; } try { // Use the specified branch or generate a new one const branchName = options.branchName || `feature-branch-${Date.now()}`; console.log(`Using branch name: ${branchName}`); const sourceBranch = options.sourceBranch || 'main'; console.log(`Using source branch: ${sourceBranch}`); // Create a pull request with the specified branch name await pushOnGithub(config, branchName, sourceBranch); console.log('Postman collections pushed to GitHub successfully.'); } catch (error) { console.error("Error during push operation:", error.message); } }); program .command('pull') .description('Pull collections from GitHub and update Postman') .option('--hard', 'Perform a hard pull, replacing all Postman collections with GitHub data') .option('--branch <branchName>', 'Specify the branch to pull collections from (default is "main")') .action(async (options) => { const config = loadConfig(); if (!config) { console.error('Please run "sync-postman setup" to configure the application.'); return; } try { const branchName = options.branch || 'main'; console.log(`Fetching collections from branch: ${branchName}`); if (options.hard) { await hardPullPostmanCollections(config, branchName); } else { await pullFromGithub(config, branchName); } console.log('Postman collections pulled from GitHub and updated successfully.'); } catch (error) { console.error("Error during pull operation:", error.message); } }); program .command('check-config') .description('My configurations') .action(async () => { const config = loadConfig(); if (!config) { console.error('Please run "sync-postman setup" to configure the application.'); return; } console.log(config); }); program.parse(process.argv);