From ad5cbde866b17c1d17274573fe19c8f7b7605300 Mon Sep 17 00:00:00 2001 From: Peter Colapietro Date: Wed, 13 Sep 2017 14:20:24 -0400 Subject: [PATCH 1/3] Add Atom version to the printed version information Closes #382 See #453 and #473 Returning unknown instead of an empty object based on @50Wliu's suggestion. See https://github.com/atom/apm/pull/745/#discussion_r140909097 --- spec/apm-cli-spec.coffee | 26 +++++++++++++++ src/apm-cli.coffee | 72 ++++++++++++++++++++++++---------------- 2 files changed, 70 insertions(+), 28 deletions(-) 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..56ef392d3 100644 --- a/src/apm-cli.coffee +++ b/src/apm-cli.coffee @@ -100,34 +100,50 @@ 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 + atomVersion: 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' + # workaround if app.asar does not exist. see: https://github.com/atom/atom/issues/6604 + if not fs.existsSync(resourcePath) + resourcePath = resourcePath.replace('.asar', path.sep) + try + {version} = require(path.join(resourcePath, 'package.json')) ? unknownVersion + callback(version) + catch error + callback(unknownVersion) getPythonVersion = (callback) -> npmOptions = From ffdb04378af34683cc4b721df2128b53d326693f Mon Sep 17 00:00:00 2001 From: Peter Colapietro Date: Tue, 17 Oct 2017 22:41:05 -0400 Subject: [PATCH 2/3] Fix variable name to align with the rest --- src/apm-cli.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/apm-cli.coffee b/src/apm-cli.coffee index 56ef392d3..fc510cc11 100644 --- a/src/apm-cli.coffee +++ b/src/apm-cli.coffee @@ -106,7 +106,7 @@ printVersions = (args, callback) -> apm: apmVersion npm: npmVersion node: nodeVersion - atomVersion: atomVersion + atom: atomVersion python: pythonVersion git: gitVersion nodeArch: process.arch From e3970212f52a100a8467a1e844e6a148808cd48a Mon Sep 17 00:00:00 2001 From: Peter Colapietro Date: Wed, 18 Oct 2017 14:33:37 -0400 Subject: [PATCH 3/3] Remove workaround as asar was added back in Atom 1.21 See https://github.com/atom/apm/pull/738\#issuecomment-337573518 --- src/apm-cli.coffee | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/apm-cli.coffee b/src/apm-cli.coffee index fc510cc11..a3cf12dbe 100644 --- a/src/apm-cli.coffee +++ b/src/apm-cli.coffee @@ -136,9 +136,6 @@ printVersions = (args, callback) -> getAtomVersion = (callback) -> config.getResourcePath (resourcePath) -> unknownVersion = 'unknown' - # workaround if app.asar does not exist. see: https://github.com/atom/atom/issues/6604 - if not fs.existsSync(resourcePath) - resourcePath = resourcePath.replace('.asar', path.sep) try {version} = require(path.join(resourcePath, 'package.json')) ? unknownVersion callback(version)