What is last-run?
The 'last-run' npm package is primarily used to record and retrieve the last time a function was executed. This is particularly useful in build tools and task runners where it's important to know when a task was last run to optimize build processes and avoid unnecessary work.
What are last-run's main functionalities?
Recording last execution time
This feature allows you to capture the last time a specific task was executed. In this example, the 'last-run' package is used with Gulp to record the last execution time of 'example' task.
const lastRun = require('last-run');
const gulp = require('gulp');
function exampleTask(done) {
// task operations
done();
}
gulp.task('example', exampleTask);
lastRun.capture('example', Date.now());
Retrieving last execution time
This feature enables retrieving the last recorded execution time of a task. Here, the last execution time of the 'example' task is retrieved and logged.
const lastRun = require('last-run');
const lastExecutionTime = lastRun('example');
console.log('The last execution time of the example task was:', lastExecutionTime);
Other packages similar to last-run
date-fns
While 'date-fns' does not directly track function executions, it provides comprehensive date utilities that can be used to manipulate and format dates, which can be helpful in calculating time differences since the last run. It differs from 'last-run' as it's a general-purpose date utility library rather than specifically designed for tracking task executions.
moment
Similar to 'date-fns', 'moment' is another robust date handling library which can be used in conjunction with task management to track and display times. It does not inherently track the last run times but can be used to format and calculate time since last execution, offering broader functionality in date manipulation compared to 'last-run'.
last-run

Capture and retrieve the last time a function was run
Usage
var lastRun = require('last-run');
function myFunc(){}
myFunc();
lastRun.capture(myFunc);
lastRun(myFunc);
API
Note: this module uses a WeakMap shim, and throws on non-extensible functions on platforms that
don't have a native WeakMap implementation
lastRun(fn, [timeResolution]) => [Timestamp]
Takes a function (fn
) and returns a timestamp of the last time the function was captured.
Returns undefined if the function has not been captured.
The timestamp is always given in millisecond but the time resolution can be reduced (rounded down).
The use case is to be able to compare a build time to a file time attribute.
On node v0.10 or with file system like HFS or FAT, fs.stat
time attributes like mtime
precision is one second.
Assuming lastRun(fn)
returns 1426000001111, lastRun(fn, 1000)
returns 1426000001000.
The default time resolution is 1000 on node v0.10, 0 on node 0.11+ and iojs.
More information at default-resolution
and
undertaker PR #17.
lastRun.capture(fn)
Takes a function (fn
) and captures the current timestamp with Date.now()
.
The captured timestamp can then be retrieved using the lastRun
function.
License
MIT