Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): ask for process restarts after updating #2155

Merged
merged 8 commits into from
Feb 25, 2019
19 changes: 19 additions & 0 deletions packages/core/src/commands/command.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { networks } from "@arkecosystem/crypto";
import Command, { flags } from "@oclif/command";
import cli from "cli-ux";
import envPaths from "env-paths";
import { existsSync, readdirSync } from "fs";
import Listr from "listr";
import { join, resolve } from "path";
import prompts from "prompts";
import { configManager } from "../helpers/config";
import { confirm } from "../helpers/prompts";
import { processManager } from "../process-manager";
import { CommandFlags, Options } from "../types";

// tslint:disable-next-line:no-var-requires
Expand Down Expand Up @@ -281,6 +284,22 @@ export abstract class BaseCommand extends Command {
return this.getNetworks().map(network => ({ title: network, value: network }));
}

protected async restartProcess(processName: string) {
if (processManager.exists(processName)) {
await confirm(`Would you like to restart the ${processName} process?`, () => {
try {
cli.action.start(`Restarting ${processName}`);

processManager.restart(processName);
} catch (error) {
this.error(error.message);
} finally {
cli.action.stop();
}
});
}
}

private getEnvPaths(flags: CommandFlags): envPaths.Paths {
return envPaths(flags.token, { suffix: "core" });
}
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/commands/config/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,19 @@ $ ark config:cli --channel=mine

await installFromChannel(this.config.name, newChannel);

this.warn(`${pkg} has been installed.`);

cli.action.stop();

this.warn(`${pkg} has been installed. Please restart your relay and forger.`);
const { flags } = await this.parseWithNetwork(CommandLineInterfaceCommand);

await this.restartProcess(`${flags.token}-core`);
await this.restartProcess(`${flags.token}-relay`);
await this.restartProcess(`${flags.token}-forger`);
} catch (err) {
this.error(err.message);
} finally {
cli.action.stop();
}
}
}
17 changes: 8 additions & 9 deletions packages/core/src/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Chalk from "chalk";
import cli from "cli-ux";
import { removeSync } from "fs-extra";
import { requestConfirmation } from "../helpers/prompts";
import { confirm } from "../helpers/prompts";
import { checkForUpdates, installFromChannel } from "../helpers/update";
import { BaseCommand } from "./command";

Expand All @@ -27,7 +27,7 @@ export class UpdateCommand extends BaseCommand {
)}.`,
);

await requestConfirmation("Would you like to update?", async () => {
await confirm("Would you like to update?", async () => {
try {
cli.action.start(`Updating from ${currentVersion} to ${newVersion}`);

Expand All @@ -38,17 +38,16 @@ export class UpdateCommand extends BaseCommand {
removeSync(state.cache);

this.warn(`Version ${newVersion} has been installed.`);
this.warn(
'Respectively run "ark relay:restart", "ark forger:restart" or "ark core:restart" to restart your processes.',
);

// @TODO ask the user if he wants to restart the core
// @TODO ask the user if he wants to restart the relay
// @TODO ask the user if he wants to restart the forger
const { flags } = await this.parseWithNetwork(UpdateCommand);

process.exit();
await this.restartProcess(`${flags.token}-core`);
await this.restartProcess(`${flags.token}-relay`);
await this.restartProcess(`${flags.token}-forger`);
} catch (err) {
this.error(err.message);
} finally {
cli.action.stop();
}
});
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/helpers/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import prompts from "prompts";

export async function requestConfirmation(message: string, callback: any): Promise<void> {
export async function confirm(message: string, callback: any): Promise<void> {
const { confirm } = await prompts([
{
type: "confirm",
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/helpers/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ export async function checkForUpdates({ config, error, warn }): Promise<any> {
}
} catch (err) {
error(err.message);
} finally {
cli.action.stop();
}

return state;
Expand Down