Skip to content

Functionality for checking if external changes have occurred since the last time some code was run #116

@dsherret

Description

@dsherret

It might be neat to have an API that can be used to tell if some external changes have occurred since the last time a command was run. This would be useful for not doing an action if unnecessary to do so.

const tracker = $.changeTracker(import.meta, "data files"); // cache keyed on this current file and a string

tracker.addMtime("data/file1.json"); // hashes based on the file path's last modified time
tracker.addPath("data/file2.json"); // hashes based on the file path's content
tracker.addMtime("some_dir"); // hashes based on the descendants mtime
tracker.addPath("some_dir"); // hashes based on the descendants contents
tracker.addValue(123456); // hashes a specific value, which could have a source anywhere else

// multiple paths or values
tracker.addPaths(paths);
tracker.addMTimes(otherPaths);
tracker.addValues(values);

// will always run if the output path doesn't exist
tracker.addOutputPath("output/data.json");

// run if changed
if (tracker.hasChangedSync()) {
  await doStuff();
  tracker.commitSync();
}

// or as a single call
await tracker.runIfChanged(async () => {
  await doStuff();
});

// builder pattern
await $.changeTracker(import.meta, "data files")
  .addPaths(paths)
  .addOutputPath("output/data.json")
  .runIfChanged(async () => {
    await createOutputDataJsonFile();
  });

The hash could be saved in local storage.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions