1717import chokidar from "chokidar"
1818import { basename } from "path"
1919
20- import { readdir } from "fs/promises"
2120import { Profiles } from "madwizard"
2221
2322import UpdateFunction from "../../update"
@@ -31,7 +30,7 @@ export const RUNS_ERROR = "No runs found"
3130 */
3231export default class ProfileRunWatcher {
3332 /** Our model */
34- private _runs : string [ ] = [ ]
33+ private _runs : { runId : string ; timestamp : number } [ ] = [ ]
3534
3635 /** Have we already performed the on-time init? */
3736 private _initDone = false
@@ -57,7 +56,6 @@ export default class ProfileRunWatcher {
5756 /** Initialize `this._runs` model */
5857 public async init ( ) : Promise < ProfileRunWatcher > {
5958 if ( ! this . _initDone ) {
60- // await this.readOnce() no need, since chokidar gives us an initial read
6159 this . initWatcher ( )
6260 this . _initDone = true
6361 }
@@ -66,18 +64,18 @@ export default class ProfileRunWatcher {
6664
6765 /** Initialize the filesystem watcher to notify us of new or removed profiles */
6866 private initWatcher ( ) {
69- this . watcher . on ( "addDir" , async ( path ) => {
67+ this . watcher . on ( "addDir" , async ( path , stats ) => {
7068 const runId = basename ( path )
71- const idx = this . runs . findIndex ( ( _ ) => _ === runId )
69+ const idx = this . runs . findIndex ( ( _ ) => _ . runId === runId )
7270 if ( idx < 0 ) {
73- this . _runs . push ( runId )
71+ this . _runs . push ( { runId, timestamp : stats ?. mtimeMs || stats ?. ctimeMs || 0 } )
7472 this . updateFn ( )
7573 }
7674 } )
7775
7876 this . watcher . on ( "unlink" , ( path ) => {
7977 const runId = basename ( path )
80- const idx = this . runs . findIndex ( ( _ ) => _ === runId )
78+ const idx = this . runs . findIndex ( ( _ ) => _ . runId === runId )
8179 if ( idx >= 0 ) {
8280 this . _runs . splice ( idx , 1 )
8381 this . updateFn ( )
@@ -91,19 +89,7 @@ export default class ProfileRunWatcher {
9189 }
9290
9391 /** Overwrite the run model state */
94- private set runs ( runs : string [ ] ) {
92+ private set runs ( runs : { runId : string ; timestamp : number } [ ] ) {
9593 this . _runs = runs
9694 }
97-
98- /** @return files of the directory of job runs for a given profile */
99- private async readOnce ( ) {
100- try {
101- // TODO do a "full" read with Dirents, so that we have filesystem
102- // timestamps, and sort, so that the `.slice(0, 10)` below pulls
103- // out the most recent runs
104- this . runs = await readdir ( ProfileRunWatcher . path ( this . profile ) )
105- } catch ( err ) {
106- this . runs = [ RUNS_ERROR ]
107- }
108- }
10995}
0 commit comments