alchemy 0.2.2 → 0.2.3
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/lib/alchemy.d.ts +1 -0
- package/lib/alchemy.js +84 -8
- package/lib/apply.d.ts +1 -1
- package/lib/apply.js +13 -10
- package/lib/aws/bucket.d.ts +1 -1
- package/lib/aws/function.d.ts +1 -1
- package/lib/aws/oidc/oidc-provider.d.ts +1 -1
- package/lib/aws/policy-attachment.d.ts +1 -1
- package/lib/aws/policy.d.ts +1 -1
- package/lib/aws/queue.d.ts +1 -1
- package/lib/aws/role.d.ts +1 -1
- package/lib/aws/ses.d.ts +1 -1
- package/lib/aws/table.d.ts +1 -1
- package/lib/cloudflare/bucket.d.ts +1 -1
- package/lib/cloudflare/kv-namespace.d.ts +1 -1
- package/lib/cloudflare/static-site.d.ts +1 -1
- package/lib/cloudflare/worker.d.ts +1 -1
- package/lib/cloudflare/wrangler.json.d.ts +1 -1
- package/lib/cloudflare/zone.d.ts +1 -1
- package/lib/context.d.ts +9 -4
- package/lib/context.js +4 -2
- package/lib/destroy.js +8 -7
- package/lib/docs/document.d.ts +123 -0
- package/lib/docs/document.js +67 -0
- package/lib/docs/index.d.ts +1 -0
- package/lib/docs/index.js +1 -0
- package/lib/esbuild/bundle.d.ts +1 -1
- package/lib/fs/file.d.ts +30 -3
- package/lib/fs/file.js +63 -3
- package/lib/fs/folder.d.ts +3 -3
- package/lib/fs/folder.js +2 -1
- package/lib/github/secret.d.ts +1 -1
- package/lib/resource.d.ts +2 -2
- package/lib/resource.js +4 -0
- package/lib/scope.d.ts +1 -1
- package/lib/scope.js +6 -6
- package/lib/shadcn/component.d.ts +1 -1
- package/lib/state.d.ts +1 -1
- package/lib/state.js +8 -1
- package/lib/stripe/price.d.ts +1 -1
- package/lib/stripe/product.d.ts +1 -1
- package/lib/stripe/webhook.d.ts +1 -1
- package/lib/util/serde.d.ts +3 -1
- package/lib/util/serde.js +6 -4
- package/lib/vite/vite.d.ts +1 -1
- package/lib/vitepress/dependencies.d.ts +16 -0
- package/lib/vitepress/dependencies.js +35 -0
- package/lib/vitepress/home-page.d.ts +133 -0
- package/lib/vitepress/home-page.js +11 -0
- package/lib/vitepress/index.d.ts +2 -0
- package/lib/vitepress/index.js +2 -0
- package/lib/vitepress/vitepress.d.ts +60 -0
- package/lib/vitepress/vitepress.js +260 -0
- package/package.json +16 -9
- package/src/alchemy.ts +105 -13
- package/src/apply.ts +25 -13
- package/src/context.ts +30 -10
- package/src/destroy.ts +10 -7
- package/src/docs/document.ts +225 -0
- package/src/docs/index.ts +1 -0
- package/src/fs/file.ts +114 -6
- package/src/fs/folder.ts +2 -1
- package/src/resource.ts +7 -2
- package/src/scope.ts +9 -9
- package/src/state.ts +9 -2
- package/src/util/serde.ts +13 -4
- package/src/vitepress/dependencies.ts +68 -0
- package/src/vitepress/home-page.ts +166 -0
- package/src/vitepress/index.md +75 -0
- package/src/vitepress/index.ts +2 -0
- package/src/vitepress/vitepress.ts +361 -0
- package/lib/project/vite.d.ts +0 -90
- package/lib/project/vite.js +0 -228
- package/src/project/vite.ts +0 -406
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { exec } from "child_process";
|
|
2
|
+
import fs from "fs/promises";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { promisify } from "util";
|
|
5
|
+
import yaml from "yaml";
|
|
6
|
+
import { Folder, JsonFile, TextFile, TypeScriptFile } from "../fs";
|
|
7
|
+
import { Resource } from "../resource";
|
|
8
|
+
import { InstallDependencies, fixedDependencies } from "./dependencies";
|
|
9
|
+
const execAsync = promisify(exec);
|
|
10
|
+
export const VitePressProject = Resource("project::VitePressProject", {
|
|
11
|
+
alwaysUpdate: true,
|
|
12
|
+
}, async function (id, props) {
|
|
13
|
+
if (this.phase === "delete") {
|
|
14
|
+
try {
|
|
15
|
+
if (await fs.exists(props.name)) {
|
|
16
|
+
// Delete the entire project directory
|
|
17
|
+
await execAsync(`rm -rf ${props.name}`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
console.error(`Error deleting VitePress project ${id}:`, error);
|
|
22
|
+
}
|
|
23
|
+
return this.destroy();
|
|
24
|
+
}
|
|
25
|
+
const cwd = (await Folder(props.name)).path;
|
|
26
|
+
// Initialize package.json
|
|
27
|
+
await JsonFile(path.join(cwd, "package.json"), {
|
|
28
|
+
name: props.name,
|
|
29
|
+
scripts: {
|
|
30
|
+
"docs:gen": "bun alchemy.run.ts",
|
|
31
|
+
"docs:dev": "vitepress dev",
|
|
32
|
+
"docs:build": "vitepress build",
|
|
33
|
+
"docs:preview": "vitepress preview",
|
|
34
|
+
},
|
|
35
|
+
dependencies: fixedDependencies(props.dependencies || {}),
|
|
36
|
+
devDependencies: fixedDependencies(props.devDependencies || {}),
|
|
37
|
+
});
|
|
38
|
+
await InstallDependencies("dependencies", {
|
|
39
|
+
cwd,
|
|
40
|
+
dependencies: props.dependencies,
|
|
41
|
+
devDependencies: {
|
|
42
|
+
vue: "latest",
|
|
43
|
+
vitepress: "latest",
|
|
44
|
+
"@shikijs/vitepress-twoslash": "latest",
|
|
45
|
+
"markdown-it-footnote": "latest",
|
|
46
|
+
...props.devDependencies,
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
// Create .vitepress directory and config
|
|
50
|
+
await Folder(path.join(cwd, ".vitepress"));
|
|
51
|
+
await Folder(path.join(cwd, ".vitepress", "theme"));
|
|
52
|
+
await Promise.all([
|
|
53
|
+
TextFile(path.join(cwd, ".gitignore"), `.vitepress/cache\n`),
|
|
54
|
+
JsonFile(path.join(cwd, "tsconfig.json"), {
|
|
55
|
+
extends: props.tsconfig?.extends,
|
|
56
|
+
references: props.tsconfig?.references?.map((path) => ({ path })),
|
|
57
|
+
compilerOptions: props.tsconfig?.compilerOptions,
|
|
58
|
+
}),
|
|
59
|
+
TextFile(path.join(cwd, "index.md"), `---\n${yaml.stringify(props.home)}---\n`),
|
|
60
|
+
TypeScriptFile(path.join(cwd, "alchemy.run.ts"), `import alchemy from "alchemy";
|
|
61
|
+
import { Folder } from "alchemy/fs";
|
|
62
|
+
import { Document } from "alchemy/docs";
|
|
63
|
+
import path from "path";
|
|
64
|
+
|
|
65
|
+
await using _ = alchemy("alchemy.run", {
|
|
66
|
+
stage: "prod",
|
|
67
|
+
phase: process.argv.includes("--destroy") ? "destroy" : "up",
|
|
68
|
+
password: process.env.SECRET_PASSPHRASE,
|
|
69
|
+
quiet: !process.argv.includes("--verbose"),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const docs = await Folder(path.join("alchemy.run", "docs"));
|
|
73
|
+
|
|
74
|
+
await Document("home.md", {
|
|
75
|
+
path: path.join(docs.path, "home.md"),
|
|
76
|
+
prompt: await alchemy\`Generate a landing page.\`,
|
|
77
|
+
});
|
|
78
|
+
`),
|
|
79
|
+
TypeScriptFile(path.join(cwd, ".vitepress", "theme", "index.ts"), `import TwoslashFloatingVue from "@shikijs/vitepress-twoslash/client";
|
|
80
|
+
import "@shikijs/vitepress-twoslash/style.css";
|
|
81
|
+
import type { Theme as ThemeConfig } from "vitepress";
|
|
82
|
+
import Theme from "vitepress/theme-without-fonts";
|
|
83
|
+
import "./style.css";
|
|
84
|
+
|
|
85
|
+
export default {
|
|
86
|
+
extends: Theme,
|
|
87
|
+
enhanceApp(ctx) {
|
|
88
|
+
ctx.app.use(TwoslashFloatingVue);
|
|
89
|
+
},
|
|
90
|
+
} satisfies ThemeConfig;
|
|
91
|
+
`),
|
|
92
|
+
TextFile(path.join(cwd, ".vitepress", "theme", "style.css"), `/**
|
|
93
|
+
* Customize default theme styling by overriding CSS variables:
|
|
94
|
+
* https://github.com/vuejs/vitepress/blob/main/src/client/theme-default/styles/vars.css
|
|
95
|
+
*/
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Colors
|
|
99
|
+
*
|
|
100
|
+
* Each colors have exact same color scale system with 3 levels of solid
|
|
101
|
+
* colors with different brightness, and 1 soft color.
|
|
102
|
+
*
|
|
103
|
+
* - \`XXX-1\`: The most solid color used mainly for colored text. It must
|
|
104
|
+
* satisfy the contrast ratio against when used on top of \`XXX-soft\`.
|
|
105
|
+
*
|
|
106
|
+
* - \`XXX-2\`: The color used mainly for hover state of the button.
|
|
107
|
+
*
|
|
108
|
+
* - \`XXX-3\`: The color for solid background, such as bg color of the button.
|
|
109
|
+
* It must satisfy the contrast ratio with pure white (#ffffff) text on
|
|
110
|
+
* top of it.
|
|
111
|
+
*
|
|
112
|
+
* - \`XXX-soft\`: The color used for subtle background such as custom container
|
|
113
|
+
* or badges. It must satisfy the contrast ratio when putting \`XXX-1\` colors
|
|
114
|
+
* on top of it.
|
|
115
|
+
*
|
|
116
|
+
* The soft color must be semi transparent alpha channel. This is crucial
|
|
117
|
+
* because it allows adding multiple "soft" colors on top of each other
|
|
118
|
+
* to create a accent, such as when having inline code block inside
|
|
119
|
+
* custom containers.
|
|
120
|
+
*
|
|
121
|
+
* - \`default\`: The color used purely for subtle indication without any
|
|
122
|
+
* special meanings attached to it such as bg color for menu hover state.
|
|
123
|
+
*
|
|
124
|
+
* - \`brand\`: Used for primary brand colors, such as link text, button with
|
|
125
|
+
* brand theme, etc.
|
|
126
|
+
*
|
|
127
|
+
* - \`tip\`: Used to indicate useful information. The default theme uses the
|
|
128
|
+
* brand color for this by default.
|
|
129
|
+
*
|
|
130
|
+
* - \`warning\`: Used to indicate warning to the users. Used in custom
|
|
131
|
+
* container, badges, etc.
|
|
132
|
+
*
|
|
133
|
+
* - \`danger\`: Used to show error, or dangerous message to the users. Used
|
|
134
|
+
* in custom container, badges, etc.
|
|
135
|
+
* -------------------------------------------------------------------------- */
|
|
136
|
+
|
|
137
|
+
:root {
|
|
138
|
+
--vp-c-default-1: var(--vp-c-gray-1);
|
|
139
|
+
--vp-c-default-2: var(--vp-c-gray-2);
|
|
140
|
+
--vp-c-default-3: var(--vp-c-gray-3);
|
|
141
|
+
--vp-c-default-soft: var(--vp-c-gray-soft);
|
|
142
|
+
|
|
143
|
+
--vp-c-brand-1: var(--vp-c-indigo-1);
|
|
144
|
+
--vp-c-brand-2: var(--vp-c-indigo-2);
|
|
145
|
+
--vp-c-brand-3: var(--vp-c-indigo-3);
|
|
146
|
+
--vp-c-brand-soft: var(--vp-c-indigo-soft);
|
|
147
|
+
|
|
148
|
+
--vp-c-tip-1: var(--vp-c-brand-1);
|
|
149
|
+
--vp-c-tip-2: var(--vp-c-brand-2);
|
|
150
|
+
--vp-c-tip-3: var(--vp-c-brand-3);
|
|
151
|
+
--vp-c-tip-soft: var(--vp-c-brand-soft);
|
|
152
|
+
|
|
153
|
+
--vp-c-warning-1: var(--vp-c-yellow-1);
|
|
154
|
+
--vp-c-warning-2: var(--vp-c-yellow-2);
|
|
155
|
+
--vp-c-warning-3: var(--vp-c-yellow-3);
|
|
156
|
+
--vp-c-warning-soft: var(--vp-c-yellow-soft);
|
|
157
|
+
|
|
158
|
+
--vp-c-danger-1: var(--vp-c-red-1);
|
|
159
|
+
--vp-c-danger-2: var(--vp-c-red-2);
|
|
160
|
+
--vp-c-danger-3: var(--vp-c-red-3);
|
|
161
|
+
--vp-c-danger-soft: var(--vp-c-red-soft);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Component: Button
|
|
166
|
+
* -------------------------------------------------------------------------- */
|
|
167
|
+
|
|
168
|
+
:root {
|
|
169
|
+
--vp-button-brand-border: transparent;
|
|
170
|
+
--vp-button-brand-text: var(--vp-c-white);
|
|
171
|
+
--vp-button-brand-bg: var(--vp-c-brand-3);
|
|
172
|
+
--vp-button-brand-hover-border: transparent;
|
|
173
|
+
--vp-button-brand-hover-text: var(--vp-c-white);
|
|
174
|
+
--vp-button-brand-hover-bg: var(--vp-c-brand-2);
|
|
175
|
+
--vp-button-brand-active-border: transparent;
|
|
176
|
+
--vp-button-brand-active-text: var(--vp-c-white);
|
|
177
|
+
--vp-button-brand-active-bg: var(--vp-c-brand-1);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Component: Home
|
|
182
|
+
* -------------------------------------------------------------------------- */
|
|
183
|
+
|
|
184
|
+
:root {
|
|
185
|
+
--vp-home-hero-name-color: transparent;
|
|
186
|
+
--vp-home-hero-name-background: -webkit-linear-gradient(
|
|
187
|
+
120deg,
|
|
188
|
+
#bd34fe 30%,
|
|
189
|
+
#41d1ff
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
--vp-home-hero-image-background-image: linear-gradient(
|
|
193
|
+
-45deg,
|
|
194
|
+
#bd34fe 50%,
|
|
195
|
+
#47caff 50%
|
|
196
|
+
);
|
|
197
|
+
--vp-home-hero-image-filter: blur(44px);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
@media (min-width: 640px) {
|
|
201
|
+
:root {
|
|
202
|
+
--vp-home-hero-image-filter: blur(56px);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
@media (min-width: 960px) {
|
|
207
|
+
:root {
|
|
208
|
+
--vp-home-hero-image-filter: blur(68px);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Component: Custom Block
|
|
214
|
+
* -------------------------------------------------------------------------- */
|
|
215
|
+
|
|
216
|
+
:root {
|
|
217
|
+
--vp-custom-block-tip-border: transparent;
|
|
218
|
+
--vp-custom-block-tip-text: var(--vp-c-text-1);
|
|
219
|
+
--vp-custom-block-tip-bg: var(--vp-c-brand-soft);
|
|
220
|
+
--vp-custom-block-tip-code-bg: var(--vp-c-brand-soft);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Component: Algolia
|
|
225
|
+
* -------------------------------------------------------------------------- */
|
|
226
|
+
|
|
227
|
+
.DocSearch {
|
|
228
|
+
--docsearch-primary-color: var(--vp-c-brand-1) !important;
|
|
229
|
+
}
|
|
230
|
+
`),
|
|
231
|
+
TypeScriptFile(path.join(cwd, ".vitepress", "config.mts"), `import { transformerTwoslash } from "@shikijs/vitepress-twoslash";
|
|
232
|
+
import footnotePlugin from "markdown-it-footnote";
|
|
233
|
+
import { defineConfig } from "vitepress";
|
|
234
|
+
|
|
235
|
+
// https://vitepress.dev/reference/site-config
|
|
236
|
+
export default defineConfig({
|
|
237
|
+
title: ${JSON.stringify(props.title || "Alchemy")},
|
|
238
|
+
description: ${JSON.stringify(props.description || "Alchemy Docs")},
|
|
239
|
+
markdown: {
|
|
240
|
+
// @ts-ignore
|
|
241
|
+
codeTransformers: [transformerTwoslash()],
|
|
242
|
+
theme: ${JSON.stringify(props.theme ?? {
|
|
243
|
+
light: "light-plus",
|
|
244
|
+
dark: "dark-plus",
|
|
245
|
+
})},
|
|
246
|
+
config: (md) => md.use(footnotePlugin),
|
|
247
|
+
},
|
|
248
|
+
// https://vitepress.dev/reference/default-theme-config
|
|
249
|
+
themeConfig: ${JSON.stringify({
|
|
250
|
+
...props.themeConfig,
|
|
251
|
+
search: props.themeConfig.search ?? { provider: "local" },
|
|
252
|
+
nav: props.themeConfig.nav ?? [{ text: "Home", link: "/" }],
|
|
253
|
+
sidebar: props.themeConfig.sidebar ?? [],
|
|
254
|
+
socialLinks: props.themeConfig.socialLinks ?? [],
|
|
255
|
+
})}
|
|
256
|
+
});
|
|
257
|
+
`),
|
|
258
|
+
]);
|
|
259
|
+
return this(props);
|
|
260
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "alchemy",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"module": "./lib/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"./aws": "./lib/aws/index.js",
|
|
13
13
|
"./aws/oidc": "./lib/aws/oidc/index.js",
|
|
14
14
|
"./cloudflare": "./lib/cloudflare/index.js",
|
|
15
|
+
"./docs": "./lib/docs/index.js",
|
|
15
16
|
"./esbuild": "./lib/esbuild/index.js",
|
|
16
17
|
"./fs": "./lib/fs/index.js",
|
|
17
18
|
"./github": "./lib/github/index.js",
|
|
@@ -22,6 +23,7 @@
|
|
|
22
23
|
},
|
|
23
24
|
"peerDependencies": {
|
|
24
25
|
"@ai-sdk/openai": "^1.1.9",
|
|
26
|
+
"@ai-sdk/openai-compatible": "^0.2.2",
|
|
25
27
|
"@aws-sdk/client-dynamodb": "^3.0.0",
|
|
26
28
|
"@aws-sdk/client-iam": "^3.0.0",
|
|
27
29
|
"@aws-sdk/client-lambda": "^3.0.0",
|
|
@@ -31,17 +33,19 @@
|
|
|
31
33
|
"@aws-sdk/client-sesv2": "^3.0.0",
|
|
32
34
|
"@aws-sdk/client-sqs": "^3.0.0",
|
|
33
35
|
"@aws-sdk/client-sts": "^3.0.0",
|
|
36
|
+
"prettier": "^3.0.0",
|
|
34
37
|
"@octokit/rest": "^21.1.1",
|
|
35
|
-
"ai": "^4.
|
|
38
|
+
"ai": "^4.0.0",
|
|
36
39
|
"diff": "^7.0.0",
|
|
37
|
-
"esbuild": "^0.
|
|
38
|
-
"glob": "^10.
|
|
39
|
-
"hono": "^4.
|
|
40
|
+
"esbuild": "^0.25.1",
|
|
41
|
+
"glob": "^10.0.0",
|
|
42
|
+
"hono": "^4.0.0",
|
|
40
43
|
"libsodium-wrappers": "^0.7.15",
|
|
41
44
|
"jszip": "^3.0.0",
|
|
42
|
-
"stripe": "^17.
|
|
43
|
-
"turndown": "^7.
|
|
44
|
-
"
|
|
45
|
+
"stripe": "^17.0.0",
|
|
46
|
+
"turndown": "^7.0.0",
|
|
47
|
+
"yaml": "^2.0.0",
|
|
48
|
+
"zod": "^3.0.0"
|
|
45
49
|
},
|
|
46
50
|
"devDependencies": {
|
|
47
51
|
"@ai-sdk/anthropic": "^1.1.6",
|
|
@@ -58,9 +62,12 @@
|
|
|
58
62
|
"change-case": "^5.4.4",
|
|
59
63
|
"libsodium-wrappers": "^0.7.15",
|
|
60
64
|
"openpgp": "^6.1.0",
|
|
65
|
+
"prettier": "^3.5.3",
|
|
61
66
|
"turndown": "^7.2.0",
|
|
62
67
|
"typescript": "latest",
|
|
63
68
|
"vite": "^6.0.7",
|
|
64
|
-
"
|
|
69
|
+
"vitepress": "^1.6.3",
|
|
70
|
+
"wrangler": "^3.114.0",
|
|
71
|
+
"yaml": "^2.7.1"
|
|
65
72
|
}
|
|
66
73
|
}
|
package/src/alchemy.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
1
4
|
import { DestroyedSignal, destroy } from "./destroy";
|
|
2
5
|
import { Scope } from "./scope";
|
|
3
6
|
import { secret } from "./secret";
|
|
@@ -6,18 +9,6 @@ import type { StateStoreType } from "./state";
|
|
|
6
9
|
// TODO: support browser
|
|
7
10
|
const DEFAULT_STAGE = process.env.ALCHEMY_STAGE ?? process.env.USER ?? "dev";
|
|
8
11
|
|
|
9
|
-
function _alchemy(appName: string, options: Omit<AlchemyOptions, "appName">) {
|
|
10
|
-
return scope(undefined, {
|
|
11
|
-
...options,
|
|
12
|
-
appName,
|
|
13
|
-
stage: options.stage,
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
_alchemy.destroy = destroy;
|
|
17
|
-
_alchemy.run = run;
|
|
18
|
-
_alchemy.scope = scope;
|
|
19
|
-
_alchemy.secret = secret;
|
|
20
|
-
|
|
21
12
|
// alchemy type is to semantically highlight `alchemy` as a type (keyword)
|
|
22
13
|
export type alchemy = Alchemy;
|
|
23
14
|
|
|
@@ -30,8 +21,106 @@ export interface Alchemy {
|
|
|
30
21
|
destroy: typeof destroy;
|
|
31
22
|
secret: typeof secret;
|
|
32
23
|
(...parameters: Parameters<typeof scope>): ReturnType<typeof scope>;
|
|
24
|
+
(template: TemplateStringsArray, ...values: any[]): Promise<string>;
|
|
33
25
|
}
|
|
34
26
|
|
|
27
|
+
function _alchemy(
|
|
28
|
+
...args:
|
|
29
|
+
| [template: TemplateStringsArray, ...values: any[]]
|
|
30
|
+
| [appName: string, options?: Omit<AlchemyOptions, "appName">]
|
|
31
|
+
): any {
|
|
32
|
+
if (typeof args[0] === "string") {
|
|
33
|
+
const [appName, options] = args;
|
|
34
|
+
return scope(undefined, {
|
|
35
|
+
...options,
|
|
36
|
+
appName,
|
|
37
|
+
stage: options?.stage,
|
|
38
|
+
});
|
|
39
|
+
} else {
|
|
40
|
+
const [template, ...values] = args;
|
|
41
|
+
const [, secondLine] = template[0].split("\n");
|
|
42
|
+
const leadingSpaces = secondLine
|
|
43
|
+
? secondLine.match(/^(\s*)/)?.[1]?.length || 0
|
|
44
|
+
: 0;
|
|
45
|
+
const indent = " ".repeat(leadingSpaces);
|
|
46
|
+
|
|
47
|
+
return (async () => {
|
|
48
|
+
const { isFileCollection, isFileRef } = await import("./fs/file");
|
|
49
|
+
|
|
50
|
+
const appendices: Record<string, string> = {};
|
|
51
|
+
|
|
52
|
+
const stringValues = await Promise.all(
|
|
53
|
+
values.map(async function resolve(value): Promise<string> {
|
|
54
|
+
if (typeof value === "string") {
|
|
55
|
+
return indent + value;
|
|
56
|
+
} else if (value instanceof Promise) {
|
|
57
|
+
return resolve(await value);
|
|
58
|
+
} else if (isFileRef(value)) {
|
|
59
|
+
if (!(value.path in appendices)) {
|
|
60
|
+
appendices[value.path] = await fs.readFile(value.path, "utf-8");
|
|
61
|
+
}
|
|
62
|
+
return `[${path.basename(value.path)}](${value.path})`;
|
|
63
|
+
} else if (isFileCollection(value)) {
|
|
64
|
+
return Object.entries(value.files)
|
|
65
|
+
.map(([filePath, content]) => {
|
|
66
|
+
appendices[filePath] = content;
|
|
67
|
+
return `[${path.basename(filePath)}](${filePath})`;
|
|
68
|
+
})
|
|
69
|
+
.join("\n\n");
|
|
70
|
+
} else if (Array.isArray(value)) {
|
|
71
|
+
return (
|
|
72
|
+
await Promise.all(
|
|
73
|
+
value.map(async (value, i) => `${i}. ${await resolve(value)}`),
|
|
74
|
+
)
|
|
75
|
+
).join("\n");
|
|
76
|
+
} else {
|
|
77
|
+
// TODO: support other types
|
|
78
|
+
throw new Error(`Unsupported value type: ${JSON.stringify(value)}`);
|
|
79
|
+
}
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
// Construct the string template by joining template parts with interpolated values
|
|
84
|
+
const lines = template
|
|
85
|
+
.map((part) =>
|
|
86
|
+
part
|
|
87
|
+
.split("\n")
|
|
88
|
+
.map((line) =>
|
|
89
|
+
line.startsWith(indent) ? line.slice(indent.length) : line,
|
|
90
|
+
)
|
|
91
|
+
.join("\n"),
|
|
92
|
+
)
|
|
93
|
+
.flatMap((part, i) =>
|
|
94
|
+
i < stringValues.length ? [part, stringValues[i] ?? ""] : [part],
|
|
95
|
+
)
|
|
96
|
+
.join("")
|
|
97
|
+
.split("\n");
|
|
98
|
+
|
|
99
|
+
// Collect and sort appendices by file path
|
|
100
|
+
return [
|
|
101
|
+
// format the user prompt and trim the first line if it's empty
|
|
102
|
+
lines.length > 1 && lines[0].replaceAll(" ", "").length === 0
|
|
103
|
+
? lines.slice(1).join("\n")
|
|
104
|
+
: lines.join("\n"),
|
|
105
|
+
|
|
106
|
+
// sort appendices by path and include at the end of the prompt
|
|
107
|
+
Object.entries(appendices)
|
|
108
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
109
|
+
.map(([filePath, content]) => {
|
|
110
|
+
const extension = path.extname(filePath).slice(1);
|
|
111
|
+
const codeTag = extension ? extension : "";
|
|
112
|
+
return `// ${filePath}\n\`\`\`${codeTag}\n${content}\n\`\`\``;
|
|
113
|
+
})
|
|
114
|
+
.join("\n\n"),
|
|
115
|
+
].join("\n");
|
|
116
|
+
})();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
_alchemy.destroy = destroy;
|
|
120
|
+
_alchemy.run = run;
|
|
121
|
+
_alchemy.scope = scope;
|
|
122
|
+
_alchemy.secret = secret;
|
|
123
|
+
|
|
35
124
|
export interface AlchemyOptions {
|
|
36
125
|
/**
|
|
37
126
|
* The name of the application.
|
|
@@ -115,13 +204,16 @@ async function run<T>(
|
|
|
115
204
|
AlchemyOptions | undefined,
|
|
116
205
|
(this: Scope, scope: Scope) => Promise<T>,
|
|
117
206
|
]);
|
|
118
|
-
|
|
207
|
+
const scope = alchemy.scope(id, options);
|
|
119
208
|
try {
|
|
120
209
|
return await fn.bind(scope)(scope);
|
|
121
210
|
} catch (error) {
|
|
122
211
|
if (!(error instanceof DestroyedSignal)) {
|
|
123
212
|
scope.fail();
|
|
213
|
+
} else {
|
|
124
214
|
}
|
|
125
215
|
throw error;
|
|
216
|
+
} finally {
|
|
217
|
+
await scope.finalize();
|
|
126
218
|
}
|
|
127
219
|
}
|
package/src/apply.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
type Resource,
|
|
8
8
|
type ResourceProps,
|
|
9
9
|
} from "./resource";
|
|
10
|
+
import { Secret } from "./secret";
|
|
10
11
|
import type { State } from "./state";
|
|
11
12
|
import { serialize } from "./util/serde";
|
|
12
13
|
|
|
@@ -17,12 +18,12 @@ export interface ApplyOptions {
|
|
|
17
18
|
|
|
18
19
|
export async function apply<Out extends Resource>(
|
|
19
20
|
resource: PendingResource<Out>,
|
|
20
|
-
props: ResourceProps,
|
|
21
|
+
props: ResourceProps | undefined,
|
|
21
22
|
options?: ApplyOptions,
|
|
22
23
|
): Promise<Awaited<Out>> {
|
|
23
24
|
const scope = resource.Scope;
|
|
24
25
|
try {
|
|
25
|
-
const quiet = props
|
|
26
|
+
const quiet = props?.quiet ?? scope.quiet;
|
|
26
27
|
await scope.init();
|
|
27
28
|
let state: State | undefined = (await scope.state.get(resource.ID))!;
|
|
28
29
|
const provider: Provider = PROVIDERS.get(resource.Kind);
|
|
@@ -55,17 +56,27 @@ export async function apply<Out extends Resource>(
|
|
|
55
56
|
|
|
56
57
|
// Skip update if inputs haven't changed and resource is in a stable state
|
|
57
58
|
if (state.status === "created" || state.status === "updated") {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
59
|
+
const oldProps = JSON.stringify(
|
|
60
|
+
state.props,
|
|
61
|
+
(_, value) =>
|
|
62
|
+
value instanceof Secret
|
|
63
|
+
? {
|
|
64
|
+
"@secret": value.unencrypted,
|
|
65
|
+
}
|
|
66
|
+
: value,
|
|
67
|
+
2,
|
|
68
|
+
);
|
|
69
|
+
const newProps = JSON.stringify(
|
|
70
|
+
await serialize(scope, props, {
|
|
71
|
+
encrypt: false,
|
|
72
|
+
}),
|
|
73
|
+
null,
|
|
74
|
+
2,
|
|
75
|
+
);
|
|
76
|
+
if (oldProps === newProps && alwaysUpdate !== true) {
|
|
63
77
|
if (!quiet) {
|
|
64
78
|
console.log(`Skip: "${resource.FQN}" (no changes)`);
|
|
65
79
|
}
|
|
66
|
-
// if (resourceState.output !== undefined) {
|
|
67
|
-
// resource[Provide](resourceState.output);
|
|
68
|
-
// }
|
|
69
80
|
return state.output as Awaited<Out>;
|
|
70
81
|
}
|
|
71
82
|
}
|
|
@@ -92,6 +103,7 @@ export async function apply<Out extends Resource>(
|
|
|
92
103
|
id: resource.ID,
|
|
93
104
|
fqn: resource.FQN,
|
|
94
105
|
seq: resource.Seq,
|
|
106
|
+
props: state.oldProps,
|
|
95
107
|
state,
|
|
96
108
|
replace: () => {
|
|
97
109
|
if (isReplaced) {
|
|
@@ -104,9 +116,9 @@ export async function apply<Out extends Resource>(
|
|
|
104
116
|
},
|
|
105
117
|
});
|
|
106
118
|
|
|
107
|
-
const output = await alchemy.run(resource.ID, async () =>
|
|
108
|
-
|
|
109
|
-
|
|
119
|
+
const output = await alchemy.run(resource.ID, async () =>
|
|
120
|
+
provider.handler.bind(ctx)(resource.ID, props),
|
|
121
|
+
);
|
|
110
122
|
|
|
111
123
|
if (!quiet) {
|
|
112
124
|
console.log(
|
package/src/context.ts
CHANGED
|
@@ -9,24 +9,33 @@ import type {
|
|
|
9
9
|
import type { Scope } from "./scope";
|
|
10
10
|
import type { State } from "./state";
|
|
11
11
|
|
|
12
|
-
export type Context<
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
export type Context<
|
|
13
|
+
Out extends Resource,
|
|
14
|
+
Props extends ResourceProps = ResourceProps,
|
|
15
|
+
> = CreateContext<Out> | UpdateContext<Out, Props> | DeleteContext<Out, Props>;
|
|
16
16
|
|
|
17
17
|
export interface CreateContext<Out extends Resource> extends BaseContext<Out> {
|
|
18
18
|
phase: "create";
|
|
19
19
|
output?: undefined;
|
|
20
|
+
props?: undefined;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
export interface UpdateContext<
|
|
23
|
+
export interface UpdateContext<
|
|
24
|
+
Out extends Resource,
|
|
25
|
+
Props extends ResourceProps = ResourceProps,
|
|
26
|
+
> extends BaseContext<Out> {
|
|
23
27
|
phase: "update";
|
|
24
28
|
output: Out;
|
|
29
|
+
props: Props;
|
|
25
30
|
}
|
|
26
31
|
|
|
27
|
-
export interface DeleteContext<
|
|
32
|
+
export interface DeleteContext<
|
|
33
|
+
Out extends Resource,
|
|
34
|
+
Props extends ResourceProps = ResourceProps,
|
|
35
|
+
> extends BaseContext<Out> {
|
|
28
36
|
phase: "delete";
|
|
29
37
|
output: Out;
|
|
38
|
+
props: Props;
|
|
30
39
|
}
|
|
31
40
|
|
|
32
41
|
export interface BaseContext<Out extends Resource> {
|
|
@@ -43,7 +52,6 @@ export interface BaseContext<Out extends Resource> {
|
|
|
43
52
|
* This will cause the resource to be deleted at the end of the stack's CREATE phase.
|
|
44
53
|
*/
|
|
45
54
|
replace(): void;
|
|
46
|
-
|
|
47
55
|
/**
|
|
48
56
|
* Terminate the resource lifecycle handler and destroy the resource.
|
|
49
57
|
*
|
|
@@ -60,12 +68,13 @@ export interface BaseContext<Out extends Resource> {
|
|
|
60
68
|
/**
|
|
61
69
|
* Create the Resource envelope (with Alchemy + User properties)
|
|
62
70
|
*/
|
|
71
|
+
(id: string, props: Omit<Out, keyof Resource>): Out;
|
|
63
72
|
(props: Omit<Out, keyof Resource>): Out;
|
|
64
73
|
}
|
|
65
74
|
|
|
66
75
|
export function context<
|
|
67
76
|
Kind extends string,
|
|
68
|
-
Props extends ResourceProps,
|
|
77
|
+
Props extends ResourceProps | undefined,
|
|
69
78
|
Out extends Resource,
|
|
70
79
|
>({
|
|
71
80
|
scope,
|
|
@@ -83,14 +92,24 @@ export function context<
|
|
|
83
92
|
id: ResourceID;
|
|
84
93
|
fqn: ResourceFQN;
|
|
85
94
|
seq: number;
|
|
95
|
+
props: Props;
|
|
86
96
|
state: State<Kind, Props, Out>;
|
|
87
97
|
replace: () => void;
|
|
88
98
|
}): Context<Out> {
|
|
89
|
-
function create(props: Omit<Out, "Kind" | "ID" | "Scope">): Out
|
|
99
|
+
function create(props: Omit<Out, "Kind" | "ID" | "Scope">): Out;
|
|
100
|
+
function create(id: string, props: Omit<Out, "Kind" | "ID" | "Scope">): Out;
|
|
101
|
+
function create(
|
|
102
|
+
...args:
|
|
103
|
+
| [props: Omit<Out, "Kind" | "ID" | "Scope">]
|
|
104
|
+
| [id: string, props: Omit<Out, "Kind" | "ID" | "Scope">]
|
|
105
|
+
): Out {
|
|
106
|
+
const [ID, props] =
|
|
107
|
+
typeof args[0] === "string" ? (args as [string, any]) : [id, args[0]];
|
|
108
|
+
|
|
90
109
|
return {
|
|
91
110
|
...props,
|
|
92
111
|
Kind: kind,
|
|
93
|
-
ID
|
|
112
|
+
ID,
|
|
94
113
|
FQN: fqn,
|
|
95
114
|
Scope: scope,
|
|
96
115
|
Seq: seq,
|
|
@@ -103,6 +122,7 @@ export function context<
|
|
|
103
122
|
fqn: fqn,
|
|
104
123
|
phase,
|
|
105
124
|
output: state.output,
|
|
125
|
+
props: state.props,
|
|
106
126
|
replace,
|
|
107
127
|
get: (key: string) => state.data[key],
|
|
108
128
|
set: async (key: string, value: any) => {
|
package/src/destroy.ts
CHANGED
|
@@ -23,12 +23,15 @@ export async function destroy<Type extends string>(
|
|
|
23
23
|
| [resource: Resource<Type> | undefined | null, options?: DestroyOptions]
|
|
24
24
|
): Promise<void> {
|
|
25
25
|
if (isScopeArgs(args)) {
|
|
26
|
-
const [scope
|
|
27
|
-
const
|
|
26
|
+
const [scope] = args;
|
|
27
|
+
const options = {
|
|
28
|
+
strategy: "sequential",
|
|
29
|
+
...(args[1] ?? {}),
|
|
30
|
+
} satisfies DestroyOptions;
|
|
31
|
+
|
|
28
32
|
// destroy all active resources
|
|
29
|
-
await destroy.all(Array.from(scope.resources.values()),
|
|
30
|
-
|
|
31
|
-
});
|
|
33
|
+
await destroy.all(Array.from(scope.resources.values()), options);
|
|
34
|
+
|
|
32
35
|
// then detect orphans and destroy them
|
|
33
36
|
const orphans = await scope.state.all();
|
|
34
37
|
await destroy.all(
|
|
@@ -67,10 +70,9 @@ export async function destroy<Type extends string>(
|
|
|
67
70
|
console.log(`Delete: "${instance.FQN}"`);
|
|
68
71
|
}
|
|
69
72
|
|
|
70
|
-
const state =
|
|
73
|
+
const state = await scope.state.get(instance.ID);
|
|
71
74
|
|
|
72
75
|
if (state === undefined) {
|
|
73
|
-
console.warn(`Resource "${instance.FQN}" not found`);
|
|
74
76
|
return;
|
|
75
77
|
}
|
|
76
78
|
|
|
@@ -81,6 +83,7 @@ export async function destroy<Type extends string>(
|
|
|
81
83
|
id: instance.ID,
|
|
82
84
|
fqn: instance.FQN,
|
|
83
85
|
seq: instance.Seq,
|
|
86
|
+
props: state.props,
|
|
84
87
|
state,
|
|
85
88
|
replace: () => {
|
|
86
89
|
throw new Error("Cannot replace a resource that is being deleted");
|