Skip to content

karlhorky/npm-tricks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 

Repository files navigation

npm Tricks

A collection of useful npm scripts tricks

npm Scripts: Get Directory where Command was Run

Get the current ("initial") directory that an npm script was run:

// <project root>/package.json
{
  "scripts": {
    "deck": "mdx-deck $INIT_CWD/index.mdx"
  }
}

Usage:

packages/presentation-1 $ npm run deck   # runs mdx-deck /Users/user/projects/decks/packages/presentation-1/index.mdx

References

Background / Discussion: npm/npm#9374 (comment)

npm Scripts: Regular expressions with sed

If you want to use regular expressions with features like backreferences, you may consider using sed within your npm script.

The escaping in the regular expression is a bit weird (probably because of JSON).

This example uses the s@pattern@replace@ syntax to avoid having to escape the folder slashes.

{
  "scripts": {
    "names": "for file in dir/*.md; do echo `echo $file | sed 's@dir/\\(.*\\)\\.md@\\1.pdf@'`; done"
  }
}

Usage:

# Files within dir:
# dir/abc.md
# dir/def.md

npm run names
abc.pdf
def.pdf

npm Scripts: Restart a Process if it Exits

If a process exits for any reason, restart it, with an optional message.

{
  "scripts": {
    "develop": "until gatsby develop; do echo "Gatsby crashed with exit code $?. Restarting.." >&2; sleep 1; done"
  }
}

Usage:

$ npm run develop

References

Stack Overflow: https://stackoverflow.com/a/697064/1268612

npm Scripts: Run a Command for All Files in a Directory

To loop / foreach over all files matching a pattern.

{
  "scripts: {
    "build": "for file in dir/*.md; do md-to-pdf $file; done"
  }
}

Usage:

npm run build   # will loop over all the .md files in the directory "dir" and run md-to-pdf with each

npm Scripts: Shell Positional Parameters

Ever want to use Bash positional parameters / arguments / variables in npm scripts?

Here's two ways:

{
  "scripts": {
    "1": "f(){ mdx-deck $1 index.mdx; }; f",
    "2": "bash -c 'mdx-deck $1 index.mdx' --"
  }
}

Usage:

npm run 1         # runs mdx-deck index.mdx
npm run 1 build   # runs mdx-deck build index.mdx

More complex examples

Using all positional parameters via the $* shell variable

{
  "scripts": {
    "1": "f(){ mdx-deck $* index.mdx; }; f",
    "2": "bash -c 'mdx-deck $* index.mdx' --"
  }
}

Usage:

npm run 1 build blah another   # runs mdx-deck build blah another index.mdx

All positional parameters via $* with default values

Default values are achieved by testing values of $1 and whether something is included in $*:

{
  "scripts": {
    "1": "f(){ mdx-deck $* $([[ $1 = build && ! $* =~ '-d' ]] && echo \"-d default\" || echo \"\") index.mdx; }; f",
    "2": "bash -c 'mdx-deck $* $* $([[ $1 = build && ! $* =~ '-d' ]] && echo \"-d default\" || echo \"\") index.mdx' --"
  }
}

Usage:

npm run 1 build                 # runs mdx-deck build -d default index.mdx
npm run 1 build -d different    # runs mdx-deck build -d different index.mdx

References

Tweet 1: https://mobile.twitter.com/karlhorky/status/1136577374072573952
Tweet 2: https://mobile.twitter.com/karlhorky/status/1136584417533730816
Background / Discussion: npm/npm#9627 (comment)

About

A collection of useful npm scripts tricks

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published