backport
Version:
A CLI tool that automates the process of backporting commits
63 lines • 2.83 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createGlobalConfigIfNotExist = exports.createGlobalConfigAndFolderIfNotExist = exports.getGlobalConfig = void 0;
const promises_1 = require("fs/promises");
const make_dir_1 = __importDefault(require("make-dir"));
const BackportError_1 = require("../../lib/BackportError");
const env_1 = require("../../lib/env");
const isErrnoError_1 = require("../../utils/isErrnoError");
const readConfigFile_1 = require("./readConfigFile");
async function getGlobalConfig(globalConfigFile) {
const globalConfigPath = (0, env_1.getGlobalConfigPath)(globalConfigFile);
await createGlobalConfigAndFolderIfNotExist(globalConfigPath);
return (0, readConfigFile_1.readConfigFile)(globalConfigPath);
}
exports.getGlobalConfig = getGlobalConfig;
async function createGlobalConfigAndFolderIfNotExist(globalConfigPath) {
// create .backport folder
await (0, make_dir_1.default)((0, env_1.getBackportDirPath)());
const configTemplate = getConfigTemplate();
const didCreate = await createGlobalConfigIfNotExist(globalConfigPath, configTemplate);
await ensureCorrectPermissions(globalConfigPath);
return didCreate;
}
exports.createGlobalConfigAndFolderIfNotExist = createGlobalConfigAndFolderIfNotExist;
function ensureCorrectPermissions(globalConfigPath) {
return (0, promises_1.chmod)(globalConfigPath, '600');
}
async function createGlobalConfigIfNotExist(globalConfigPath, configTemplate) {
try {
await (0, promises_1.writeFile)(globalConfigPath, configTemplate, {
flag: 'wx', // create and write file. Error if it already exists
mode: 0o600, // give the owner read-write privleges, no access for others
});
return true;
}
catch (e) {
if ((0, isErrnoError_1.isErrnoError)(e)) {
// ignore error if file already exists
const FILE_ALREADY_EXISTS = 'EEXIST';
if (e.code === FILE_ALREADY_EXISTS) {
return false;
}
// handle error if folder does not exist
const FOLDER_NOT_EXISTS = 'ENOENT';
if (e.code === FOLDER_NOT_EXISTS) {
throw new BackportError_1.BackportError(`The .backport folder (${globalConfigPath}) does not exist. `);
}
throw e;
}
}
}
exports.createGlobalConfigIfNotExist = createGlobalConfigIfNotExist;
function getConfigTemplate() {
return `{
// Create a personal access token here: https://github.com/settings/tokens
// Must have "Repo: Full control of private repositories"
"accessToken": ""
}`;
}
//# sourceMappingURL=globalConfig.js.map