pass command line argument in vscode test runner script (index.ts) - node.js

Scenario:
script in package.json file looks like:
"test": "node ./out/test/runTest.js"
this runTest.js then calls test runner script src/test/suite/index.ts . Source of this file is : https://code.visualstudio.com/api/working-with-extensions/testing-extension
In the index.ts, it uses source like:
return new Promise((c, e) => {
glob('**/**.test.js', { cwd: testsRoot }, (err, files) => {
Here, glob is pointing to the src files to consider for test run.
Question:
For specific reason, I don't want to test all files using regex like: glob('**/**.test.js'
I want to test specific file and I want to pass it by my test script in package.json file.
For ex: I want
"test": "node ./out/test/runTest.js Filename"
And glob() in index.ts file will consider only this Filename for execution.
I need some help in this regards.
Thanks again

The way to pass arguments from npm scripts inside package.json is via environment variables. The syntax is slightly different depending on the platform. The example below is for Windows.
"test": "set FileName=foo.js&node ./out/test/runTest.js"
The argument and the value is now awailable inside runTest.js in process.env
console.log(process.env.FileName); //foo.js

In the src/test/suite/index.ts open your package.json file and search for the "test" script line and parse it for the arguments.
Edit
I needed something similar and added a file: test-arguments.txt next to index.js or maybe in .vscode folder if you use typescript.
Inside index.js I open the file and parse the arguments. I allow lines to be commented with #.
By adding this test-arguments.txt file to .gitignore I don't have to modify package.json and my team members can have different arguments.

Related

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/

How to prepend text to a file as an npm script command

I'm writing a bookmarklet. I need to prepend "javascript:" to the compiled, minified JavaScript. I'm looking for a way to accomplish this using an NPM package.json script.
{
"scripts": {
"oar:transpile-typescript": "tsc --target es6 --lib dom,es6 ./OarBookmarklet/Oar.ts",
"oar:minify-javascript": "jsmin -o ./OarBookmarklet/oar.min.js ./OarBookmarklet/oar.js",
"oar:prepend-javascript": "[??? prepend `javascript:` to minified JavaScript ???]",
"oar": "run-s oar:transpile-typescript oar:minify-javascript oar:prepend-javascript",
"build": "run-s oar"
}
}
For a cross-platform solution utilize node.js and it's builtin fs.readFileSync(...) and fs.writeFileSync(...). This way it doesn't matter which shell your npm script runs in (sh, cmd.exe, bash, bash.exe, pwsh, ... )
To achieve this consider either of the following two solutions - they're essentially the same just different methods of application.
Solution A. Using a separate node.js script
Create the following script, lets save it as prepend.js in the root of the project directory, i.e. at the same level as where package.json resides.
prepend.js
const fs = require('fs');
const filepath = './OarBookmarklet/oar.min.js';
const data = fs.readFileSync(filepath);
fs.writeFileSync(filepath, 'javascript:' + data);
package.json
Define the oar:prepend-javascript npm script in package.json as follows::
"scripts": {
...
"oar:prepend-javascript": "node prepend",
...
},
Note: Above node.js invokes the script and performs the required task. If you choose to save prepend.js in a different directory than the aforementioned then ensure you define the correct path to it, i.e. "oar:prepend-javascript": "node ./some/other/path/to/prepend.js"
Solution B. Inline the node.js script in package.json
Alternatively, you can inline the content of prepend.js in your npm script, therefore negating the use of a separate .js file.
package.json
Define the oar:prepend-javascript script in package.json as follows:
"scripts": {
...
"oar:prepend-javascript": "node -e \"const fs = require('fs'); const fp = './OarBookmarklet/oar.min.js'; const d = fs.readFileSync(fp); fs.writeFileSync(fp, 'javascript:' + d);\""
...
},
Note: Here the nodejs command line option -e is utilized to evaluate the inline JavaScript.
If this is running on something Unix-like then:
(printf 'javascript:' ; cat ./OarBookmarklet/oar.min.js) > ./OarBookmarklet/oar.bm.min.js
should do the job.
Edit in response to OP's comment:
My execution environment is Windows, ...
In that case you should be able to use:
(set /p junk="javascript:" <nul & type ./OarBookmarklet/oar.min.js) > ./OarBookmarklet/oar.bm.min.js
The set /p ... <nul weirdness is a way to get some text sent to stdout without a newline being appended to it.

node.js Command Line Interface app on windows

I am creating a simple CLI (Command line interface) application using NodeJs, involving two files:
package.json:
index.js
I want to print "hello world" to STDOUT and it is working when running command $ node index.js
But I want to use it globally via command test. So, I put a bin entry in package.json. Then build the application using npm link .. But then when I run "test" command, Windows shows me the following error:
How can I use console.log in separate app?
Thank you!
In package.json file, you need to write the code as follows:
"name" : "test",
"version": "1.0.0",
"scripts": {
"start": "node index.js",
"build": "webpack",
},
...
...
after this, use the command npm start to run the application.
1- Create bin folder in root folder and place your index.js inside the bin.
type the shebang code on the first line of index.js:
bin/index.js
#!/usr/bin/env node
console.log("hello world");
2- Add below code into the package.json file.
"bin": {
"test": "./bin/index.js" //this is relative path
}
3-finally run this code on command line
npm link
now you run "test" in your command line, it will log "hello world"
note:pay attention to the relative path that i mentioned above.

Mocha, how to ignore node_modules folder

I am trying to create a test environment which all source file is side by side with its test code. This is due to easy to track which file is without its test code. Below is the example of my directory after run build
built/api/a.js
built/api/a-test.js
built/api/b.js
built/api/b-test.js
built/index.js
built/index-test.js
built/node_modules/...
built/package.json
src/api/a.js
src/api/a-test.js
src/api/b.js
src/api/b-test.js
src/index.js
src/index-test.js
src/package.json
package.json
I am going to run my test by run 'npm test', below is my package.json content:
{ "name": "sample",
"scripts": {
"build": "babel ./src -d ./built && cd built && npm install",
"test": "mocha built/**/*-test.js"
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-core": "^6.18.0",
"mocha": "^3.1.2"
}
}
My question is how can I skip those files in node_modules folder coincidently have name end with -test.js
A little late to the party (13+ months!?), but...
Mocha doesn't support this out of the box. You need to use your glob-fu and get a little fancy. Running something like this on your command line should do the trick:
mocha './built/{,!(node_modules)/**}/*-test.js'
The glob pattern {,!(node_modules)/**} says
Take entries in the current directory, or in any subdirectory of the current directory, regardless of depth, except for ones rooted in ./build/node_modules.
Note that the single quotes are essentially required on OS X or Linux boxen. Left as a bareword (unquoted), or quoted with double quotes, you'll get shell globbing instead of the internal glob() used by mocha, so you're unlikely to get the results you might expect... unless you have an atypical shell.
The Windows shell only supports double quoting... and it doesn't have command line globbing, so on Windows, you'll probably want to leave this unquoted (but don't, err, quote me on that).
A better, more platform-agnostic way would be to put the glob pattern in your test/mocha.opts file, thus:
--require ./test/setup.js
--colors
--inline-diffs
./built/{,!(node_modules)/**}/*-test.js
Cheers!
mocha '**/*-test.js' --ignore 'built/node_modules/**/*'
Another alternative to the existing answers... for those who use a mocha config file (.mocharc.js), you can include the ignore parameter:
{
ignore: 'built/node_modules/**/*'
}
Calling this with mocha --config .mocharc.js built/**/*-test.js

How to use nodemon/grunt with .env files?

I am using an .env file to work with foreman, but am trying to configure nodemon to start my server using grunt, because I enjoy how nodemon restarts when files become modified.
I am trying avoid having an .env file for foreman AND having environment variables stored in my ~/.bash_profile for nodemon. Instead, I would like to configure my .env file to work for both cases.
I found some answers here, and the second answer should work for grunt.
My .env file is of JSON format, which should flatten environment variables via concatenation (see here).
When I run the following command $ env $(cat .env) nodemon app.js, I receive the following error: env: {: No such file or directory.
Anyone have an idea of what the problem may be? Cheers.
I'd suggest filing this at http://github.com/remy/nodemon/issues/new - but I'd also say that there's environment config support in nodemon as of 1.0.9 - though I'm not 100% sure it'll solve what you want.
Basically you put a nodemon.json file in your home directory, and have:
{
"env": {
"USER": "remy",
"PORT": "8000",
"ETC": "etc"
}
}
An example of the config can be seen here and a few more details here.
I haven't tried using the nodemon. But I've figured out how to do restart the server using foreman.
Define a key on your Procfile to run your application with node-supervisor
My proc file have a dev key that is like this: dev: node-supervisor -w .,lib/ webserver.js
The -w option is a comma separated list of the folders that you want to watch.

Resources