Skip to content

Latest commit

 

History

History
101 lines (81 loc) · 4.28 KB

EXAMPLES.md

File metadata and controls

101 lines (81 loc) · 4.28 KB

package-scripts examples

nps-utils

Many common patterns in nps can be accomplished with the nps-utils package. Definitely recommended to check it out!

Links to projects

Examples of how people use nps:

  • nps: scripts, package-scripts.js
  • react-component-template uses nps to implement shareable npm scripts. See then how dependent react-swap can reuse them. (Gotcha: - use process.cwd() as the base for all paths).
  • Hypercubed/EventsSpeedTests uses nps to automate benchmark running and reporting in node and the browser. package-scripts.js enables us to keep our scripts DRY. Combined with grunion allows benchmarks to be run, serially or concurrently, on glob patterns.
  • SmithersAssistant/Smithers is an electron based personal assistant. Smithers works on multiple platforms. Smithers uses nps to dynamically find the current platform and execute the dev environment. Now we don't have to manually update the package.json scripts when you are on a different platform! scripts, package-scripts.js

Inline Examples

cross platform scripts

One of the big challenges with open source projects is that users and contributors have varying platforms. Because you can't determine the platform in the package.json, you have to either have to duplicate scripts (like having a build:windows and build:unix script), find CLIs that are cross platform (like cross-env), or write your logic in a separate file to handle the platform.

You can also use cross-var in basically the same way to do the same for using environment variables in your scripts so it works on both windows and mac/linux

With package-scripts, you can really easily have a single script that uses the platform to determine what should be run. For example:

var isWindows = require('is-os').isWindows
var removeDist = isWindows ? 'rmdir ./dist' : 'rm ./dist'
module.exports = {
  scripts: {
    build: {
      description: 'Build the project (built based on the platform)',
      script: removeDist + ' && babel --copy-files --out-dir dist src'
    }
  }
}

Note, in this specific scenario, I'd recommend that you actually use rimraf, but I think you get the idea 😄. This is a pretty nice win over traditional npm scripts 👍

parallel scripts

Often, scripts can run concurrently because they are not interdependent. We recommend nps-utils which uses concurrently for this:

const npsUtils = require('nps-utils')

module.exports = {
  scripts: {
    sayThings: npsUtils.concurrent({
      hi: {script: 'echo hi'},
      hey: {script: 'echo hey', color: 'blue.bgGreen.dim'},
      hello: 'echo hello there',
    }),
    validate: npsUtils.concurrent.nps(
      'build',
      'lint',
      'test',
      'order.sandwich',
    ),
    build: 'webpack',
    lint: 'eslint .',
    test: 'jest',
    order: {sandwich: 'makemeasandwich'}
    // etc...
  }
}

Instructions

Thanks for using nps! I'm glad/I hope it's been helpful to you. Please add a link to your example here. If you're adding a GitHub link, please make sure you hard-link so future changes in your codebase don't break the link. The keyboard shortcut for this is y.

Also, if you'd like to be included as a contributor, please follow the Contribution Guidelines and add yourself as a contributor to the .all-contributorsrc. The best way to do this is by running:

npm start addContributor <YOUR_GITHUB_USERNAME> example