sanoma 0.1.0 → 0.4.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/README.md +91 -24
- package/dist/check.d.ts +10 -5
- package/dist/check.js +77 -17
- package/dist/check.js.map +1 -1
- package/dist/cli.js +91 -23
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +5 -0
- package/dist/config.js +29 -3
- package/dist/config.js.map +1 -1
- package/dist/data.d.ts +1 -0
- package/dist/data.js +4 -0
- package/dist/data.js.map +1 -0
- package/dist/dev.d.ts +3 -1
- package/dist/dev.js +5 -2
- package/dist/dev.js.map +1 -1
- package/dist/env.js +9 -0
- package/dist/env.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts +41 -0
- package/dist/init.js +141 -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/oxlint-plugin.d.ts +7 -1
- package/dist/oxlint-plugin.js +10 -4
- package/dist/oxlint-plugin.js.map +1 -1
- package/dist/resources.d.ts +1 -0
- package/dist/resources.js +5 -0
- package/dist/resources.js.map +1 -0
- package/dist/serve.d.ts +25 -8
- package/dist/serve.js +160 -9
- package/dist/serve.js.map +1 -1
- package/dist/site-lint.d.ts +33 -0
- package/dist/site-lint.js +70 -0
- package/dist/site-lint.js.map +1 -0
- package/dist/sites.d.ts +7 -0
- package/dist/sites.js +11 -0
- package/dist/sites.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 +113 -18
- package/package.json +24 -9
- package/site.d.ts +3 -0
- package/site.js +3 -0
- package/template/.env.example +24 -0
- package/template/AGENTS.md +41 -0
- package/template/gitignore +11 -0
- package/template/package.json +17 -0
- package/template/pnpm-workspace.yaml +4 -0
- package/template/policies/policy.cedar +17 -0
- package/template/resources/billing/.gitkeep +0 -0
- package/template/resources/data.ts +21 -0
- package/template/resources/finance/.gitkeep +0 -0
- package/template/resources/identity/.gitkeep +0 -0
- package/template/resources/people/.gitkeep +0 -0
- package/template/resources/people.ts +20 -0
- package/template/sanoma.config.ts +10 -0
- package/template/scenarios/publish-post/publish.feature +28 -0
- package/template/tsconfig.json +4 -0
- package/template/workflows/publish-post.ts +20 -0
package/dist/init.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { execFile, spawn } from "node:child_process";
|
|
2
|
+
import { appendFileSync, cpSync, existsSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync, } from "node:fs";
|
|
3
|
+
import { userInfo } from "node:os";
|
|
4
|
+
import { basename, join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { promisify } from "node:util";
|
|
7
|
+
import { RUNTIME_AGENTS } from "@sanoma-ai/workflows";
|
|
8
|
+
import { readPeople } from "@sanoma-ai/workflows/describe";
|
|
9
|
+
import { groupNamesOf, principalIdsOf } from "@sanoma-ai/workflows/shared";
|
|
10
|
+
import { addToEnv, writeSecretsKey } from "./key.js";
|
|
11
|
+
const run = promisify(execFile);
|
|
12
|
+
/** The company template this package ships (`files`): `template/` beside `src/` and `dist/`. */
|
|
13
|
+
export const TEMPLATE = new URL("../template/", import.meta.url);
|
|
14
|
+
/** The runtime's default database (`resolveDatabaseUrl`), which this module does not load `@sanoma-ai/workflows` for; a test holds them equal. */
|
|
15
|
+
export const DATABASE_URL = "postgresql://postgres:dbos@localhost:5433/sanoma";
|
|
16
|
+
export const FIRST_COMMIT = "Start a Sanoma company";
|
|
17
|
+
/** What each package manager writes beside `package.json`. */
|
|
18
|
+
const LOCKFILES = ["pnpm-lock.yaml", "package-lock.json", "yarn.lock", "bun.lock"];
|
|
19
|
+
/**
|
|
20
|
+
* `sanoma` where there is no `sanoma.config.ts`: in an empty directory (dotfiles aside) it starts
|
|
21
|
+
* a company there and says so; anywhere else it does nothing and says it did not. Starting copies
|
|
22
|
+
* the template, names the package after the directory, declares whoever starts it as the first
|
|
23
|
+
* person in `resources/people.ts` (so the app admits them), writes `.env` (the default database and a
|
|
24
|
+
* new `SANOMA_SECRETS_KEY`, keeping what it already holds), commits what it wrote as the first
|
|
25
|
+
* commit of a git repository, as the machine's git user, then installs with the package manager
|
|
26
|
+
* that ran this (`npm_config_user_agent`, else pnpm) and adds the lockfile to that commit. Throws
|
|
27
|
+
* before writing anything when git is missing or has no user to commit as.
|
|
28
|
+
*/
|
|
29
|
+
export async function startCompany(cwd, { install = installWith, creator = whoIsStarting, log = () => { } } = {}) {
|
|
30
|
+
if (readdirSync(cwd).some((name) => !name.startsWith(".")))
|
|
31
|
+
return false;
|
|
32
|
+
await run("git", ["var", "GIT_AUTHOR_IDENT"]).catch((err) => {
|
|
33
|
+
const missing = err.code === "ENOENT";
|
|
34
|
+
throw new Error(missing ? "git is not installed" : "git needs a user to commit as: set git config user.name and user.email", { cause: err });
|
|
35
|
+
});
|
|
36
|
+
log(`starting a Sanoma company in ${cwd}`);
|
|
37
|
+
cpSync(TEMPLATE, cwd, { recursive: true, force: false });
|
|
38
|
+
// npm drops a package's .gitignore, so the template carries it as gitignore.
|
|
39
|
+
const [carried, ignore] = [join(cwd, "gitignore"), join(cwd, ".gitignore")];
|
|
40
|
+
if (!existsSync(ignore))
|
|
41
|
+
renameSync(carried, ignore);
|
|
42
|
+
else {
|
|
43
|
+
appendFileSync(ignore, `\n${readFileSync(carried, "utf8")}`);
|
|
44
|
+
rmSync(carried);
|
|
45
|
+
}
|
|
46
|
+
const own = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
47
|
+
const manifest = join(cwd, "package.json");
|
|
48
|
+
const pkg = JSON.parse(readFileSync(manifest, "utf8"));
|
|
49
|
+
const name = packageName(basename(cwd));
|
|
50
|
+
// npm refuses to install a package into one of the same name (`sanoma` in `sanoma/`).
|
|
51
|
+
pkg.name = name in pkg.dependencies ? `${name}-company` : name;
|
|
52
|
+
// The template's packages at this launcher's version: Sanoma releases them together.
|
|
53
|
+
for (const dep of Object.keys(pkg.dependencies))
|
|
54
|
+
pkg.dependencies[dep] = `^${own.version}`;
|
|
55
|
+
writeFileSync(manifest, `${JSON.stringify(pkg, null, 2)}\n`);
|
|
56
|
+
const people = join(cwd, "resources", "people.ts");
|
|
57
|
+
writeFileSync(people, declareFounder(readFileSync(people, "utf8"), await creator()));
|
|
58
|
+
addToEnv(join(cwd, ".env"), { SANOMA_DATABASE_URL: DATABASE_URL });
|
|
59
|
+
writeSecretsKey(join(cwd, ".env"));
|
|
60
|
+
const packageManager = packageManagerOf(process.env.npm_config_user_agent);
|
|
61
|
+
if (packageManager !== "pnpm")
|
|
62
|
+
rmSync(join(cwd, "pnpm-workspace.yaml"));
|
|
63
|
+
// Committed before the install, so a failed install leaves a repository to rerun it in; the
|
|
64
|
+
// lockfile joins the same commit after. Only what this wrote: a dotfile already here may be a secret.
|
|
65
|
+
const commit = async (...args) => {
|
|
66
|
+
const wrote = [...readdirSync(TEMPLATE), ...LOCKFILES]
|
|
67
|
+
.map((file) => (file === "gitignore" ? ".gitignore" : file))
|
|
68
|
+
.filter((file) => existsSync(join(cwd, file)));
|
|
69
|
+
await run("git", ["add", "--", ...wrote], { cwd });
|
|
70
|
+
await run("git", ["commit", "--quiet", "-m", FIRST_COMMIT, ...args], { cwd });
|
|
71
|
+
};
|
|
72
|
+
await run("git", ["init", "--quiet", "--initial-branch=main"], { cwd });
|
|
73
|
+
await commit();
|
|
74
|
+
log(`committed "${FIRST_COMMIT}"; installing with ${packageManager}`);
|
|
75
|
+
await install(cwd, packageManager);
|
|
76
|
+
await commit("--amend");
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
/** The template's founder, `export const founder = sanoma.person(…)`, which `sanoma init` declares as the creator. */
|
|
80
|
+
const FOUNDER = /^export const founder = sanoma\.person\(.*\);$/m;
|
|
81
|
+
/** The people file with its founder declared as `creator`. */
|
|
82
|
+
export function declareFounder(text, creator) {
|
|
83
|
+
const fields = Object.entries(creator)
|
|
84
|
+
.filter(([, value]) => value !== undefined)
|
|
85
|
+
.map(([key, value]) => `${key}: ${JSON.stringify(value)}`);
|
|
86
|
+
if (!FOUNDER.test(text))
|
|
87
|
+
throw new Error("the template's resources/people.ts declares no `founder`");
|
|
88
|
+
// A replacer, so a `$` in a name is kept as written.
|
|
89
|
+
return text.replace(FOUNDER, () => `export const founder = sanoma.person({ ${fields.join(", ")} });`);
|
|
90
|
+
}
|
|
91
|
+
/** What a command prints, trimmed; empty when it fails or is missing. */
|
|
92
|
+
const output = (cmd, args) => run(cmd, args, { timeout: 10_000 }).then(({ stdout }) => stdout.trim(), () => "");
|
|
93
|
+
/**
|
|
94
|
+
* `id`, unless it is empty or taken, by a person, agent or group the template declares or by one
|
|
95
|
+
* of the runtime's own agents: then "founder".
|
|
96
|
+
*/
|
|
97
|
+
function free(id) {
|
|
98
|
+
const { people } = readPeople(fileURLToPath(TEMPLATE));
|
|
99
|
+
const taken = [...principalIdsOf(people), ...groupNamesOf(people), ...RUNTIME_AGENTS];
|
|
100
|
+
return id && !taken.includes(id) ? id : "founder";
|
|
101
|
+
}
|
|
102
|
+
/** This machine's user name as one word; empty when the OS names none. */
|
|
103
|
+
function machineUser() {
|
|
104
|
+
try {
|
|
105
|
+
return userInfo().username.replace(/\s+/g, "-");
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return "";
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Who is starting the company: their GitHub login, as `gh` is signed in, which the GitHub sign-in
|
|
113
|
+
* matches and the loopback sign-in takes as the id; else this machine's user name. Named as git's
|
|
114
|
+
* `user.name`, which `startCompany` requires.
|
|
115
|
+
*/
|
|
116
|
+
export async function whoIsStarting() {
|
|
117
|
+
const [login, name] = await Promise.all([
|
|
118
|
+
output("gh", ["api", "user", "--jq", ".login"]),
|
|
119
|
+
output("git", ["config", "user.name"]),
|
|
120
|
+
]);
|
|
121
|
+
const named = name ? { name } : {};
|
|
122
|
+
if (/^[A-Za-z0-9-]+$/.test(login))
|
|
123
|
+
return { id: free(login), github: login, ...named };
|
|
124
|
+
return { id: free(machineUser()), ...named };
|
|
125
|
+
}
|
|
126
|
+
/** The package manager named first in `npm_config_user_agent` (`pnpm/11.1.0 npm/? node/v24…`), else pnpm. */
|
|
127
|
+
export function packageManagerOf(userAgent) {
|
|
128
|
+
const name = userAgent?.split("/")[0];
|
|
129
|
+
return name === "npm" || name === "yarn" || name === "bun" ? name : "pnpm";
|
|
130
|
+
}
|
|
131
|
+
/** A directory's name as a package's: lower case, runs of anything else made one `-`; `company` when nothing is left. */
|
|
132
|
+
export const packageName = (dir) => dir
|
|
133
|
+
.toLowerCase()
|
|
134
|
+
.replace(/[^a-z0-9._-]+/g, "-")
|
|
135
|
+
.replace(/^[-._]+|-+$/g, "") || "company";
|
|
136
|
+
const installWith = (cwd, packageManager) => new Promise((resolve, reject) => {
|
|
137
|
+
const child = spawn(packageManager, ["install"], { cwd, stdio: "inherit" });
|
|
138
|
+
child.once("error", reject);
|
|
139
|
+
child.once("exit", (code) => code === 0 ? resolve() : reject(new Error(`${packageManager} install failed; run it in ${cwd}, then sanoma`)));
|
|
140
|
+
});
|
|
141
|
+
//# sourceMappingURL=init.js.map
|
package/dist/init.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"init.js","sourceRoot":"","sources":["../src/init.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EACL,cAAc,EACd,MAAM,EACN,UAAU,EACV,WAAW,EACX,YAAY,EACZ,UAAU,EACV,MAAM,EACN,aAAa,GACd,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AA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,EAAE,MAAM,CAAC,CAAC;IAC5B,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE,CAC1B,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,GAAG,cAAc,8BAA8B,GAAG,eAAe,CAAC,CAAC,CAC9G,CAAC;AACJ,CAAC,CAAC,CAAC"}
|
package/dist/key.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adds each variable `file` (a `.env`) does not set yet, an empty value counting as unset, and
|
|
3
|
+
* keeps the file's own; a new file is readable by its owner alone. Returns the names it added.
|
|
4
|
+
*/
|
|
5
|
+
export declare function addToEnv(file: string, values: Record<string, string>): string[];
|
|
6
|
+
/**
|
|
7
|
+
* The one writer of `SANOMA_SECRETS_KEY`, which encrypts the stored credentials: unless `file`
|
|
8
|
+
* already sets it, makes one (32 random bytes, base64) and adds it (`addToEnv`). Returns the key
|
|
9
|
+
* it made, or undefined when it kept the file's.
|
|
10
|
+
*/
|
|
11
|
+
export declare function writeSecretsKey(file: string): string | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Makes sure `SANOMA_SECRETS_KEY` is set: when the environment (with `<dir>/.env` loaded) has
|
|
14
|
+
* none, writes one to `<dir>/.env` (`writeSecretsKey`), sets it here, and logs one line.
|
|
15
|
+
*/
|
|
16
|
+
export declare function ensureSecretsKey(dir: string, log: (line: string) => void): void;
|
package/dist/key.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { appendFileSync, existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { parseEnv } from "node:util";
|
|
5
|
+
const KEY = "SANOMA_SECRETS_KEY";
|
|
6
|
+
/**
|
|
7
|
+
* Adds each variable `file` (a `.env`) does not set yet, an empty value counting as unset, and
|
|
8
|
+
* keeps the file's own; a new file is readable by its owner alone. Returns the names it added.
|
|
9
|
+
*/
|
|
10
|
+
export function addToEnv(file, values) {
|
|
11
|
+
const text = existsSync(file) ? readFileSync(file, "utf8") : "";
|
|
12
|
+
const set = parseEnv(text);
|
|
13
|
+
const added = Object.keys(values).filter((name) => !set[name]);
|
|
14
|
+
if (!added.length)
|
|
15
|
+
return added;
|
|
16
|
+
const gap = text === "" || text.endsWith("\n") ? "" : "\n";
|
|
17
|
+
appendFileSync(file, gap + added.map((name) => `${name}=${values[name]}\n`).join(""), { mode: 0o600 });
|
|
18
|
+
return added;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The one writer of `SANOMA_SECRETS_KEY`, which encrypts the stored credentials: unless `file`
|
|
22
|
+
* already sets it, makes one (32 random bytes, base64) and adds it (`addToEnv`). Returns the key
|
|
23
|
+
* it made, or undefined when it kept the file's.
|
|
24
|
+
*/
|
|
25
|
+
export function writeSecretsKey(file) {
|
|
26
|
+
const key = randomBytes(32).toString("base64");
|
|
27
|
+
return addToEnv(file, { [KEY]: key }).length ? key : undefined;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Makes sure `SANOMA_SECRETS_KEY` is set: when the environment (with `<dir>/.env` loaded) has
|
|
31
|
+
* none, writes one to `<dir>/.env` (`writeSecretsKey`), sets it here, and logs one line.
|
|
32
|
+
*/
|
|
33
|
+
export function ensureSecretsKey(dir, log) {
|
|
34
|
+
if (process.env[KEY])
|
|
35
|
+
return;
|
|
36
|
+
const file = join(dir, ".env");
|
|
37
|
+
const key = writeSecretsKey(file);
|
|
38
|
+
if (key === undefined)
|
|
39
|
+
return;
|
|
40
|
+
process.env[KEY] = key;
|
|
41
|
+
log(`created ${KEY} in ${file}: it encrypts the stored credentials, so keep it with the database`);
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=key.js.map
|
package/dist/key.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"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MAAM,GAAG,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAC/C,OAAO,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC;AACjE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,GAAW,EAAE,GAA2B;IACvE,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;QAAE,OAAO;IAC7B,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IAC/B,MAAM,GAAG,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC;IAClC,IAAI,GAAG,KAAK,SAAS;QAAE,OAAO;IAC9B,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;IACvB,GAAG,CAAC,WAAW,GAAG,OAAO,IAAI,oEAAoE,CAAC,CAAC;AACrG,CAAC"}
|
package/dist/oxlint-plugin.d.ts
CHANGED
package/dist/oxlint-plugin.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
// The oxlint JS plugin `sanoma
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
|
|
1
|
+
// The oxlint JS plugin `sanoma`, loaded by this package's `oxlint.json` from `dist/`, inside this
|
|
2
|
+
// package (a path into another package's directory would depend on how an installer lays out
|
|
3
|
+
// `node_modules`): `@sanoma-ai/workflows/plugin`'s rule `data-file`, and `site-file` (`site-lint.ts`).
|
|
4
|
+
import workflows from "@sanoma-ai/workflows/plugin";
|
|
5
|
+
import { siteFile } from "./site-lint.js";
|
|
6
|
+
const plugin = {
|
|
7
|
+
...workflows,
|
|
8
|
+
rules: { ...workflows.rules, "site-file": siteFile },
|
|
9
|
+
};
|
|
10
|
+
export default plugin;
|
|
5
11
|
//# sourceMappingURL=oxlint-plugin.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oxlint-plugin.js","sourceRoot":"","sources":["../src/oxlint-plugin.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"oxlint-plugin.js","sourceRoot":"","sources":["../src/oxlint-plugin.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,6FAA6F;AAC7F,uGAAuG;AACvG,OAAO,SAAS,MAAM,6BAA6B,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,MAAM,MAAM,GAA+D;IACzE,GAAG,SAAS;IACZ,KAAK,EAAE,EAAE,GAAG,SAAS,CAAC,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE;CACrD,CAAC;AACF,eAAe,MAAM,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { sanoma, secret, type Secret } from "@sanoma-ai/workflows/resources";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// What a data file under `resources/` imports from sanoma: `secret`, which names a stored secret
|
|
2
|
+
// for a write-only field (`secret("SUPABASE_DB_PASSWORD", { generate: true })`), and `sanoma`,
|
|
3
|
+
// which declares the company's people, agents and groups (`sanoma.person({ id: "ada" })`).
|
|
4
|
+
export { sanoma, secret } from "@sanoma-ai/workflows/resources";
|
|
5
|
+
//# sourceMappingURL=resources.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resources.js","sourceRoot":"","sources":["../src/resources.ts"],"names":[],"mappings":"AAAA,iGAAiG;AACjG,+FAA+F;AAC/F,2FAA2F;AAC3F,OAAO,EAAE,MAAM,EAAE,MAAM,EAAe,MAAM,gCAAgC,CAAC"}
|
package/dist/serve.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type AppOptions } from "@sanoma-ai/app";
|
|
2
|
+
import { type ResolvedConfig, type SanomaConfig } from "@sanoma-ai/workflows";
|
|
2
3
|
export interface Stoppable {
|
|
3
4
|
stop(): Promise<void>;
|
|
4
5
|
}
|
|
@@ -8,11 +9,27 @@ export interface Served extends Stoppable {
|
|
|
8
9
|
}
|
|
9
10
|
/** Starts the worker, promoted: a local worker is always the version to take queued runs, even one seen before. */
|
|
10
11
|
export declare function runWorker(config: SanomaConfig): Promise<Stoppable>;
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
export interface ServeOptions {
|
|
13
|
+
/** The app's port: 0 for any free port. */
|
|
13
14
|
port: number;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
/** Receives what the sites' build has to say. */
|
|
16
|
+
log: (line: string) => void;
|
|
17
|
+
/** With sites, serve them from Vite's dev server, edits showing as they are saved, instead of building them. */
|
|
18
|
+
dev?: boolean;
|
|
19
|
+
/** The config, resolved, when the caller has it: the Cedar schema is made from it. */
|
|
20
|
+
resolved?: ResolvedConfig;
|
|
21
|
+
}
|
|
22
|
+
/** Starts the app on `port`, with the host and login `appOptions` reads, and the sites built; `url` says where. */
|
|
23
|
+
export declare function runApp(config: SanomaConfig, options: ServeOptions): Promise<Served>;
|
|
24
|
+
/** Reads stored secrets by name (`SanomaClient.secrets`): the values found, the environment's first. */
|
|
25
|
+
export type ReadSecrets = (names: string[]) => Promise<Map<string, string>>;
|
|
26
|
+
/**
|
|
27
|
+
* The app's host (`SANOMA_APP_HOST`, default 127.0.0.1) and, when the `SANOMA_GITHUB_*` variables
|
|
28
|
+
* are set, GitHub sign-in; both or neither, and any host but this machine needs it. An empty
|
|
29
|
+
* value is the same as unset. On a host beyond this machine, or with one of them set, one the
|
|
30
|
+
* environment leaves unset is read from the stored secrets (`readSecrets`), where creating the
|
|
31
|
+
* GitHub App on the app's connect page put them. A mistake in them is a UsageError.
|
|
32
|
+
*/
|
|
33
|
+
export declare function appOptions(readSecrets?: ReadSecrets): Promise<Pick<AppOptions, "host" | "login">>;
|
|
34
|
+
/** The sites' build, the worker, then the app, in this process; `stop` stops the app, then the worker. */
|
|
35
|
+
export declare function serve(config: SanomaConfig, serveOptions: ServeOptions): Promise<Served>;
|
package/dist/serve.js
CHANGED
|
@@ -1,21 +1,172 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { realpathSync } from "node:fs";
|
|
3
|
+
import { dirname, extname, isAbsolute, relative, sep } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { githubLogin, startApp } from "@sanoma-ai/app";
|
|
6
|
+
import { errorMessage, isLoopback, resolveConfig, SanomaClient, startWorker, } from "@sanoma-ai/workflows";
|
|
7
|
+
import { describeConfig, readPeople, schemaOf, writeGenerated } from "@sanoma-ai/workflows/describe";
|
|
8
|
+
import { UsageError } from "./config.js";
|
|
9
|
+
import { runtimeFrom, siteBuild } from "./sites.js";
|
|
10
|
+
import { localGitSource } from "./source.js";
|
|
11
|
+
/** The checkout the config was loaded from (`loadConfig` sets its `file`). */
|
|
12
|
+
const rootOf = (config) => config.root ?? dirname(config.file);
|
|
13
|
+
/** The checkout, as the worker's and the app's `source`. */
|
|
14
|
+
const sourceOf = (config) => localGitSource(rootOf(config));
|
|
3
15
|
/** Starts the worker, promoted: a local worker is always the version to take queued runs, even one seen before. */
|
|
4
16
|
export async function runWorker(config) {
|
|
5
|
-
const worker = await startWorker(config, { promote: true });
|
|
17
|
+
const worker = await startWorker(config, { promote: true, source: sourceOf(config) });
|
|
6
18
|
return { stop: () => worker.stop() };
|
|
7
19
|
}
|
|
8
|
-
/** Starts the app on `port`
|
|
9
|
-
export async function runApp(config,
|
|
10
|
-
|
|
20
|
+
/** Starts the app on `port`, with the host and login `appOptions` reads, and the sites built; `url` says where. */
|
|
21
|
+
export async function runApp(config, options) {
|
|
22
|
+
return served(config, await appStart(config, options));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* What the app starts with: the port, the host and login `appOptions` reads, and what it serves
|
|
26
|
+
* (`appSites`); the generated files written first (`writeGenerated`: `.sanoma/api.ts` and the
|
|
27
|
+
* Cedar schema), for an editor, the sites and the policies' author.
|
|
28
|
+
*/
|
|
29
|
+
async function appStart(config, { port, log, dev = false, resolved = resolveConfig(config) }) {
|
|
30
|
+
const options = { port, ...(await appOptions(secretsOf(config))) };
|
|
31
|
+
// For an editor and the sites' types only: a checkout it cannot write still serves.
|
|
32
|
+
await writeGenerated(rootOf(config), describeConfig(config), schemaOf(resolved, readPeople(rootOf(config)).people)).catch((err) => log(`could not write .sanoma/: ${errorMessage(err)}`));
|
|
33
|
+
const sites = await appSites(rootOf(config), log, dev ? options.host : false);
|
|
34
|
+
return { ...options, ...sites, ...(sites.dev && { dev: { ...sites.dev, onFileChange: apiWriter(config, log) } }) };
|
|
35
|
+
}
|
|
36
|
+
async function served(config, options) {
|
|
37
|
+
const app = await startApp(config, { ...options, source: sourceOf(config) });
|
|
11
38
|
return { url: app.url, stop: () => app.close() };
|
|
12
39
|
}
|
|
13
|
-
/**
|
|
14
|
-
|
|
40
|
+
/**
|
|
41
|
+
* What the app serves: with sites, its own build of the app and every site, built into
|
|
42
|
+
* `.sanoma/app/build/<key>` or reused when the key matches, or with `dev` (the app's host, when
|
|
43
|
+
* it is this machine's) the app and the sites on Vite's dev server; with none, the build the app
|
|
44
|
+
* ships. A folder that is not a site for its name is logged and left out.
|
|
45
|
+
*/
|
|
46
|
+
async function appSites(root, log, dev) {
|
|
47
|
+
const build = await siteBuild(root);
|
|
48
|
+
if (!build)
|
|
49
|
+
return {};
|
|
50
|
+
const { sites, refused } = build.sitesOf(root);
|
|
51
|
+
const named = `${sites.length} site${sites.length === 1 ? "" : "s"} (${sites.join(", ")})`;
|
|
52
|
+
// The dev server listens on this machine only.
|
|
53
|
+
const hot = dev !== false && (dev === undefined || isLoopback(dev));
|
|
54
|
+
if (sites.length && !hot) {
|
|
55
|
+
if (dev)
|
|
56
|
+
log(`SANOMA_APP_HOST=${dev} is not this machine: the sites are built, not served hot`);
|
|
57
|
+
log(`building the app with ${named}`);
|
|
58
|
+
const { distDir, built } = await build.buildApp(root, { log, runtimeFrom: runtimeFrom() });
|
|
59
|
+
if (!built)
|
|
60
|
+
log("app build up to date");
|
|
61
|
+
return { distDir };
|
|
62
|
+
}
|
|
63
|
+
// buildApp says the same of each when there is a build to run.
|
|
64
|
+
for (const name of refused) {
|
|
65
|
+
log(`sites/${name} is not mounted: a site's folder name is a URL segment (lowercase letters, digits and dashes)`);
|
|
66
|
+
}
|
|
67
|
+
if (!sites.length)
|
|
68
|
+
return {};
|
|
69
|
+
log(`serving the app with ${named} from Vite's dev server: edits show as they are saved`);
|
|
70
|
+
return { dev: { repo: root } };
|
|
71
|
+
}
|
|
72
|
+
/** Where a change cannot reach the config from: the sites, what sanoma writes, the install, git. */
|
|
73
|
+
const NOT_CONFIG = new Set(["sites", ".sanoma", "node_modules", ".git"]);
|
|
74
|
+
/**
|
|
75
|
+
* Writes the generated files again on a change to a file the config may be read from: the config's
|
|
76
|
+
* own, or TypeScript under the root outside `NOT_CONFIG` (a workflow may import a relative file
|
|
77
|
+
* anywhere). In a child `sanoma api`, in the config's directory, where it loads the config: this
|
|
78
|
+
* process holds the config as it was loaded, and Node imports a module once. One child at a time;
|
|
79
|
+
* changes while one runs are one more run after it.
|
|
80
|
+
*/
|
|
81
|
+
function apiWriter(config, log) {
|
|
82
|
+
const cli = fileURLToPath(new URL(`./cli${extname(fileURLToPath(import.meta.url))}`, import.meta.url));
|
|
83
|
+
// By real path, as the dev server's watcher names files.
|
|
84
|
+
const root = realpathSync(rootOf(config));
|
|
85
|
+
const cwd = config.file ? dirname(config.file) : root;
|
|
86
|
+
const readsConfig = (file) => {
|
|
87
|
+
if (file === config.file)
|
|
88
|
+
return true;
|
|
89
|
+
const path = relative(root, file);
|
|
90
|
+
return /\.tsx?$/.test(path) && !path.startsWith("..") && !isAbsolute(path) && !NOT_CONFIG.has(path.split(sep)[0]);
|
|
91
|
+
};
|
|
92
|
+
let running = false;
|
|
93
|
+
let again = false;
|
|
94
|
+
const run = () => {
|
|
95
|
+
running = true;
|
|
96
|
+
again = false;
|
|
97
|
+
spawn(process.execPath, [cli, "api"], { cwd, stdio: "inherit" })
|
|
98
|
+
.once("error", (err) => log(`could not write .sanoma/: ${err.message}`))
|
|
99
|
+
.once("close", () => {
|
|
100
|
+
running = false;
|
|
101
|
+
if (again)
|
|
102
|
+
run();
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
return (file) => {
|
|
106
|
+
if (!readsConfig(file))
|
|
107
|
+
return;
|
|
108
|
+
if (running)
|
|
109
|
+
again = true;
|
|
110
|
+
else
|
|
111
|
+
run();
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
const GITHUB = ["SANOMA_GITHUB_CLIENT_ID", "SANOMA_GITHUB_CLIENT_SECRET"];
|
|
115
|
+
/** The config's stored secrets, through a client opened for the read. */
|
|
116
|
+
const secretsOf = (config) => async (names) => {
|
|
117
|
+
const client = await SanomaClient.connect(config);
|
|
118
|
+
try {
|
|
119
|
+
return new Map([...(await client.secrets(names))].map(([name, { value }]) => [name, value]));
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
await client.close();
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* The app's host (`SANOMA_APP_HOST`, default 127.0.0.1) and, when the `SANOMA_GITHUB_*` variables
|
|
127
|
+
* are set, GitHub sign-in; both or neither, and any host but this machine needs it. An empty
|
|
128
|
+
* value is the same as unset. On a host beyond this machine, or with one of them set, one the
|
|
129
|
+
* environment leaves unset is read from the stored secrets (`readSecrets`), where creating the
|
|
130
|
+
* GitHub App on the app's connect page put them. A mistake in them is a UsageError.
|
|
131
|
+
*/
|
|
132
|
+
export async function appOptions(readSecrets) {
|
|
133
|
+
const env = process.env;
|
|
134
|
+
const host = env.SANOMA_APP_HOST || undefined;
|
|
135
|
+
const beyond = host !== undefined && !isLoopback(host);
|
|
136
|
+
const found = new Map(GITHUB.flatMap((name) => (env[name] ? [[name, env[name]]] : [])));
|
|
137
|
+
if ((beyond || found.size) && readSecrets && found.size < GITHUB.length) {
|
|
138
|
+
const stored = await readSecrets([...GITHUB]).catch((err) => {
|
|
139
|
+
throw new UsageError(`GitHub sign-in could not read the stored client id and secret: ${errorMessage(err)}`);
|
|
140
|
+
});
|
|
141
|
+
for (const [name, value] of stored)
|
|
142
|
+
if (value && !found.has(name))
|
|
143
|
+
found.set(name, value);
|
|
144
|
+
}
|
|
145
|
+
const [clientId, clientSecret] = GITHUB.map((name) => found.get(name));
|
|
146
|
+
if (!clientId && !clientSecret) {
|
|
147
|
+
if (beyond)
|
|
148
|
+
throw new UsageError(`SANOMA_APP_HOST=${host} needs GitHub sign-in: set ${GITHUB.join(", ")}`);
|
|
149
|
+
return { host };
|
|
150
|
+
}
|
|
151
|
+
if (!clientId || !clientSecret) {
|
|
152
|
+
const missing = GITHUB.filter((name) => !found.has(name));
|
|
153
|
+
throw new UsageError(`GitHub sign-in needs ${GITHUB.join(", ")}: ${missing.join(", ")} not set`);
|
|
154
|
+
}
|
|
155
|
+
try {
|
|
156
|
+
return { host, login: githubLogin({ clientId, clientSecret }) };
|
|
157
|
+
}
|
|
158
|
+
catch (err) {
|
|
159
|
+
throw new UsageError(errorMessage(err));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/** The sites' build, the worker, then the app, in this process; `stop` stops the app, then the worker. */
|
|
163
|
+
export async function serve(config, serveOptions) {
|
|
164
|
+
// Read and built before the worker starts, so a mistake in the variables or a site starts nothing.
|
|
165
|
+
const options = await appStart(config, serveOptions);
|
|
15
166
|
const worker = await runWorker(config);
|
|
16
167
|
let app;
|
|
17
168
|
try {
|
|
18
|
-
app = await
|
|
169
|
+
app = await served(config, options);
|
|
19
170
|
}
|
|
20
171
|
catch (err) {
|
|
21
172
|
await worker.stop().catch(() => { });
|
package/dist/serve.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version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G,MAAM,CAAC,KAAK,UAAU,KAAK,CAAC,MAAoB,EAAE,YAA0B;IAC1E,mGAAmG;IACnG,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IACrD,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,MAAM,CAAC,CAAC;IACvC,IAAI,GAAW,CAAC;IAChB,IAAI,CAAC;QACH,GAAG,GAAG,MAAM,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACtC,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;QACpC,MAAM,GAAG,CAAC;IACZ,CAAC;IACD,OAAO;QACL,GAAG,EAAE,GAAG,CAAC,GAAG;QACZ,KAAK,CAAC,IAAI;YACR,IAAI,CAAC;gBACH,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC;YACnB,CAAC;oBAAS,CAAC;gBACT,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;YACtB,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** An ESTree node, by the little this rule reads of it. */
|
|
2
|
+
interface AstNode {
|
|
3
|
+
type: string;
|
|
4
|
+
range: [number, number];
|
|
5
|
+
[field: string]: any;
|
|
6
|
+
}
|
|
7
|
+
interface RuleContext {
|
|
8
|
+
filename: string;
|
|
9
|
+
cwd: string;
|
|
10
|
+
report(diagnostic: {
|
|
11
|
+
message: string;
|
|
12
|
+
node: Pick<AstNode, "range">;
|
|
13
|
+
}): void;
|
|
14
|
+
}
|
|
15
|
+
/** What a site does instead of server code: the remedy every refusal under `sites/` names. */
|
|
16
|
+
export declare const REMEDY = "call an operation or start a workflow through `api` from `sanoma/site`.";
|
|
17
|
+
export declare const siteFile: {
|
|
18
|
+
meta: {
|
|
19
|
+
type: string;
|
|
20
|
+
docs: {
|
|
21
|
+
description: string;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
create(context: RuleContext): {
|
|
25
|
+
Program(program: AstNode): void;
|
|
26
|
+
ImportDeclaration: (node: AstNode) => void;
|
|
27
|
+
ExportNamedDeclaration: (node: AstNode) => void;
|
|
28
|
+
ExportAllDeclaration: (node: AstNode) => void;
|
|
29
|
+
ImportExpression: (node: AstNode) => void;
|
|
30
|
+
CallExpression(call: AstNode): void;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { basename, dirname, join, relative, resolve, sep } from "node:path";
|
|
2
|
+
/** What a site does instead of server code: the remedy every refusal under `sites/` names. */
|
|
3
|
+
export const REMEDY = "call an operation or start a workflow through `api` from `sanoma/site`.";
|
|
4
|
+
/** The server-side builders TanStack Start exports; a site has no server code. */
|
|
5
|
+
const SERVER_CALLS = new Set(["createServerFn", "createServerOnlyFn", "createMiddleware"]);
|
|
6
|
+
/** `f` for a call `f(…)`: a namespace import of TanStack Start is refused by the imports rule. */
|
|
7
|
+
const calleeName = (callee) => (callee.type === "Identifier" ? callee.name : undefined);
|
|
8
|
+
/** Whether `path` is `dir` or inside it. */
|
|
9
|
+
const inside = (dir, path) => {
|
|
10
|
+
const to = relative(dir, path);
|
|
11
|
+
return to !== ".." && !to.startsWith(`..${sep}`);
|
|
12
|
+
};
|
|
13
|
+
export const siteFile = {
|
|
14
|
+
meta: {
|
|
15
|
+
type: "problem",
|
|
16
|
+
docs: {
|
|
17
|
+
description: "A file under sites/ is client code: no *.server.* file, server function, middleware, route `server` handlers or relative import out of sites/",
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
create(context) {
|
|
21
|
+
// oxlint runs in the repo's root, where the override's `sites/**` matches.
|
|
22
|
+
const sites = join(context.cwd, "sites");
|
|
23
|
+
/** A relative import, not of types alone, that resolves outside `sites/`. */
|
|
24
|
+
const leaves = (node) => {
|
|
25
|
+
const source = node.source?.value;
|
|
26
|
+
if (typeof source !== "string" || !source.startsWith(".") || !inside(sites, context.filename))
|
|
27
|
+
return false;
|
|
28
|
+
if (node.importKind === "type" || node.exportKind === "type")
|
|
29
|
+
return false;
|
|
30
|
+
return !inside(sites, resolve(dirname(context.filename), source));
|
|
31
|
+
};
|
|
32
|
+
const imported = (node) => {
|
|
33
|
+
if (leaves(node)) {
|
|
34
|
+
context.report({
|
|
35
|
+
message: `A site imports only from inside sites/, \`sanoma/site\`, \`@sanoma-ai/app/ui\` and its packages: ${String(node.source.value)} leaves sites/.`,
|
|
36
|
+
node: node.source,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
Program(program) {
|
|
42
|
+
if (basename(context.filename).includes(".server.")) {
|
|
43
|
+
context.report({
|
|
44
|
+
message: `A site has no server code, so no *.server.* file: ${REMEDY}`,
|
|
45
|
+
node: program,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
ImportDeclaration: imported,
|
|
50
|
+
ExportNamedDeclaration: imported,
|
|
51
|
+
ExportAllDeclaration: imported,
|
|
52
|
+
ImportExpression: imported,
|
|
53
|
+
CallExpression(call) {
|
|
54
|
+
const name = calleeName(call.callee);
|
|
55
|
+
if (name !== undefined && SERVER_CALLS.has(name)) {
|
|
56
|
+
context.report({ message: `A site has no server code, so no ${name}: ${REMEDY}`, node: call.callee });
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
// createFileRoute("/path")({ server: { handlers } })
|
|
60
|
+
if (call.callee.type !== "CallExpression" || calleeName(call.callee.callee) !== "createFileRoute")
|
|
61
|
+
return;
|
|
62
|
+
const server = call.arguments[0]?.properties?.find((p) => p.type === "Property" && !p.computed && (p.key.name ?? p.key.value) === "server");
|
|
63
|
+
if (server) {
|
|
64
|
+
context.report({ message: `A site's route has no \`server\` handlers: ${REMEDY}`, node: server });
|
|
65
|
+
}
|
|
66
|
+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