{"size": 1405, "ext": "js", "max_stars_repo_path": "packages/cli/lib/utils/version.js", "max_stars_repo_name": "morphatic/gridsome", "max_stars_repo_stars_event_min_datetime": "2018-09-18T09:59:23.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T08:41:57.000Z", "max_issues_repo_path": "packages/cli/lib/utils/version.js", "max_issues_repo_name": "morphatic/gridsome", "max_issues_repo_issues_event_min_datetime": "2018-09-15T15:48:37.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-30T18:56:45.000Z", "max_forks_repo_path": "packages/cli/lib/utils/version.js", "max_forks_repo_name": "morphatic/gridsome", "max_forks_repo_forks_event_min_datetime": "2018-09-28T07:15:45.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-31T06:23:02.000Z"}
{"max_stars_count": 8577, "max_issues_count": 1524, "max_forks_count": 667, "avg_line_length": 27.0192307692, "max_line_length": 67, "alphanum_fraction": 0.6576512456}
const chalk = require('chalk')
const resolveCwd = require('resolve-cwd')

module.exports = function resolveVersions (pkgPath) {
  const cliVersion = require('../../package.json').version
  const versions = [`@gridsome/cli v${cliVersion}`]

  if (pkgPath) {
    try {
      versions.push(...resolveProjectVersions(pkgPath))
    } catch (err) {
      versions.push('\nFailed to read installed gridsome version:')
      versions.push(chalk.red(err.message))
    }
  }

  return versions.join('\n')
}

function resolveProjectVersions (pkgPath) {
  const versions = []

  const projectPkgJson = require(pkgPath)
  const { devDependencies = {}, dependencies = {}} = projectPkgJson
  const packages = { ...devDependencies, ...dependencies }

  if (packages.gridsome) {
    const version = resolvePackageVersion('gridsome')
    if (version) versions.push(`gridsome v${version}`)
  }

  // for (const name in packages) {
  //   if (/^@?gridsome[-|\/]/.test(name)) {
  //     const version = resolvePackageVersion(name)
  //     if (version) versions.push(`- ${name} v${version}`)
  //   }
  // }

  // if (versions.length) {
  //   versions.unshift(chalk.grey('\nProject dependencies:'))
  // }

  return versions
}

function resolvePackageVersion (name) {
  const pkgPath = resolveCwd.silent(`${name}/package.json`)
  const pkgJson = pkgPath ? require(pkgPath) : null

  return pkgJson ? pkgJson.version : null
}
