diff --git a/spec/apm-cli-spec.coffee b/spec/apm-cli-spec.coffee index 2c8843f47..0c9856787 100644 --- a/spec/apm-cli-spec.coffee +++ b/spec/apm-cli-spec.coffee @@ -1,3 +1,5 @@ +path = require 'path' +temp = require 'temp' fs = require 'fs' apm = require '../lib/apm-cli' @@ -25,6 +27,11 @@ describe 'apm command line interface', -> callback = jasmine.createSpy('callback') apm.run(['-v', '--no-color'], callback) + testAtomVersion = '0.0.0' + tempAtomResourcePath = temp.mkdirSync('apm-resource-dir-') + fs.writeFileSync(path.join(tempAtomResourcePath, 'package.json'), JSON.stringify(version: testAtomVersion)) + process.env.ATOM_RESOURCE_PATH = tempAtomResourcePath + waitsFor -> callback.callCount is 1 @@ -35,6 +42,25 @@ describe 'apm command line interface', -> expect(lines[0]).toBe "apm #{require('../package.json').version}" expect(lines[1]).toBe "npm #{require('npm/package.json').version}" expect(lines[2]).toBe "node #{process.versions.node} #{process.arch}" + expect(lines[3]).toBe "atom #{testAtomVersion}" + + describe 'when the version flag is specified and apm is unable find package.json on the resourcePath', -> + it 'prints unknown atom version', -> + callback = jasmine.createSpy('callback') + apm.run(['-v', '--no-color'], callback) + + testAtomVersion = 'unknown' + tempAtomResourcePath = temp.mkdirSync('apm-resource-dir-') + process.env.ATOM_RESOURCE_PATH = tempAtomResourcePath + + waitsFor -> + callback.callCount is 1 + + runs -> + expect(console.error).not.toHaveBeenCalled() + expect(console.log).toHaveBeenCalled() + lines = console.log.argsForCall[0][0].split('\n') + expect(lines[3]).toBe "atom #{testAtomVersion}" describe 'when an unrecognized command is specified', -> it 'prints an error message and exits', -> diff --git a/src/apm-cli.coffee b/src/apm-cli.coffee index fde686552..a3cf12dbe 100644 --- a/src/apm-cli.coffee +++ b/src/apm-cli.coffee @@ -100,34 +100,47 @@ printVersions = (args, callback) -> getPythonVersion (pythonVersion) -> git.getGitVersion (gitVersion) -> - if args.json - versions = - apm: apmVersion - npm: npmVersion - node: nodeVersion - python: pythonVersion - git: gitVersion - nodeArch: process.arch - if config.isWin32() - versions.visualStudio = config.getInstalledVisualStudioFlag() - console.log JSON.stringify(versions) - else - pythonVersion ?= '' - gitVersion ?= '' - versions = """ - #{'apm'.red} #{apmVersion.red} - #{'npm'.green} #{npmVersion.green} - #{'node'.blue} #{nodeVersion.blue} #{process.arch.blue} - #{'python'.yellow} #{pythonVersion.yellow} - #{'git'.magenta} #{gitVersion.magenta} - """ - - if config.isWin32() - visualStudioVersion = config.getInstalledVisualStudioFlag() ? '' - versions += "\n#{'visual studio'.cyan} #{visualStudioVersion.cyan}" - - console.log versions - callback() + getAtomVersion (atomVersion) -> + if args.json + versions = + apm: apmVersion + npm: npmVersion + node: nodeVersion + atom: atomVersion + python: pythonVersion + git: gitVersion + nodeArch: process.arch + if config.isWin32() + versions.visualStudio = config.getInstalledVisualStudioFlag() + console.log JSON.stringify(versions) + else + pythonVersion ?= '' + gitVersion ?= '' + atomVersion ?= '' + versions = """ + #{'apm'.red} #{apmVersion.red} + #{'npm'.green} #{npmVersion.green} + #{'node'.blue} #{nodeVersion.blue} #{process.arch.blue} + #{'atom'.cyan} #{atomVersion.cyan} + #{'python'.yellow} #{pythonVersion.yellow} + #{'git'.magenta} #{gitVersion.magenta} + """ + + if config.isWin32() + visualStudioVersion = config.getInstalledVisualStudioFlag() ? '' + versions += "\n#{'visual studio'.cyan} #{visualStudioVersion.cyan}" + + console.log versions + callback() + +getAtomVersion = (callback) -> + config.getResourcePath (resourcePath) -> + unknownVersion = 'unknown' + try + {version} = require(path.join(resourcePath, 'package.json')) ? unknownVersion + callback(version) + catch error + callback(unknownVersion) getPythonVersion = (callback) -> npmOptions =