Parse package.json "script" commands to see how they resolve
Parse a given npm
script command from package.json
& return information.
script
lifecycle looks likeUseful for parsing dependencies actually used and figuring out wtf is happening in a complex package.json
.
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'
}
*/
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'
}