From 7f5c080f8ec4de3cfaa5a8b055b1033d2b4939ce Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Mon, 10 Feb 2020 17:00:33 +0100 Subject: [PATCH 1/2] fix(doctor): add electron checks --- cli/src/android/doctor.ts | 2 +- cli/src/common.ts | 12 ++++++++- cli/src/electron/doctor.ts | 54 ++++++++++++++++++++++++++++++++++++++ cli/src/ios/doctor.ts | 12 ++------- cli/src/tasks/doctor.ts | 28 +++++++++++++------- 5 files changed, 87 insertions(+), 21 deletions(-) create mode 100644 cli/src/electron/doctor.ts diff --git a/cli/src/android/doctor.ts b/cli/src/android/doctor.ts index 2185f9f28d..65b5112eac 100644 --- a/cli/src/android/doctor.ts +++ b/cli/src/android/doctor.ts @@ -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'; diff --git a/cli/src/common.ts b/cli/src/common.ts index 6aafc2d317..3a90ec3d1d 100644 --- a/cli/src/common.ts +++ b/cli/src/common.ts @@ -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; @@ -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; +} diff --git a/cli/src/electron/doctor.ts b/cli/src/electron/doctor.ts new file mode 100644 index 0000000000..532b79e3ee --- /dev/null +++ b/cli/src/electron/doctor.ts @@ -0,0 +1,54 @@ +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 appSrcMainAssetsWwwIndexHtmlDir = join(appDir, 'index.html'); + if (!await existsAsync(appSrcMainAssetsWwwIndexHtmlDir)) { + 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'; + } +} + diff --git a/cli/src/ios/doctor.ts b/cli/src/ios/doctor.ts index 7d7324f7c6..9c78c87a12 100644 --- a/cli/src/ios/doctor.ts +++ b/cli/src/ios/doctor.ts @@ -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'; @@ -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; -} + diff --git a/cli/src/tasks/doctor.ts b/cli/src/tasks/doctor.ts index 47be8b0da2..a282cd960e 100644 --- a/cli/src/tasks/doctor.ts +++ b/cli/src/tasks/doctor.ts @@ -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'; @@ -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`); @@ -39,17 +41,23 @@ 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) { @@ -57,6 +65,8 @@ export async function doctor(config: Config, platformName: string) { 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 { From 974024968a22fe8e24734a7a572eafa347605fb5 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Mon, 10 Feb 2020 18:03:53 +0100 Subject: [PATCH 2/2] change name --- cli/src/electron/doctor.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cli/src/electron/doctor.ts b/cli/src/electron/doctor.ts index 532b79e3ee..02db526dfc 100644 --- a/cli/src/electron/doctor.ts +++ b/cli/src/electron/doctor.ts @@ -27,8 +27,8 @@ async function checkAppSrcDirs(config: Config) { return `"app" directory is missing in: ${config.electron.platformDir}`; } - const appSrcMainAssetsWwwIndexHtmlDir = join(appDir, 'index.html'); - if (!await existsAsync(appSrcMainAssetsWwwIndexHtmlDir)) { + const appIndexHtml = join(appDir, 'index.html'); + if (!await existsAsync(appIndexHtml)) { return `"index.html" directory is missing in: ${appDir}`; } return checkElectronIndexFile(config, config.electron.platformDir); @@ -51,4 +51,3 @@ async function checkElectronInstall(config: Config) { return 'electron not installed'; } } -