How to have gruntjs skip eslint task if no files are found? - gruntjs

I'm using grunt with the grunt-eslint plugin. This is part of a larger grunt task that first finds changed files and does various tasks with them. If there aren't any JS files changed (for example if I just change a CSS file) the whole task aborts because eslint fails with a "could not find any files to validate" message.
$ grunt eslint:precommit
Running "eslint:precommit" (eslint) task
Could not find any files to validate.
Warning: Task "eslint:precommit" failed. Use --force to continue.
Aborted due to warnings.
I don't want the task to fail if there aren't any JS files found.
Is there a way to either:
A. Have the grunt task not even call eslint and not fail if no files are run?
B. Have eslint not fail if no files are run?
(Related, but specific to a different tool called from grunt: Can an assemble target be silenced if there are no matching files? (Without using --force))

Using Dynamic task is your solution, here the link for the docs: http://gruntjs.com/frequently-asked-questions#dynamic-alias-tasks
Related links:
How can I skip a grunt task if a directory is empty

Related

PHPStorm / FileWatcher - automatic deploy of grunt processed files

I've configured some grunt tasks (concat/uglify) that generates minified files in my project. These tasks are triggered with the File Watcher plugin that comes with PHPStorm.
I've also configured automatic deployment, but that does not work for files that have been generated by grunt tasks.
I thought this would be handled by the "Output paths to refresh" option in the File Watcher (where I specified the full path to the minified file), but this does not trigger the automatic deploy.
Have I forgotten something? Please help.

Browserify failing with cannot find module "lodash" error for some files

I am running browserify for an app.js located at some path and it fails everytime with cannot find module lodash from [PATH].
Running "browserify:build" (browserify) task
Error: Cannot find module 'lodash' from '/var/lib/jenkins/buildcode/output/mydir/app_store_richUI/cartridge/js'
Warning: Error running grunt-browserify. Use --force to continue.
The [PATH] is same where the app.js file is present. But, if I change the file name to some other js file at same path, it works. So, the scene is that it succeeds for some js file and fails for others at same path.
Can someone suggest something ?
I have the Browserify.js script installed globally.
Browserify.js
module.exports = {
build: {
files: {
'<%= settings["local.build.dir"] %>/output/<%= grunt.config("build") %>/app_eyeconic_richUI/cartridge/static/default/js/eyeconic.app.js':'<%= settings["local.build.dir"] %>/output/<%= grunt.config("build") %>/app_eyeconic_richUI/cartridge/js/app.js'
},
}
}
The path is shown correctly in the logs with other files. It fails only with app.js file
It was a very trivial issue but took quite some time to resolve.
The issue was that the build suite was at a different location than the build source.
The browserify task contained require statements and it searches for modules in the parent directories so it was not able to find the required module.
After copying the build suite at the same path as the source, it worked.
So currently, my gruntfile.js(and other files/folders in the suite), exports and output directory at are same path.

Grunt concat includes a file that it should ignore; why is it ignoring Gruntfile.js?

I have a Grunt task to concat 3x JS files into a single plugins.js file. I now no longer require one of the files (let's call it unrequired.js), so I've removed it from the list of source files in Gruntfile.js. However, whenever I run grunt concat, the output file still contains unrequired.js. The only way around it is to trash unrequired.js and then run the task again. Is there some kind of caching feature at play here? Does the concat task ignore changes being made to Gruntfile? What am I missing?

Using a grunt plugin's Gruntfile task rather than one from the executing Gruntfile

I have a grunt plugin, let's call it grunt-my-plugin and within the plugin's Gruntfile I have a specific cssmin task that needs to be run. Everything works as expected when testing the grunt plugin, however, when I install it on another project it gives me the following error:
Warning: Task "cssmin" not found. Use --force to continue.
At first, I was just calling grunt.task.run('cssmin'); from the plugin's task file, but then after seeing the error I realized that the Gruntfile that uses this plugin needs to be pointed to the appropriate task config, so I added:
grunt.task.loadTasks('./Gruntfile.js');
That still have me the error, so I tried:
grunt.task.loadTasks('./node_modules/grunt-my-plugin/Gruntfile.js');
Still getting the error. So, how do I fix this? The plugin does not need specific config from a Gruntfile that uses it. The cssmin task is being used to create a tmp copy and run metrics against it. Any ideas on how to achieve this? Thanks!
Ensure the following:
your plugin has the grunt-contrib-cssmin plugin listed as a dependency in package.json
you have npm install-ed in the other project (check with npm ls grunt-contrib-cssmin)
supply configuration for the cssmin task using grunt.config() before calling grunt.task.run('cssmin') (see this GitHub comment for an idea)

Is there a way to read grunt tasks via terminal?

I would like to get the registered Tasks from a gruntfile via terminal.
Example:
grunt.registerTask('test', ['clean', 'compass', 'uglify', 'cssmin', 'imagemin', 'copy:test', 'ftp-deploy:test', 'clean']);
then on the terminal I would type something like
$> grunt --listtasks
Even better if I could register each task with a description like so:
grunt.registerTask('test', 'this task deploys to testserver',['clean', 'compass', 'uglify', 'cssmin', 'imagemin', 'copy:test', 'ftp-deploy:test', 'clean']);
Then the output could be something like:
$> test: this task deploys to testserver (clean, compass, uglify, cssmin, imagemin, copy:test, ftp-deploy:test, clean)
Run grunt --help to get a list of all registered tasks. This shows the command and the task description. Example output:
$ grunt --help
Grunt: The JavaScript Task Runner (v0.4.1)
Usage
grunt [options] [task [task ...]]
Options
--help, -h Display this help text.
--base Specify an alternate base path. By default, all file paths
are relative to the Gruntfile. (grunt.file.setBase) *
--no-color Disable colored output.
--gruntfile Specify an alternate Gruntfile. By default, grunt looks in
the current or parent directories for the nearest
Gruntfile.js or Gruntfile.coffee file.
--debug, -d Enable debugging mode for tasks that support it.
--stack Print a stack trace when exiting with a warning or fatal
error.
--force, -f A way to force your way past warnings. Want a suggestion?
Don't use this option, fix your code.
--tasks Additional directory paths to scan for task and "extra"
files. (grunt.loadTasks) *
--npm Npm-installed grunt plugins to scan for task and "extra"
files. (grunt.loadNpmTasks) *
--no-write Disable writing files (dry run).
--verbose, -v Verbose mode. A lot more information output.
--version, -V Print the grunt version. Combine with --verbose for more
info.
--completion Output shell auto-completion rules. See the grunt-cli
documentation for more information.
Options marked with * have methods exposed via the grunt API and should instead
be specified inside the Gruntfile wherever possible.
Available tasks
rosetta Shared variables between JS and CSS. *
asciify Ascii awesomizer. A Grunt task for better banners and hot
logs. *
clear Clear your terminal window
clean Clean files and folders. *
concat Concatenate files. *
jshint Validate files with JSHint. *
sass Compile Sass to CSS *
watch Run predefined tasks whenever watched files change.
csso Minify CSS files with CSSO. *
lintspaces Checking spaces *
newer Run a task with only those source files that have been
modified since the last successful run.
any-newer Run a task with all source files if any have been modified
since the last successful run.
newer-timestamp Internal task.
svgmin Minify SVG *
trimtrailingspaces Removing the trailing spaces *
webfont Compile separate SVG files to webfont *
Tasks run in the order specified. Arguments may be passed to tasks that accept
them by using colons, like "lint:files". Tasks marked with * are "multi tasks"
and will iterate over all sub-targets if no argument is specified.
The list of available tasks may change based on tasks directories or grunt
plugins specified in the Gruntfile or via command-line options.
For more information, see http://gruntjs.com/
Or have a look at grunt-available-tasks:

Resources