|
| 1 | +#!/usr/bin/env ts-node |
| 2 | +/* ============================================================================ |
| 3 | + * Copyright (c) Palo Alto Networks |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + * ========================================================================== */ |
| 8 | + |
| 9 | +import { execSync } from "child_process"; |
| 10 | +import fs from "fs"; |
| 11 | +import path from "path"; |
| 12 | + |
| 13 | +import { version } from "../lerna.json"; |
| 14 | +import { createDryRun } from "./utils/dry-run"; |
| 15 | +import { getOutput } from "./utils/get-output"; |
| 16 | +import { printBanner } from "./utils/print-utils"; |
| 17 | + |
| 18 | +const ORG = "PaloAltoNetworks"; |
| 19 | +const REPO = "docusaurus-openapi-docs"; |
| 20 | +let REPO_ROOT = undefined; |
| 21 | + |
| 22 | +// Makes the script crash on unhandled rejections instead of silently |
| 23 | +// ignoring them. In the future, promise rejections that are not handled will |
| 24 | +// terminate the Node.js process with a non-zero exit code. |
| 25 | +process.on("unhandledRejection", (err) => { |
| 26 | + throw err; |
| 27 | +}); |
| 28 | + |
| 29 | +const safeExec = createDryRun(execSync); |
| 30 | +const safeRmdir = createDryRun(fs.rmSync); |
| 31 | +const safeMkdir = createDryRun(fs.mkdirSync); |
| 32 | + |
| 33 | +function getGitUserName() { |
| 34 | + return getOutput("git config user.name"); |
| 35 | +} |
| 36 | + |
| 37 | +function getGitUserEmail() { |
| 38 | + return getOutput("git config user.email"); |
| 39 | +} |
| 40 | + |
| 41 | +function ensureCleanDir(path: string) { |
| 42 | + if (fs.existsSync(path)) { |
| 43 | + safeRmdir(path, { recursive: true }); |
| 44 | + } |
| 45 | + safeMkdir(path, { recursive: true }); |
| 46 | +} |
| 47 | + |
| 48 | +function checkoutCode() { |
| 49 | + printBanner("Retrieving source code"); |
| 50 | + |
| 51 | + const BUILD_PATH = "build"; |
| 52 | + ensureCleanDir(BUILD_PATH); |
| 53 | + |
| 54 | + safeExec(`git clone [email protected]:${ORG}/${REPO}.git ${REPO}`, { |
| 55 | + cwd: BUILD_PATH, |
| 56 | + }); |
| 57 | + |
| 58 | + REPO_ROOT = path.join(BUILD_PATH, REPO); |
| 59 | +} |
| 60 | + |
| 61 | +function configureGit() { |
| 62 | + const gitUserName = getGitUserName(); |
| 63 | + const gitUserEmail = getGitUserEmail(); |
| 64 | + safeExec(`git config user.name ${gitUserName}`, { |
| 65 | + cwd: REPO_ROOT, |
| 66 | + }); |
| 67 | + safeExec(`git config user.email ${gitUserEmail}`, { |
| 68 | + cwd: REPO_ROOT, |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +function buildAndPublish() { |
| 73 | + safeExec(`yarn install --frozen-lockfile`, { |
| 74 | + cwd: REPO_ROOT, |
| 75 | + stdio: "ignore", |
| 76 | + }); |
| 77 | + |
| 78 | + printBanner("Building Packages"); |
| 79 | + |
| 80 | + safeExec(`yarn lerna run build --no-private`, { |
| 81 | + cwd: REPO_ROOT, |
| 82 | + }); |
| 83 | + |
| 84 | + printBanner("Publishing Packages"); |
| 85 | + |
| 86 | + // --no-verify-access enables lerna publish to work in ci with access token. |
| 87 | + safeExec( |
| 88 | + `lerna publish --yes from-package --no-verify-access --dist-tag beta`, |
| 89 | + { |
| 90 | + cwd: REPO_ROOT, |
| 91 | + } |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +function tag() { |
| 96 | + const tag = `v${version}`; |
| 97 | + const message = `Version ${version}`; |
| 98 | + safeExec(`git tag -a ${tag} -m "${message}"`, { |
| 99 | + cwd: REPO_ROOT, |
| 100 | + }); |
| 101 | + safeExec(`git push origin ${tag}`, { |
| 102 | + cwd: REPO_ROOT, |
| 103 | + }); |
| 104 | +} |
| 105 | + |
| 106 | +function versions() { |
| 107 | + return getOutput(`git tag --list 'v*'`).split("\n"); |
| 108 | +} |
| 109 | + |
| 110 | +function main() { |
| 111 | + if (versions().includes(`v${version}`)) { |
| 112 | + console.log(`\x1b[33mSKIPPING: Version ${version} already exists.\x1b[0m`); |
| 113 | + return; |
| 114 | + } |
| 115 | + if (!process.env.CI) { |
| 116 | + checkoutCode(); |
| 117 | + } |
| 118 | + configureGit(); |
| 119 | + buildAndPublish(); |
| 120 | + tag(); |
| 121 | +} |
| 122 | + |
| 123 | +main(); |
0 commit comments