Getting version from package.json

kneumei's Avatar

kneumei

08 Dec, 2015 03:29 PM

I'm using appveyor to build a node app. Some of the artifacts produced have the version number (specified in my package.json file) in their name. So I'd like to be able to get the version number out of the package.json file into a environment variable. I know I could write some powershell without too much trouble, but I was wondering if there was a first class way of doing this? Does appveyor understand package.json?

  1. Support Staff 1 Posted by Feodor Fitsner on 08 Dec, 2015 05:49 PM

    Feodor Fitsner's Avatar

    It's not supported out-of-the-box, so PowerShell is your option :)

  2. 2 Posted by kneumei on 08 Dec, 2015 05:52 PM

    kneumei's Avatar

    Thanks. For anyone coming along later, you can do it like this:

    (Get-Content -Raw -Path package.json | ConvertFrom-Json).version
    
  3. 3 Posted by Kosmas Papadato... on 11 Jan, 2017 09:49 AM

    Kosmas Papadatos's Avatar

    Tried this but didn't work:

    init:
      - git config --global core.autocrlf true
      - ps: $env:package_version = (Get-Content -Raw -Path package.json | ConvertFrom-Json).version

    version: $(package_version)-{build}

  4. 4 Posted by Ilya Finkelshte... on 11 Jan, 2017 05:25 PM

    Ilya Finkelshteyn's Avatar

    Hi Kosmas,

    init scrips happens before clone, therefore package.json is not available at that moment. Also you have to update build details with API, if you need to do it during the build. Please try the following at install stage:

    install:
     - ps: $env:package_version = (Get-Content -Raw -Path package.json | ConvertFrom-Json).version
     - ps: Update-AppveyorBuild -Version "$(package_version)-$env:APPVEYOR_BUILD_NUMBER"
    

    Let us know if this helps.

    --ilya.

  5. 5 Posted by Kosmas Papadato... on 12 Jan, 2017 08:48 AM

    Kosmas Papadatos's Avatar

    It had to be $env:package_version isntead of $(package_version), thanks!

    install:
      - ps: $env:package_version = (Get-Content -Raw -Path package.json | ConvertFrom-Json).version
      - ps: Update-AppveyorBuild -Version "$env:package_version-$env:APPVEYOR_BUILD_NUMBER"

  6. 6 Posted by Ilya Finkelshte... on 12 Jan, 2017 09:18 AM

    Ilya Finkelshteyn's Avatar

    Oh, sure you are right :)

  7. 7 Posted by joelb on 30 Mar, 2018 12:24 AM

    joelb's Avatar

    So, based on the above I have the following line in the 'Install script' section of the Environment configuration page:
    PowerShell (Get-Content -Raw -Path package.json | ConvertFrom-JSON).version

    I'm getting an error stating "'ConvertFrom-JSON' is not recognized as an internal or external command".

    I've confirmed that we're using PS v5.
    What am I missing?
    Thanks

  8. 8 Posted by Ilya Finkelshte... on 30 Mar, 2018 01:59 AM

    Ilya Finkelshteyn's Avatar

    @joelb please share your configuration in YAML format.

  9. 9 Posted by joelb on 30 Mar, 2018 02:03 AM

    joelb's Avatar

    version: 1.0.{build}

    branches:

      only:

      - master

    init:

    - cmd: PowerShell Install-Product node 7.2.1

    install:

    - cmd: "PowerShell $env:package_version = (Get-Content -Raw -Path package.json | ConvertFrom-JSON).version \necho %$env:package_version%\n\nUpdate-AppveyorBuild -Version \"$($env.package_version)\"\n\nnpm install"

    build_script:

    - cmd: >-

        set NODE_ENV=development

     

        npm run build

     

        7z a nexus-client-dev.zip @deploy-package.txt

    test: off

    artifacts:

    - path: nexus-client-dev.zip

      name: nexus-client-dev

     

    Above is the exported yaml.

     

     

    Thanks

     

    From: Ilya Finkelshteyn [***@***]
    Sent: Thursday, March 29, 2018 6:59 PM
    To: Joel Brighton
    Subject: Re: Getting version from package.json [Questions #1954]

     

    // Please reply above this line
    ==================================================

    From: Ilya Finkelshteyn (Support staff)

    @joelb please share your configuration in YAML format.

    On Thu, Mar 29 at 05:24 PM PDT, joelb wrote:

    So, based on the above I have the following line in the 'Install script' section of the Environment configuration page:
    PowerShell (Get-Content -Raw -Path package.json | ConvertFrom-JSON).version

    I'm getting an error stating "'ConvertFrom-JSON' is not recognized as an internal or external command".

    I've confirmed that we're using PS v5.
    What am I missing?
    Thanks

    Having trouble reading this? View this discussion online:

  10. 10 Posted by Ilya Finkelshte... on 30 Mar, 2018 06:27 PM

    Ilya Finkelshteyn's Avatar

    If you run PowerShell (Get-Content -Raw -Path package.json | ConvertFrom-JSON).version on your local Windows machine in command prompt, you will get the same result. If you run PowerShell "(Get-Content -Raw -Path package.json | ConvertFrom-JSON).version" (see quotes added), you will get expected results (provided you have package.json in the current directory.

    Additional notes:

    • not sure if next line with %% will work, it is CMD environment variable notation, not PowerShell
    • not sure why you need to run this in CMD mode at all. It can be easily called in PS mode without calling PowerShell explicitly, as in previous examples in this thread.
  11. Ilya Finkelshteyn closed this discussion on 25 Aug, 2018 02:27 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac