|
| 1 | +<!-- front-matter |
| 2 | +id: api-lastrun |
| 3 | +title: lastRun() |
| 4 | +hide_title: true |
| 5 | +sidebar_label: lastRun() |
| 6 | +--> |
| 7 | + |
| 8 | +# lastRun() |
| 9 | + |
| 10 | +Retrieves the last time a task was successfully completed during the current running process. Most useful on subsequent task runs while a watcher is running. |
| 11 | + |
| 12 | +When combined with `src()`, enables incremental builds to speed up your execution times by skipping files that haven't changed since the last successful task completion. |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +```js |
| 17 | +const { src, dest, lastRun, watch } = require('gulp'); |
| 18 | +const imagemin = require('gulp-imagemin'); |
| 19 | + |
| 20 | +function images() { |
| 21 | + return src('src/images/**/*.jpg', { since: lastRun(images) }) |
| 22 | + .pipe(imagemin()) |
| 23 | + .pipe(dest('build/img/')); |
| 24 | +} |
| 25 | + |
| 26 | +function watch() { |
| 27 | + watch('src/images/**/*.jpg', images); |
| 28 | +} |
| 29 | + |
| 30 | +exports.watch = watch; |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | +## Signature |
| 35 | + |
| 36 | +```js |
| 37 | +lastRun(task, [precision]) |
| 38 | +``` |
| 39 | + |
| 40 | +### Parameters |
| 41 | + |
| 42 | +| parameter | type | note | |
| 43 | +|:--------------:|:------:|-------| |
| 44 | +| task <br> **(required)** | function <br> string | The task function or the string alias of a registered task. | |
| 45 | +| precision | number | Default: `1000` on Node v0.10, `0` on Node v0.12+. Detailed in Timestamp precision][timestamp-precision-section] section below. | |
| 46 | + |
| 47 | +### Returns |
| 48 | + |
| 49 | +A timestamp (in milliseconds), matching the last completion time of the task. If the task has not been run or has failed, returns `undefined`. |
| 50 | + |
| 51 | +To avoid an invalid state being cached, the returned value will be `undefined` if a task errors. |
| 52 | + |
| 53 | +### Errors |
| 54 | + |
| 55 | +When called with a value other than a string or function, throws an error with the message, "Only functions can check lastRun". |
| 56 | + |
| 57 | +When called on a non-extensible function and Node is missing WeakMap, throws an error with the message, "Only extensible functions can check lastRun". |
| 58 | + |
| 59 | +## Timestamp precision |
| 60 | + |
| 61 | +While there are sensible defaults for the precision of timestamps, they can be rounded using the `precision` parameter. Useful if your file system or Node version has a lossy precision on file time attributes. |
| 62 | + |
| 63 | +* `lastRun(someTask)` returns 1426000001111 |
| 64 | +* `lastRun(someTask, 100)` returns 1426000001100 |
| 65 | +* `lastRun(someTask, 1000)` returns 1426000001000 |
| 66 | + |
| 67 | +A file's [mtime stat][fs-stats-concepts] precision may vary depending on the node version and/or the file system used: |
| 68 | + |
| 69 | + |
| 70 | +| platform | precision | |
| 71 | +|:-----------:|:------------:| |
| 72 | +| Node v0.10 | 1000ms | |
| 73 | +| Node v0.12+ | 1ms | |
| 74 | +| FAT32 file system | 2000ms | |
| 75 | +| HFS+ or Ext3 file systems | 1000ms | |
| 76 | +| NTFS using Node v0.10 | 1s | |
| 77 | +| NTFS using Node 0.12+ | 100ms | |
| 78 | +| Ext4 using Node v0.10 | 1000ms | |
| 79 | +| Ext4 using Node 0.12+ | 1ms | |
| 80 | + |
| 81 | + |
| 82 | +[timestamp-precision-section]: #timestamp-precision |
| 83 | +[fs-stats-concepts]: concepts.md#file-system-stats |
0 commit comments