Skip to content

Commit

Permalink
fix(doctor): add electron checks (#2434)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Feb 10, 2020
1 parent 4ff1943 commit d5efb05
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cli/src/android/doctor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { accessSync } from 'fs';
import { check, isInstalled, logFatal, logSuccess, readXML } from '../common';
import { check, logFatal, logSuccess, readXML } from '../common';
import { existsAsync, readFileAsync } from '../util/fs';
import { Config } from '../config';
import { join } from 'path';
Expand Down
12 changes: 11 additions & 1 deletion cli/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ export function resolveNode(config: Config, ...pathSegments: any[]): string | nu
return join(modulePath, ...path);
}

function resolveNodeFrom(start: string, id: string): string | null {
export function resolveNodeFrom(start: string, id: string): string | null {
const rootPath = parse(start).root;
let basePath = resolve(start);
let modulePath;
Expand Down Expand Up @@ -451,3 +451,13 @@ export const hasYarn = async (config: Config, projectDir?: string) => {
export async function installDeps(projectDir: string, deps: string[], config: Config) {
return runCommand(`cd "${projectDir}" && ${await hasYarn(config, projectDir) ? 'yarn add' : 'npm install --save'} ${deps.join(' ')}`);
}

export async function checkNPMVersion() {
const minVersion = '5.5.0';
const version = await runCommand('npm -v');
const semver = await import('semver');
if (semver.gt(minVersion, version)) {
return `Capacitor CLI requires at least NPM ${minVersion}`;
}
return null;
}
53 changes: 53 additions & 0 deletions cli/src/electron/doctor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { check, checkNPMVersion, checkWebDir, logFatal, logSuccess, resolveNodeFrom } from '../common';
import { Config } from '../config';
import { existsAsync } from '../util/fs';
import { join } from 'path';

export async function doctorElectron(config: Config) {
try {
await check(
config,
[
checkWebDir,
checkNPMVersion,
checkAppSrcDirs,
checkElectronInstall
]
);
logSuccess('electron looking great! 👌');
} catch (e) {
logFatal(e);
}

}

async function checkAppSrcDirs(config: Config) {
const appDir = join(config.electron.platformDir, 'app');
if (!await existsAsync(appDir)) {
return `"app" directory is missing in: ${config.electron.platformDir}`;
}

const appIndexHtml = join(appDir, 'index.html');
if (!await existsAsync(appIndexHtml)) {
return `"index.html" directory is missing in: ${appDir}`;
}
return checkElectronIndexFile(config, config.electron.platformDir);
}

async function checkElectronIndexFile(config: Config, electronDir: string) {
const indexFileName = 'index.js';
const indexFilePath = join(electronDir, indexFileName);

if (!await existsAsync(indexFilePath)) {
return `"${indexFilePath}" is missing in: ${electronDir}`;
}
return null;
}

async function checkElectronInstall(config: Config) {
if (resolveNodeFrom(config.electron.platformDir, 'electron')) {
return null;
} else {
return 'electron not installed';
}
}
12 changes: 2 additions & 10 deletions cli/src/ios/doctor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { checkCocoaPods, checkIOSProject } from './common';
import { check, checkWebDir, isInstalled, logFatal, logSuccess, runCommand } from '../common';
import { check, checkNPMVersion, checkWebDir, isInstalled, logFatal, logSuccess } from '../common';
import { Config } from '../config';
import { getPlugins, printPlugins } from '../plugin';

Expand Down Expand Up @@ -46,12 +46,4 @@ async function checkXcode() {
}


async function checkNPMVersion() {
const minVersion = '5.5.0';
const version = await runCommand('npm -v');
const semver = await import('semver');
if (semver.gt(minVersion, version)) {
return `Capacitor CLI requires at least NPM ${minVersion}`;
}
return null;
}

28 changes: 19 additions & 9 deletions cli/src/tasks/doctor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Config } from '../config';
import { log, readJSON, resolveNode, runCommand } from '../common';
import { log, readJSON, resolveNode, resolveNodeFrom, runCommand } from '../common';
import { doctorAndroid } from '../android/doctor';
import { doctorElectron } from '../electron/doctor';
import { doctorIOS } from '../ios/doctor';
import { existsAsync, readFileAsync } from '../util/fs';
import { emoji as _e } from '../util/emoji';

import { join } from 'path';
Expand All @@ -24,12 +24,14 @@ export async function doctorCore(config: Config) {
let cliVersion = await runCommand(`npm info @capacitor/cli version`);
let coreVersion = await runCommand(`npm info @capacitor/core version`);
let androidVersion = await runCommand(`npm info @capacitor/android version`);
let electronVersion = await runCommand(`npm info @capacitor/android version`);
let iosVersion = await runCommand(`npm info @capacitor/ios version`);

log(`${chalk.bold.blue('Latest Dependencies:')}\n`);
log(` ${chalk.bold('@capacitor/cli:')}`, cliVersion);
log(` ${chalk.bold('@capacitor/core:')}`, coreVersion);
log(` ${chalk.bold('@capacitor/android:')}`, androidVersion);
log(` ${chalk.bold('@capacitor/electron:')}`, electronVersion);
log(` ${chalk.bold('@capacitor/ios:')}`, iosVersion);

log(`${chalk.bold.blue('Installed Dependencies:')}\n`);
Expand All @@ -39,24 +41,32 @@ export async function doctorCore(config: Config) {
log('');
}

async function printInstalledPackages( config: Config) {
async function printInstalledPackages(config: Config) {
const packageNames = ['@capacitor/cli', '@capacitor/core', '@capacitor/android', '@capacitor/ios'];
await Promise.all(packageNames.map(async packageName => {
let version;
const packagePath = resolveNode(config, packageName, 'package.json');
if (packagePath) {
version = (await readJSON(packagePath)).version;
}
log(` ${chalk.bold(packageName)}`, version || 'not installed');
log('');
printPackageVersion(packageName, packagePath);
}));
const packagePath = resolveNodeFrom(config.electron.platformDir, '@capacitor/electron');
printPackageVersion('@capacitor/electron', packagePath ? join(packagePath, 'package.json') : packagePath);
}

async function printPackageVersion(packageName: string, packagePath: string | null) {
let version;
if (packagePath) {
version = (await readJSON(packagePath)).version;
}
log(` ${chalk.bold(packageName)}`, version || 'not installed');
log('');
}

export async function doctor(config: Config, platformName: string) {
if (platformName === config.ios.name) {
await doctorIOS(config);
} else if (platformName === config.android.name) {
await doctorAndroid(config);
} else if (platformName === config.electron.name) {
await doctorElectron(config);
} else if (platformName === config.web.name) {
return Promise.resolve();
} else {
Expand Down

0 comments on commit d5efb05

Please sign in to comment.