Commit f3a729e

Anton Golub <antongolub@antongolub.com>
2022-06-02 00:25:28
refactor: straighten init store (#420)
1 parent e8ac8fc
Changed files (1)
src/core.ts
@@ -37,8 +37,8 @@ type Options = {
 
 const storage = new AsyncLocalStorage<Options>()
 
-function init() {
-  storage.enterWith({
+function initStore(): Options {
+  const context = {
     verbose: true,
     cwd: process.cwd(),
     env: process.env,
@@ -46,7 +46,8 @@ function init() {
     prefix: '',
     quote,
     spawn,
-  })
+  }
+  storage.enterWith(context)
   if (process.env.ZX_VERBOSE) $.verbose = process.env.ZX_VERBOSE == 'true'
   try {
     $.shell = which.sync('bash')
@@ -54,16 +55,11 @@ function init() {
   } catch (err) {
     // ¯\_(ツ)_/¯
   }
+  return context
 }
 
 function getStore() {
-  let context = storage.getStore()
-  if (!context) {
-    init()
-    context = storage.getStore()
-    assert(context)
-  }
-  return context
+  return storage.getStore() || initStore()
 }
 
 export const $ = new Proxy<Shell & Options>(