I am familiar with adding individual environment variables through the CLI by entering:
./node_modules/.bin/cypress run -- --env itemToOverride="val"
But let's say I have an entire JSON that can be a valid cypress.env, how do I pass that in via CLI without having to add each item from the JSON individually?
For example, let's say I have env1.json and env2.json which both have valid structure for my test, but I want to be able to run all my tests with env1 then env2? Similar to:
./node_modules/.bin/cypress run -- --env env1.json
For others that stumble here and want a working answer: The easiest solution to this would be to create a config folder in your project directory containing all the environment JSONs you want. I wrote a script to take an environment file's name as an argument when running it. I decided on a bash script, but you could also use an npm script to achieve this. Basically, to run the tests in "env1" you would type on your command line:
./cypress --env=env1
The way I achieved this was by copying the selected JSON from the config folder to the main directory, then renaming it 'cypress.env.json'. This is a setup function in my bash script to get you started:
function doSetup {
echo "Selecting environment variables from $ENV.env.json"
cp ./cypress/config/"$ENV.env.json" ./
mv "$ENV.env.json" "cypress.env.json"
}
It's an obscure problem that does not have very many answers online, but I hope this helps!
Working with cypress to run a different folder's python script. but I guess the directory is not changing perfectly.
gotoPythonProject() {
cy.exec("cd /Users/**/Desktop/project/");
cy.exec("ls"); // which does not reflect the files of project that's been changed
cy.reload();
}
As with many CI tools it is normal behavior that every command starts in a fresh scope. This way a command is unaffected by the state of a previous one.
There are two practical solutions to this case:
Combine your commands using &&. For example:
cy.exec("cd .. && pwd");
Call a bash script. For example: cy.exec("test_me.sh");, your bash script would than look something like:
#!/bin/bash
cd ..
pwd
Several Node.js packages have the following two steps as their starting point (just using Jasmine as an example):
npm install --save-dev jasmine
./node_modules/.bin/jasmine init
The first statement is straightforward, but I could not for the life of me figure out what the second statement does under the hood. The Jasmine docs only say that it initializes it (I am searching for something more technical).
./node_modules/.bin/jasmine looks like this:
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac
if [ -x "$basedir/node" ]; then
"$basedir/node" "$basedir/../jasmine/bin/jasmine.js" "$#"
ret=$?
else
node "$basedir/../jasmine/bin/jasmine.js" "$#"
ret=$?
fi
exit $ret
In case helpful, I did this to clone and install the package locally:
mkdir testProj : Creates new project folder
code testProj --add : Adds folder to workspace
cd testProj
npm init : Creates package.json
npm install [--save-dev] : Installs dependencies
./node_modules/.bin/ init : What does this do, specifically?
Short: npx init
Any pointers/documentation explaining that init would be deeply appreciated.
Edit:
Just to clarify, I know what init does (clear from testing and the Jasmine documentation), I just do not understand how it does it. I am basically trying to find out why init is needed behind the script name when running it from the CLI, and where the init code is located.
I managed to finally solve this myself. If anyone comes across this in the future, the following is the explanation of ./node_modules/.bin/jasmine init.
./node_modules/.bin/jasmine init is executed from the command line
This runs the jasmine Unix script in ./node_modules/.bin/ (init argument is not used yet)
The script resolves path to jasmine.js (./node_modules/jasmine/bin/jasmine.js) and runs it
Jasmine.js contains this code: var Command = require('../lib/command.js')
Jasmine.js creates a new instance of the Command object (command) and executes: command.run(jasmine, process.argv.slice(2));
process.argv is an array of all arguments given when starting the application from command line. Recalling the command, one sees that slice(2) equals init
The run function inside command.js launches initJasmine by having mapped init to initJasmine at the very top
Finally, initJasmine makes the directory spec and all its contents
Hope that helps someone else in the future.
jasmine init initializes a basic folder structure for Jasmine by creating a spec directory and configuration json for you. You can look for more details here https://github.com/jasmine/jasmine-npm
The title of my question is how to run a command line tool from a node.js application because I think an answer here will apply to all command line utilities installable from npm. I have seen questions related to running command line from node.js, but they don't seem to be working for my situation. Specifically I am trying to run a node command line utility similar to npm (in how it is used, but not its function) called tilemantle.
tilemantle's documentation shows installing tilemantle globally and running the program from the command line.
What I would like to do is install tilemantle locally as a part of a npm project using npm install tilemantle --save and then run tilemantle from inside my project.
I've tried `tilemantle = require('tilemantle'), but the index.js file in the tilemantle project is empty, so I think this won't help with anything.
I tried the project node-cmd
const cmd = require('node-cmd');
cmd.run('./node_modules/tilemantle/bin/tilemantle', 'http://localhost:5000/layer/{z}/{x}/{y}/tile.png', '-z 0-11', '--delay=100ms', '--point=37.819895,-122.478674', '--buffer=100mi'
This doesn't throw any errors, but it also just doesn't work.
I also tried child processes
const child_process = require('child_process');
child_process.exec('./node_modules/tilemantle/bin/tilemantle', 'http://localhost:5000/layer/{z}/{x}/{y}/tile.png, -z 0-11 --delay=100ms --point=37.819895,-122.478674 --buffer=100mi'
This also doesn't throw any errors, but it also doesn't work.
Is there a way to get this working, so that I can run tilemantle from inside my program and not need to install it globally?
Update
I can get tilemantle to run from my terminal with
node './node_modules/tilemantle/bin/tilemantle', 'http://localhost:5000/layer/{z}/{x}/{y}/tile.png', '--delay=100ms', '--point=37.819895,-122.478674', '--buffer=100mi', '-z 0-11'
If I run the following as suggested by jkwok
child_process.spawn('tilemantle', ['http://myhost.com/{z}/{x}/{y}.png',
'--point=44.523333,-109.057222', '--buffer=12mi', '-z', '10-14'],
{ stdio: 'inherit' });
I am getting spawn tilemantle ENOENT and if I replace tilemantle with ./node_modules/tilemantle/bin/tilemantle.js I get spawn UNKNOWN
Based on jfriend00's answer it sounds like I need to actually be spawning node, so I tried the following
child_process.spawn('node', ['./node_modules/tilemantle/bin/tilemantle.js', 'http://myhost.com/{z}/{x}/{y}.png', '--point=44.523333,-109.057222', '--buffer=12mi', '-z', '10-14'], { stdio: 'inherit' });
Which gives me the error spawn node ENOENT which seems strange since I can run it from my terminal and I checked my path variable and C:\Program Files\nodejs is on my path.
Just to check I tried running the following with a full path to node.
child_process.spawn('c:/program files/nodejs/node.exe', ['./node_modules/tilemantle/bin/tilemantle.js', 'http://myhost.com/{z}/{x}/{y}.png', '--point=44.523333,-109.057222', '--buffer=12mi', '-z', '10-14'], { stdio: 'inherit' });
which runs without the ENOENT error, but again it is failing silently and is just not warming up my tile server.
I am running Windows 10 x64 with Node 6.11.0
You can install any executable locally and then run it with child_process. To do so, you just have to figure out what the exact path is to the executable and pass that path to the child_process.exec() or child_process.spawn() call.
What it looks like you ultimately want to run is a command line that does:
node <somepath>/tilemantle.js
When you install on windows, it will do most of that for you if you run:
node_modules\.bin\tilemantle.cmd
If you want to run the js file directly (e.g. on other platforms), then you need to run:
node node_modules/tilemantle/bin/tilemantle.js
Note, with child_process, you have to specify the actual executable which in this case is "node" itself and then the path to the js file that you wish to run.
This, of course, all assumes that node is in your path so the system can find it. If it is not in your path, then you will have to use the full path to the node executable also.
It looks like you are trying to capture the output of tilemantle from running a file rather than from the command line. If so, I did the following and got it to work.
Installed tilemantle and child_process locally into a new npm project as you did, and added the following into a file in that project.
// test.js file
var spawn = require('child_process').spawn;
spawn('tilemantle', ['http://myhost.com/{z}/{x}/{y}.png', '--
point=44.523333,-109.057222', '--buffer=12mi', '-z', '10-14'], { stdio: 'inherit' });
Run it using node test.js inside the project.
I tried a bunch of the other options in this post but could only get the above one to print the progress along with other output. Hope this helps!
Many command line utilities come with a "programmatic" API. Unfortunately, tilemantle does not, which is why you are unable to require that module in your code.
You can, however, easily access a locally installed version of the CLI from npm scripts. I don't know anything about tilemantle, so I'll provide an example using the tldr command line tool. In your package.json:
{
"name": "my-lib",
"version": "1.0.0",
"scripts": {
"test": "tldr curl"
},
"dependencies": {
"tldr": "^2.0.1"
}
}
You can now run npm test from the terminal in your project as an alias for tldr curl.
Per this post, you can use the global-npm package to run npm scripts from your code:
const npm = require('global-npm')
npm.load({}, (err) => {
if (err) throw err
npm.commands.run(['test'])
})
And voila, you can now run the locally installed CLI programmatically(ish), even though it has not offered an API for that.
I have operating system environment called KEY and when i called it with
echo $KEY in Ubuntu and it will print out the value
How can I get this value in typescript?
I already looked out for this and every one say i must call it with process.env.KEY in my typescript code, but if i print the process.env.KEY it would print undefined
If your executing your TypeScript code in the node environment process.env.KEY should work; at least it works on my Windows 10 machine.
However, you can alternatively use cross-env while executing your code and set custom value to any custom code.
Let's say the typescript code looks as follows:
console.log(process.env.MYKey);
While executing the code you can use cross-env to set the value for MYKey, as follows.
tsc myfile; cross-env MYKey=MyVal node myfile
And certainly you can concisely put this invocation script in your npm run script (in package.json), and use that directly.
Example:
"scripts": {
"prerun-myfile":"tsc myfile",
"run-myfile":"cross-env MYKey=MyVal node myfile"
}
And execute your code using npm run run-myfile.
Hope this helps.