sanoma 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +105 -0
- package/README.md +116 -2
- package/dist/check.d.ts +27 -0
- package/dist/check.js +129 -0
- package/dist/check.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +232 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +21 -0
- package/dist/config.js +40 -0
- package/dist/config.js.map +1 -0
- package/dist/dev.d.ts +58 -0
- package/dist/dev.js +180 -0
- package/dist/dev.js.map +1 -0
- package/dist/env.d.ts +18 -0
- package/dist/env.js +58 -0
- package/dist/env.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/init.d.ts +24 -0
- package/dist/init.js +86 -0
- package/dist/init.js.map +1 -0
- package/dist/key.d.ts +16 -0
- package/dist/key.js +43 -0
- package/dist/key.js.map +1 -0
- package/dist/ledger.d.ts +7 -0
- package/dist/ledger.js +17 -0
- package/dist/ledger.js.map +1 -0
- package/dist/oxlint-plugin.d.ts +1 -0
- package/dist/oxlint-plugin.js +5 -0
- package/dist/oxlint-plugin.js.map +1 -0
- package/dist/postgres.d.ts +32 -0
- package/dist/postgres.js +134 -0
- package/dist/postgres.js.map +1 -0
- package/dist/resources.d.ts +1 -0
- package/dist/resources.js +4 -0
- package/dist/resources.js.map +1 -0
- package/dist/serve.d.ts +29 -0
- package/dist/serve.js +95 -0
- package/dist/serve.js.map +1 -0
- package/dist/source.d.ts +7 -0
- package/dist/source.js +42 -0
- package/dist/source.js.map +1 -0
- package/oxlint.json +211 -0
- package/package.json +69 -3
- package/template/.env.example +25 -0
- package/template/AGENTS.md +34 -0
- package/template/gitignore +11 -0
- package/template/package.json +17 -0
- package/template/pnpm-workspace.yaml +4 -0
- package/template/policies/policy.ts +19 -0
- package/template/resources/billing/.gitkeep +0 -0
- package/template/resources/finance/.gitkeep +0 -0
- package/template/resources/identity/.gitkeep +0 -0
- package/template/resources/people/.gitkeep +0 -0
- package/template/sanoma.config.ts +12 -0
- package/template/scenarios/publish-post/publish.feature +18 -0
- package/template/tsconfig.json +4 -0
- package/template/workflows/publish-post.ts +22 -0
- package/tsconfig.base.json +20 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-ALv2
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 Sanoma-AI
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
93
|
+
will apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,117 @@
|
|
|
1
|
-
#
|
|
1
|
+
# sanoma
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
One command over a company repo: `sanoma` runs its workflows and the app, `sanoma check` checks it, `sanoma env` writes its `.env.example`, `sanoma ledger export` exports a run. It also carries the surface a workflow or policy imports (`defineWorkflow`, `definePolicy`, the decisions, `defineConfig` and the types), so a repo depends on `sanoma` and its connectors, and writes no `worker.ts`, `app.ts`, compose file or vitest config.
|
|
4
|
+
|
|
5
|
+
## Start a company
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
mkdir acme && cd acme
|
|
9
|
+
npx sanoma
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
In an empty directory (dotfiles aside), `sanoma` starts a company there, then runs it: it copies the template this package ships (`template/`: `sanoma.config.ts`, a `publish-post` workflow with its scenarios, a policy, an empty `resources/` tree, `AGENTS.md`, `.env.example`), names the package after the directory, writes `.env` with the default `SANOMA_DATABASE_URL` and a new `SANOMA_SECRETS_KEY` (keeping what a `.env` there already sets), commits what it wrote (never a dotfile that was already there) as the first commit of a git repository on `main`, "Start a Sanoma company", as your git user, then installs with the package manager that ran it (`npm_config_user_agent`; pnpm when it names none) and adds the lockfile to that commit. The `sanoma` it installed then serves the company. Without git or a git user it stops before writing anything; a failed install leaves the commit, to install and run `sanoma` again in. A directory holding anything else and no `sanoma.config.ts` is refused, as for every command.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
npm install sanoma @sanoma-ai/connector-ghost
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`sanoma` brings `@sanoma-ai/workflows`, `@sanoma-ai/app`, `zod`, `typescript`, `@types/node` and `oxlint` with it: a repo installs nothing else but its connectors. Node's types reach the repo's `tsc` through `sanoma` itself (its declarations reference them, resolved from where it is installed), under pnpm, npm or a link alike, so a repo lists no `@types/node`. A repo that lists its own of another major gets two sets of Node's declarations, which conflict: keep one, by matching sanoma's or letting the installer dedupe them. A tsconfig project that imports nothing from `sanoma` (a scripts directory with a tsconfig of its own, say) gets none this way: it sets `"types": ["node"]` and lists `@types/node` itself. Node 24.2 or later, but not 25 (the bin uses `import.meta.main`, and Cucumber, which runs the scenarios, does not support 25); Docker for the Postgres it starts when none answers.
|
|
21
|
+
|
|
22
|
+
A repo holds `sanoma.config.ts` at its root, with `workflows/`, `policies/`, optional `resources/` (data files) and `scenarios/` (feature files) beside it, and a `tsconfig.json` that extends the one this package ships:
|
|
23
|
+
|
|
24
|
+
```json
|
|
25
|
+
{
|
|
26
|
+
"extends": "sanoma/tsconfig.json",
|
|
27
|
+
"include": ["sanoma.config.ts", "workflows", "policies", "resources"]
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
// sanoma.config.ts
|
|
33
|
+
import { ghost } from "@sanoma-ai/connector-ghost";
|
|
34
|
+
import { defineConfig } from "sanoma";
|
|
35
|
+
import policy from "./policies/policy.ts";
|
|
36
|
+
import publish from "./workflows/publish.ts";
|
|
37
|
+
|
|
38
|
+
export default defineConfig({
|
|
39
|
+
workflows: [publish],
|
|
40
|
+
connectors: [ghost],
|
|
41
|
+
policy,
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Each connector carries its driver and its fake, so the config names neither. The rest is the repo's layout: the app's name is `package.json`'s `name`, the scenarios are `scenarios/` and the data files `resources/`, beside `sanoma.config.ts`. The ledger is Postgres's, on the config's database; `ledger: jsonlLedger(dir)` keeps it in files instead.
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
// workflows/publish.ts
|
|
49
|
+
import { ghost } from "@sanoma-ai/connector-ghost";
|
|
50
|
+
import { defineWorkflow, z } from "sanoma";
|
|
51
|
+
|
|
52
|
+
export default defineWorkflow({
|
|
53
|
+
name: "publish",
|
|
54
|
+
trigger: "manual",
|
|
55
|
+
input: z.object({ id: z.string() }),
|
|
56
|
+
uses: [ghost.post.publish],
|
|
57
|
+
run: (ctx, { id }) => ctx.ghost.post.publish({ id }),
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
A workflow or policy imports from `sanoma` what the lint lets it (`defineWorkflow`, `definePolicy`, `allow`, `deny`, `approve`, `approvedFor`, `allowAll`, `mayDecide`, `errorCode`, `z`, and types); `zod` itself and `@sanoma-ai/workflows` are accepted too. The config file imports `defineConfig` (and a ledger store, `jsonlLedger`, `memoryLedger` or `postgresLedger`, when it names one) from `sanoma`, and its connectors from their packages.
|
|
62
|
+
|
|
63
|
+
## Commands
|
|
64
|
+
|
|
65
|
+
Run them from the directory that holds `sanoma.config.ts`. Each loads `.env` there first, when there is one (a variable already set wins), then the config.
|
|
66
|
+
|
|
67
|
+
| Command | What it does |
|
|
68
|
+
| --------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
69
|
+
| `sanoma` | Makes `SANOMA_SECRETS_KEY`, which encrypts the stored credentials, when the environment and `.env` have none, appending it to `.env` with one line saying so; makes sure Postgres answers (see [Postgres](#postgres)), then starts the worker (promoted, so a local worker takes every queued run) and the app in one process, and prints `<appName> running <workflows>` and `<appName> at http://127.0.0.1:3000; ctrl-c to stop`; the worker says `sanoma: 2 of 5 connectors not configured yet: ghost, resend; …` first when some vendors' variables are missing or invalid (a call to one fails, naming them, until they are set; nothing when all are set). In [dev mode](#dev-mode) it runs them in a child it restarts. In an empty directory it first [starts a company](#start-a-company). The worker and the app are given the repo's git checkout as their `source`, which commits to the current branch. |
|
|
70
|
+
| `sanoma worker`, `sanoma app` | One of the two alone, for a deployment that runs them apart. Neither touches Docker. |
|
|
71
|
+
| `sanoma check` | `tsc -p tsconfig.json`; oxlint over `workflows/`, `policies/` and `resources/` with the repo's `.oxlintrc.json`, written when missing (see [the lint](#the-lint)); every problem `describeConfig` finds (an import a workflow may not make, what its graph cannot draw, a data file outside the subset: what the worker would refuse); then every scenario against the fakes, on a database of its own (`<database>_check`), each step that did not hold a problem. Prints each problem on a line, `file:line:col: message` or `scenario › step: message`, and exits 1 when there is one. No vitest. |
|
|
72
|
+
| `sanoma env [--check]` | Writes `.env.example`: the variables `sanoma` reads, then every variable the connectors' drivers declare, by vendor, with its description; optional ones commented out. `--check` writes nothing and exits 1 with "`.env.example` is out of date: run `sanoma env`" when the file differs, for CI. |
|
|
73
|
+
| `sanoma ledger export <runId> [--out <file>]` | A run's ledger records from the config's store (the Postgres ledger on its database when it names none), as JSON lines (the shape `jsonlLedger` keeps), to stdout or `--out`. |
|
|
74
|
+
| `sanoma --help` | The usage. |
|
|
75
|
+
|
|
76
|
+
Exit codes: 2 for a mistake in the call or the repo (no `sanoma.config.ts`, an unknown command or option, a bad argument), printed with the usage where it helps; 1 for failed checks and failures while running; 0 otherwise. `SANOMA_APP_PORT` is the app's port (default 3000; 0 for any free port, which the printed address reports). `SANOMA_APP_HOST` is its address (default 127.0.0.1); any other needs [GitHub sign-in](../app/README.md#github), which `SANOMA_GITHUB_CLIENT_ID`, `SANOMA_GITHUB_CLIENT_SECRET` and `SANOMA_GITHUB_REPO` turn on (all three or none, with `SANOMA_SECRETS_KEY` set; the client id and secret may instead be the stored ones the app's Go live page saved when it created the GitHub App); a mistake in them exits 2 before anything starts. On SIGINT or SIGTERM the app, then the worker, stop, and the process exits 0.
|
|
77
|
+
|
|
78
|
+
## Postgres
|
|
79
|
+
|
|
80
|
+
The worker and the app run on `SANOMA_DATABASE_URL` (default `postgresql://postgres:dbos@localhost:5433/sanoma`), or the config's `databaseUrl`. `sanoma` (through `sanoma serve`) and `sanoma check` first probe it (`SELECT 1` as its user; `check` meanwhile runs tsc, oxlint and reads the config, and waits for Postgres only before the scenarios). When nothing answers and the host is this machine (`localhost`, `127.x.x.x`, `::1`), they start Postgres in Docker: the container `sanoma-<appName>-postgres` is started when it exists and is stopped, created from `postgres:17` on the URL's port, user, password and database when it does not, and left alone when it runs; then the probe is repeated until it answers (up to a minute). A container is never removed: `docker rm sanoma-<appName>-postgres` does that. A server that answers with an error (a wrong password), nothing answering at another host, and a missing Docker each stop the command with a message.
|
|
81
|
+
|
|
82
|
+
## `.env.example`
|
|
83
|
+
|
|
84
|
+
`sanoma env` keeps `.env.example` the manual of the variables: `SANOMA_DATABASE_URL`, `SANOMA_APP_PORT`, `SANOMA_APP_HOST` and the GitHub sign-in variables first, with their defaults, then a block per vendor in the connectors' order, each variable its driver's `env` declares with its `.describe()` text. A required variable is `NAME=`; an optional one is commented out, `# NAME=`, with `Optional.` before its description. The same config gives the same text, which `--check` compares byte for byte. Copy it to `.env` (ignored by git) and fill it in; the vendor variables can also be set on the app's connector pages, where a value from the environment wins over a stored one.
|
|
85
|
+
|
|
86
|
+
## The lint
|
|
87
|
+
|
|
88
|
+
`sanoma check` lints `workflows/`, `policies/` and `resources/` with oxlint, run in the repo's root on the repo's own `.oxlintrc.json`, so an editor's oxlint reads the same file and the rules' `workflows/**` globs match against the root. `sanoma check` writes that file when there is none:
|
|
89
|
+
|
|
90
|
+
```json
|
|
91
|
+
{
|
|
92
|
+
"extends": ["./node_modules/sanoma/oxlint.json"]
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
`sanoma/oxlint.json` is self-contained (it extends nothing): `@sanoma-ai/workflows`'s rules (the clock, randomness, the network, the environment, the imports a workflow may make, the data-file subset, with its plugin inside `sanoma`) and the rules this repo holds its own code to. Add your own rules beside the `extends`; `sanoma check` leaves an existing file alone.
|
|
97
|
+
|
|
98
|
+
## Generated files
|
|
99
|
+
|
|
100
|
+
| File | Written by | Commit it |
|
|
101
|
+
| ---------------- | ---------------------------- | --------- |
|
|
102
|
+
| `.env.example` | `sanoma env` | yes |
|
|
103
|
+
| `.oxlintrc.json` | `sanoma check`, when missing | yes |
|
|
104
|
+
|
|
105
|
+
## Dev mode
|
|
106
|
+
|
|
107
|
+
When the repo's `node_modules/sanoma` or any `node_modules/@sanoma-ai/*` is a link into a sanoma checkout (`pnpm link`), or `SANOMA_DEV_REPO` names one, `sanoma` runs `sanoma serve` in a child process and, in every linked package's directory, its `pnpm run watch` (each package's own watch build: `tsc --watch`, and `vite build --watch` too for the app). A change under any linked package's `dist/` restarts the child, after three quarters of a second of quiet, one restart at a time; a child that exits on its own is reported and started again on the next change. The child and each watch run in a process group of their own, so a terminal's ctrl-c reaches `sanoma` alone, which then ends each group (SIGTERM, SIGKILL after 5 seconds) and exits.
|
|
108
|
+
|
|
109
|
+
Linking some of `sanoma`, `@sanoma-ai/workflows`, `@sanoma-ai/app` and `@sanoma-ai/bridge` and not the others works, with two copies of a package in one process (the linked one, and the one an installed package depends on), and `sanoma` says which still come from their tarballs before it starts the child: `dev mode: sanoma, @sanoma-ai/app, @sanoma-ai/bridge still come from their tarballs: edits to them in the checkout will not show until you pnpm link them too`. Link all four to run the checkout alone.
|
|
110
|
+
|
|
111
|
+
## Hosting it yourself
|
|
112
|
+
|
|
113
|
+
`sanoma worker` and `sanoma app` are the deployment's two processes. A host that composes its own process keeps `startWorker` from `@sanoma-ai/workflows` and `startApp` from `@sanoma-ai/app`, which the bin runs.
|
|
114
|
+
|
|
115
|
+
[`runtime/`](runtime/Dockerfile) is the image a container host runs: Node 24, git, pnpm (corepack) and `provider-bridge`, and no company code. At start it clones `SANOMA_SOURCE` at `SANOMA_REVISION` (with `SANOMA_SOURCE_TOKEN` as an HTTP header when set), runs `pnpm install --frozen-lockfile` and `sanoma serve` on `0.0.0.0` and `PORT` (default 3000); the host passes `SANOMA_DATABASE_URL`, `SANOMA_SECRETS_KEY` and, as the app is not on loopback, the `SANOMA_GITHUB_*` sign-in variables too. Its health check is the app's `/healthz`. `provider-bridge` is built from source at a pinned commit; `--build-arg BRIDGE_FROM=release --build-arg BRIDGE_VERSION=<x.y.z>` downloads a release instead. CI builds the image and pushes it nowhere.
|
|
116
|
+
|
|
117
|
+
Status: early (0.x). License: FSL-1.1-ALv2.
|
package/dist/check.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Loaded } from "./config.ts";
|
|
2
|
+
export interface CheckResult {
|
|
3
|
+
ok: boolean;
|
|
4
|
+
/** One printable line each: `<file>:<line>:<col>: <message>`, or `<scenario> › <step>: <message>`. */
|
|
5
|
+
problems: string[];
|
|
6
|
+
}
|
|
7
|
+
export interface CheckOptions {
|
|
8
|
+
/** The Postgres the scenarios' worker runs on: a database of its own, never the config's. */
|
|
9
|
+
databaseUrl: string;
|
|
10
|
+
/**
|
|
11
|
+
* Settles once Postgres answers at `databaseUrl`'s server (the bin's `ensurePostgres`, started
|
|
12
|
+
* before `check`): awaited only before the scenarios, so tsc, oxlint and the config's reading
|
|
13
|
+
* do not wait on it. A failure is the check's only when there are scenarios to run.
|
|
14
|
+
*/
|
|
15
|
+
ready?: Promise<unknown>;
|
|
16
|
+
log: (line: string) => void;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Checks a repo the way its tests would, without a test framework: `tsc -p tsconfig.json` in
|
|
20
|
+
* its root; oxlint in the root, with the repo's `.oxlintrc.json` (written when missing, to extend
|
|
21
|
+
* this package's `oxlint.json`), over `workflows/`, `policies/` and `resources/`; every problem
|
|
22
|
+
* `describeConfig` finds in the data files and the workflows' files (the import rules and what
|
|
23
|
+
* the outline cannot draw: what the worker refuses to start with); these three at once, their
|
|
24
|
+
* problems in that order. Then every scenario against the fakes (`runScenarios`), each step that
|
|
25
|
+
* did not hold a problem. `ok` when there are no problems.
|
|
26
|
+
*/
|
|
27
|
+
export declare function check({ config, root, resolved }: Loaded, { databaseUrl, ready, log }: CheckOptions): Promise<CheckResult>;
|
package/dist/check.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join, relative } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { promisify } from "node:util";
|
|
6
|
+
import { describeConfig } from "@sanoma-ai/workflows/describe";
|
|
7
|
+
import { problemLine } from "@sanoma-ai/workflows/shared";
|
|
8
|
+
import { runScenarios } from "@sanoma-ai/workflows/scenarios";
|
|
9
|
+
/** The directories oxlint reads, those that exist. */
|
|
10
|
+
const LINTED = ["workflows", "policies", "resources"];
|
|
11
|
+
/**
|
|
12
|
+
* Checks a repo the way its tests would, without a test framework: `tsc -p tsconfig.json` in
|
|
13
|
+
* its root; oxlint in the root, with the repo's `.oxlintrc.json` (written when missing, to extend
|
|
14
|
+
* this package's `oxlint.json`), over `workflows/`, `policies/` and `resources/`; every problem
|
|
15
|
+
* `describeConfig` finds in the data files and the workflows' files (the import rules and what
|
|
16
|
+
* the outline cannot draw: what the worker refuses to start with); these three at once, their
|
|
17
|
+
* problems in that order. Then every scenario against the fakes (`runScenarios`), each step that
|
|
18
|
+
* did not hold a problem. `ok` when there are no problems.
|
|
19
|
+
*/
|
|
20
|
+
export async function check({ config, root, resolved }, { databaseUrl, ready = Promise.resolve(), log }) {
|
|
21
|
+
// Awaited below when there are scenarios; otherwise its failure is nobody's.
|
|
22
|
+
ready.catch(() => { });
|
|
23
|
+
const found = await Promise.all([typecheck(root, log), lint(root, log), configProblems(config, root, log)]);
|
|
24
|
+
const problems = found.flat();
|
|
25
|
+
if (resolved.scenarios === undefined) {
|
|
26
|
+
log("no scenarios: the config's root has no `scenarios/` directory");
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
await ready;
|
|
30
|
+
log("scenarios");
|
|
31
|
+
const { failed } = await runScenarios(config, { databaseUrl, log });
|
|
32
|
+
for (const f of failed)
|
|
33
|
+
problems.push(`${f.scenario} › ${f.step}: ${f.message}`);
|
|
34
|
+
}
|
|
35
|
+
return { ok: problems.length === 0, problems };
|
|
36
|
+
}
|
|
37
|
+
/** `tsc -p tsconfig.json` in the root. */
|
|
38
|
+
async function typecheck(root, log) {
|
|
39
|
+
log("tsc");
|
|
40
|
+
const tsc = await run(binOf("typescript"), ["-p", "tsconfig.json", "--pretty", "false"], root);
|
|
41
|
+
return tsc.code === 0 ? [] : tscProblems(tsc);
|
|
42
|
+
}
|
|
43
|
+
/** oxlint in the root, on its `.oxlintrc.json` (written when missing), over the directories that exist. */
|
|
44
|
+
async function lint(root, log) {
|
|
45
|
+
const dirs = LINTED.filter((dir) => existsSync(join(root, dir)));
|
|
46
|
+
if (!dirs.length)
|
|
47
|
+
return [];
|
|
48
|
+
if (writeOxlintrc(root))
|
|
49
|
+
log("wrote .oxlintrc.json");
|
|
50
|
+
log(`oxlint ${dirs.join(" ")}`);
|
|
51
|
+
return oxlintProblems(await run(binOf("oxlint"), ["--deny-warnings", "-f", "json", ...dirs], root));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* `describeConfig`'s problems: the data files', then each workflow's, its file relative to the
|
|
55
|
+
* root. A file defining several workflows gives each its problems: one line each here.
|
|
56
|
+
*/
|
|
57
|
+
async function configProblems(config, root, log) {
|
|
58
|
+
log("config");
|
|
59
|
+
const { problems, workflows } = describeConfig(config);
|
|
60
|
+
const lines = new Set([
|
|
61
|
+
...problems.map(problemLine),
|
|
62
|
+
...workflows.flatMap((wf) => wf.problems.map((p) => problemLine({ file: relative(root, wf.file), ...p }))),
|
|
63
|
+
]);
|
|
64
|
+
return [...lines];
|
|
65
|
+
}
|
|
66
|
+
/** Runs a JavaScript bin with this Node, in `cwd`; never throws for its exit code. */
|
|
67
|
+
async function run(bin, args, cwd) {
|
|
68
|
+
try {
|
|
69
|
+
const { stdout, stderr } = await promisify(execFile)(process.execPath, [bin, ...args], {
|
|
70
|
+
cwd,
|
|
71
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
72
|
+
});
|
|
73
|
+
return { code: 0, stdout, stderr };
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
const { code, stdout, stderr } = err;
|
|
77
|
+
if (typeof code !== "number")
|
|
78
|
+
throw err;
|
|
79
|
+
return { code, stdout: stdout ?? "", stderr: stderr ?? "" };
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** A package's bin (its only one), found from this package by its `package.json`: neither `typescript` nor `oxlint` exports it. */
|
|
83
|
+
function binOf(pkg) {
|
|
84
|
+
const manifest = new URL(import.meta.resolve(`${pkg}/package.json`));
|
|
85
|
+
const { bin } = JSON.parse(readFileSync(manifest, "utf8"));
|
|
86
|
+
return fileURLToPath(new URL(typeof bin === "string" ? bin : Object.values(bin)[0], manifest));
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* tsc's `--pretty false` lines, `file(line,col): error TS1234: message`, as `file:line:col: message`;
|
|
90
|
+
* anything else it printed as it is, and its exit code when it printed nothing.
|
|
91
|
+
*/
|
|
92
|
+
function tscProblems({ code, stdout, stderr }) {
|
|
93
|
+
const lines = `${stdout}${stderr}`.split(/\r?\n/).filter((l) => l.trim());
|
|
94
|
+
if (lines.length === 0)
|
|
95
|
+
return [`tsc exited with ${code}`];
|
|
96
|
+
return lines.map((line) => line.replace(/^(.+?)\((\d+),(\d+)\): /, "$1:$2:$3: "));
|
|
97
|
+
}
|
|
98
|
+
/** The repo's `.oxlintrc.json`: this package's lint, for `sanoma check` and an editor alike. */
|
|
99
|
+
const OXLINTRC = `${JSON.stringify({ extends: ["./node_modules/sanoma/oxlint.json"] }, null, 2)}\n`;
|
|
100
|
+
/**
|
|
101
|
+
* Writes `<root>/.oxlintrc.json` when there is none, and says whether it did. oxlint, run in the
|
|
102
|
+
* root, reads it and matches the rules' `workflows/**` globs against the root.
|
|
103
|
+
*/
|
|
104
|
+
function writeOxlintrc(root) {
|
|
105
|
+
const file = join(root, ".oxlintrc.json");
|
|
106
|
+
if (existsSync(file))
|
|
107
|
+
return false;
|
|
108
|
+
writeFileSync(file, OXLINTRC);
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
/** oxlint's JSON diagnostics as lines; what it printed instead when the output is not JSON (a config it refused). */
|
|
112
|
+
function oxlintProblems({ code, stdout, stderr }) {
|
|
113
|
+
let diagnostics;
|
|
114
|
+
try {
|
|
115
|
+
({ diagnostics } = JSON.parse(stdout));
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return code === 0 ? [] : [`oxlint exited with ${code}: ${stderr.trim() || stdout.trim()}`];
|
|
119
|
+
}
|
|
120
|
+
const lines = diagnostics.map(({ filename, message, help, labels }) => {
|
|
121
|
+
const span = labels[0]?.span;
|
|
122
|
+
const where = span ? `${filename}:${span.line}:${span.column}` : filename;
|
|
123
|
+
return `${where}: ${message}${help ? ` ${help}` : ""}`;
|
|
124
|
+
});
|
|
125
|
+
if (code !== 0 && lines.length === 0)
|
|
126
|
+
lines.push(`oxlint exited with ${code}: ${stderr.trim()}`);
|
|
127
|
+
return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AAC,QAAQ,CAAC;QAC1E,OAAO,GAAG,KAAK,KAAK,OAAO,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;IACzD,CAAC,CAAC,CAAC;IACH,IAAI,IAAI,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,KAAK,CAAC,IAAI,CAAC,sBAAsB,IAAI,KAAK,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IACjG,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
3
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
4
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
5
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
return path;
|
|
9
|
+
};
|
|
10
|
+
import { spawn } from "node:child_process";
|
|
11
|
+
import { existsSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
14
|
+
import { parseArgs } from "node:util";
|
|
15
|
+
import { loadConfig, UsageError } from "./config.js";
|
|
16
|
+
// Only Node's modules and ./config.ts are imported here: each command imports what it runs, so
|
|
17
|
+
// `--help`, `env`, `ledger export` and the dev-mode parent load neither the worker nor the app.
|
|
18
|
+
const USAGE = `usage: sanoma [command]
|
|
19
|
+
|
|
20
|
+
Runs the repo in the working directory, from its sanoma.config.ts (after its .env, when there is one).
|
|
21
|
+
|
|
22
|
+
sanoma the worker and the app; starts Postgres in Docker when nothing answers. In an
|
|
23
|
+
empty directory, first starts a company there: the template, .env, an install
|
|
24
|
+
and a git repository
|
|
25
|
+
sanoma worker the worker alone, for a deployment
|
|
26
|
+
sanoma app the app alone, for a deployment
|
|
27
|
+
sanoma check typecheck, lint, read the config, run every scenario; exit 1 on a problem
|
|
28
|
+
sanoma env [--check] write .env.example from the drivers' variables; --check exits 1 when it is out of date
|
|
29
|
+
sanoma ledger export <runId> [--out <file>]
|
|
30
|
+
a run's ledger records as JSON lines
|
|
31
|
+
|
|
32
|
+
SANOMA_DATABASE_URL names Postgres (default postgresql://postgres:dbos@localhost:5433/sanoma); SANOMA_APP_PORT the
|
|
33
|
+
app's port (default 3000; 0 for any free port); SANOMA_APP_HOST its address (default 127.0.0.1; any other needs
|
|
34
|
+
GitHub sign-in, see .env.example). With sanoma or a @sanoma-ai package linked from a sanoma checkout,
|
|
35
|
+
or SANOMA_DEV_REPO set, \`sanoma\` also runs each linked package's watch build and restarts on a new build.
|
|
36
|
+
`;
|
|
37
|
+
/** Exit codes: 2 for how the bin was called or what it found, 1 for failed checks and failures while running. */
|
|
38
|
+
const USAGE_EXIT = 2;
|
|
39
|
+
const log = (line) => console.error(line);
|
|
40
|
+
const messageOf = (err) => (err instanceof Error ? err.message : String(err));
|
|
41
|
+
/** Runs the command line; returns the exit code, or never, for a command that serves until a signal. */
|
|
42
|
+
export async function main(argv) {
|
|
43
|
+
let args;
|
|
44
|
+
try {
|
|
45
|
+
args = parse(argv);
|
|
46
|
+
}
|
|
47
|
+
catch (err) {
|
|
48
|
+
throw new UsageError(`${messageOf(err)}\n\n${USAGE.trimEnd()}`);
|
|
49
|
+
}
|
|
50
|
+
const { positionals, values } = args;
|
|
51
|
+
if (values.help) {
|
|
52
|
+
console.log(USAGE.trimEnd());
|
|
53
|
+
return 0;
|
|
54
|
+
}
|
|
55
|
+
const [command, ...rest] = positionals;
|
|
56
|
+
switch (command) {
|
|
57
|
+
case undefined: {
|
|
58
|
+
if (!existsSync(join(process.cwd(), "sanoma.config.ts"))) {
|
|
59
|
+
const { startCompany } = await import("./init.js");
|
|
60
|
+
if (await startCompany(process.cwd(), { log }))
|
|
61
|
+
return serveStarted(process.cwd());
|
|
62
|
+
}
|
|
63
|
+
const { devRepo, linkedPackages } = await import("./dev.js");
|
|
64
|
+
const linked = linkedPackages(process.cwd());
|
|
65
|
+
const repo = devRepo(process.cwd(), linked);
|
|
66
|
+
return repo === undefined ? serveCommand() : dev(repo, linked);
|
|
67
|
+
}
|
|
68
|
+
case "serve":
|
|
69
|
+
return serveCommand();
|
|
70
|
+
case "worker": {
|
|
71
|
+
const { runWorker } = await import("./serve.js");
|
|
72
|
+
return keep(await load(), runWorker);
|
|
73
|
+
}
|
|
74
|
+
case "app": {
|
|
75
|
+
const { runApp } = await import("./serve.js");
|
|
76
|
+
return keep(await load(), (config) => runApp(config, { port: port() }));
|
|
77
|
+
}
|
|
78
|
+
case "check":
|
|
79
|
+
return runCheck();
|
|
80
|
+
case "env":
|
|
81
|
+
return env(values.check ?? false);
|
|
82
|
+
case "ledger": {
|
|
83
|
+
const [sub, runId, ...extra] = rest;
|
|
84
|
+
if (sub !== "export" || !runId || extra.length)
|
|
85
|
+
throw new UsageError(`usage: sanoma ledger export <runId> [--out <file>]`);
|
|
86
|
+
return ledgerExport(runId, values.out);
|
|
87
|
+
}
|
|
88
|
+
default:
|
|
89
|
+
throw new UsageError(`unknown command "${command}"\n\n${USAGE.trimEnd()}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const parse = (argv) => parseArgs({
|
|
93
|
+
args: argv,
|
|
94
|
+
allowPositionals: true,
|
|
95
|
+
options: { help: { type: "boolean", short: "h" }, check: { type: "boolean" }, out: { type: "string" } },
|
|
96
|
+
});
|
|
97
|
+
const port = () => Number(process.env.SANOMA_APP_PORT ?? 3000);
|
|
98
|
+
async function load() {
|
|
99
|
+
const loaded = await loadConfig();
|
|
100
|
+
const { resolveConfig } = await import("@sanoma-ai/workflows");
|
|
101
|
+
return { ...loaded, resolved: resolveConfig(loaded.config) };
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Starts what `start` starts over the loaded config, prints what runs (and where, when it
|
|
105
|
+
* listens), and stops it on SIGINT or SIGTERM, then exits 0. The worker names the vendors not
|
|
106
|
+
* configured yet itself, in one line.
|
|
107
|
+
*/
|
|
108
|
+
async function keep({ config, resolved }, start) {
|
|
109
|
+
const started = await start(config);
|
|
110
|
+
const running = `${resolved.appName} running ${[...resolved.workflows.keys()].join(", ")}`;
|
|
111
|
+
if (started.url === undefined) {
|
|
112
|
+
console.log(`${running}; ctrl-c to stop`);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
console.log(running);
|
|
116
|
+
console.log(`${resolved.appName} at ${started.url}; ctrl-c to stop`);
|
|
117
|
+
}
|
|
118
|
+
onSignal(() => started.stop());
|
|
119
|
+
return new Promise(() => { });
|
|
120
|
+
}
|
|
121
|
+
/** `sanoma serve` (and `sanoma` outside dev mode): Postgres, then the worker and the app in this process. */
|
|
122
|
+
async function serveCommand() {
|
|
123
|
+
const loaded = await load();
|
|
124
|
+
(await import("./key.js")).ensureSecretsKey(process.cwd(), log);
|
|
125
|
+
const { appName, databaseUrl } = loaded.resolved;
|
|
126
|
+
const { ensurePostgres } = await import("./postgres.js");
|
|
127
|
+
await ensurePostgres(databaseUrl, { appName, log });
|
|
128
|
+
const { serve } = await import("./serve.js");
|
|
129
|
+
return keep(loaded, (config) => serve(config, { port: port() }));
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* A company just started in `cwd`: the `sanoma` it installed serves it, in this process, so that
|
|
133
|
+
* the config and the worker load the same packages; this one when it installed none there.
|
|
134
|
+
*/
|
|
135
|
+
async function serveStarted(cwd) {
|
|
136
|
+
const installed = join(cwd, "node_modules/sanoma/dist/cli.js");
|
|
137
|
+
if (!existsSync(installed))
|
|
138
|
+
return serveCommand();
|
|
139
|
+
const { main: run } = (await import(__rewriteRelativeImportExtension(pathToFileURL(installed).href)));
|
|
140
|
+
return run([]);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Dev mode: `sanoma serve` in a child it restarts on a new build, and each linked package's
|
|
144
|
+
* `watch`. Each runs in a process group of its own, so a terminal's ctrl-c reaches this process
|
|
145
|
+
* alone, and its `stop` is the one path that ends them.
|
|
146
|
+
*/
|
|
147
|
+
async function dev(repo, packages) {
|
|
148
|
+
const { partialLink, startWatchers, supervise } = await import("./dev.js");
|
|
149
|
+
const cwd = process.cwd();
|
|
150
|
+
log(`dev mode: ${repo}; building ${packages.map((p) => p.name).join(", ") || "nothing"} as it changes`);
|
|
151
|
+
const fromTarballs = partialLink(cwd);
|
|
152
|
+
if (fromTarballs.length) {
|
|
153
|
+
log(`dev mode: ${fromTarballs.join(", ")} still come from their tarballs: ` +
|
|
154
|
+
"edits to them in the checkout will not show until you pnpm link them too");
|
|
155
|
+
}
|
|
156
|
+
const watchers = startWatchers(packages, log);
|
|
157
|
+
const cli = fileURLToPath(import.meta.url);
|
|
158
|
+
const child = supervise({
|
|
159
|
+
spawn: () => spawn(process.execPath, [cli, "serve"], { cwd, env: process.env, stdio: "inherit", detached: true }),
|
|
160
|
+
watch: packages.map((p) => join(p.dir, "dist")),
|
|
161
|
+
log,
|
|
162
|
+
});
|
|
163
|
+
onSignal(async () => {
|
|
164
|
+
await child.stop();
|
|
165
|
+
await watchers.stop();
|
|
166
|
+
});
|
|
167
|
+
return new Promise(() => { });
|
|
168
|
+
}
|
|
169
|
+
/** `sanoma check`: Postgres made to answer while tsc, oxlint and the config are read; the scenarios on `<database>_check`. */
|
|
170
|
+
async function runCheck() {
|
|
171
|
+
const loaded = await load();
|
|
172
|
+
const { appName, databaseUrl } = loaded.resolved;
|
|
173
|
+
const [{ ensurePostgres }, { check }] = await Promise.all([import("./postgres.js"), import("./check.js")]);
|
|
174
|
+
const own = new URL(databaseUrl);
|
|
175
|
+
own.pathname = `${own.pathname}_check`;
|
|
176
|
+
// Started here, handed to check at once (which handles its failure), awaited before the scenarios.
|
|
177
|
+
const ready = ensurePostgres(databaseUrl, { appName, log });
|
|
178
|
+
const { ok, problems } = await check(loaded, { databaseUrl: own.toString(), ready, log });
|
|
179
|
+
for (const problem of problems)
|
|
180
|
+
console.log(problem);
|
|
181
|
+
log(ok ? "ok" : `${problems.length} problem${problems.length === 1 ? "" : "s"}`);
|
|
182
|
+
return ok ? 0 : 1;
|
|
183
|
+
}
|
|
184
|
+
async function env(checkOnly) {
|
|
185
|
+
const loaded = await load();
|
|
186
|
+
const { writeEnvExample } = await import("./env.js");
|
|
187
|
+
const { changed } = writeEnvExample(loaded, { check: checkOnly });
|
|
188
|
+
if (!checkOnly) {
|
|
189
|
+
log(changed ? "wrote .env.example" : ".env.example is up to date");
|
|
190
|
+
return 0;
|
|
191
|
+
}
|
|
192
|
+
if (changed)
|
|
193
|
+
log("`.env.example` is out of date: run `sanoma env`");
|
|
194
|
+
return changed ? 1 : 0;
|
|
195
|
+
}
|
|
196
|
+
async function ledgerExport(runId, out) {
|
|
197
|
+
const { config } = await loadConfig();
|
|
198
|
+
const { exportLedger } = await import("./ledger.js");
|
|
199
|
+
const text = await exportLedger(config, runId);
|
|
200
|
+
if (out === undefined)
|
|
201
|
+
process.stdout.write(text);
|
|
202
|
+
else {
|
|
203
|
+
writeFileSync(out, text);
|
|
204
|
+
log(`wrote ${out}`);
|
|
205
|
+
}
|
|
206
|
+
return 0;
|
|
207
|
+
}
|
|
208
|
+
/** On the first SIGINT or SIGTERM, stops what runs and exits 0; a second signal exits at once. */
|
|
209
|
+
function onSignal(stop) {
|
|
210
|
+
let stopping = false;
|
|
211
|
+
const handle = () => {
|
|
212
|
+
if (stopping)
|
|
213
|
+
process.exit(130);
|
|
214
|
+
stopping = true;
|
|
215
|
+
stop().then(() => process.exit(0), (err) => {
|
|
216
|
+
log(messageOf(err));
|
|
217
|
+
process.exit(1);
|
|
218
|
+
});
|
|
219
|
+
};
|
|
220
|
+
process.once("SIGINT", handle);
|
|
221
|
+
process.once("SIGTERM", handle);
|
|
222
|
+
}
|
|
223
|
+
if (import.meta.main) {
|
|
224
|
+
try {
|
|
225
|
+
process.exitCode = await main(process.argv.slice(2));
|
|
226
|
+
}
|
|
227
|
+
catch (err) {
|
|
228
|
+
log(messageOf(err));
|
|
229
|
+
process.exitCode = err instanceof UsageError ? USAGE_EXIT : 1;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
//# sourceMappingURL=cli.js.map
|