Commit 0c3313d

Anton Golub <antongolub@antongolub.com>
2025-02-09 12:12:36
refactor: introduce internal API bus (#1083)
* refactor: introduce internal API bus * chore: assemle bus api * chore: strengthen vendor core types
1 parent d5a6dff
scripts/build-dts.mjs
@@ -68,7 +68,7 @@ const entries = [
 ]
 
 const compilationOptions = {
-  preferredConfigPath: './tsconfig.prod.json', // args.project,
+  preferredConfigPath: './tsconfig.json', // args.project,
   followSymlinks: true,
 }
 
src/vendor-core.ts
@@ -12,6 +12,30 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+import { default as _chalk } from 'chalk'
+import { default as _which } from 'which'
+import { default as _ps } from '@webpod/ps'
+
+type TCallable = (...args: any[]) => any
+const store = new Map<string, any>()
+const override = store.set.bind(store)
+const wrap = <T extends object>(name: string, api: T): T => {
+  override(name, api)
+  return new Proxy<T>(api, {
+    get(_, key) {
+      return store.get(name)[key]
+    },
+    apply(_, self, args) {
+      return (store.get(name) as TCallable).apply(self, args)
+    },
+  })
+}
+export const bus = {
+  override,
+  store,
+  wrap,
+}
+
 export {
   type TSpawnStore,
   type TSpawnStoreChunks,
@@ -24,6 +48,7 @@ export {
 export type RequestInfo = Parameters<typeof globalThis.fetch>[0]
 export type RequestInit = Parameters<typeof globalThis.fetch>[1]
 
-export { default as chalk, type ChalkInstance } from 'chalk'
-export { default as which } from 'which'
-export { default as ps } from '@webpod/ps'
+export { type ChalkInstance } from 'chalk'
+export const chalk: typeof _chalk = wrap('chalk', _chalk)
+export const which: typeof _which = wrap('which', _which)
+export const ps: typeof _ps = wrap('ps', _ps)
src/vendor.ts
@@ -12,5 +12,31 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+import { bus } from './vendor-core.js'
+import {
+  depseek as _depseek,
+  dotenv as _dotenv,
+  type minimist as TMinimistNamespace,
+  minimist as _minimist,
+  fs as _fs,
+  YAML as _YAML,
+  glob as _glob,
+  nodeFetch as _nodeFetch,
+} from './vendor-extra.js'
+
 export * from './vendor-core.js'
-export * from './vendor-extra.js'
+export { createRequire } from './vendor-extra.js'
+
+const { wrap } = bus
+export const depseek: typeof _depseek = wrap('depseek', _depseek)
+export const dotenv: typeof _dotenv = wrap('dotenv', _dotenv)
+export const fs: typeof _fs = wrap('fs', _fs)
+export const YAML: typeof _YAML = wrap('YAML', _YAML)
+export const glob: typeof _glob = wrap('glob', _glob)
+export const nodeFetch: typeof _nodeFetch = wrap('nodeFetch', _nodeFetch)
+
+export const minimist: typeof _minimist = wrap('minimist', _minimist)
+export namespace minimist {
+  export type Opts = TMinimistNamespace.Opts
+  export type ParsedArgs = TMinimistNamespace.ParsedArgs
+}
.size-limit.json
@@ -9,28 +9,28 @@
   {
     "name": "zx/index",
     "path": "build/*.{js,cjs}",
-    "limit": "810 kB",
+    "limit": "809 kB",
     "brotli": false,
     "gzip": false
   },
   {
     "name": "dts libdefs",
     "path": "build/*.d.ts",
-    "limit": "38.7 kB",
+    "limit": "38.5 kB",
     "brotli": false,
     "gzip": false
   },
   {
     "name": "vendor",
     "path": "build/vendor-*",
-    "limit": "765 kB",
+    "limit": "766 kB",
     "brotli": false,
     "gzip": false
   },
   {
     "name": "all",
     "path": "build/*",
-    "limit": "849 kB",
+    "limit": "848 kB",
     "brotli": false,
     "gzip": false
   }
package.json
@@ -66,7 +66,7 @@
     "build:js": "node scripts/build-js.mjs --format=cjs --hybrid --entry=src/*.ts:!src/error.ts:!src/repl.ts && npm run build:vendor",
     "build:vendor": "node scripts/build-js.mjs --format=cjs --entry=src/vendor-*.ts --bundle=all",
     "build:tests": "node scripts/build-tests.mjs",
-    "build:dts": "tsc --project tsconfig.prod.json && rm build/error.d.ts build/repl.d.ts && node scripts/build-dts.mjs",
+    "build:dts": "tsc --project tsconfig.json && rm build/error.d.ts build/repl.d.ts && node scripts/build-dts.mjs",
     "docs:dev": "vitepress dev docs",
     "docs:build": "vitepress build docs",
     "docs:preview": "vitepress preview docs",
tsconfig.prod.json
@@ -1,4 +0,0 @@
-{
-  "extends": "./tsconfig.json",
-  "exclude": []
-}