npm pre/post hook with wildcard match - node.js

Let's say I have the following scripts in my package.json
{
"scripts": {
"release:public": "....",
"release:beta": "...."
}
}
But now I want to add a prerelease script that is identical for both release:public and release:beta. Is that possible to have a prerelease:* or another way to runs before both scripts?

I do understand your question and the example from RobC is usable, but the naming was not recommended. When you use pre in a script command name followed by a command name that also exists. The part wil run before (previous) the other (https://docs.npmjs.com/cli/v8/using-npm/scripts#npm-run-user-defined)
{
...
"scripts": {
"beforerelease": "....",
"release:public": "npm run beforerelease && ....",
"release:beta": "npm run beforerelease && ...."
},
...
}
But that is an even amount of work then using the pre-tag functionality like this
{
...
"scripts": {
"prerelease:public": "....",
"release:public": "....",
"prerelease:beta": "....",
"release:beta": "...."
},
...
}
Like Kousha probably has, i have a package with a lot of different run scripts. And i want to use one script that runs before a lot of the others. So the question is still standing: Is it possible in any way to use wildcards in de command part of a script tag in a package.json?

Related

Run NPM script before all other scripts

I'm looking for a way to run a script any time I use npm run <script-name>.
My current use-case is that I want to run a script that ensures the dev environment is setup correctly before running any of my other scripts.
So, what I need is something like this:
{
"run-before-all-scripts": "./setup_environment.sh",
"scripts": {
"script_one": "do something",
"script_two": "do somethingelse"
}
...
}
The best I have come up with is this:
{
"scripts": {
"setup_environment": "./setup_environment.sh",
"script_one": "npm run setup_environment && do something",
"script_two": "npm run setup_environment && do somethingelse"
}
...
}
The only better solution I've come up with is to just create a wrapper shell script around the npm command, so I can call fancy_npm_run <command>. That just seems kind of messy though.
Is there a better way to handle this?
I've read npm script hook on multiple scripts but it is not about running before all scripts and also is not answered.

Single npm package with two commands

I have a node.js package described by this JSON :
"name": "mycommand",
"main": "index.js",
"bin": {
"mycommand": "./index.js",
The index.js file contains this code :
#!/usr/bin/env node
const app = require('./src/app.js')
const { Logger } = require('./utils/Logger')
app.init()
And the app.js contains the code of a command line tool based on yargs.
Now, I would like to add a 2nd command in this package, but I don't know how should I proceed since there can be only one "main".
Anybody has an example somewhere ?
Maybe you are looking for scripts instead of bin?
Now, I would like to add a 2nd command in this package, but I don't know how should I proceed since there can be only one "main".
The bin mapping has nothing to do with your main file.
Your command is getting symlinked, you can simply add more:
"bin": {
"mycommand": "./index.js",
"my-other-command": "./other-command.js",
Or you can use your mycommand and parse the arguments in the script app.js using process.argv:
https://nodejs.org/en/knowledge/command-line/how-to-parse-command-line-arguments/

Using NodeJS development dependencies in Heroku review-app post-deploy step

I have a (demo) application hosted on Heroku. I've enabled Heroku's "review app" feature to spin up new instances for pull request reviews. These review instances all get a new MongoDB (on mLab) provisioned for them through Heroku's add-on system. This works great.
In my repository, I've defined some seeder scripts to quickly get a test database up and running. Running yarn seed (or npm run seed) will fill the database with test data. This works great during development, and it would be perfect for review apps as well. I want to execute the seeder command in the postdeploy hook of the Heroku review app, which can be done by specifying it under the environment.review section of the app.json file. Like so:
{
"name": "...",
"addons": [
"mongolab:sandbox"
],
"environments": {
"review": {
"addons": [
"mongolab"
],
"scripts": {
"postdeploy": "npm run seed"
}
}
}
}
The problem is, the seeder script relies on some development-only dependencies (faker, ts-node [this is a TypeScript project], and mongo-seeding) to execute. And these dependencies are not available in the postdeploy phase of an Heroku app.
I also don't think that "compiling" the typescript in the regular build step is the best idea. This seeder script is only used in development (and review apps). Besides, I'm not sure that would resolve the issue with missing dependencies like faker.
How would one go about this? Any tricks I'm missing?
Can I maybe skip Heroku's step where it actively deletes development dependencies? But only for review apps? Or even better, can I "exclude" just the couple of dependencies I need, and only for review apps?
The Heroku docs indicate that when the NODE_ENV variable contains anything but "production", the devDependencies will not be removed after the build step.
To make sure this only happens for Heroku review apps, you can set the NODE_ENV variable under the environments.review section of the app.json file. The following config should do the trick:
{
"name": "...",
"addons": [
"mongolab"
],
"environments": {
"review": {
"addons": [
"mongolab:sandbox"
],
"env": {
"NODE_ENV": "development"
},
"scripts": {
"postdeploy": "npm run seed"
}
}
}
}

Defining a argument/wildcard as part of an npm's script key/name

I have a package.json defined like this:
{
"name": "example",
"version": "1.0.0",
"description": "example",
(...)
"scripts": {
"something:special": "script-that-does-something.sh",
(...)
},
(...)
}
The script-that-does-something.sh gets an argument to do something, meaning I'll usually do something like:
npm run something:special ARGUMENT
The above is perfectly fine and works, but I wonder if it's possible for me to define the script in the package.json in order to be able use any argument while still running the npm command like this:
npm run something:special:ARGUMENT
I too was looking for a way to avoid having to write out every single script for my tests and commands.
At least it forces you to do some good documentation.

Nodejs example for pachyderm

I am new to Pachyderm.
I have a pipeline to extract, transform and then save in the db.
Everything is already written in nodejs, docekrized.
Now, I would like to move and use pachyderm.
I tried following the python examples they provided, but creating this new pipeline always fails and the job never starts.
All my code does is take the /pfs/data and copy it to /pfs/out.
Here is my pipeline definition
{
"pipeline": {
"name": "copy"
},
"transform": {
"cmd": ["npm", "start"],
"image": "simple-node-docker"
},
"input": {
"pfs": {
"repo": "data",
"glob": "/*"
}
}
}
All that happens is that the pipeline fails and the job never starts.
Is there a way to debug on why the pipeline is failing?
Is there something special about my docker image that needs to happen?
Offhand I see two possible issues:
The image name doesn't have a prefix. By default, images are pulled from dockerhub, and dockerhub images are prefixed with the user who owns the image (e.g. maths/simple-node-docker)
The cmd doesn't seem to include a command for copying anything. I'm not familiar with node, but it looks like this starts npm and then does nothing else. Perhaps npm loads and runs your script by default? If so, it might help to post your script as well.

Resources