1 | # grunt-newer
|
2 |
|
3 | Configure [Grunt](http://gruntjs.com/) tasks to run with newer files only.
|
4 |
|
5 | **Synopsis:** The [`newer`](#newer) task will configure another task to run with `src` files that are *a)* newer than the `dest` files or *b)* newer than the last successful run (if there are no `dest` files). See below for examples and more detail.
|
6 |
|
7 | ## Getting Started
|
8 | This plugin requires Grunt `~0.4.1`
|
9 |
|
10 | If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [`gruntfile.js`](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
|
11 |
|
12 | ```shell
|
13 | npm install grunt-newer --save-dev
|
14 | ```
|
15 |
|
16 | Once the plugin has been installed, it may be enabled inside your `gruntfile.js` with this line:
|
17 |
|
18 | ```js
|
19 | grunt.loadNpmTasks('grunt-newer');
|
20 | ```
|
21 |
|
22 | <a name="newer"></a>
|
23 | ## The `newer` task
|
24 |
|
25 | The `newer` task doesn't require any special configuration. To use it, just add `newer` as the first argument when running other tasks.
|
26 |
|
27 | For example, if you want to use [Uglify](https://npmjs.org/package/grunt-contrib-uglify) to minify your source files only when one or more of them is newer than the previously minified destination file, configure the `uglify` task as you would otherwise, and then register a task with `newer` at the front.
|
28 |
|
29 | ```js
|
30 | grunt.initConfig({
|
31 | uglify: {
|
32 | all: {
|
33 | files: {
|
34 | 'dest/app.min.js': ['src/**/*.js']
|
35 | }
|
36 | }
|
37 | }
|
38 | });
|
39 |
|
40 | grunt.loadNpmTasks('grunt-contrib-uglify');
|
41 | grunt.loadNpmTasks('grunt-newer');
|
42 |
|
43 | grunt.registerTask('minify', ['newer:uglify:all']);
|
44 | ```
|
45 |
|
46 | With the above configuration the `minify` task will only run `uglify` if one or more of the `src/**/*.js` files is newer than the `dest/app.min.js` file.
|
47 |
|
48 | The above example shows how the `newer` task works with other tasks that specify both `src` and `dest` files. In this case, the modification time of `src` files are compared to modification times of corresponding `dest` files to determine which `src` files to include.
|
49 |
|
50 | The `newer` task can also be used with tasks that don't generate any `dest` files. In this case, `newer` will only use files that are newer than the last successful run of the same task.
|
51 |
|
52 | For example, if you want to run [JSHint](https://npmjs.org/package/grunt-contrib-jshint) on only those files that have been modified since the last successful run, configure the `jshint` task as you would otherwise, and then register a task with `newer` at the front.
|
53 |
|
54 | ```js
|
55 | grunt.initConfig({
|
56 | jshint: {
|
57 | all: {
|
58 | src: 'src/**/*.js'
|
59 | }
|
60 | }
|
61 | });
|
62 |
|
63 | grunt.loadNpmTasks('grunt-contrib-jshint');
|
64 | grunt.loadNpmTasks('grunt-newer');
|
65 |
|
66 | grunt.registerTask('lint', ['newer:jshint:all']);
|
67 | ```
|
68 |
|
69 | With the above configuration, running `grunt lint` will configure your `jshint:all` task to use only files in the `jshint.all.src` config that have been modified since the last successful run of the same task. The first time the `jshint:newer:all` task runs, all source files will be used. After that, only the files you modify will be run through the linter.
|
70 |
|
71 | Another example is to use the `newer` task in conjunction with `watch`. For example, you might want to set up a watch to run a linter on all your `.js` files whenever one changes. With the `newer` task, instead of re-running the linter on all files, you only need to run it on the files that changed.
|
72 |
|
73 | ```js
|
74 | var srcFiles = 'src/**/*.js';
|
75 |
|
76 | grunt.initConfig({
|
77 | jshint: {
|
78 | all: {
|
79 | src: srcFiles
|
80 | }
|
81 | },
|
82 | watch: {
|
83 | all: {
|
84 | files: srcFiles,
|
85 | tasks: ['newer:jshint:all']
|
86 | }
|
87 | }
|
88 | });
|
89 |
|
90 | grunt.loadNpmTasks('grunt-contrib-jshint');
|
91 | grunt.loadNpmTasks('grunt-contrib-watch');
|
92 | grunt.loadNpmTasks('grunt-newer');
|
93 |
|
94 | ```
|
95 |
|
96 | With the above configuration, running `grunt jshint watch` will first lint all your files with `jshint` and then set up a watch. Whenever one of your source files changes, the `jshint` task will be run on just the modified file.
|
97 |
|
98 | *Note:* If your task is configured with `dest` files, `newer` will run your task with only those files that are newer than the corresponding `dest` files.
|
99 |
|
100 | ## Options for the `newer` task
|
101 |
|
102 | In most cases, you shouldn't need to add any special configuration for the `newer` task. Just `grunt.loadNpmTasks('grunt-newer')` and you can use `newer` as a prefix to your other tasks. The options below are available for advanced usage.
|
103 |
|
104 | #### <a id="optionscache">options.cache</a>
|
105 | * type: `string`
|
106 | * default: `node_modules/grunt-newer/.cache`
|
107 |
|
108 | To keep track of timestamps for successful runs, the `newer` task writes to a cache directory. The default is to use a `.cache` directory within the `grunt-newer` installation directory. If you need timestamp info to be written to a different location, configure the task with a `cache` option.
|
109 |
|
110 | Example use of the `cache` option:
|
111 |
|
112 | ```js
|
113 | grunt.initConfig({
|
114 | newer: {
|
115 | options: {
|
116 | cache: 'path/to/custom/cache/directory'
|
117 | }
|
118 | }
|
119 | });
|
120 | ```
|
121 |
|
122 | #### <a id="optionsoverride">options.override</a>
|
123 | * type: `function(Object, function(boolean))`
|
124 | * default: `null`
|
125 |
|
126 | The `newer` task determines which files to include for a specific task based on file modification time. There are occassions where you may want to include a file even if it has not been modified. For example, if a LESS file imports some other files, you will want to include it if any of the imports have been modified. To support this, you can provide an `override` function that takes two arguments:
|
127 |
|
128 | * **details** - `Object`
|
129 | * **task** - `string` The currently running task name.
|
130 | * **target** - `string` The currently running target name.
|
131 | * **path** - `string` The path to a `src` file that appears to be "older" (not modified since the time below).
|
132 | * **time** - `Date` The comparison time. For tasks with `dest` files, this is the modification time of the `dest` file. For tasks without `dest` files, this is the last successful run time of the same task.
|
133 | * **include** - `function(boolean)` A callback that determines whether this `src` file should be included. Call with `true` to include or `false` to exclude the file.
|
134 |
|
135 | Example use of the `override` option:
|
136 |
|
137 | ```js
|
138 | grunt.initConfig({
|
139 | newer: {
|
140 | options: {
|
141 | override: function(detail, include) {
|
142 | if (detail.task === 'less') {
|
143 | checkForModifiedImports(detail.path, detail.time, include);
|
144 | } else {
|
145 | include(false);
|
146 | }
|
147 | }
|
148 | }
|
149 | }
|
150 | });
|
151 | ```
|
152 |
|
153 | ## That's it
|
154 |
|
155 | Please [submit an issue](https://github.com/tschaub/grunt-newer/issues) if you encounter any trouble. Contributions or suggestions for improvements welcome!
|
156 |
|
157 | [![Current Status](https://secure.travis-ci.org/tschaub/grunt-newer.png?branch=master)](https://travis-ci.org/tschaub/grunt-newer)
|
158 |
|
159 | ## Known limitations
|
160 |
|
161 | The `newer` task relies on Grunt's convention for specifying [`src`/`dest` mappings](http://gruntjs.com/configuring-tasks#files). So it should be expected to work with two types of tasks:
|
162 |
|
163 | 1) Tasks that specify both `src` and `dest` files. In this case, the task prefixed by `newer` will be configured to run with `src` files that are newer than the corresponding `dest` file (based on the `mtime` of files).
|
164 |
|
165 | 2) Tasks that specify only `src` files. In this case, the task prefixed by `newer` will be configured to run with `src` files that are newer than the previous successful run of the same task.
|
166 |
|
167 | The `newer` task will *not* work as a prefix for the following tasks:
|
168 |
|
169 | * [`grunt-rsync`](http://npmjs.org/package/grunt-rsync) - Though this task specifies `src` and `dest` files, the `dest` file is not generated based on `src` files (instead it is a directory).
|