How use backticks in npm scripts on windows

Viewed 1039

My workflow is npm scripts, running commands in my node_modules along with simple shell commands.

Unfortunately this makes it difficult for windows users due to my using backticks in some commands (see Example below). I have a pull request volunteering to convert to shellJS/shx for my build so my repo will build cross-platform but we can't figure out a solution for backticks in npm scripts.

Question:

  • What shell does npm use? On windows it appears to not support backticks.
  • Is there a workaround? Piping doesn't help, alas, rm, mkdir etc don't use stdin.

Example backtick use in package.json:

  "mkdirs": [
    "dist/AS",
    "libs",
    "models/scripts"
  ],
  "scripts": {
    "mkdirs": "mkdir -p `bin/pkgkey.js mkdirs`",
    ....

.. where the bin/pkgkey.js mkdirs script simply returns the mkdirs array. This may seem odd but it's great for organizing npm-style workflow.

The pkgkey script (simplified):

#!/usr/bin/env node

const fs = require('fs')
const json = JSON.parse(fs.readFileSync('package.json'))
const key = process.argv[2]
let val = json[key]
if (Array.isArray(val)) val = val.join(' ')

process.stdout.write(val)
1 Answers
Related