run-example.js 1.1 KB

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/env node
  2. /**
  3. * Lerna installs all the example dependencies into the root `node_modules`.
  4. * To run the examples they need to have access to executables from npm dependencies in their $PATH.
  5. * If you run `npm start` in a dependency folder, the root `node_modules/.bin` is not in the $PATH.
  6. *
  7. * This proxy executable can be run from the repository root using `npm run example`, so the root
  8. * `node_modules/.bin` will be in the $PATH. It then runs `npm start` in the specific example folder,
  9. * which will inherit the $PATH, so the example has access to executables from npm dependencies in both
  10. * its own and in the root `node_modules`.
  11. */
  12. const path = require('path')
  13. const { execSync } = require('child_process')
  14. const exampleName = process.argv[2]
  15. if (!exampleName) {
  16. console.error('Usage: npm run example "name-of-example"')
  17. process.exit(1)
  18. }
  19. const exampleDir = path.join(__dirname, '../examples', exampleName)
  20. const pkg = require(path.join(exampleDir, 'package.json'))
  21. if (pkg.scripts && pkg.scripts.build) {
  22. execSync('npm run build', { cwd: exampleDir, stdio: 'inherit' })
  23. }
  24. execSync('npm start', { cwd: exampleDir, stdio: 'inherit' })