UNPKG

qforce

Version:

Commands to help with salesforce development.

69 lines (68 loc) 3.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const command_1 = require("@oclif/command"); const cli_ux_1 = require("cli-ux"); const utility_1 = require("../../helper/utility"); const execa = require('execa'); const path = require('path'); const fs = require('fs'); class DevPatch extends command_1.Command { async run() { cli_ux_1.default.action.start('Preparing patch'); const { args, flags } = this.parse(DevPatch); let settings; if (fs.existsSync(utility_1.getAbsolutePath('.qforce/settings.json'))) { settings = JSON.parse(fs.readFileSync(utility_1.getAbsolutePath('.qforce/settings.json'))); } const featureBranch = args.featureBranch; const developBranch = args.developBranch || settings.developBranch; const patchPathBase = flags.patchPath || settings.patchPath || 'patches'; if (!fs.existsSync(utility_1.getAbsolutePath(patchPathBase))) { fs.mkdirSync(utility_1.getAbsolutePath(patchPathBase)); } const patchPath = patchPathBase + '/' + featureBranch.replace(/\//g, '-') + '.patch'; const mergeBase = await execa('git', ['merge-base', featureBranch, developBranch]); const baseCommit = mergeBase.stdout; console.log('Base commit: ' + baseCommit); let diff; if (flags.syncUp) { const diffFiles = await execa('git', ['diff', '--name-only', baseCommit, featureBranch]); const filePaths = diffFiles.stdout.replace(/\n/g, ' '); const commandString = 'git diff ' + baseCommit + ' ' + developBranch + ' ' + filePaths; diff = await execa.command(commandString); } else if (flags.partialCommit) { const diffFiles = await execa('git', ['diff', '--name-only', baseCommit, featureBranch]); const filePaths = diffFiles.stdout.replace(/\n/g, ' '); const commandString = 'git diff ' + flags.partialCommit + ' ' + featureBranch + ' ' + filePaths; diff = await execa.command(commandString); } else { diff = await execa('git', ['diff', baseCommit, featureBranch]); } const diffContent = diff.stdout + '\n'; fs.writeFileSync(utility_1.getAbsolutePath(patchPath), diffContent, { encoding: 'utf-8' }); if (flags.apply) { try { await execa('git', ['apply', '--reject', '--whitespace=fix', utility_1.getAbsolutePath(patchPath)]); } catch (error) { this.log('Check for .rej files and resolve issues before proceeding.'); this.log(error); } } cli_ux_1.default.action.stop(); } } exports.default = DevPatch; DevPatch.description = 'Prepare and apply a patch to current branch from another branch.'; DevPatch.aliases = ['patch', 'dev:patch']; DevPatch.flags = { help: command_1.flags.help({ char: 'h' }), apply: command_1.flags.boolean({ char: 'a', description: 'Set to true if want to apply calculated patch to current branch.' }), patchPath: command_1.flags.string({ char: 'p', description: 'Path to save the patch file.' }), syncUp: command_1.flags.boolean({ char: 's', description: 'Set to true if need to retrieve' }), partialCommit: command_1.flags.string({ char: 'c', description: 'Commit hash for calculating partial patch.' }) }; DevPatch.args = [{ name: 'featureBranch' }, { name: 'developBranch' }];