UNPKG

@topgroup/diginext

Version:

A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.

100 lines (99 loc) 5.03 kB
"use strict"; 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 }); exports.initalizeAndCreateDefaultBranches = void 0; // import { log } from "diginext-utils/dist/xconsole/log"; const Timer_1 = __importDefault(require("diginext-utils/dist/Timer")); const log_1 = require("diginext-utils/dist/xconsole/log"); const fs = __importStar(require("fs")); const path_1 = __importDefault(require("path")); const simple_git_1 = require("simple-git"); const updateBranchProtection_1 = __importDefault(require("../../modules/git/updateBranchProtection")); const updateRepoSetting_1 = __importDefault(require("../../modules/git/updateRepoSetting")); const plugins_1 = require("../../plugins"); const slug_1 = require("../../plugins/slug"); const initalizeAndCreateDefaultBranches = async (options) => { if (options.isDebugging) console.log("initalizeAndCreateDefaultBranches > directory :>> ", options.targetDirectory); if (options.isDebugging) console.log("initalizeAndCreateDefaultBranches > repoSSH :>> ", options.repoSSH); if (options.isDebugging) console.log("initalizeAndCreateDefaultBranches > repoURL :>> ", options.repoURL); try { // Remove current git (if any) & initialize new git... const gitDir = path_1.default.resolve(options.targetDirectory, ".git/"); if (fs.existsSync(gitDir)) fs.rmSync(gitDir, { recursive: true, force: true }); // Initialize local git... const git = (0, simple_git_1.simpleGit)(options.targetDirectory); await git.init(); // create "main" branch await git.checkout(["-b", "main"]); // just to make sure the remote git is ready... await (0, plugins_1.wait)(1000); // add git origin remote: await git.addRemote("origin", options.repoSSH); // await git.addRemote("origin", options.repoURL); // stage all deployment files & commit it await git.fetch(["--all"]); await git.add("."); await git.commit("feat(initial): initial commit"); // debug if (options.isDebugging) { const branch = await git.branch(); (0, log_1.log)("initalizeAndCreateDefaultBranches > branch.current :>> ", branch === null || branch === void 0 ? void 0 : branch.current); const remote = await git.remote(["-v"]); (0, log_1.log)("initalizeAndCreateDefaultBranches > remote :>> ", remote); } await git.push(["--set-upstream", "origin", "main", "--force"]); // Update main branch protection (async () => { await Timer_1.default.wait(1000); await (0, updateBranchProtection_1.default)(options); await (0, updateRepoSetting_1.default)(options); })(); // create developer branches const gitUsername = (await git.getConfig(`user.name`, "global")).value; const username = options.username || (gitUsername ? (0, slug_1.makeSlug)(gitUsername).toLowerCase() : undefined) || "developer"; const devBranch = `dev/${username}`; if (options.isDebugging) { console.log("initalizeAndCreateDefaultBranches > username :>> ", username); console.log("initalizeAndCreateDefaultBranches > devBranch :>> ", devBranch); } await git.checkout(["-b", devBranch]); await git.push(["--set-upstream", "origin", devBranch, "--force"]); if (options.isDebugging) console.log(`✅ Finished initializing git!`); return options; } catch (error) { console.error(`Unable to initialize default branches:\n${options.repoSSH}\n${options.repoURL}\n${options.targetDirectory}`); throw new Error(`[GIT] Unable to initialize default branches: ${error}`); } }; exports.initalizeAndCreateDefaultBranches = initalizeAndCreateDefaultBranches;