Skip to content

Install, uninstall CLI #72

Open
Open
@steve8708

Description

@steve8708

With meteor I dearly miss the simplicity of npm install {pkg} and npm uninstall {pkg}

My OCD brain was hating having to look up the latest version of packages and manually add them to my packages.json file, so I made a little CLI so I can ./cli install {pkg} and ./cli uninstall {pkg} but this is not the most elegant of solutions

Any thoughts on a possible better solution that can be worked into this package? My quick and dirty CLI tool below

#!/usr/bin/env coffee

# USAGE:
#   ./cli install chalk
#   ./cli uninstall chalk

exec = require('child_process').exec
fs = require 'fs'

argv = process.argv

handleError = (error) ->
  throw error if error

command = argv[2]

switch command
  when 'install'
    pkg = argv[3]

    exec "npm show #{pkg} version", (error, output) ->
      handleError error
      latestVersion = output.trim()

      fs.readFile 'packages.json', 'utf8', (error, contents) ->
        handleError error

        json = JSON.parse contents
        json[pkg] = latestVersion
        newString = JSON.stringify json, null, 2
        newString += '\n'

        fs.writeFile 'packages.json', newString, (error) ->
          handleError error

          console.info "Latest #{pkg} added to ./packages.json"

  when 'uninstall'
    pkg = argv[3]

    fs.readFile 'packages.json', 'utf8', (error, contents) ->
      handleError error

      json = JSON.parse contents
      if json[pkg]
        delete json[pkg]
      else
        console.warn "Huh? #{pkg} is not in your packages.json"
        return

      newString = JSON.stringify json, null, 2
      newString += '\n'

      fs.writeFile 'packages.json', newString, (error) ->
        handleError error

        console.info "#{pkg} removed from packages.json"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions