question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[feature request] lerna bootstrap triggering root postinstall hook

See original GitHub issue

Im not sure if this makes sense, as the intention for having root package.json like this is unclear from the docs:

lerna-repo/
  packages/
  package.json
  lerna.json

however its respected during the bootstraping so I think it makes sense to execute (after this line?) postinstall script.

If you think its the right thing to do, I can provide PR for it.

Also if you provide short explanation of how combination of per package package.jsons and root package.json should work together it would be really helpful

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
wberncommented, Aug 21, 2018

While it’s not completely clear to me if this thread wants to address the issue of having to run 2 separate commands for a locally development-ready environment or not, I wanted to contribute with the below solution.

package.json

  ...
  "config": {
    "envs": "node_modules='$INIT_CWD/node_modules'"
  },
  "scripts": {
    "postinstall": "node ./scripts/bootstrap.js",
    "bootstrap": "cross-env $npm_package_config_envs lerna bootstrap --hoist",
    ...

./scripts/bootstrap.js

#!/usr/bin/env node

var cp = require('child_process');
var os = require('os');

var npmCmd = os.platform().startsWith('win') ? 'npm.cmd' : 'npm';

const runCommand = function(command, args, options = {}) {
    console.log(
        'EXECUTING COMMAND:',
        npmCmd,
        args.join(' '),
        '( path:',
        options.path || process.cwd(),
        ')'
    );
    const { error, statusCode } = cp.spawn(npmCmd, args, {
        env: process.env,
        cwd: options.path,
        stdio: [
            process.stdin,
            process.stdout,
            options.ignoreErrors ? 'ignore' : process.stderr,
        ],
    });

    if (statusCode !== 0 && options.ignoreErrors) {
        console.warn('Command had errors, but ignoring.');
        console.error(error);
    }
};

const alreadyExecuted = process.env['LERNA_EXECUTED'];

if(!alreadyExecuted) {
    process.env['LERNA_EXECUTED'] = true;
    runCommand(npmCmd, ['run', 'bootstrap'], { });
}

Any developer can do npm install, and the following will/should happen.

  1. npm install runs as per usual process
  2. postinstall hook triggers, executing bootstrap.js
  3. bootstrap.js executes, which checks if LERNA_EXECUTED is set. If not set, does npm run bootstrap
  4. npm run bootstrap will run lerna, which (with hoisting) implies another npm install being done.
  5. Upon completion by lerna, another postinstall hook triggers, which again executes ./scripts/bootstrap.js
  6. bootstrap.js will this time recognize that LERNA_EXECUTED is set to true, and will gracefully exit.
  7. When script execution is finally over, LERNA_EXECUTED will again become empty (ie echo $LERNA_EXECUTED in the shell will return an empty string)

Tested with: npm: 5.6.0 (package-lock.json-files enabled) node: 8.11.3 (LTS by current writing)

This will ease transition to lerna for me and my colleagues. Since I just discovered this, I will get back if we find any issues. The only real downside now is that you need to run a regular npm install before lerna does it again. On the other hand, we have listed lerna as a local dependency, so it would not be possible for lerna to execute without the initial installation.

PS. The cross-env $npm_package_config_envs part can be excluded (it’s a devDependency). For me it provides a neat way of accessing hoisted node_modules from my lerna packages inside the package.json. This is completely unnecessary if you intend to just use the bin’s like webpack for example, but really valuable if you need to access files directly inside node_modules dependencies. To not use this, just omit that part, and exclude the config section altogether. If anyone knows a more intregrated way of accomplishing this, do let me know!

0reactions
lock[bot]commented, Dec 27, 2018

This thread has been automatically locked because there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

@lerna/bootstrap | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
Husky - Git hooks
Modern native git hooks made easy. Husky improves your commits and more woof! You can use it to lint your commit messages, run...
Read more >
How to Deploy a React Monorepo to Netlify Using Lerna and ...
GitHub Actions workflow that triggers push/pull requests to the main branch. Assuming that all deployments will come from the main branch, you ...
Read more >
@lerna/bootstrap - npm
Bootstrap the packages in the current Lerna repo. Installs all of their dependencies and links any cross-dependencies. When run, this command will:.
Read more >
Third Party Software 7.2 - Confluent
Apache Directory LDAP API Extras Trigger, Apache-2.0; Multi-license: ... Core functionality for the Reactor Netty library, Apache-2.0 ... exit-hook, MIT.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found