Writing a script to automate npm command - javascript

I am working on an angular js project and I would like to automate The following two commands.
./node_modules/protractor/bin/webdriver-manager update
./node_modules/protractor/bin/webdriver-manager start
The issue is that I am working on a small angular project on github. I added all dependencies required to my package.json, However when my friend pulled it from git he was able to install protractor but he could not get webdriver to start unless he ran the above two commands. So i wanted to write some script to automate it and better yet even add protractor ./conf.js to it.
So I did research and I am aware that I can write a npm script but I was not able to find a proper document that showed where to write the script and how to execute it. I would appreciate all suggestions.

You can add a scripts property to your package.json with the command you wish you run.
"scripts": {
"prostart": "./node_modules/protractor/bin/webdriver-manager start",
"proupdate": "./node_modules/protractor/bin/webdriver-manager update"
}
you would then run these by typing npm run prostart or npm run proupdate which would look for those commands in your package.json.

In addition to Josh's answer, the script start could be run as npm start as start is a special keyword, but update should be run as npm run update because npm update is another npm command entirely.
For any other command besides start and test (I think), you have to preface it with npm run ...

Related

Can I include bash/sh files as dependencies in package.json?

I have a bash script which I wrote for publishing modules to npm: publish.sh
As I still work on tweaking this script a lot, every time I change it, I need to make the changes in every copy of it in every npm module I am managing.
Is there a way to include this as a dependency in my package.json file so that I just need to run npm update; npm install in order to update it? Maybe the sh file would need to be executed by some wrapper javascript or something like that..?
"scripts": {
"start": "sh ./scripts/publish.sh",
...
}
If you need access to the script in a single project, you can put it in the "scripts" object in package.json.
"scripts": {
"publish": "/bin/sh publish.sh"
}
The above can be run with npm run publish. See npm run-script docs for more information.
If you literally want to add the shell script as a dependency that can be installed (from the public registry or a private registry) with npm install, you can absolutely do that. Lots of not-JavaScript executable things are available in the npm repository. You'll want to create a package.json for the dependency and specify the location of your shell script with the "bin" entry.
An example package you can look at if you get stuck is notes.sh. The source code is on GitHub. Look at the package.json to see how they specified the scripts to run in the "bin" entry. You'll still need to create a "script" entry in your other project to run the installed shell script (or you can run it as an npm hook) though, unless you're running it manually or spawning a child process in your code or something.

NPM package for NPM scripts

I'm trying to write an npm package that will add a specific npm script to whatever package.json on which it is depended. Nothing in the npm package.json / script docs is bringing me remotely close.
I'm basically trying to do this:
I install an npm package (call it 'cool-thing')
cool-thing, by installing, adds an npm script to my existent package.json
I can call cool-thing on the command line and it will perform whatever action is specified in the package.json
Anyone know how I could do this?
I am assuming you are working on a package cool-parent which depends on package cool-thing because you want to run the executable that cool-thing provides.
Normally, you would manually modify the cool-parent package.json to add a script:
"scripts": {
"cool-thing": "cool-thing"
}
Now for user convenience, you would like 1) to automate this modification. Not only that, you want to 2) make the modification automatically after the package is installed as a dependency.
There are some drawbacks to part 2, the developer of cool-parent
might not want to add a script,
might already have a script called cool-thing,
might not have a package.json,
...
I consider making modification outside of the package itself during install time to be an undesirable side effect.
If you still want to do it, you can using a postinstall script in cool-thing. You would need to figure out the location of the package.json of the parent if there is one through working directories and perhaps environment variables that npm provides.

Is it possible to run a script on a node.js dependency?

I'm working on a node package which has an installation script to set up a simple application structure. It's not doing anything too strenuous, just creating a few folders and creating an "admin" user if one doesn't exist.
At the moment it's doing a bunch of checks every time the application starts and does the setup process if required. Is there any way of doing it through the command line? Something along the lines of the user just typing my-package install or npm run my-package-install to call the script?
yes, with npm, you can build a simple commandline tool.
And you can define/parse the args from terminal.
You need to define the script you want to run in your package.json.
"bin": {
"your-command": "bin/commit.js"
}
And run npm link, this would make the command available.
For more detail, check Building a simple command line to with npm
Hope this can answer your question. : )

How to run specific tests with frisby?

We are using frisby to run our integration tests and while developing them, it would be handy to execute one specific one or a group of tests, without having run all of them and see extra noise. Right now I am commenting out all the ones I don't want to run, which is getting tedious.
Is there a way, from the command line, to run specific frisby tests?
So basically, instead of
npm test
I want to be able to say
npm test --name: posts
Or something like that. I found this post about jasmine-only, but I'm not sure it will satisfy my needs.
Thanks so much!
I'm not sure if you're still looking for answer, but this is pretty simple.
Firstly install latest version of jasmine-node from command line: npm install jasmine-node -g
Then to run particular test use: jasmine-node --coffee putTestNameHere
Install jasmine-node module. Execute one file at a time - you can group your test cases in specific file:
jasmin-node moduleTestCases_spec.js
Also, if you want to specify exact test case name to be executed, you can make use of sequenty module. It is a nodejs module, which you can specify the order(and thus the exact test cases to execute).
To run specific test in Frishby just run:
npm test ./folder/filename.js
So lets assume you have an folder say test under that you have a file called api.spec.js
then you will execute like this:
npm test ./test/api.spec.js
Parallely don't forget to specify these below things into your package.json file
"scripts": {
"test": "mocha" }

Not Getting $ in command prompt in Node.Js

My NodeJs is working fine but i have an issue I'm not seeing $ in the prompt as most example point.
Another issue is when i put sudo I dont get anything.Things I have tried are the following
$ sudo npm install npm -g
/usr/bin/npm -> /usr/lib/node_modules/npm/bin/npm-cli.js
npm#2.7.1 /usr/lib/node_modules/npm
given on the following Website
http://www.tutorialspoint.com/nodejs/nodejs_npm.htm
Apologies am very new to Node.JS.Please help
I'm going to explain this in terms familiar to MS windows.
$ npm --version
^ dollar sign is the same as "C:\" in windows.
It just means "from here..." in the most basic terms I can use.
You don't need the dollar sign for anything in that tutorial.
"sudo" means "elevate to an administrator level" similar to opening a command line terminal "in administrator mode." But for Linux (Ubuntu and Mac as *NIX) systems.
--version can be called as "-v" most of the time and means "for the thing I've named before, in this case "npm" show me the version.
Once you've installed NodeJS it comes with a "package manager" called NPM. The best way to relate this to windows is by considering it a command line version of an "installation" that installs different programs as you tell it to with different options.
"npm install -g" means "Hey NPM! Install to EVERYWHERE(call from command line/terminal/bash) the thing I Tell you next. "npm install -g express" for example, means hey NPM, install "expressJS" globally, so I can use the terminal to write commands(micro apps) from the expressJS I just installed with node.
"npm install --save" means hey, install this microapp, but ONLY let me use it in THIS EXACT FOLDER I'm in, and let anyone else that is in this folder know they need to install it to use this application I'm making.
"npm init" Is actually the FIRST thing you should do in any node project folder. It creates the "package.json" file in the current directory, and it will define the folder you're in as the folder to start installing stuff you "npm install" to the "node_modules" folder that will show in the folder you're currently in.
If you want to tinker with NodeJS code, and you don't want to tamper with your local machine and install all kinds of stuff you're not totally sure about yet you can use "REPL.it" (https://repl.it/languages/nodejs) the white window on the right is treated like a file you'd run in node. The dark window on the right is an actual NODE TERMINAL that you can run nodejs commands/code in directly.
There's one other good resources in general and that is here (https://devdocs.io/) it's called "Devdocs" and it has Node, npm, and express code examples, clean explanations, and examples that you can download directly to your local machine.
I hope that gets you moving with NodeJS. It's hard to understand, but with a bit of try and fail you'll start to try more and fail less. Cheers!

Categories

Resources