UNPKG

@smartimpact-it/json-merge-shopify

Version:
89 lines (88 loc) 3.57 kB
#! /usr/bin/env node "use strict"; // Credits: https://gist.github.com/jphaas/ad7823b3469aac112a52 // This is a custom merge driver for json files. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * How to install: * * Install locally: * git config merge.json-merge-driver.name "custom JSON merge driver" * git config merge.json-merge-driver.driver "node ./path/to/json-merge-driver.js %A %O %B %P" * * Then add this to .gitattributes: * *.json merge=json-merge-driver */ const Merger_1 = require("../merger/Merger"); const fs = __importStar(require("fs")); const formatJson_1 = __importDefault(require("../formatter/formatJson")); /** * This is the information we pass through in the driver config * via the placeholders `%A %O %B %P` * %A = tmp filepath to our version of the conflicted file * %O = tmp filepath to the base version of the file * %B = tmp filepath to the other branches version of the file * %P = placeholder / real file name * %L = conflict marker size (to be able to still serve according to this setting) */ const oursPath = process.argv[2]; const basePath = process.argv[3]; const theirsPath = process.argv[4]; const filename = process.argv[5]; // Read in and parse the files const ancestor = JSON.parse(fs.readFileSync(basePath).toString()); const ours = JSON.parse(fs.readFileSync(oursPath).toString()); const theirs = JSON.parse(fs.readFileSync(theirsPath).toString()); // This gets set to true if we find a conflict let conflicts = false; // Get the preferred side const preferred = 'theirs' || process.env.JSON_MERGE_DRIVER_PREFERRED_SIDE; // Kick off the merge on the top of the json tree const merger = new Merger_1.Merger({ ancestor, ours, theirs, preferred, filename, }); const merged = merger.merge(); /** * We write the merged version of ours back to the file we got it from, which * is what git expects us to do with the results of the merge. * * We use the custom Shopify json formatter to format the json. */ fs.writeFileSync(theirsPath, (0, formatJson_1.default)(merged)); console.error('Have there been conflicts?', merger.hasConflicts() ? 'Yes' : 'No'); /** * If there were conflicts, we exit with an error code of 1 to tell git that * the conflicts need manual resolution. * Otherwise, we exit with a code of 0 to tell git that the merge was successful. */ process.exit(conflicts ? 1 : 0);