UNPKG

@onboardbase/cli

Version:

[![Version](https://img.shields.io/npm/v/@onboardbase/cli.svg)](https://www.npmjs.com/package/@onboardbase/cli) [![Downloads/week](https://img.shields.io/npm/dw/@onboardbase/cli.svg)](https://www.npmjs.com/package/@onboardbase/cli) [![License](https://img

87 lines (86 loc) 4.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const command_1 = require("@oclif/command"); const utils_1 = require("../utils"); const targz = require("tar.gz"); const chalk = require("chalk"); const path_1 = require("path"); const fse = require("fs-extra"); const rimraf = require("rimraf"); class Update extends command_1.Command { async run() { // const canUpdateDirectory = await isDirectoryWritable(this.config.root); // if (!canUpdateDirectory) { // // write a small file into that directory so it throws an error // try { // writeFileSync( // join(this.config.root, "install.txt"), // "Throw error message", // "utf8" // ); // } catch (error) { // throw new Error(error); // } // } // console.log({ canUpdateDirectory }); if (process.platform !== "win32") { if (!(0, utils_1.isSudoUser)()) { console.log(chalk.greenBright(`This command requires sudo to run successfully. Please run ${chalk.bold("sudo onboardbase update")} instead`)); process.exit(1); } } const cliRootDirectory = this.config.root; const currentCliVersion = this.config.version; const latestCliVersion = await (0, utils_1.getLatestCliVersion)(); const updatedTarballs = `https://onboardbase-cli.fra1.digitaloceanspaces.com/onboardbase-v${latestCliVersion.latest}/onboardbase-v${latestCliVersion.latest}-${process.platform}-x64.tar.gz`; /** * Check if there's a new CLI version using the NPM registry */ if (latestCliVersion.latest === currentCliVersion) { console.log(chalk.greenBright("Your CLI is updated already")); process.exit(1); } if (["darwin", "linux"].includes(process.platform)) { this.log(`Updating your CLI from ${chalk.bold.greenBright(currentCliVersion)} to ${chalk.bold.greenBright(latestCliVersion.latest)} ...`); /** * You might be wondering what all this Unix command below does? * * It downloads the current OS tarballs from digital ocean * then extracts it inside the CLI directory, then deletes * the existing node_modules folder inside the CLI directory * * You might be wondering why I'm deleting the node_modules file? * That's an explanation for another day. * * it then changes the permission of the newly extracted folder (onboardbase) * Note: the permission change is highly needed so we can make the CLI directory * executable without root access (sudo). * * It then copies the contents of the onboardbase folder into the CLI directory * and deletes the onboardbase tarballs fike & the onboardbase folder (the folder we got from tarballs extraction) */ (0, utils_1.executeCommand)(`cd ${cliRootDirectory} && curl -Ls --output onboardbase.tar.gz -L ${updatedTarballs} && tar xf onboardbase.tar.gz && rm -rf node_modules && chmod -R 777 onboardbase && cd onboardbase && cp -R -p * ../ && cd .. && rm -rf onboardbase.tar.gz && rm -rf onboardbase `, {}) .on("error", (error) => { console.log("An Error Occurred while trying to update your CLI.", chalk.red(error)); }) .on("exit", async () => { console.log(chalk.greenBright(`Your CLI has been updated to ${latestCliVersion.latest}`)); process.exit(1); }); } if (process.platform === "win32") { await (0, utils_1.downloadTarballs)(updatedTarballs, (0, path_1.join)(cliRootDirectory, "onboardbase.tar.gz")); await targz().extract((0, path_1.join)(cliRootDirectory, "onboardbase.tar.gz"), cliRootDirectory); const updateFileContents = fse.readdirSync((0, path_1.join)(cliRootDirectory, "onboardbase"), "utf8"); await Promise.all(updateFileContents.map((directory) => { fse.copySync(directory, cliRootDirectory, { overwrite: true }); })); // Delete the onboardbase folder that was extracted rimraf((0, path_1.join)(cliRootDirectory, "onboardbase"), () => { }); console.log(chalk.greenBright(`Your CLI has been updated to ${latestCliVersion.latest}`)); process.exit(1); } } } exports.default = Update; Update.description = "Update CLI to the latest version";