secundant / neodx

A collection of frontend-focused tools aimed at enhancing your development experience
MIT License
78 stars 6 forks source link

`@neodx/vfs` Implement Layers API #148

Open secundant opened 4 months ago

secundant commented 4 months ago

The current implementation of @neodx/vfs relies on granular change tracking, which works well in typical cases. However, I've found that this approach has significant limitations and lacks the potential for future growth. For instance, the following scenarios are blocked:

Illustration of the real issue (from the docs)

Unfortunately, the following code will not work as expected:

async function reinitWorkingDir(vfs: Vfs) {
  // 1. Cleanup working dir
  await vfs.delete('.');
  // 2. Write some initial files
  await vfs.write('index.ts', `export const foo = 'bar';`);
}

Instead of the deletion of the working directory, our current implementation will "forget" about it because of child file writes.

We plan to fix this in the nearest future. Currently, you can use the following workarounds:

// Variant A - scan and delete all files directly
async function reinitWorkingDir(vfs: Vfs) {
  // 1. Delete all files and directories
  await concurrently(await vfs.glob('**/*'), vfs.delete);
  // 2. Write some initial files
  await vfs.write('index.ts', `export const foo = 'bar';`);
}

// Variant B - use `apply` method right after the deletion
async function reinitWorkingDir(vfs: Vfs) {
  // 1. Cleanup working dir
  await vfs.delete('.');
  await vfs.apply();
  // 2. Write some initial files
  await vfs.write('index.ts', `export const foo = 'bar';`);
}