Skip to content

Commit df6be20

Browse files
authored
feat(cli)!: breaking change, default deploy stage is live_$USER (#1421)
1 parent e0d7226 commit df6be20

13 files changed

Lines changed: 144 additions & 67 deletions

File tree

‎packages/alchemy/src/Cli/commands/dev.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import {
1414
configPath,
1515
envFile,
1616
force,
17+
devStage,
1718
optionalConfig,
1819
profile,
1920
resolveConfig,
20-
stage,
2121
} from "./flags.ts";
2222
import { suppressInterruptMessages } from "./errors.ts";
2323

@@ -40,7 +40,7 @@ export const devCommand = Command.make(
4040
config: optionalConfig,
4141
configPath,
4242
envFile,
43-
stage,
43+
stage: devStage,
4444
profile,
4545
},
4646
Effect.fn(

‎packages/alchemy/src/Cli/commands/flags.ts‎

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,43 @@ export const STAGE = Config.string("STAGE").pipe(
1919
Effect.map(Option.getOrUndefined),
2020
);
2121

22-
export const stage = Flag.string("stage").pipe(
23-
Flag.withSchema(
24-
Schema.String.check(Schema.isPattern(/^[a-z0-9]+([-_a-z0-9]+)*$/gi)),
25-
),
26-
Flag.withDescription("Stage to deploy to, defaults to dev_${USER}"),
27-
Flag.optional,
28-
Flag.map(Option.getOrUndefined),
29-
Flag.mapEffect(
30-
Effect.fn(function* (stage) {
31-
if (stage) return stage;
32-
return yield* STAGE.pipe(
33-
Effect.catch(() =>
34-
Effect.fail(new CliError.MissingOption({ option: "stage" })),
35-
),
36-
Effect.flatMap((configured) =>
37-
configured === undefined
38-
? USER.pipe(
39-
Effect.map((user) => `dev_${user}`),
40-
Effect.catch(() => Effect.succeed("unknown")),
41-
)
42-
: Effect.succeed(configured),
43-
),
44-
);
45-
}),
46-
),
47-
);
22+
const makeStageFlag = (kind: "live" | "dev") =>
23+
Flag.string("stage").pipe(
24+
Flag.withSchema(
25+
Schema.String.check(Schema.isPattern(/^[a-z0-9]+([-_a-z0-9]+)*$/gi)),
26+
),
27+
Flag.withDescription(
28+
kind === "live"
29+
? "Stage to deploy to, defaults to live_${USER}"
30+
: "Stage to use for dev, defaults to dev_${USER}",
31+
),
32+
Flag.optional,
33+
Flag.map(Option.getOrUndefined),
34+
Flag.mapEffect(
35+
Effect.fn(function* (stage) {
36+
if (stage) return stage;
37+
return yield* STAGE.pipe(
38+
Effect.catch(() =>
39+
Effect.fail(new CliError.MissingOption({ option: "stage" })),
40+
),
41+
Effect.flatMap((configured) =>
42+
configured === undefined
43+
? USER.pipe(
44+
Effect.map((user) => `${kind}_${user}`),
45+
Effect.catch(() => Effect.succeed("unknown")),
46+
)
47+
: Effect.succeed(configured),
48+
),
49+
);
50+
}),
51+
),
52+
);
53+
54+
/** `--stage` for deploy / destroy / plan / logs / drift. Default: `live_$USER`. */
55+
export const stage = makeStageFlag("live");
56+
57+
/** `--stage` for `alchemy dev`. Default: `dev_$USER`. */
58+
export const devStage = makeStageFlag("dev");
4859

4960
export const envFile = Flag.file("env-file").pipe(
5061
Flag.optional,

‎packages/alchemy/test/Cli/StageEnvironment.test.ts‎

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import { stage } from "@/Cli/commands/flags.ts";
1+
import { devStage, stage } from "@/Cli/commands/flags.ts";
22
import { PlatformServices } from "@/Util/PlatformServices.ts";
33
import { describe, expect, test } from "alchemy-test";
44
import * as ConfigProvider from "effect/ConfigProvider";
55
import * as Effect from "effect/Effect";
66
import * as Layer from "effect/Layer";
77

8-
const TestEnv = Layer.mergeAll(
9-
PlatformServices,
10-
ConfigProvider.layer(
11-
ConfigProvider.fromEnv({ env: { STAGE: "production", USER: "name" } }),
12-
),
13-
);
8+
const envLayer = (env: Record<string, string>) =>
9+
Layer.mergeAll(
10+
PlatformServices,
11+
ConfigProvider.layer(ConfigProvider.fromEnv({ env })),
12+
);
13+
14+
const TestEnv = envLayer({ STAGE: "production", USER: "name" });
1415

1516
describe("STAGE environment variable", () => {
1617
test.effect("takes precedence over the user default", () =>
@@ -32,3 +33,42 @@ describe("STAGE environment variable", () => {
3233
}).pipe(Effect.provide(TestEnv)),
3334
);
3435
});
36+
37+
describe("default stages", () => {
38+
test.effect("deploy/destroy default to live_${USER}", () =>
39+
Effect.gen(function* () {
40+
const [, selected] = yield* stage.parse({ arguments: [], flags: {} });
41+
expect(selected).toBe("live_sam");
42+
}).pipe(Effect.provide(envLayer({ USER: "sam" }))),
43+
);
44+
45+
test.effect("alchemy dev defaults to dev_${USER}", () =>
46+
Effect.gen(function* () {
47+
const [, selected] = yield* devStage.parse({
48+
arguments: [],
49+
flags: {},
50+
});
51+
expect(selected).toBe("dev_sam");
52+
}).pipe(Effect.provide(envLayer({ USER: "sam" }))),
53+
);
54+
55+
test.effect("alchemy dev honors --stage", () =>
56+
Effect.gen(function* () {
57+
const [, selected] = yield* devStage.parse({
58+
arguments: [],
59+
flags: { stage: ["prod"] },
60+
});
61+
expect(selected).toBe("prod");
62+
}).pipe(Effect.provide(envLayer({ USER: "sam" }))),
63+
);
64+
65+
test.effect("alchemy dev honors $STAGE", () =>
66+
Effect.gen(function* () {
67+
const [, selected] = yield* devStage.parse({
68+
arguments: [],
69+
flags: {},
70+
});
71+
expect(selected).toBe("production");
72+
}).pipe(Effect.provide(TestEnv)),
73+
);
74+
});

‎website/src/content/docs/cli/deploy.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ When the plan contains no changes, the approval prompt is skipped automatically.
4848
| Option | Description |
4949
| ------------------- | -------------------------------------------------------------------------------------------------------------------------- |
5050
| `--config, -c <file>` | Stack file to deploy (defaults to `alchemy.run.ts`) |
51-
| `--stage <name>` | Stage to deploy to (defaults to `dev_$USER`) |
51+
| `--stage <name>` | Stage to deploy to (defaults to `live_$USER`) |
5252
| `--yes` | Skip the approval prompt |
5353
| `--dry-run` | Show the plan without applying (same as `alchemy plan`) |
5454
| `--detailed` | Show declared resource properties as YAML |

‎website/src/content/docs/cli/destroy.mdx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Proceed?
2828
| Option | Description |
2929
| ------------------- | ----------------------------------------------------------------- |
3030
| `--config, -c <file>` | Stack file to destroy (defaults to `alchemy.run.ts`) |
31-
| `--stage <name>` | Stage to destroy (defaults to `dev_$USER`) |
31+
| `--stage <name>` | Stage to destroy (defaults to `live_$USER`) |
3232
| `--yes` | Skip the approval prompt |
3333
| `--dry-run` | Show what would be deleted without actually deleting |
3434
| `--profile <name>` | Auth profile to use (defaults to `$ALCHEMY_PROFILE` or `default`) |
@@ -40,7 +40,10 @@ Proceed?
4040
# Destroy a PR preview environment
4141
alchemy destroy --stage pr-42 --yes
4242

43-
# Destroy your personal dev stage
43+
# Destroy your personal cloud sandbox
44+
alchemy destroy --stage live_sam
45+
46+
# Destroy the local-dev stage created by alchemy dev
4447
alchemy destroy --stage dev_sam
4548
```
4649

‎website/src/content/docs/cli/dev.mdx‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,22 @@ A failed apply keeps dev alive so healthy resources keep serving — fix the err
2727
| `--env-file <path>` | Load environment variables from a file |
2828
| `--config, -c <file>` | Stack file to run (defaults to `alchemy.run.ts`) |
2929

30+
`alchemy deploy` defaults to `live_$USER`. Using a different default
31+
here means a bare `alchemy dev` will not replace a stage you deployed.
32+
Pass `--stage` (or `$STAGE`) to run local emulation against a specific
33+
stage — including one you also deploy.
34+
35+
Tear the default local-dev stage down with
36+
`alchemy destroy --stage dev_$USER`.
37+
3038
## Examples
3139

3240
```sh
33-
# Start dev mode
41+
# Start dev mode (stage dev_$USER)
3442
alchemy dev
3543

36-
# Use a custom stage
37-
alchemy dev --stage dev
44+
# Emulate the prod program locally (replaces that stage's local/live mode)
45+
alchemy dev --stage prod
3846
```
3947

4048
## Where next

‎website/src/content/docs/cli/drift.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ before applying the repair.
2323
| Option | Description |
2424
| ------------------- | ------------------------------------------------------------------ |
2525
| `--repair` | Repair detected drift after showing and confirming the plan |
26-
| `--stage <name>` | Stage to check (defaults to `dev_$USER`) |
26+
| `--stage <name>` | Stage to check (defaults to `live_$USER`) |
2727
| `--yes` | Skip the repair confirmation prompt |
2828
| `--profile <name>` | Auth profile to use (defaults to `$ALCHEMY_PROFILE` or `default`) |
2929
| `--env-file <path>` | Load environment variables from a file |

‎website/src/content/docs/cli/index.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Each command has its own page: [deploy](/cli/deploy), [plan](/cli/plan), [destro
3939

4040
| Option | Description |
4141
| ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
42-
| `--stage <name>` | Stage to target. Falls back to the `stage` env config, then `dev_$USER`. Must match `[a-z0-9]+([-_a-z0-9]+)*` (case-insensitive). |
42+
| `--stage <name>` | Stage to target. Falls back to `$STAGE`, then `live_$USER` (`dev_$USER` for `alchemy dev`). Must match `[a-z0-9]+([-_a-z0-9]+)*`. |
4343
| `--profile <name>` | Auth profile from `~/.alchemy/profiles.json`. Overrides `$ALCHEMY_PROFILE`; defaults to `default`. |
4444
| `--env-file <path>` | File to load environment variables from. Defaults to `.env`. |
4545
| `--yes` | Yes to all prompts. |

‎website/src/content/docs/cli/inspecting-state.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ This is the persisted props/attrs the planner diffs against — see
6666
[state](/cli/state) for the output encoding. Compare it with what
6767
`alchemy plan` says it wants to change. Three usual causes:
6868

69-
**Wrong stage.** `--stage` defaults to `dev_$USER` (e.g. `dev_sam`),
69+
**Wrong stage.** `--stage` defaults to `live_$USER` (e.g. `live_sam`),
7070
so a plan that "wants to create everything" often just means you're
7171
looking at a stage you never deployed. Run
7272
`state list myapp` and confirm which stage actually has state.

‎website/src/content/docs/cli/logs.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Resources must be deployed and their provider must implement log fetching; if no
2121

2222
| Option | Description |
2323
| ------------------- | ---------------------------------------------------------------------------------------------- |
24-
| `--stage <name>` | Stage to fetch logs from (defaults to `dev_$USER`) |
24+
| `--stage <name>` | Stage to fetch logs from (defaults to `live_$USER`) |
2525
| `--resource, -r <ids>` | Comma-separated logical resource IDs to include, such as `Worker,Api` |
2626
| `--limit <n>` | Number of log entries to fetch (default: 100) |
2727
| `--since <time>` | Fetch logs since this time — a duration (`30m`, `1h`, `2d`; units `s`/`m`/`h`/`d`) or ISO date |

0 commit comments

Comments
 (0)