@wwchen/stack 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 +21 -0
- package/README.md +79 -2
- package/blocks/AGENTS.md +77 -0
- package/blocks/CLAUDE.md +113 -0
- package/blocks/env.example +34 -0
- package/blocks/gitignore +18 -0
- package/configs/biome.json +45 -0
- package/configs/tsconfig.next.json +22 -0
- package/dist/auth/better-auth/index.d.ts +316 -0
- package/dist/auth/better-auth/index.js +237 -0
- package/dist/auth/better-auth/tables.d.ts +628 -0
- package/dist/auth/better-auth/tables.js +74 -0
- package/dist/auth/better-auth/users.d.ts +27 -0
- package/dist/auth/better-auth/users.js +128 -0
- package/dist/auth/index.d.ts +58 -0
- package/dist/auth/index.js +120 -0
- package/dist/auth/proxy.d.ts +14 -0
- package/dist/auth/proxy.js +50 -0
- package/dist/auth/safe-next.d.ts +17 -0
- package/dist/auth/safe-next.js +36 -0
- package/dist/auth/types.d.ts +47 -0
- package/dist/auth/types.js +5 -0
- package/dist/cli/context.d.ts +38 -0
- package/dist/cli/context.js +43 -0
- package/dist/cli/index.d.ts +4 -0
- package/dist/cli/index.js +95 -0
- package/dist/cli/neon.d.ts +97 -0
- package/dist/cli/neon.js +334 -0
- package/dist/cli/sync-blocks.d.ts +16 -0
- package/dist/cli/sync-blocks.js +97 -0
- package/dist/cli/user-create.d.ts +2 -0
- package/dist/cli/user-create.js +35 -0
- package/dist/cli/vercel-build.d.ts +4 -0
- package/dist/cli/vercel-build.js +72 -0
- package/dist/configs/vitest.d.ts +4 -0
- package/dist/configs/vitest.js +50 -0
- package/dist/connections/crypto.d.ts +8 -0
- package/dist/connections/crypto.js +50 -0
- package/dist/connections/index.d.ts +63 -0
- package/dist/connections/index.js +212 -0
- package/dist/connections/providers.d.ts +30 -0
- package/dist/connections/providers.js +47 -0
- package/dist/connections/table.d.ts +156 -0
- package/dist/connections/table.js +29 -0
- package/dist/db/explaining-pool.d.ts +16 -0
- package/dist/db/explaining-pool.js +62 -0
- package/dist/db/index.d.ts +21 -0
- package/dist/db/index.js +36 -0
- package/dist/db/migration-status.d.ts +8 -0
- package/dist/db/migration-status.js +41 -0
- package/dist/db/target.d.ts +26 -0
- package/dist/db/target.js +72 -0
- package/dist/db/testing/index.d.ts +27 -0
- package/dist/db/testing/index.js +71 -0
- package/dist/db/testing/isolate.d.ts +1 -0
- package/dist/db/testing/isolate.js +4 -0
- package/dist/db/testing/pglite.d.ts +21 -0
- package/dist/db/testing/pglite.js +71 -0
- package/dist/env/define-env.d.ts +37 -0
- package/dist/env/define-env.js +134 -0
- package/dist/env/index.d.ts +3 -0
- package/dist/env/index.js +3 -0
- package/dist/env/load-env-file.d.ts +15 -0
- package/dist/env/load-env-file.js +52 -0
- package/dist/env/stack-env.d.ts +40 -0
- package/dist/env/stack-env.js +60 -0
- package/dist/env/testing.d.ts +8 -0
- package/dist/env/testing.js +16 -0
- package/dist/http/client-ip.d.ts +12 -0
- package/dist/http/client-ip.js +28 -0
- package/dist/http/guards.d.ts +17 -0
- package/dist/http/guards.js +57 -0
- package/dist/http/rate-limit.d.ts +34 -0
- package/dist/http/rate-limit.js +79 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/jobs/index.d.ts +37 -0
- package/dist/jobs/index.js +51 -0
- package/dist/mail/index.d.ts +45 -0
- package/dist/mail/index.js +97 -0
- package/dist/next/api.d.ts +71 -0
- package/dist/next/api.js +118 -0
- package/dist/next/config.d.ts +8 -0
- package/dist/next/config.js +104 -0
- package/dist/next/cron.d.ts +14 -0
- package/dist/next/cron.js +40 -0
- package/dist/next/health.d.ts +19 -0
- package/dist/next/health.js +36 -0
- package/dist/next/index.d.ts +5 -0
- package/dist/next/index.js +4 -0
- package/dist/testing/next.d.ts +14 -0
- package/dist/testing/next.js +39 -0
- package/dist/ui/app.css +56 -0
- package/dist/ui/globals.css +152 -0
- package/dist/ui/icons.d.ts +17 -0
- package/dist/ui/icons.js +104 -0
- package/dist/ui/index.d.ts +33 -0
- package/dist/ui/index.js +41 -0
- package/dist/ui/theme.d.ts +18 -0
- package/dist/ui/theme.js +24 -0
- package/package.json +134 -4
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { realpathSync } from "node:fs";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { CliError, resolveDeps } from "./context.js";
|
|
6
|
+
import { devSetup, neonSeed, resetPreview, withNeon } from "./neon.js";
|
|
7
|
+
import { syncBlocksCommand } from "./sync-blocks.js";
|
|
8
|
+
import { userCreate } from "./user-create.js";
|
|
9
|
+
import { vercelBuild } from "./vercel-build.js";
|
|
10
|
+
const USAGE = `usage: stack <command>
|
|
11
|
+
with-neon <command> [args] run a command against your Neon dev branch
|
|
12
|
+
dev-setup create your dev branch from seed and migrate it
|
|
13
|
+
neon seed (re)create the seed branch and load the app's seed data
|
|
14
|
+
neon reset-preview (re)create the shared preview branch from seed
|
|
15
|
+
vercel-build Vercel's build command: migrate, then next build
|
|
16
|
+
user:create <email> [--admin] add a user through the app's auth
|
|
17
|
+
sync-blocks [--check] refresh the stack-owned blocks in AGENTS.md, CLAUDE.md, .gitignore, .env.example`;
|
|
18
|
+
/** The testable entry: every outside effect arrives through `deps`; the exit code comes back. */
|
|
19
|
+
export async function runCli(argv, deps) {
|
|
20
|
+
const cli = resolveDeps(deps);
|
|
21
|
+
const [command, ...args] = argv;
|
|
22
|
+
try {
|
|
23
|
+
switch (command) {
|
|
24
|
+
case "with-neon":
|
|
25
|
+
return await withNeon(cli, args);
|
|
26
|
+
case "dev-setup":
|
|
27
|
+
return await devSetup(cli);
|
|
28
|
+
case "neon":
|
|
29
|
+
if (args[0] === "seed")
|
|
30
|
+
return await neonSeed(cli);
|
|
31
|
+
if (args[0] === "reset-preview")
|
|
32
|
+
return await resetPreview(cli);
|
|
33
|
+
break;
|
|
34
|
+
case "vercel-build":
|
|
35
|
+
return await vercelBuild(cli);
|
|
36
|
+
case "user:create":
|
|
37
|
+
return await userCreate(cli, args);
|
|
38
|
+
case "sync-blocks":
|
|
39
|
+
return await syncBlocksCommand(cli, args);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
cli.error(error instanceof CliError
|
|
44
|
+
? error.message
|
|
45
|
+
: error instanceof Error
|
|
46
|
+
? error.message
|
|
47
|
+
: String(error));
|
|
48
|
+
return 1;
|
|
49
|
+
}
|
|
50
|
+
cli.error(USAGE);
|
|
51
|
+
return 2;
|
|
52
|
+
}
|
|
53
|
+
/** Runs a program with an argument array (never a shell), forwarding signals to an inherited child. */
|
|
54
|
+
function exec(command, args, options = {}) {
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
const child = spawn(command, args, {
|
|
57
|
+
env: options.env,
|
|
58
|
+
stdio: options.inherit ? "inherit" : ["ignore", "pipe", "inherit"],
|
|
59
|
+
});
|
|
60
|
+
let stdout = "";
|
|
61
|
+
child.stdout?.on("data", (chunk) => {
|
|
62
|
+
stdout += chunk;
|
|
63
|
+
});
|
|
64
|
+
const forward = (signal) => () => child.kill(signal);
|
|
65
|
+
const handlers = ["SIGINT", "SIGTERM"].map((s) => [s, forward(s)]);
|
|
66
|
+
if (options.inherit)
|
|
67
|
+
for (const [s, h] of handlers)
|
|
68
|
+
process.on(s, h);
|
|
69
|
+
child.on("error", reject);
|
|
70
|
+
child.on("close", (code, signal) => {
|
|
71
|
+
for (const [s, h] of handlers)
|
|
72
|
+
process.off(s, h);
|
|
73
|
+
resolve({ stdout, code: code ?? (signal ? 1 : 0) });
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Whether Node is running this file as its program, through any symlink (node_modules/.bin).
|
|
79
|
+
* Not `import.meta.main`: that needs Node 24.2, and the package accepts any 24.x.
|
|
80
|
+
*/
|
|
81
|
+
function isEntryPoint() {
|
|
82
|
+
const program = process.argv[1];
|
|
83
|
+
if (!program)
|
|
84
|
+
return false;
|
|
85
|
+
try {
|
|
86
|
+
return realpathSync(program) === fileURLToPath(import.meta.url);
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// The one place besides src/env/ that reads process.env.
|
|
93
|
+
if (isEntryPoint()) {
|
|
94
|
+
process.exit(await runCli(process.argv.slice(2), { env: process.env, exec, cwd: process.cwd() }));
|
|
95
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { type Cli, type Env } from "./context.ts";
|
|
2
|
+
export declare const SEED = "seed";
|
|
3
|
+
export declare const PREVIEW = "preview";
|
|
4
|
+
export type Branch = {
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
default: boolean;
|
|
8
|
+
parent_id?: string;
|
|
9
|
+
};
|
|
10
|
+
/** Where the new seed is built; it takes the name `seed` only once it is clean. */
|
|
11
|
+
export declare const SEED_NEXT = "seed-next";
|
|
12
|
+
export declare function projectId(env: Env): string;
|
|
13
|
+
/**
|
|
14
|
+
* Refuses a dev branch name that is not a dev branch: production (whatever this project calls its
|
|
15
|
+
* default branch, and the name `production` itself), the shared `seed` and `preview`, and `seed-next`
|
|
16
|
+
* (the seed being built, which may briefly hold production's data). One check
|
|
17
|
+
* for every command that uses your dev branch: with-neon, dev-setup and user:create.
|
|
18
|
+
*/
|
|
19
|
+
export declare function assertDevBranchName(name: string, production: Branch): void;
|
|
20
|
+
/** `dev/<os user>` unless NEON_DEV_BRANCH names another. */
|
|
21
|
+
export declare const devBranchName: (cli: Cli) => string;
|
|
22
|
+
export declare function findBranch(cli: Cli, name: string): Promise<Branch | undefined>;
|
|
23
|
+
/** Production: the project's default branch (Neon's `default` flag), not a branch name. */
|
|
24
|
+
export declare function defaultBranch(branches: Branch[]): Branch;
|
|
25
|
+
/**
|
|
26
|
+
* What `.env` used to carry for local work: `branch`'s URLs, and the host of production (the
|
|
27
|
+
* default branch; `production` when the caller already has it, else listed).
|
|
28
|
+
*/
|
|
29
|
+
export declare function branchEnv(cli: Cli, branch: string, production?: Branch): Promise<Record<string, string>>;
|
|
30
|
+
/** The migrations, run against whatever database `env` names, through the direct endpoint. */
|
|
31
|
+
export declare function migrate(cli: Cli, env: Env): Promise<number>;
|
|
32
|
+
/**
|
|
33
|
+
* `stack with-neon <command> [args]` — runs a local command against your Neon dev branch with no
|
|
34
|
+
* `.env`: the connection strings and production's host come from the Neon CLI on every run.
|
|
35
|
+
*
|
|
36
|
+
* Steps aside, and runs the command untouched, when something else already says where the database
|
|
37
|
+
* is: DATABASE_URL in the environment (Vercel, an e2e run, a local Postgres), ENV_FILE
|
|
38
|
+
* (a deliberate production command). A `.env` never decides: what this sets wins over it, in Next
|
|
39
|
+
* and in `loadEnvFile` alike.
|
|
40
|
+
*/
|
|
41
|
+
export declare function withNeon(cli: Cli, [command, ...args]: string[]): Promise<number>;
|
|
42
|
+
/**
|
|
43
|
+
* Your dev branch's `branchEnv`, as `with-neon` hands it to a command; nothing (`{}`) when
|
|
44
|
+
* DATABASE_URL or ENV_FILE already says where the database is. Refuses the default branch.
|
|
45
|
+
*/
|
|
46
|
+
export declare function devBranchEnv(cli: Cli): Promise<Record<string, string>>;
|
|
47
|
+
/**
|
|
48
|
+
* `stack dev-setup` — once per developer: makes your Neon dev branch from `seed` (never from
|
|
49
|
+
* production) if it does not exist yet (`dev/<os user>`, or NEON_DEV_BRANCH) and migrates it. Safe
|
|
50
|
+
* to re-run. Sign-in needs nothing on the branch: Better Auth runs in the app, with a development
|
|
51
|
+
* BETTER_AUTH_SECRET and magic links written to the sink file. There is no `.env` to write:
|
|
52
|
+
* `stack with-neon` reads the branch from the Neon CLI on every command.
|
|
53
|
+
*/
|
|
54
|
+
export declare function devSetup(cli: Cli): Promise<number>;
|
|
55
|
+
/**
|
|
56
|
+
* Schemas Neon's own services install, which hold functions and settings rather than anyone's rows:
|
|
57
|
+
* `auth` is the pg_session_jwt extension the Data API installs (Neon: "do not install this extension
|
|
58
|
+
* manually"), `pgrst` is the Data API's PostgREST configuration. Left in place so the branch keeps
|
|
59
|
+
* working like any other. Neon Auth's `neon_auth` is *not* here: it holds users.
|
|
60
|
+
*/
|
|
61
|
+
export declare const NEON_SCHEMAS: readonly string[];
|
|
62
|
+
/**
|
|
63
|
+
* Empties a database down to an empty `public`: drops every schema but Postgres's own
|
|
64
|
+
* (`pg_catalog`, `pg_toast*`, `information_schema`, any `pg_*`) and Neon's service schemas, with
|
|
65
|
+
* everything in them — tables, rows, drizzle's journal, Neon Auth's users. Then the migrations
|
|
66
|
+
* run from nothing, as on a new database. A schema-only branch needs this as much as a full copy:
|
|
67
|
+
* it keeps drizzle's journal *table* but not its rows, so migrate would re-run 0000 into tables
|
|
68
|
+
* that already exist. Large objects belong to the database rather than to a schema, so no drop
|
|
69
|
+
* removes them: every one is unlinked too.
|
|
70
|
+
*
|
|
71
|
+
* Neon's schemas are kept by name, so first it checks they hold what Neon puts there: functions and
|
|
72
|
+
* settings, no rows. A table, partitioned table, materialized view, sequence or foreign table in
|
|
73
|
+
* one (an app's own `auth.accounts`, say) would carry production's rows into the seed, so it
|
|
74
|
+
* refuses before dropping anything.
|
|
75
|
+
*/
|
|
76
|
+
export declare function emptyDatabase(sql: (statement: string) => Promise<Record<string, unknown>[]>): Promise<string[]>;
|
|
77
|
+
/**
|
|
78
|
+
* `stack neon seed` — (re)creates the `seed` branch: exactly the app's migrations plus its seed
|
|
79
|
+
* rows, and none of production's data. Cuts it from production (the project's default branch) as
|
|
80
|
+
* Neon's `--schema-only` branch (a normal branch where that is unsupported), empties it of every
|
|
81
|
+
* non-system schema either way (`emptyDatabase`), migrates it from nothing and runs the app's
|
|
82
|
+
* `db/seed.ts`: `export default async function seed(db)`.
|
|
83
|
+
*
|
|
84
|
+
* The new seed is built as `seed-next` and takes the name `seed` only once it is clean, so a
|
|
85
|
+
* failure part-way never leaves a branch holding production's data under the name every dev and
|
|
86
|
+
* preview branch is cut from; `seed-next` is deleted on any failure. Neon will not delete a branch
|
|
87
|
+
* that has children, so a seed with dev/preview branches on it is refused up front: remove them
|
|
88
|
+
* yourself (this never deletes them).
|
|
89
|
+
*/
|
|
90
|
+
export declare function neonSeed(cli: Cli): Promise<number>;
|
|
91
|
+
/**
|
|
92
|
+
* `stack neon reset-preview` — (re)creates the one shared `preview` branch from `seed` and prints
|
|
93
|
+
* its pooled URL. A build cannot change the DATABASE_URL a preview deployment uses at runtime, so
|
|
94
|
+
* previews share this branch: set the URL printed here once as a Preview-scoped Vercel variable.
|
|
95
|
+
* `stack vercel-build` migrates it on each preview build; run this again to start it clean.
|
|
96
|
+
*/
|
|
97
|
+
export declare function resetPreview(cli: Cli): Promise<number>;
|
package/dist/cli/neon.js
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
// Your Neon branches, driven through the Neon CLI rather than a `.env` file. `stack dev-setup`
|
|
2
|
+
// makes your dev branch, `stack with-neon` hands its connection to every local command, and
|
|
3
|
+
// `stack neon seed` / `reset-preview` keep the two shared branches. Needs the Neon CLI signed in
|
|
4
|
+
// (`neon auth`) or NEON_API_KEY, and NEON_PROJECT_ID in the environment.
|
|
5
|
+
//
|
|
6
|
+
// Runs before any database is set, so it reads its own `deps.env`, never the app's validated env.
|
|
7
|
+
//
|
|
8
|
+
// The branch tree: production -> `seed` (the app's migrations and seed rows, no production data)
|
|
9
|
+
// -> `dev/<user>` for each developer and one shared `preview`. Nothing local or preview is ever
|
|
10
|
+
// cut from production, so production's data never leaves it. Production is the project's default
|
|
11
|
+
// branch, whatever it is named (Neon calls a new project's `main`): `defaultBranch`.
|
|
12
|
+
import { endpointOf } from "../db/index.js";
|
|
13
|
+
import { CliError, importApp, loadAppEnv } from "./context.js";
|
|
14
|
+
export const SEED = "seed";
|
|
15
|
+
export const PREVIEW = "preview";
|
|
16
|
+
/** Where the new seed is built; it takes the name `seed` only once it is clean. */
|
|
17
|
+
export const SEED_NEXT = "seed-next";
|
|
18
|
+
export function projectId(env) {
|
|
19
|
+
const id = env.NEON_PROJECT_ID?.trim();
|
|
20
|
+
if (!id) {
|
|
21
|
+
throw new CliError("NEON_PROJECT_ID is not set. Export your Neon project's id (Neon console -> Project settings).");
|
|
22
|
+
}
|
|
23
|
+
return id;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Refuses a dev branch name that is not a dev branch: production (whatever this project calls its
|
|
27
|
+
* default branch, and the name `production` itself), the shared `seed` and `preview`, and `seed-next`
|
|
28
|
+
* (the seed being built, which may briefly hold production's data). One check
|
|
29
|
+
* for every command that uses your dev branch: with-neon, dev-setup and user:create.
|
|
30
|
+
*/
|
|
31
|
+
export function assertDevBranchName(name, production) {
|
|
32
|
+
const yours = "set NEON_DEV_BRANCH to your own branch (default dev/<os user>)";
|
|
33
|
+
if (name === production.name || name === "production") {
|
|
34
|
+
throw new CliError(`${name} is production, not a dev branch: production commands use ENV_FILE=.env.neon; ${yours}.`);
|
|
35
|
+
}
|
|
36
|
+
if (name === SEED || name === SEED_NEXT || name === PREVIEW) {
|
|
37
|
+
throw new CliError(`${name} is a shared branch, not a dev branch; ${yours}.`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** `dev/<os user>` unless NEON_DEV_BRANCH names another. */
|
|
41
|
+
export const devBranchName = (cli) => cli.env.NEON_DEV_BRANCH?.trim() || `dev/${cli.username}`;
|
|
42
|
+
/** Runs `neon <args> --project-id <id>`; stdout trimmed, a failure is the CLI's own message plus the exit code. */
|
|
43
|
+
async function neon(cli, args) {
|
|
44
|
+
const { stdout, code } = await cli.exec("neon", [...args, "--project-id", projectId(cli.env)]);
|
|
45
|
+
return { stdout: stdout.trim(), code };
|
|
46
|
+
}
|
|
47
|
+
async function neonOk(cli, args) {
|
|
48
|
+
const { stdout, code } = await neon(cli, args);
|
|
49
|
+
if (code !== 0)
|
|
50
|
+
throw new CliError(`neon ${args.slice(0, 2).join(" ")} failed (exit ${code}).`);
|
|
51
|
+
return stdout;
|
|
52
|
+
}
|
|
53
|
+
async function neonJson(cli, args) {
|
|
54
|
+
return JSON.parse(await neonOk(cli, [...args, "--output", "json"]));
|
|
55
|
+
}
|
|
56
|
+
const listBranches = (cli) => neonJson(cli, ["branches", "list"]);
|
|
57
|
+
export async function findBranch(cli, name) {
|
|
58
|
+
return (await listBranches(cli)).find((b) => b.name === name);
|
|
59
|
+
}
|
|
60
|
+
/** Production: the project's default branch (Neon's `default` flag), not a branch name. */
|
|
61
|
+
export function defaultBranch(branches) {
|
|
62
|
+
const production = branches.find((b) => b.default);
|
|
63
|
+
if (!production) {
|
|
64
|
+
throw new CliError("the Neon project has no default branch, so there is no production to tell apart; set one in the Neon console.");
|
|
65
|
+
}
|
|
66
|
+
return production;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* What `.env` used to carry for local work: `branch`'s URLs, and the host of production (the
|
|
70
|
+
* default branch; `production` when the caller already has it, else listed).
|
|
71
|
+
*/
|
|
72
|
+
export async function branchEnv(cli, branch, production) {
|
|
73
|
+
const productionId = (production ?? defaultBranch(await listBranches(cli))).id;
|
|
74
|
+
const [pooled, direct, productionUrl] = await Promise.all([
|
|
75
|
+
neonOk(cli, ["connection-string", branch, "--pooled"]),
|
|
76
|
+
neonOk(cli, ["connection-string", branch]),
|
|
77
|
+
neonOk(cli, ["connection-string", productionId]),
|
|
78
|
+
]);
|
|
79
|
+
return {
|
|
80
|
+
DATABASE_URL: pooled,
|
|
81
|
+
DATABASE_URL_UNPOOLED: direct,
|
|
82
|
+
// Keeps assertDevDatabase armed: it refuses this host and allows the dev branch.
|
|
83
|
+
PRODUCTION_DATABASE_HOST: new URL(productionUrl).hostname,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/** Cuts `name` from `parent`; undefined when the Neon CLI refuses. */
|
|
87
|
+
async function createBranch(cli, name, parent, extra = []) {
|
|
88
|
+
const created = await neon(cli, [
|
|
89
|
+
"branches",
|
|
90
|
+
"create",
|
|
91
|
+
"--name",
|
|
92
|
+
name,
|
|
93
|
+
"--parent",
|
|
94
|
+
parent,
|
|
95
|
+
...extra,
|
|
96
|
+
"--output",
|
|
97
|
+
"json",
|
|
98
|
+
]);
|
|
99
|
+
if (created.code !== 0)
|
|
100
|
+
return undefined;
|
|
101
|
+
return JSON.parse(created.stdout).branch;
|
|
102
|
+
}
|
|
103
|
+
/** Deletes `name` if it exists, then cuts it afresh from `parent`; the new branch. */
|
|
104
|
+
async function recreate(cli, name, parent) {
|
|
105
|
+
const existing = await findBranch(cli, name);
|
|
106
|
+
if (existing)
|
|
107
|
+
await neonOk(cli, ["branches", "delete", existing.id]);
|
|
108
|
+
return createBranch(cli, name, parent);
|
|
109
|
+
}
|
|
110
|
+
/** The migrations, run against whatever database `env` names, through the direct endpoint. */
|
|
111
|
+
export async function migrate(cli, env) {
|
|
112
|
+
return (await cli.exec("pnpm", ["exec", "drizzle-kit", "migrate"], { env, inherit: true })).code;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* `stack with-neon <command> [args]` — runs a local command against your Neon dev branch with no
|
|
116
|
+
* `.env`: the connection strings and production's host come from the Neon CLI on every run.
|
|
117
|
+
*
|
|
118
|
+
* Steps aside, and runs the command untouched, when something else already says where the database
|
|
119
|
+
* is: DATABASE_URL in the environment (Vercel, an e2e run, a local Postgres), ENV_FILE
|
|
120
|
+
* (a deliberate production command). A `.env` never decides: what this sets wins over it, in Next
|
|
121
|
+
* and in `loadEnvFile` alike.
|
|
122
|
+
*/
|
|
123
|
+
export async function withNeon(cli, [command, ...args]) {
|
|
124
|
+
if (!command) {
|
|
125
|
+
cli.error("usage: stack with-neon <command> [args]");
|
|
126
|
+
return 2;
|
|
127
|
+
}
|
|
128
|
+
const env = { ...cli.env, ...(await devBranchEnv(cli)) };
|
|
129
|
+
return (await cli.exec(command, args, { env, inherit: true })).code;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Your dev branch's `branchEnv`, as `with-neon` hands it to a command; nothing (`{}`) when
|
|
133
|
+
* DATABASE_URL or ENV_FILE already says where the database is. Refuses the default branch.
|
|
134
|
+
*/
|
|
135
|
+
export async function devBranchEnv(cli) {
|
|
136
|
+
if (cli.env.DATABASE_URL || cli.env.ENV_FILE)
|
|
137
|
+
return {};
|
|
138
|
+
const name = devBranchName(cli);
|
|
139
|
+
const branches = await listBranches(cli);
|
|
140
|
+
const production = defaultBranch(branches);
|
|
141
|
+
assertDevBranchName(name, production);
|
|
142
|
+
const branch = branches.find((b) => b.name === name);
|
|
143
|
+
if (!branch) {
|
|
144
|
+
throw new CliError(`no branch ${name} yet; run \`stack dev-setup\` once.`);
|
|
145
|
+
}
|
|
146
|
+
const env = await branchEnv(cli, branch.id, production);
|
|
147
|
+
cli.error(`[neon] ${name}`);
|
|
148
|
+
return env;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* `stack dev-setup` — once per developer: makes your Neon dev branch from `seed` (never from
|
|
152
|
+
* production) if it does not exist yet (`dev/<os user>`, or NEON_DEV_BRANCH) and migrates it. Safe
|
|
153
|
+
* to re-run. Sign-in needs nothing on the branch: Better Auth runs in the app, with a development
|
|
154
|
+
* BETTER_AUTH_SECRET and magic links written to the sink file. There is no `.env` to write:
|
|
155
|
+
* `stack with-neon` reads the branch from the Neon CLI on every command.
|
|
156
|
+
*/
|
|
157
|
+
export async function devSetup(cli) {
|
|
158
|
+
const name = devBranchName(cli);
|
|
159
|
+
const branches = await listBranches(cli);
|
|
160
|
+
const production = defaultBranch(branches);
|
|
161
|
+
assertDevBranchName(name, production);
|
|
162
|
+
let branch = branches.find((b) => b.name === name);
|
|
163
|
+
if (branch) {
|
|
164
|
+
cli.log(`[dev-setup] using ${name} (${branch.id})`);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
if (!branches.some((b) => b.name === SEED)) {
|
|
168
|
+
throw new CliError(`no \`${SEED}\` branch to cut ${name} from; run \`stack neon seed\` first.`);
|
|
169
|
+
}
|
|
170
|
+
branch = (await recreate(cli, name, SEED)) ?? undefined;
|
|
171
|
+
if (!branch)
|
|
172
|
+
throw new CliError(`could not create ${name} from ${SEED}.`);
|
|
173
|
+
cli.log(`[dev-setup] created ${name} (${branch.id}) from ${SEED}`);
|
|
174
|
+
}
|
|
175
|
+
const status = await migrate(cli, {
|
|
176
|
+
...cli.env,
|
|
177
|
+
...(await branchEnv(cli, branch.id, production)),
|
|
178
|
+
});
|
|
179
|
+
if (status === 0)
|
|
180
|
+
cli.log("[dev-setup] done — `stack with-neon pnpm dev` serves the app");
|
|
181
|
+
return status;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Schemas Neon's own services install, which hold functions and settings rather than anyone's rows:
|
|
185
|
+
* `auth` is the pg_session_jwt extension the Data API installs (Neon: "do not install this extension
|
|
186
|
+
* manually"), `pgrst` is the Data API's PostgREST configuration. Left in place so the branch keeps
|
|
187
|
+
* working like any other. Neon Auth's `neon_auth` is *not* here: it holds users.
|
|
188
|
+
*/
|
|
189
|
+
export const NEON_SCHEMAS = ["auth", "pgrst"];
|
|
190
|
+
const isKept = (schema) => schema === "information_schema" || schema.startsWith("pg_") || NEON_SCHEMAS.includes(schema);
|
|
191
|
+
const quoteIdent = (name) => `"${name.replaceAll('"', '""')}"`;
|
|
192
|
+
const quoteLiteral = (value) => `'${value.replaceAll("'", "''")}'`;
|
|
193
|
+
/**
|
|
194
|
+
* Empties a database down to an empty `public`: drops every schema but Postgres's own
|
|
195
|
+
* (`pg_catalog`, `pg_toast*`, `information_schema`, any `pg_*`) and Neon's service schemas, with
|
|
196
|
+
* everything in them — tables, rows, drizzle's journal, Neon Auth's users. Then the migrations
|
|
197
|
+
* run from nothing, as on a new database. A schema-only branch needs this as much as a full copy:
|
|
198
|
+
* it keeps drizzle's journal *table* but not its rows, so migrate would re-run 0000 into tables
|
|
199
|
+
* that already exist. Large objects belong to the database rather than to a schema, so no drop
|
|
200
|
+
* removes them: every one is unlinked too.
|
|
201
|
+
*
|
|
202
|
+
* Neon's schemas are kept by name, so first it checks they hold what Neon puts there: functions and
|
|
203
|
+
* settings, no rows. A table, partitioned table, materialized view, sequence or foreign table in
|
|
204
|
+
* one (an app's own `auth.accounts`, say) would carry production's rows into the seed, so it
|
|
205
|
+
* refuses before dropping anything.
|
|
206
|
+
*/
|
|
207
|
+
export async function emptyDatabase(sql) {
|
|
208
|
+
const held = await sql("SELECT n.nspname, c.relname FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace " +
|
|
209
|
+
`WHERE n.nspname IN (${NEON_SCHEMAS.map(quoteLiteral).join(", ")}) ` +
|
|
210
|
+
"AND c.relkind IN ('r', 'p', 'm', 'S', 'f') ORDER BY 1, 2");
|
|
211
|
+
if (held.length > 0) {
|
|
212
|
+
const names = held.map((r) => `${String(r.nspname)}.${String(r.relname)}`);
|
|
213
|
+
throw new CliError(`${names.join(", ")} ${names.length === 1 ? "is a relation" : "are relations"} in a schema kept as Neon's own (${NEON_SCHEMAS.join(", ")}), ` +
|
|
214
|
+
"so it could carry production's rows into the seed. Refusing; move the app's tables out of those schemas.");
|
|
215
|
+
}
|
|
216
|
+
const rows = await sql("SELECT nspname FROM pg_namespace");
|
|
217
|
+
const dropped = rows.map((r) => String(r.nspname)).filter((name) => !isKept(name));
|
|
218
|
+
if (dropped.length > 0)
|
|
219
|
+
await sql(`DROP SCHEMA ${dropped.map(quoteIdent).join(", ")} CASCADE`);
|
|
220
|
+
await sql("SELECT lo_unlink(oid) FROM pg_largeobject_metadata");
|
|
221
|
+
await sql("CREATE SCHEMA public");
|
|
222
|
+
await sql("GRANT USAGE ON SCHEMA public TO PUBLIC");
|
|
223
|
+
return dropped;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* `stack neon seed` — (re)creates the `seed` branch: exactly the app's migrations plus its seed
|
|
227
|
+
* rows, and none of production's data. Cuts it from production (the project's default branch) as
|
|
228
|
+
* Neon's `--schema-only` branch (a normal branch where that is unsupported), empties it of every
|
|
229
|
+
* non-system schema either way (`emptyDatabase`), migrates it from nothing and runs the app's
|
|
230
|
+
* `db/seed.ts`: `export default async function seed(db)`.
|
|
231
|
+
*
|
|
232
|
+
* The new seed is built as `seed-next` and takes the name `seed` only once it is clean, so a
|
|
233
|
+
* failure part-way never leaves a branch holding production's data under the name every dev and
|
|
234
|
+
* preview branch is cut from; `seed-next` is deleted on any failure. Neon will not delete a branch
|
|
235
|
+
* that has children, so a seed with dev/preview branches on it is refused up front: remove them
|
|
236
|
+
* yourself (this never deletes them).
|
|
237
|
+
*/
|
|
238
|
+
export async function neonSeed(cli) {
|
|
239
|
+
projectId(cli.env);
|
|
240
|
+
const branches = await listBranches(cli);
|
|
241
|
+
const production = defaultBranch(branches);
|
|
242
|
+
const old = branches.find((b) => b.name === SEED);
|
|
243
|
+
const children = old ? branches.filter((b) => b.parent_id === old.id) : [];
|
|
244
|
+
if (children.length > 0) {
|
|
245
|
+
const names = children.map((b) => b.name);
|
|
246
|
+
throw new CliError(`${SEED} has child branches (${names.join(", ")}), and Neon will not delete a branch that has children. ` +
|
|
247
|
+
`Delete them first (${names.map((n) => `\`neon branches delete ${n}\``).join(", ")}), ` +
|
|
248
|
+
`then re-run; re-cut ${PREVIEW} with \`stack neon reset-preview\` and your dev branch with \`stack dev-setup\` afterwards.`);
|
|
249
|
+
}
|
|
250
|
+
const stale = branches.find((b) => b.name === SEED_NEXT);
|
|
251
|
+
if (stale)
|
|
252
|
+
await neonOk(cli, ["branches", "delete", stale.id]);
|
|
253
|
+
let next = await createBranch(cli, SEED_NEXT, production.name, ["--schema-only"]);
|
|
254
|
+
const schemaOnly = next !== undefined;
|
|
255
|
+
next ??= await createBranch(cli, SEED_NEXT, production.name);
|
|
256
|
+
if (!next)
|
|
257
|
+
throw new CliError(`could not create the ${SEED} branch from ${production.name}.`);
|
|
258
|
+
try {
|
|
259
|
+
cli.log(schemaOnly
|
|
260
|
+
? `[seed] built ${SEED_NEXT} (schema only)`
|
|
261
|
+
: `[seed] built ${SEED_NEXT}; --schema-only is unsupported here, so it is a full copy`);
|
|
262
|
+
const env = await branchEnv(cli, next.id, production);
|
|
263
|
+
const direct = env.DATABASE_URL_UNPOOLED;
|
|
264
|
+
// The destructive step below must be pointed at the new branch, never at production.
|
|
265
|
+
if (endpointOf(direct) === endpointOf(env.PRODUCTION_DATABASE_HOST)) {
|
|
266
|
+
throw new CliError(`${SEED_NEXT} resolves to production's endpoint (${new URL(direct).hostname}); refusing to touch it.`);
|
|
267
|
+
}
|
|
268
|
+
const dropped = await emptyDatabase((statement) => cli.sql(direct, statement));
|
|
269
|
+
cli.log(`[seed] emptied ${SEED_NEXT} (dropped ${dropped.join(", ") || "nothing"})`);
|
|
270
|
+
// The app's modules read process.env: the branch's connection wins over its `.env`.
|
|
271
|
+
Object.assign(cli.env, env);
|
|
272
|
+
loadAppEnv(cli);
|
|
273
|
+
const status = await migrate(cli, cli.env);
|
|
274
|
+
if (status !== 0) {
|
|
275
|
+
await deleteOrWarn(cli, next);
|
|
276
|
+
return status;
|
|
277
|
+
}
|
|
278
|
+
const { db } = await importApp(cli, "src/lib/db.ts");
|
|
279
|
+
const seedFn = (await importApp(cli, "db/seed.ts")).default;
|
|
280
|
+
if (!db || typeof seedFn !== "function") {
|
|
281
|
+
throw new CliError("src/lib/db.ts must export `db` and db/seed.ts a default `seed(db)` function.");
|
|
282
|
+
}
|
|
283
|
+
await seedFn(db);
|
|
284
|
+
}
|
|
285
|
+
catch (error) {
|
|
286
|
+
await deleteOrWarn(cli, next);
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
// Clean: now it replaces the old seed. From here `seed-next` holds only the fixtures, so a
|
|
290
|
+
// failure leaves it in place and says how to finish.
|
|
291
|
+
const rename = `\`neon branches rename ${SEED_NEXT} ${SEED}\``;
|
|
292
|
+
if (old) {
|
|
293
|
+
const deleted = await neon(cli, ["branches", "delete", old.id]);
|
|
294
|
+
if (deleted.code !== 0) {
|
|
295
|
+
throw new CliError(`could not delete the old ${SEED} (exit ${deleted.code}); it is unchanged. ${SEED_NEXT} is built and clean: ` +
|
|
296
|
+
`finish by hand with \`neon branches delete ${SEED}\`, then ${rename}.`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const renamed = await neon(cli, ["branches", "rename", next.id, SEED]);
|
|
300
|
+
if (renamed.code !== 0) {
|
|
301
|
+
throw new CliError(`could not rename ${SEED_NEXT} to ${SEED} (exit ${renamed.code})${old ? `; the old ${SEED} is already deleted` : ""}. ` +
|
|
302
|
+
`${SEED_NEXT} is built and clean: finish by hand with ${rename}.`);
|
|
303
|
+
}
|
|
304
|
+
cli.log(`[seed] ${SEED} is ready`);
|
|
305
|
+
return 0;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Deletes a half-built `seed-next`; the failure being reported comes first, so this only warns. But
|
|
309
|
+
* loudly: until it is emptied, a full-copy `seed-next` holds production's data.
|
|
310
|
+
*/
|
|
311
|
+
async function deleteOrWarn(cli, branch) {
|
|
312
|
+
const code = await neon(cli, ["branches", "delete", branch.id]).then((result) => result.code, () => -1);
|
|
313
|
+
if (code !== 0) {
|
|
314
|
+
cli.error(`[seed] DELETE ${branch.name} BY HAND — it may hold production data. ` +
|
|
315
|
+
`Cleanup failed (exit ${code}); run \`neon branches delete ${branch.name}\` now.`);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* `stack neon reset-preview` — (re)creates the one shared `preview` branch from `seed` and prints
|
|
320
|
+
* its pooled URL. A build cannot change the DATABASE_URL a preview deployment uses at runtime, so
|
|
321
|
+
* previews share this branch: set the URL printed here once as a Preview-scoped Vercel variable.
|
|
322
|
+
* `stack vercel-build` migrates it on each preview build; run this again to start it clean.
|
|
323
|
+
*/
|
|
324
|
+
export async function resetPreview(cli) {
|
|
325
|
+
if (!(await findBranch(cli, SEED))) {
|
|
326
|
+
throw new CliError(`no \`${SEED}\` branch to cut ${PREVIEW} from; run \`stack neon seed\` first.`);
|
|
327
|
+
}
|
|
328
|
+
const preview = await recreate(cli, PREVIEW, SEED);
|
|
329
|
+
if (!preview)
|
|
330
|
+
throw new CliError(`could not create ${PREVIEW} from ${SEED}.`);
|
|
331
|
+
cli.error(`[preview] created ${PREVIEW} from ${SEED}; set this as Preview DATABASE_URL on Vercel:`);
|
|
332
|
+
cli.log(await neonOk(cli, ["connection-string", preview.id, "--pooled"]));
|
|
333
|
+
return 0;
|
|
334
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Brings the stack-owned blocks in AGENTS.md, CLAUDE.md, .gitignore and .env.example up to date and
|
|
3
|
+
* returns the files that were (or, with `check`, would be) changed. Every file is checked before
|
|
4
|
+
* any is written, so a malformed one leaves all of them untouched.
|
|
5
|
+
*/
|
|
6
|
+
export declare function syncBlocks(cwd: string, opts?: {
|
|
7
|
+
check?: boolean;
|
|
8
|
+
}): Promise<{
|
|
9
|
+
changed: string[];
|
|
10
|
+
}>;
|
|
11
|
+
/** `stack sync-blocks [--check]`: exits 1 when `--check` finds a stale file. */
|
|
12
|
+
export declare function syncBlocksCommand(cli: {
|
|
13
|
+
cwd: string;
|
|
14
|
+
log(m: string): void;
|
|
15
|
+
error(m: string): void;
|
|
16
|
+
}, args: string[]): Promise<number>;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { CliError } from "./context.js";
|
|
5
|
+
const MARKDOWN = { begin: "<!-- BEGIN:stack -->", end: "<!-- END:stack -->" };
|
|
6
|
+
const HASH = { begin: "# BEGIN:stack", end: "# END:stack" };
|
|
7
|
+
/** Each target file, the block file it takes its contents from, and its marker style. */
|
|
8
|
+
const TARGETS = [
|
|
9
|
+
{ file: "AGENTS.md", block: "AGENTS.md", style: MARKDOWN },
|
|
10
|
+
{ file: "CLAUDE.md", block: "CLAUDE.md", style: MARKDOWN },
|
|
11
|
+
{ file: ".gitignore", block: "gitignore", style: HASH },
|
|
12
|
+
{ file: ".env.example", block: "env.example", style: HASH },
|
|
13
|
+
];
|
|
14
|
+
/** `blocks/` sits beside `src/` and `dist/`, so one relative path serves both and an installed package. */
|
|
15
|
+
const BLOCKS_DIR = fileURLToPath(new URL("../../blocks/", import.meta.url));
|
|
16
|
+
/** The lines of `source` with their offsets; `end` includes the line break, `text` does not. */
|
|
17
|
+
function linesOf(source) {
|
|
18
|
+
const lines = [];
|
|
19
|
+
let start = 0;
|
|
20
|
+
while (start < source.length) {
|
|
21
|
+
const nl = source.indexOf("\n", start);
|
|
22
|
+
const end = nl === -1 ? source.length : nl + 1;
|
|
23
|
+
lines.push({ start, end, text: source.slice(start, nl === -1 ? end : nl).replace(/\r$/, "") });
|
|
24
|
+
start = end;
|
|
25
|
+
}
|
|
26
|
+
return lines;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Replaces the text between the markers, or appends a marked block when there are none. Anything
|
|
30
|
+
* ambiguous (a marker without its partner, reversed or repeated markers) throws: guessing which
|
|
31
|
+
* text the stack owns could delete the app's own.
|
|
32
|
+
*/
|
|
33
|
+
function apply(file, source, body, style) {
|
|
34
|
+
const eol = source.includes("\r\n") ? "\r\n" : "\n";
|
|
35
|
+
const block = body.map((line) => line + eol).join("");
|
|
36
|
+
const lines = linesOf(source);
|
|
37
|
+
const near = lines.find((l) => (l.text.trim() === style.begin && l.text !== style.begin) ||
|
|
38
|
+
(l.text.trim() === style.end && l.text !== style.end));
|
|
39
|
+
if (near) {
|
|
40
|
+
throw new CliError(`${file}: marker "${near.text.trim()}" has leading or trailing whitespace. Fix the markers by hand; nothing was changed.`);
|
|
41
|
+
}
|
|
42
|
+
const begins = lines.filter((l) => l.text === style.begin);
|
|
43
|
+
const ends = lines.filter((l) => l.text === style.end);
|
|
44
|
+
if (begins.length === 0 && ends.length === 0) {
|
|
45
|
+
const lead = source === "" ? "" : source.endsWith("\n") ? eol : eol + eol;
|
|
46
|
+
return `${source}${lead}${style.begin}${eol}${block}${style.end}${eol}`;
|
|
47
|
+
}
|
|
48
|
+
const [begin] = begins;
|
|
49
|
+
const [end] = ends;
|
|
50
|
+
if (begins.length !== 1 || ends.length !== 1 || !begin || !end) {
|
|
51
|
+
throw new CliError(`${file}: expected exactly one "${style.begin}" and one "${style.end}", found ${begins.length} and ${ends.length}. Fix the markers by hand; nothing was changed.`);
|
|
52
|
+
}
|
|
53
|
+
if (end.start < begin.start) {
|
|
54
|
+
throw new CliError(`${file}: "${style.end}" comes before "${style.begin}". Fix the markers by hand; nothing was changed.`);
|
|
55
|
+
}
|
|
56
|
+
return source.slice(0, begin.end) + block + source.slice(end.start);
|
|
57
|
+
}
|
|
58
|
+
/** The block's inner lines, without trailing blank lines. */
|
|
59
|
+
function readBody(name) {
|
|
60
|
+
return readFileSync(join(BLOCKS_DIR, name), "utf8").replace(/\s+$/, "").split(/\r?\n/);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Brings the stack-owned blocks in AGENTS.md, CLAUDE.md, .gitignore and .env.example up to date and
|
|
64
|
+
* returns the files that were (or, with `check`, would be) changed. Every file is checked before
|
|
65
|
+
* any is written, so a malformed one leaves all of them untouched.
|
|
66
|
+
*/
|
|
67
|
+
export async function syncBlocks(cwd, opts = {}) {
|
|
68
|
+
const writes = [];
|
|
69
|
+
for (const { file, block, style } of TARGETS) {
|
|
70
|
+
const path = join(cwd, file);
|
|
71
|
+
const before = existsSync(path) ? readFileSync(path, "utf8") : "";
|
|
72
|
+
const after = apply(file, before, readBody(block), style);
|
|
73
|
+
if (after !== before)
|
|
74
|
+
writes.push({ path, file, text: after });
|
|
75
|
+
}
|
|
76
|
+
if (!opts.check)
|
|
77
|
+
for (const w of writes)
|
|
78
|
+
writeFileSync(w.path, w.text);
|
|
79
|
+
return { changed: writes.map((w) => w.file) };
|
|
80
|
+
}
|
|
81
|
+
/** `stack sync-blocks [--check]`: exits 1 when `--check` finds a stale file. */
|
|
82
|
+
export async function syncBlocksCommand(cli, args) {
|
|
83
|
+
const unknown = args.filter((a) => a !== "--check");
|
|
84
|
+
if (unknown.length > 0)
|
|
85
|
+
throw new CliError(`sync-blocks: unknown argument ${unknown[0]}`);
|
|
86
|
+
const check = args.includes("--check");
|
|
87
|
+
const { changed } = await syncBlocks(cli.cwd, { check });
|
|
88
|
+
if (check && changed.length > 0) {
|
|
89
|
+
cli.error(`stale stack blocks: ${changed.join(", ")}. Run \`stack sync-blocks\`.`);
|
|
90
|
+
return 1;
|
|
91
|
+
}
|
|
92
|
+
for (const file of changed)
|
|
93
|
+
cli.log(`updated ${file}`);
|
|
94
|
+
if (changed.length === 0)
|
|
95
|
+
cli.log("stack blocks are up to date");
|
|
96
|
+
return 0;
|
|
97
|
+
}
|