@adinsure-ops/ops-cli
Version:
Operations CLI for working with AdInsure
295 lines (294 loc) • 10.6 kB
JavaScript
import { __awaiter } from "tslib";
import { Flags, ux } from '@oclif/core';
import chalk from 'chalk';
import fs from 'fs-extra';
import CommandBase from '../command.base.js';
import ChildProcessCommand from '../lib/command.js';
class Doctor extends CommandBase {
constructor() {
super(...arguments);
this.cp = new ChildProcessCommand();
this.nvm = false;
this.yarn = false;
this.node = false;
this.missingPackages = 0;
}
/**
* Check if Git is installed
* @param {boolean} fix Try to install Git if found missing
* @returns {void}
*/
gitCheck(fix) {
return __awaiter(this, void 0, void 0, function* () {
ux.action.start('Checking git', undefined, { stdout: true });
try {
const output = yield this.cp.runCommand(['git', '--version']);
if (output.error) {
ux.action.stop(`${chalk.red(output.error)}`);
}
else {
const version = output.data.split(' ')[2].split('\n')[0];
ux.action.stop(`${chalk.green(version)}`);
}
}
catch (_a) {
ux.action.stop(`${chalk.red('git is missing')}`);
if (fix) {
yield this.gitInstall();
}
else {
this.missingPackages++;
this.warn(`Looks like git is missing.
Official documentation on how to install git can be found here: (https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)`);
}
}
});
}
gitInstall() {
return __awaiter(this, void 0, void 0, function* () {
switch (process.platform) {
case 'linux': {
yield this.gitLinuxInstall();
break;
}
case 'win32': {
break;
}
default: {
this.warn('unsupported platform. skipping git install.');
}
}
});
}
gitLinuxInstall() {
return __awaiter(this, void 0, void 0, function* () {
this.log(`Try to install git on ubuntu. Sudo is required`);
try {
yield this.cp.runCommand(['sudo', 'apt', 'update']);
yield this.cp.runCommand(['sudo', 'apt', 'install', 'git']);
yield this.gitCheck(false);
}
catch (_a) {
ux.action.stop(`${chalk.red('error installing git.')}`);
}
});
}
gitWindowsInstall() {
return __awaiter(this, void 0, void 0, function* () {
// https://github.com/git-for-windows/git/releases/download/v2.33.1.windows.1/Git-2.33.1-64-bit.exe
// %USERPROFILE%\Downloads
});
}
/**
* Check if nvm is installed
* @returns {void}
*/
nvmCheck() {
return __awaiter(this, void 0, void 0, function* () {
ux.action.start('Checking nvm', undefined, { stdout: true });
if (process.platform !== 'win32') {
if (process.env.NVM_DIR) {
try {
const data = yield fs.readJSON(`${process.env.NVM_DIR}/package.json`);
ux.action.stop(`${chalk.green(data.version)}`);
this.nvm = true;
}
catch (_a) {
ux.action.stop(`${chalk.red('not installed')}`);
}
}
else {
ux.action.stop(`${chalk.red('nvm is missing (install manually from https://github.com/nvm-sh/nvm#installing-and-updating')}`);
}
return;
}
try {
const output = yield this.cp.runCommand(['nvm', 'version']);
if (output.error) {
ux.action.stop(`${chalk.red(output.error)}`);
}
else {
const version = output.data.split('\n')[0];
this.nvm = true;
ux.action.stop(`${chalk.green(version)}`);
}
}
catch (_b) {
ux.action.stop(`${chalk.red('nvm is missing (install manually from https://github.com/coreybutler/nvm-windows/releases)')}`);
}
});
}
/**
* Check which node is installed
* @param {boolean} fix Try to install Node if found missing
* @returns {void}
*/
nodeCheck(fix) {
return __awaiter(this, void 0, void 0, function* () {
ux.action.start('Checking node', undefined, { stdout: true });
try {
const output = yield this.cp.runCommand(['node', '--version']);
if (output.error) {
ux.action.stop(`${chalk.red(output.error)}`);
}
else {
const version = output.data.split('\n')[0];
if (version) {
ux.action.stop(`${chalk.green(version)}`);
this.node = true;
// only check yarn if node is installed
yield this.yarnCheck(fix);
}
else {
ux.action.stop(`${chalk.red('failed getting version')}`);
}
}
}
catch (_a) {
ux.action.stop(`${chalk.red('node is missing')}`);
if (fix && this.nvm) {
yield this.nodeInstall();
}
else {
this.missingPackages++;
}
}
});
}
/**
* Check which node is installed
* @returns {void}
*/
nodeInstall() {
return __awaiter(this, void 0, void 0, function* () {
ux.action.start('installing node 15.6.0', undefined, { stdout: true });
if (!this.nvm) {
return ux.action.stop(`${chalk.red('only works with nvm isntalled')}`);
}
try {
const output = yield this.cp.runCommand(['nvm', 'install', '15.6.0']);
if (output.error) {
ux.action.stop(`${chalk.red(output.error)}`);
}
else {
const nvmUserOutput = yield this.cp.runCommand(['nvm', 'use', '15.6.0']);
if (nvmUserOutput.error) {
ux.action.stop(`${chalk.red(output.error)}`);
}
ux.action.stop(`${chalk.green('Done')}`);
}
}
catch (_a) {
ux.action.stop(`${chalk.red('failed')}`);
}
});
}
/**
* Check which yarn is installed
* @param {boolean} fix Try to install yarn if found missing
* @returns {void}
*/
yarnCheck(fix) {
return __awaiter(this, void 0, void 0, function* () {
ux.action.start('Checking yarn', undefined, { stdout: true });
try {
const output = yield this.cp.runCommand(['yarn', '--version']);
if (output.error) {
ux.action.stop(`${chalk.red(output.error)}`);
}
else {
const version = output.data.split('\n')[0];
if (version) {
this.yarn = true;
ux.action.stop(`${chalk.green(version)}`);
}
else {
ux.action.stop(`${chalk.red('yarn is missing')}`);
}
}
}
catch (_a) {
this.missingPackages++;
ux.action.stop(`${chalk.red('yarn is missing')}`);
if (fix) {
yield this.yarnInstall();
}
}
});
}
/**
* Check which yarn is installed
* @returns {void}
*/
yarnInstall() {
return __awaiter(this, void 0, void 0, function* () {
ux.action.start('Install yarn', undefined, { stdout: true });
try {
const output = yield this.cp.runCommand(['npm', '-g', 'install', 'yarn']);
if (output.error) {
ux.action.stop(`${chalk.red(output.error)}`);
}
else {
this.yarn = true;
ux.action.stop(`${chalk.green('yarn installed')}`);
}
}
catch (_a) {
this.missingPackages++;
ux.action.stop(`${chalk.red('yarn failed to install')}`);
}
});
}
/**
* Check which VSCode is installed
* @returns {void}
*/
vscodeCheck() {
return __awaiter(this, void 0, void 0, function* () {
ux.action.start('Checking vscode', undefined, { stdout: true });
try {
const output = yield this.cp.runCommand(['code', '--version']);
if (output.error) {
ux.action.stop(`${chalk.red(output.error)}`);
}
else {
const version = output.data.split('\n')[0];
ux.action.stop(`${chalk.green(version)}`);
}
}
catch (_a) {
this.missingPackages++;
ux.action.stop(`${chalk.red('VSCode not installed')}`);
}
});
}
run() {
const _super = Object.create(null, {
run: { get: () => super.run }
});
return __awaiter(this, void 0, void 0, function* () {
_super.run.call(this);
const { flags } = yield this.parse(Doctor);
yield this.gitCheck(flags.fix);
yield this.nvmCheck();
yield this.nodeCheck(flags.fix);
yield this.vscodeCheck();
if (this.missingPackages > 0) {
this.warn(`${chalk.yellow('Some packages are missing. Try --fix to automatically install.')}`);
}
});
}
}
Doctor.description = 'check if everything is installed';
Doctor.usage = 'doctor';
Doctor.examples = [
`$ ops doctor`,
];
Doctor.flags = {
fix: Flags.boolean({
char: 'p',
description: 'try to install missing packages',
default: false,
}),
};
export default Doctor;