Skip to content

Instantly share code, notes, and snippets.

@davidbarral
Created May 13, 2019 08:21
Show Gist options
  • Save davidbarral/17f942ac22f4547acb049cca8f797222 to your computer and use it in GitHub Desktop.
Save davidbarral/17f942ac22f4547acb049cca8f797222 to your computer and use it in GitHub Desktop.
Monorepo: new publish script
#!/bin/bash -e
REPO=$@
publishable() {
export REPO
/usr/bin/env node <<'SCRIPT'
const { execSync } = require("child_process");
const local = require("./package.json");
try {
const info = execSync(`npm info ${local.name} --json ${process.env.REPO} 2> /dev/null`).toString();
const remote = JSON.parse(info);
if (remote.versions.includes(local.version)) {
console.log(`[skip] ${local.name}@${local.version}`);
process.exit(1);
} else {
console.log(`[publish] ${local.name}@${local.version} (was ${remote.version})`);
process.exit(0);
}
} catch(e) {
console.log(`[publish] ${local.name}@${local.version} (new package)` );
process.exit(0);
}
SCRIPT
}
publish() {
(
cd $1
publishable && npm publish $REPO || echo -n ""
)
}
for i in ./packages/awesome-*; do publish $i; done
@davidbarral
Copy link
Author

davidbarral commented May 17, 2019

I should have mentioned that we call these script like this:

./scripts/publish --registry https://our.private.reg

That's the reason why we do not put the --registry flag in the script.

@ng-hai
Copy link

ng-hai commented May 18, 2019

Do you have to copy .npmrc file from the root project to each leaf package before publishing?
I have tried and got the unauthorized error so many times until today I figure out that npm does not read the .npmrc file from the root project. The private registry I'm using is Verdaccio.

@davidbarral
Copy link
Author

For us is enough to have an .npmrc in the root of the project. Our CI (Jenkins + Docker) setups this .npmrc file before publishing. We use Nexus as our private registry. Our customer CI (Bamboo) uses an .npmrc in the home folder. They use Artifactory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment