dsherret / dax

Cross-platform shell tools for Deno and Node.js inspired by zx.
MIT License
997 stars 34 forks source link

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

Open dsherret opened 1 year ago

dsherret commented 1 year ago

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.