|
| 1 | +# The zx architecture |
| 2 | +This section helps to better understand the `zx` concepts and logic, and will be useful for those who want to become a project contributor, make tools based on it, or create something similar from scratch. |
| 3 | + |
| 4 | +## High-level modules |
| 5 | +| Module | Description | |
| 6 | +|-------------------------------------------------------------------------|---------------------------------------------------------------------| |
| 7 | +| [zurk](https://github.com/webpod/zurk) | Execution engine for spawning and managing child processes. | |
| 8 | +| [./src/core.ts](https://github.com/google/zx/blob/main/src/core.ts) | `$` factory, presets, utilities, high-level APIs. | |
| 9 | +| [./src/goods.ts](https://github.com/google/zx/blob/main/src/goods.ts) | Utilities for common tasks like fs ops, glob search, fetching, etc. | |
| 10 | +| [./src/cli.ts](https://github.com/google/zx/blob/main/src/cli.ts) | CLI interface and scripts pre-processors. | |
| 11 | +| [./src/deps.ts](https://github.com/google/zx/blob/main/src/deps.ts) | Dependency analyzing and installation. | |
| 12 | +| [./src/vendor.ts](https://github.com/google/zx/blob/main/src/vendor.ts) | Third-party libraries. | |
| 13 | +| [./src/utils.ts](https://github.com/google/zx/blob/main/src/utils.ts) | Generic helpers. | |
| 14 | +| [./src/md.ts](https://github.com/google/zx/blob/main/src/md.ts) | Markdown scripts extractor. | |
| 15 | +| [./src/error.ts](https://github.com/google/zx/blob/main/src/error.ts) | Error handling and formatting. | |
| 16 | +| [./src/global.ts](https://github.com/google/zx/blob/main/src/global.ts) | Global injectors. | |
| 17 | + |
| 18 | + |
| 19 | +## Core design |
| 20 | + |
| 21 | +### `Options` |
| 22 | +A set of options for `$` and `ProcessPromise` configuration. `defaults` holds the initial library preset. `Snapshot` captures the current `Options `context and attaches isolated subparts. |
| 23 | + |
| 24 | +### `$` |
| 25 | +A piece of template literal magic. |
| 26 | +```ts |
| 27 | +interface Shell< |
| 28 | + S = false, |
| 29 | + R = S extends true ? ProcessOutput : ProcessPromise, |
| 30 | +> { |
| 31 | + (pieces: TemplateStringsArray, ...args: any[]): R |
| 32 | + <O extends Partial<Options> = Partial<Options>, R = O extends { sync: true } ? Shell<true> : Shell>(opts: O): R |
| 33 | + sync: { |
| 34 | + (pieces: TemplateStringsArray, ...args: any[]): ProcessOutput |
| 35 | + (opts: Partial<Omit<Options, 'sync'>>): Shell<true> |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +$`cmd ${arg}` // ProcessPromise |
| 40 | +$(opts)`cmd ${arg}` // ProcessPromise |
| 41 | +$.sync`cmd ${arg}` // ProcessOutput |
| 42 | +$.sync(opts)`cmd ${arg}` // ProcessOutput |
| 43 | +``` |
| 44 | + |
| 45 | +The `$` factory creates `ProcessPromise` instances and bounds with snapshot-context via `Proxy` and `AsyncLocalStorage`. The trick: |
| 46 | +```ts |
| 47 | +const storage = new AsyncLocalStorage<Options>() |
| 48 | + |
| 49 | +const getStore = () => storage.getStore() || defaults |
| 50 | + |
| 51 | +function within<R>(callback: () => R): R { |
| 52 | + return storage.run({ ...getStore() }, callback) |
| 53 | +} |
| 54 | +// Inside $ factory ... |
| 55 | +const opts = getStore() |
| 56 | +if (!Array.isArray(pieces)) { |
| 57 | + return function (this: any, ...args: any) { |
| 58 | + return within(() => Object.assign($, opts, pieces).apply(this, args)) |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +### `ProcessPromise` |
| 64 | +A promise-inherited class represents and operates a child process, provides methods for piping, killing, response formatting. |
| 65 | + |
| 66 | +#### Lifecycle |
| 67 | +| Stage | Description | |
| 68 | +|--------------|------------------------| |
| 69 | +| `initial` | Blank instance | |
| 70 | +| `halted` | Awaits running | |
| 71 | +| `running` | Process in action | |
| 72 | +| `fulfilled` | Successfully completed | |
| 73 | +| `rejected` | Failed | |
| 74 | + |
| 75 | +| Gear | Description | |
| 76 | +|--------------|---------------------------------------------------------------------------------------------| |
| 77 | +| `build()` | Produces `cmd` from template and context, applies `quote` to arguments | |
| 78 | +| `run()` | Spawns the process and captures its data via `zurk` events listeners | |
| 79 | +| `finalize()` | Assigns the result to the instance: analyzes status code, invokes `_resolve()`, `_reject()` | |
| 80 | + |
| 81 | +### `ProcessOutput` |
| 82 | +A class that represents the output of a `ProcessPromise`. It provides methods to access the process's stdout, stderr, exit code and extra methods for formatting the output and checking the process's success. |
| 83 | + |
| 84 | +### `Fail` |
| 85 | +Consolidates error handling functionality across the zx library: errors codes mapping, formatting, stack parsing. |
| 86 | + |
| 87 | +## Building |
| 88 | +In the early stages of the project, we [had some difficulties](https://dev.to/antongolub/how-and-why-do-we-bundle-zx-1ca6) with zx packaging. We couldn't find a suitable tool for assembly, so we made our own toolkit based on [esbuild](https://github.com/evanw/esbuild) and [dts-bundle-generator](https://github.com/timocov/dts-bundle-generator). This process is divided into several scripts. |
| 89 | + |
| 90 | +| Script | Description | |
| 91 | +|----------------------------------------------------------------------------------------------|------------------------------------------------------------------------| |
| 92 | +| [`./scripts/build-dts.mjs`](https://github.com/google/zx/blob/main/scripts/build-dts.mjs) | Extracts and merges 3rd-party types, generates `dts` files. | |
| 93 | +| [`./scripts/build-js.mjs`](https://github.com/google/zx/blob/main/scripts/build-js.mjs) | Produces [hybrid bundles](./setup#hybrid) for each package entry point | |
| 94 | +| [`./scripts/build-jsr.mjs`](https://github.com/google/zx/blob/main/scripts/build-jsr.mjs) | Builds extra assets for [JSR](https://jsr.io/@webpod/zx) publishing | |
| 95 | +| [`./scripts/build-tests.mjs`](https://github.com/google/zx/blob/main/scripts/build-test.mjs) | Generates autotests to verify exports consistency | |
0 commit comments