Commit 199416f

Anton Medvedev <anton@medv.io>
2022-06-06 16:06:34
Add more sync cd async hooks
1 parent 1a9ced6
Changed files (1)
src/core.ts
@@ -37,12 +37,20 @@ type Options = {
 
 const storage = new AsyncLocalStorage<Options>()
 const hook = createHook({
-  before() {
-    if ($.cwd != process.cwd()) process.chdir($.cwd)
-  },
+  init: syncCwd,
+  before: syncCwd,
+  promiseResolve: syncCwd,
+  after: syncCwd,
+  destroy: syncCwd,
 })
 hook.enable()
 
+function syncCwd() {
+  if ($.cwd != process.cwd()) {
+    process.chdir($.cwd)
+  }
+}
+
 function initStore(): Options {
   const context = {
     verbose: true,
@@ -344,6 +352,8 @@ export class ProcessOutput extends Error {
 
 export function within<R>(callback: () => R): R {
   const result = storage.run({ ...getStore() }, callback)
-  if ($.cwd != process.cwd()) process.chdir($.cwd)
+  if ($.cwd != process.cwd()) {
+    process.chdir($.cwd)
+  }
   return result
 }