taylo
Version:
Make changes to a branch a plugin. A command-line tool to manage and apply plugins '.taylored'. Supports applying, removing, verifying plugins, and generating them from branch (GIT).
92 lines (91 loc) • 4.44 kB
JavaScript
;
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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleSaveOperation = handleSaveOperation;
const fs = __importStar(require("fs/promises"));
const fsExtra = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const constants_1 = require("../constants");
const utils_1 = require("../utils");
async function handleSaveOperation(branchName, CWD) {
const outputFileName = `${branchName.replace(/[/\\]/g, '-')}${constants_1.TAYLORED_FILE_EXTENSION}`;
const targetDirectoryPath = path.join(CWD, constants_1.TAYLORED_DIR_NAME);
const resolvedOutputFileName = path.join(targetDirectoryPath, outputFileName);
try {
await fsExtra.ensureDir(targetDirectoryPath);
}
catch (mkdirError) {
console.error(`CRITICAL ERROR: Failed to create directory '${targetDirectoryPath}'. Details: ${mkdirError.message}`);
throw mkdirError;
}
const diffResult = (0, utils_1.getAndAnalyzeDiff)(branchName, CWD);
if (diffResult.success && diffResult.isPure) {
if (typeof diffResult.diffOutput === 'string') {
try {
await fs.writeFile(resolvedOutputFileName, diffResult.diffOutput);
}
catch (writeError) {
console.error(`CRITICAL ERROR: Failed to write diff file '${resolvedOutputFileName}'. Details: ${writeError.message}`);
throw writeError;
}
}
else {
console.error(`CRITICAL ERROR: Diff output is unexpectedly undefined for branch '${branchName}' despite successful analysis.`);
throw new Error(`Undefined diff output for pure diff on branch '${branchName}'.`);
}
}
else {
if (!diffResult.success && diffResult.errorMessage) {
console.error(diffResult.errorMessage);
}
else {
console.error(`ERROR: Taylored file '${resolvedOutputFileName}' was NOT generated.`);
if (!diffResult.isPure) {
console.error(`Reason: The diff between "${branchName}" and HEAD contains a mix of content line additions and deletions.`);
console.error(` Total lines added: ${diffResult.additions}`);
console.error(` Total lines deleted: ${diffResult.deletions}`);
console.error('This script, for the --save operation, requires the diff to consist exclusively of additions or exclusively of deletions (of lines).');
}
else if (typeof diffResult.diffOutput === 'undefined') {
console.error(`Reason: Failed to obtain diff output for branch '${branchName}'. This may be due to an invalid branch name or other git error.`);
}
else {
console.error(`Reason: An unspecified error occurred during diff generation or analysis for branch '${branchName}'.`);
}
}
throw new Error(diffResult.errorMessage ||
`Failed to save taylored file for branch '${branchName}' due to purity or diff generation issues.`);
}
}