0.0.4 • Published 5 years ago

parse-npm-script v0.0.4

Weekly downloads
465
License
MIT
Repository
-
Last release
5 years ago

Parse NPM Script

Parse a given npm script command from package.json & return information.

  • What the script lifecycle looks like
  • How the command really resolves
  • & What the final command run is

Useful for parsing dependencies actually used and figuring out wtf is happening in a complex package.json.

Usage

const path = require('path')
const util = require('util')
const parse = require('./lib')

/* path to your package.json file */
const packagePath = path.join(__dirname, 'tests/fixtures/one.json')

async function runParser() {
  const parsed = await parse(packagePath, 'npm run build')
  console.log(util.inspect(parsed, {
    showHidden: false,
    depth: null
  }))
}

runParser()

/* Parsed contents
{
  command: 'npm run build',
  steps: [{
      name: 'prebuild',
      raw: 'echo a && npm run foo',
      parsed: ['echo a', 'echo foo']
    },
    {
      name: 'build',
      raw: 'echo b && npm run cleanup',
      parsed: ['echo b', 'echo cleanup']
    },
    {
      name: 'postbuild',
      raw: 'echo c',
      parsed: 'echo c'
    }
  ],
  raw: ['echo a', 'echo foo', 'echo b', 'echo cleanup', 'echo c'],
  combined: 'echo a && echo foo && echo b && echo cleanup && echo c'
}
*/

Example:

Parsing a package.json

{
  "name": "parse-npm-script",
  "scripts": {
    "foo": "echo foo",
    "cleanup": "echo cleanup",
    "prebuild": "echo a && npm run foo",
    "build": "echo b && npm run cleanup",
    "postbuild": "echo c"
  },
  "author": "David Wells",
  "license": "MIT"
}

Will result in this output:

{
  command: 'npm run build',
  steps: [{
      name: 'prebuild',
      raw: 'echo a && npm run foo',
      parsed: ['echo a', 'echo foo']
    },
    {
      name: 'build',
      raw: 'echo b && npm run cleanup',
      parsed: ['echo b', 'echo cleanup']
    },
    {
      name: 'postbuild',
      raw: 'echo c',
      parsed: 'echo c'
    }
  ],
  raw: ['echo a', 'echo foo', 'echo b', 'echo cleanup', 'echo c'],
  combined: 'echo a && echo foo && echo b && echo cleanup && echo c'
}