How to pass argument to gulp? - command-line

I am using gulp and gulp-shell packages for a php Laravel application, I just need to know if this is possible to pass argument from cmd to gulpfile.js ? this is my file:
gulp.task('default', shell.task([
'echo user',
]));
Question:
Is it possible to pass an argument from command-line when running gulp and then inside the gulpfile print it out instead of user?

var command_line_args = require('yargs').argv
Not sure if this is of any use to you or others but I did this manually by passing the arguments explicitly through a custom function. It's not super elegant but it gets the job done.
var appendWithCommandLineArguments = function(cmd, arguments) {
var to_append = _.chain(command_line_args)
.pick(arguments)
.reduce(function(string, val, prop){
return string+"--"+prop+"="+val+" ";
}, " ")
.value();
return cmd + to_append
}
gulp.task('taskmailer', shell.task([
appendWithCommandLineArguments('node automate/build/mail/taskMailer.js', ["email", "template"])
]))

Related

Gulp: Passing multiple command line parameters to the same task

Goal:
Build /src/assets/(theme)/*.scss by passing the parameter in the command line using flag --theme. I solved step one using gulp-utils
function sass() {
// return gulp.src('/src/assets/scss/**/*.scss')
return gulp.src(['src/assets/scss/' + (util.env.theme ? util.env.theme : 'app') + '.scss'])
.pipe($.sourcemaps.init())
.pipe($.sass({
includePaths: PATHS.sass
})
.on('error', $.sass.logError))
.pipe($.autoprefixer({
browsers: COMPATIBILITY
}))
// Comment in the pipe below to run UnCSS in production
//.pipe($.if(PRODUCTION, $.uncss(UNCSS_OPTIONS)))
.pipe($.if(PRODUCTION, $.cleanCss({ compatibility: 'ie9' })))
.pipe($.if(!PRODUCTION, $.sourcemaps.write()))
.pipe(gulp.dest(PATHS.dist + '/assets/css'))
.pipe(browser.reload({ stream: true }));
}
Remaining:
gulp build --production --theme folderName1, folderName2, folderName*
Passing multiple theme values --theme folderName1 folderName2 to the same sass() function/task.
This would send folderName to function sass()
function sass() {
// return gulp.src('/src/assets/scss/**/*.scss')
return gulp.src([
'src/assets/scss/folderName1/*.scss',
'src/assets/scss/folderName2/*.scss',
'src/assets/scss/folderName3/*.scss'
])
...
...
}
Would creating an argsList like this SitePoint article discusses work?
Please review your idea. This package looks like solve similar topic
https://www.npmjs.com/package/gulp-sass-themes
You need only group your themes in sub-folder.
From other side gulp.src accepts string and array so you can pass this information from command line.
https://github.com/gulpjs/gulp/blob/master/docs/API.md
One of my colleagues was able to help resolve this issue. Instead of using gulp.utils he was able to use yargs.argv.theme with slipt(,). This allows for a user to pass the --theme param and pass as many comma-separated items as desired.
function sass() {
if (yargs.argv.theme) {
let collection = yargs.argv.theme.split(','),
results = collection.map(item => `./src/assets/scss/${item}/*.scss`);
return gulp.src(results)
.pipe($.sourcemaps.init())
.pipe($.sass({
includePaths: PATHS.sass
})
.on('error', $.sass.logError))
.pipe($.autoprefixer({
browsers: COMPATIBILITY
}))
// Comment in the pipe below to run UnCSS in production
//.pipe($.if(PRODUCTION, $.uncss(UNCSS_OPTIONS)))
.pipe($.if(PRODUCTION, $.cleanCss({ compatibility: 'ie9' })))
.pipe($.if(!PRODUCTION, $.sourcemaps.write()))
.pipe(gulp.dest(PATHS.dist + '/assets/css'))
.pipe(browser.reload({ stream: true }));
}
}

command line arguments make file

So I know how to create variables and store values from command line.
But normally, I would type my command this way: make run VAR="abc", and VAR will be assigned the new value "abc"
However, if I want to do something like this VAR="abc" make run, how can I change my make file? Right now, if I run this, VAR still has the initial value when it was created in the make file.
This is my make file:
VAR = ""
.PHONY : build run
build : program.c
gcc -o prog -g program.c
run : build
./prog $(VAR)
When you write VAR = "", you're overwriting any value VAR might have had (e.g. from the environment, or the command line). You can use a conditional assignment instead, which looks like this:
VAR ?= ""
This sets VAR only if it wasn't set already. It's equivalent to e.g.
ifeq ($(origin VAR), undefined)
VAR = ""
endif
VAR = ${VAR}
if i remember right

Why doesn't karma-cli accept files as command line argument?

I'm using the config from my project but would like to run karma just for one specific test script one time. I don't want to have to create a whole new config file just for this case and would prefer just passing in the script I want run (so basically telling karma to use files: ['myTest.js'].
But there don't seem to be any options for that AFAICT in the docs. Why would this be missing? It seems like a fundamental feature IMO.
in karma.conf something like that:
function mergeFilesWithArgv(staticFiles) {
var source = staticFiles, argv = process.argv;
argv.forEach(function (arg) {
var index = arg.indexOf('--check=');
if (index !== -1) {
source.push(arg.substring(8));
}
});
return source;
}
config.set({
...
files: mergeFilesWithArgv([
'js_src/tests/*.test.js'
]),
...
});
use: karma start --check='./path/to/file.js'
or for multiple files: karma start --check='./path/to/file.js' --check='/another/path/to/another/file.js'

MongoDB eval() with arguments

I'm getting a strange result when trying to use eval with the args argument. The following works fine:
> db.eval(function(coll) {
var res = db[coll].find({});
return(res.count());
}, ['KenColl'])
1438
But when I pass a second argument, I always get empty results, even if I don't use it:
> db.eval(function(coll, query) {
var res = db[coll].find({});
return(res.count());
}, ['KenColl', {}])
0
Am I misunderstanding something about eval and args? I'm running version 2.4.3 of both mongod and the MongoDB shell.
For db.eval you shouldn't pass the arguments as an array, just pass them into the function.
The following example should work:
db.eval(function(coll, query) {
var res = db[coll].find(query);
return(res.count());
}, 'KenColl', {})
p.s. your first example only works because in javascript db['KenColl'] === db[['KenColl']]

Parse script output from shell wanted in JScript

I want to execute a command and parse the output from the shell. I am using JScript inside TestComplete. I already found out that I can run commands using WScript.shell. But I do not know how to parse the output in my JScript. Any hints?
var shell = new ActiveXObject("WScript.shell");
if (shell)
{
shell.run("myCommandIWantToParseOutputfrom.sh");
}
Take a look at the Exec method instead of Run.
var wsh = new ActiveXObject("WScript.Shell");
var cmd = wsh.Exec("cmd /c dir C:\ /on");
while (cmd.Status === 0) {
WScript.Sleep(100);
}
var output = cmd.StdOut.ReadAll();
WScript.Echo(output);