"lerna changed" lists all packages - npm

I've cloned a monorepo I was working on onto a new dev machine, all packages from the repo have been published already. But when I run lerna changed - it says "assuming all packages changed" and lists all the packages from the repo. And if I run lerna publish from-package - it correctly says that there's nothing to publish.
My lerna.json
{
"packages": [
"packages/*"
],
"command": {
"publish": {
"registry": "https://npm.pkg.github.com/"
}
},
"npmClient": "yarn",
"useWorkspaces": true,
"version": "independent"
}
Any idea why it's doing this and how I can make changed correctly detect only changed packages?

I ended up using lerna ls --since master instead. Since all PRs in our case are done from other branches into master, comparing working branch to master seems to make sense.

I had the same problem. This worked for me:
lerna publish ----include-merged-tags

Related

Monorepo with npm workspaces - Lerna version fails when inter-package dependency is introduced for existing packages

I am working on a monorepo with 2 packages, say child and parent, initially not dependent on each other and already published to registry.
However, when I add inter-package dependency, lerna version command fails.
npm workspaces was provided in root package.json
useWorkspaces was provided in lerna.json
All was fine, till I decided to add package parent as dependency of package child.
packages/child/package.json was updated like so:
{
"name": "child",
"version": "0.1.13",
"description": "this package is being updated to depend on parent",
"main": "dist/index.js",
"dependencies": {
"parent": "*" // I added this
}
}
I had updates to both child and parent. Lets say published versions of parent was 0.2.13 and child was 0.1.13 prior to update.
npm install and the symlinks worked.
Build was successful.
During version however, after prompting for version bump with conventional-commits, lerna attempts npm install command and fails due to package parent version 0.2.14 being not available in npm remote registry.
It will not be available, as this version is going to be published only now.
On version command (i am using conventional-commits),
Lerna asked if parent -> 0.2.14 and child -> 0.1.14 was okay, it was..
Lerna updated the package.json of package child to be as follows:
{
"name": "child",
"version": "0.1.14", // lerna changed this
"description": "this package is being updated to depend on parent",
"main": "dist/index.js",
"dependencies": {
"parent": "^0.2.14" // lerna changed this
}
}
Lerna updated the package.json of package parent to be as follows:
{
"name": "parent",
"version": "0.2.14", // lerna changed this
"description": "this package will be dependency of child",
"main": "dist/index.js",
}
Lerna tried to do npm install on both child and parent packages
This fails with reason being package parent version 0.2.14 is not yet available to install, since its just going to be published now.
I followed this https://lerna.js.org/docs/getting-started.
It says
The "header": "*" and "footer": "*" tell Lerna to link the contents of the header and footer as if they were published to the registry.
I was expecting the lerna version command to work and create new version commit and tag as it was doing before I added inter-package dependencies using "*".
Should I be publishing parent first with new version 0.2.14 and then update child to point to this ?
If that is the case, should I always publish parent first whenever there are breaking changes. ?
What is the right way to do this ?
PS: Looks like it fails here: https://github.com/lerna/lerna/blob/main/commands/version/index.js#L634
Should I just remove package-lock.json from root ?

VSCode Launch.json -- Use globally installed jest

I have a current workspace structure as follows
Each of the folders (auth, client, tickets, common) have their own node_modules folder (I'm writing microservices). The .vscode folder has a launch.json file.
My tickets folder is as follows
I want to be able to add the following script to launch.json
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"${fileBasenameNoExtension}",
"--config",
"${workspaceRoot}/jest.config.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true
},
I.e, I want to be able to use the debugger on the current open test file.
But, as you can see, we have
"program": "${workspaceFolder}/node_modules/.bin/jest",
The root workspacefolder has no node_modules. Jest is installed separately in each microservice.
I got do a hack like ${workspaceFolder}/tickets/node_modules/.bin/jest. But that seems messy -- What if I was running tests in the auth folder? It'll work but ... just messy.
Alternatively, I can point to my global installation of jest... but i can't seem to find the correct syntax to include it (I know that my global npm installed packages are here: /usr/local/lib/node_modules)....
Do you guys have any solutions? I'd appreciate the help/advice, thanks!
Open a terminal and install jest as a globally available package
$ npm install jest --global
$ jest --version
26.6.3
$ whereis jest
/usr/local/bin/jest
Update your launch.json
"program": "/usr/local/bin/jest",

lerna publish and npm pack failing to package all the files in "dist" folder

I am trying to build my first Angular Component package using lerna and it was working pretty well until I realized I had to add "ng-packagr" to get all of the HTML bundled inline with the rest of the code. After adding that support and getting it to work now all of a sudden my files are not getting published into the tarball.
Here is my package.json
{
"name": "#custom/core",
"version": "0.0.7",
"description": "Test",
"main": "./dist/bundles/custom-core.umd.min.js",
"module": "./dist/esm2015/custom-core.js",
"typings": "./dist/index.d.ts",
"$schema": "./node_modules/ng-packagr/package.schema.json",
"ngPackage": {
"lib": {
"entryFile": "./src/index.ts"
},
"whitelistedNonPeerDependencies": [
"."
]
},
"scripts": {
"build": "ng-packagr -p package.json"
},
"files": [
"dist"
],
...
My dist folder contains all kinds of folders like this:
But then when I run a lerna publish or npm pack this is what happens:
As you can see only 1 file gets added to the tarball...
Does anyone know why this is happening all of a sudden? I've tried playing around with my .gitignore thinking maybe it was forcing the packaging to ignore these other files but it wasn't that.
UPDATE
Ok so I found that the culprit is ng-packagr. When I run my npm run build which uses ng-packagr -p package.json to build the different module packages, that CLI is also generating a package.json that goes inside of my dist folder. When the npm pack or lerna publish attempt to package everything using a package.json they must be looking at the generated on in dist rather than the one in the folder above it.
I'm not sure how I should be fixing this.
My solution was to abandon my plan to use the files key in the package.json file and to instead use a .npmignore file. Here is a copy of mine:
# Node generated files
node_modules
npm-debug.log
assets
package-lock.json
# aot files
aot
# OS generated files
Thumbs.db
.DS_Store
# Ignored files
*.ts
!*.d.ts
tsconfig.json
tsconfig-aot.json
tslint.json
*.tgz
config
src

How to make a forked dependency in package.json run its prepare script during npm install?

I forked a NPM module and modified it in my fork. In my fork, the command gulp needs to be run to compile the module. Running this command will output the file ical-expander-dist.js into the dist folder. This file should become part of the fork package.
The adjusted package.json in my fork looks like this:
{
"name": "ical-expander",
"version": "1.1.1",
"description": "ICS / iCal / iCalendar parser / expander",
"main": "dist/ical-expander-dist.js", // adjusted path
"scripts": {
"prepare": "gulp" // added script
},
// [SNIP] - excluded irrelevant lines
"files": [
"dist/ical-expander-dist.js" // adjusted path
]
}
Now I want to make my fork a dependency of another project.
{
"name": "otherproject",
"title": "Other Project",
// [SNIP] - excluded irrelevant lines
"dependencies": {
"ical-expander": "github:haukepribnow/ical-expander"
}
// [SNIP] - excluded irrelevant lines
}
After executing npm install in otherproject's root folder, the path ./node_modules/ical-expander will contain the files LICENSE, README.md and package.json. It will however not contain dist/ical-expander-dist.js.
It looks like the prepare script of my ical-expander fork is not being run during npm install in otherproject.
So my question boils down to: How can I make sure that preparation scripts for compiling my fork are being run when executing npm install in a project that has my fork as one of its dependencies?
Sometimes it's very simple: Make sure to use npm version 5.0.0 or higher.
According to the npm changelog, npm install will run dependencies' prepare scripts correctly from 5.0.0 on.

Grunt task to clone/checkout a git project

I'm working on a project that is built using grunt. It depends on an external repo (https://github.com/facebook/xctool) that I would like to clone/pull during npm install or grunt mySetupTask.
I've seen trails of a grunt-gitco plugin at http://gruntjs.com/plugins/checkout, but it does not seem to be available.
Any good starting point for this?
Either set up a npm postinstall script in your package.json:
{
"name": "mypackage",
"scripts": {
"postinstall": "git clone git://github.com/facebook/xctool.git"
}
}
Or use grunt-shell to execute the command to clone the repo:
grunt.initConfig({
shell: {
gitclone: {
command: 'git clone git://github.com/facebook/xctool.git'
}
}
});
There is now a Grunt plugin for this. Not sure if it was available at the time. I'm still having some issues getting it working.
https://npmjs.org/package/grunt-git
gitclone:
clone:
options:
repository: "https://github.com/imaginethepoet/autojqm"
branch: "master"
directory: "repo"
Did you know that npm install support git URLs?
npm install git://github.com/facebook/xctool.git
Docs