use-multiple-gits
Version:
CLI tool to manage multiple git configurations (user.name, user.email, SSH keys) with easy switching between identities
95 lines • 4 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.isAutoSwitchEnabled = exports.disableAutoSwitch = exports.enableAutoSwitch = exports.getConfigForDirectory = exports.removeDirectoryMapping = exports.addDirectoryMapping = void 0;
const path = __importStar(require("path"));
const configStorage_1 = require("./configStorage");
const addDirectoryMapping = async (directoryPath, configName) => {
const data = await (0, configStorage_1.readConfig)();
if (!data.directoryMappings) {
data.directoryMappings = {};
}
// Normalize path
const normalizedPath = path.resolve(directoryPath);
data.directoryMappings[normalizedPath] = configName;
await (0, configStorage_1.writeConfig)(data);
};
exports.addDirectoryMapping = addDirectoryMapping;
const removeDirectoryMapping = async (directoryPath) => {
const data = await (0, configStorage_1.readConfig)();
if (!data.directoryMappings) {
return;
}
const normalizedPath = path.resolve(directoryPath);
delete data.directoryMappings[normalizedPath];
await (0, configStorage_1.writeConfig)(data);
};
exports.removeDirectoryMapping = removeDirectoryMapping;
const getConfigForDirectory = async (currentDir = process.cwd()) => {
const data = await (0, configStorage_1.readConfig)();
if (!data.directoryMappings) {
return null;
}
const normalizedCurrentDir = path.resolve(currentDir);
// Find the longest matching path
let bestMatch = null;
for (const [mappedPath, configName] of Object.entries(data.directoryMappings)) {
if (normalizedCurrentDir.startsWith(mappedPath)) {
if (!bestMatch || mappedPath.length > bestMatch.path.length) {
bestMatch = { path: mappedPath, config: configName };
}
}
}
return bestMatch ? bestMatch.config : null;
};
exports.getConfigForDirectory = getConfigForDirectory;
const enableAutoSwitch = async () => {
const data = await (0, configStorage_1.readConfig)();
data.autoSwitchEnabled = true;
await (0, configStorage_1.writeConfig)(data);
};
exports.enableAutoSwitch = enableAutoSwitch;
const disableAutoSwitch = async () => {
const data = await (0, configStorage_1.readConfig)();
data.autoSwitchEnabled = false;
await (0, configStorage_1.writeConfig)(data);
};
exports.disableAutoSwitch = disableAutoSwitch;
const isAutoSwitchEnabled = async () => {
const data = await (0, configStorage_1.readConfig)();
return data.autoSwitchEnabled === true;
};
exports.isAutoSwitchEnabled = isAutoSwitchEnabled;
//# sourceMappingURL=directoryMapper.js.map