alchemy 0.2.1 → 0.2.2

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.
@@ -19,11 +19,62 @@ export interface ViteProjectProps {
19
19
  */
20
20
  references?: string[];
21
21
  /**
22
+ * Add Tailwind CSS to the project
22
23
  * @default false
23
24
  */
24
25
  tailwind?: boolean;
25
26
  /**
26
- * Force overwrite the project config tfiles during the update phase
27
+ * Add Tanstack Router to the project
28
+ * @default false
29
+ */
30
+ tanstack?: boolean;
31
+ /**
32
+ * Add Shadcn UI to the project
33
+ * @default false
34
+ */
35
+ shadcn?: {
36
+ /**
37
+ * The base color to use
38
+ * @default "neutral"
39
+ */
40
+ baseColor?: "neutral" | "gray" | "zinc" | "stone" | "slate";
41
+ /**
42
+ * Use default configuration
43
+ * @default false
44
+ */
45
+ defaults?: boolean;
46
+ /**
47
+ * Force overwrite of existing configuration
48
+ * @default false
49
+ */
50
+ force?: boolean;
51
+ /**
52
+ * The working directory
53
+ * @default current directory
54
+ */
55
+ cwd?: string;
56
+ /**
57
+ * Mute output
58
+ * @default false
59
+ */
60
+ silent?: boolean;
61
+ /**
62
+ * Use the src directory when creating a new project
63
+ * @default false
64
+ */
65
+ srcDir?: boolean;
66
+ /**
67
+ * Use css variables for theming
68
+ * @default true
69
+ */
70
+ cssVariables?: boolean;
71
+ /**
72
+ * The components to add
73
+ */
74
+ components?: string[];
75
+ };
76
+ /**
77
+ * Force overwrite the project config files during the update phase
27
78
  *
28
79
  * @default false
29
80
  */
@@ -3,9 +3,13 @@ import fs from "fs/promises";
3
3
  import path from "path";
4
4
  import { promisify } from "util";
5
5
  import { Resource } from "../resource";
6
+ import { ShadcnComponent } from "../shadcn/component";
6
7
  import { rm } from "../util/rm";
7
8
  const execAsync = promisify(exec);
8
- export const ViteProject = Resource("project::ViteProject", async function (id, props) {
9
+ export const ViteProject = Resource("project::ViteProject", {
10
+ alwaysUpdate: true,
11
+ }, async function (id, props) {
12
+ const phase = this.phase;
9
13
  if (this.phase === "delete") {
10
14
  try {
11
15
  if (await fs.exists(props.name)) {
@@ -27,42 +31,198 @@ export const ViteProject = Resource("project::ViteProject", async function (id,
27
31
  }
28
32
  }
29
33
  else {
30
- // Create phase
31
34
  await execAsync(`bun create vite ${id} --template ${props.template}`);
32
35
  await modifyConfig(props);
33
36
  }
34
37
  return this(props);
35
- });
36
- async function modifyConfig(props) {
37
- if (props.tailwind) {
38
- await execAsync(`bun add tailwindcss @tailwindcss/vite`);
39
- await fs.writeFile(path.join(props.name, "vite.config.ts"), `import tailwindcss from "@tailwindcss/vite";
38
+ async function modifyConfig(props) {
39
+ const tailwind = props.tailwind ?? false;
40
+ const tanstack = props.tanstack ?? false;
41
+ const plugins = [
42
+ tailwind && "tailwindcss()",
43
+ tanstack &&
44
+ "TanStackRouterVite({ target: 'react', autoCodeSplitting: true })",
45
+ "react()",
46
+ ].filter((s) => typeof s === "string");
47
+ const cwd = path.resolve(process.cwd(), props.name);
48
+ const exec = (command) => execAsync(command, { cwd });
49
+ if (phase === "create" || props.overwrite) {
50
+ await removeUnnecessaryFiles();
51
+ }
52
+ await patchTsConfig();
53
+ if (props.tailwind) {
54
+ await installTailwind();
55
+ }
56
+ if (props.tanstack) {
57
+ await installTanstack();
58
+ }
59
+ if (props.shadcn !== undefined) {
60
+ await installShadcn();
61
+ }
62
+ await build();
63
+ async function build() {
64
+ // tsc -b will fail if we have not invoked tan stacks' code gen
65
+ await execAsync(`bun vite build`, { cwd: props.name });
66
+ }
67
+ async function installTailwind() {
68
+ await exec(`bun add tailwindcss @tailwindcss/vite`);
69
+ await fs.writeFile(path.join(props.name, "vite.config.ts"), `import path from "path";
40
70
  import react from "@vitejs/plugin-react";
41
71
  import { defineConfig } from "vite";
72
+ ${tailwind ? "import tailwindcss from '@tailwindcss/vite';" : ""}
73
+ ${tanstack ? 'import { TanStackRouterVite } from "@tanstack/router-plugin/vite";' : ""}
42
74
 
43
75
  // https://vite.dev/config/
44
76
  export default defineConfig({
45
- plugins: [react(), tailwindcss()],
77
+ plugins: [${plugins.join(", ")}],
78
+ resolve: {
79
+ alias: {
80
+ "@": path.resolve(__dirname, "./src"),
81
+ },
82
+ },
46
83
  });`);
47
- // Add Tailwind CSS import to index.css
48
- const indexCssPath = path.join(props.name, "src", "index.css");
49
- const currentCss = await fs.readFile(indexCssPath, "utf-8");
50
- if (!currentCss.includes('@import "tailwindcss";')) {
51
- await fs.writeFile(indexCssPath, '@import "tailwindcss";\n\n' + currentCss);
84
+ // Add Tailwind CSS import to index.css
85
+ const indexCssPath = path.join(props.name, "src", "index.css");
86
+ const currentCss = await fs.readFile(indexCssPath, "utf-8");
87
+ if (!currentCss.includes('@import "tailwindcss";')) {
88
+ await fs.writeFile(indexCssPath, '@import "tailwindcss";\n\n' + currentCss);
89
+ }
52
90
  }
53
- }
54
- await Promise.all([
55
- rm(path.join(props.name, "tsconfig.app.json")),
56
- rm(path.join(props.name, "tsconfig.node.json")),
57
- fs.writeFile(path.join(props.name, "tsconfig.json"), JSON.stringify({
58
- extends: props.extends,
59
- compilerOptions: {
60
- types: ["@cloudflare/workers-types"],
61
- allowImportingTsExtensions: true,
62
- jsx: "react-jsx",
63
- },
64
- include: ["vite/*.ts", "src/**/*.ts", "src/**/*.tsx", "src/env.d.ts"],
65
- references: props.references?.map((path) => ({ path })),
66
- }, null, 2)),
67
- ]);
91
+ async function installShadcn() {
92
+ await exec("bun add -D @types/node");
93
+ // Build the shadcn init command with all options
94
+ const shadcnOptions = props.shadcn;
95
+ const initCommand = [
96
+ "bunx --bun shadcn@latest init",
97
+ shadcnOptions?.baseColor && `-b ${shadcnOptions.baseColor}`,
98
+ shadcnOptions?.defaults && "-d",
99
+ shadcnOptions?.force && "-f",
100
+ shadcnOptions?.cwd && `-c ${shadcnOptions.cwd}`,
101
+ shadcnOptions?.silent && "-s",
102
+ shadcnOptions?.srcDir && "--src-dir",
103
+ shadcnOptions?.cssVariables === false && "--no-css-variables",
104
+ ]
105
+ .filter(Boolean)
106
+ .join(" ");
107
+ await exec(initCommand);
108
+ // Install requested components using the ShadcnComponent resource
109
+ for (const componentName of props.shadcn?.components ?? []) {
110
+ await ShadcnComponent(`shadcn-component-${componentName}`, {
111
+ name: componentName,
112
+ cwd: props.name,
113
+ force: props.shadcn?.force,
114
+ silent: props.shadcn?.silent,
115
+ });
116
+ }
117
+ }
118
+ async function installTanstack() {
119
+ await exec(`bun add @tanstack/react-router`);
120
+ await exec(`bun add -D @tanstack/router-plugin @tanstack/react-router-devtools`);
121
+ const src = path.join(props.name, "src");
122
+ const routes = path.join(src, "routes");
123
+ await fs.mkdir(routes, { recursive: true });
124
+ await fs.writeFile(path.join(routes, "__root.tsx"), `import { Link, Outlet, createRootRoute } from "@tanstack/react-router";
125
+ import { TanStackRouterDevtools } from "@tanstack/react-router-devtools";
126
+
127
+ export const Route = createRootRoute({
128
+ component: () => (
129
+ <div className="w-full min-h-screen flex flex-col">
130
+ <div className="p-2 flex gap-2">
131
+ <Link to="/" className="[&.active]:font-bold">
132
+ Home
133
+ </Link>{" "}
134
+ <Link to="/about" className="[&.active]:font-bold">
135
+ About
136
+ </Link>
137
+ </div>
138
+ <hr />
139
+ <Outlet />
140
+ <TanStackRouterDevtools />
141
+ </div>
142
+ ),
143
+ });
144
+ `);
145
+ await fs.writeFile(path.join(routes, "index.tsx"), `import { createLazyFileRoute } from '@tanstack/react-router'
146
+
147
+ export const Route = createLazyFileRoute('/')({
148
+ component: Index,
149
+ })
150
+
151
+ function Index() {
152
+ return (
153
+ <div className="p-2">
154
+ <h3>Welcome Home!</h3>
155
+ </div>
156
+ )
157
+ }`);
158
+ await fs.writeFile(path.join(routes, "about.tsx"), `import { createLazyFileRoute } from '@tanstack/react-router'
159
+
160
+ export const Route = createLazyFileRoute('/about')({
161
+ component: About,
162
+ })
163
+
164
+ function About() {
165
+ return <div className="p-2">Hello from About!</div>
166
+ }`);
167
+ await fs.writeFile(path.join(src, "main.tsx"), `import { StrictMode } from 'react'
168
+ import ReactDOM from 'react-dom/client'
169
+ import { RouterProvider, createRouter } from '@tanstack/react-router'
170
+ import './index.css'
171
+
172
+ // Import the generated route tree
173
+ import { routeTree } from './routeTree.gen'
174
+
175
+ // Create a new router instance
176
+ const router = createRouter({ routeTree })
177
+
178
+ // Register the router instance for type safety
179
+ declare module '@tanstack/react-router' {
180
+ interface Register {
181
+ router: typeof router
182
+ }
68
183
  }
184
+
185
+ // Render the app
186
+ const rootElement = document.getElementById('root')!
187
+ if (!rootElement.innerHTML) {
188
+ const root = ReactDOM.createRoot(rootElement)
189
+ root.render(
190
+ <StrictMode>
191
+ <RouterProvider router={router} />
192
+ </StrictMode>,
193
+ )
194
+ }`);
195
+ }
196
+ async function removeUnnecessaryFiles() {
197
+ await Promise.all([
198
+ rm(path.join(props.name, "src", "App.tsx")),
199
+ rm(path.join(props.name, "src", "App.css")),
200
+ ]);
201
+ }
202
+ async function patchTsConfig() {
203
+ await Promise.all([
204
+ rm(path.join(props.name, "tsconfig.app.json")),
205
+ rm(path.join(props.name, "tsconfig.node.json")),
206
+ fs.writeFile(path.join(props.name, "tsconfig.json"), JSON.stringify({
207
+ extends: props.extends,
208
+ compilerOptions: {
209
+ baseUrl: ".",
210
+ paths: {
211
+ "@/*": ["./src/*"],
212
+ },
213
+ types: ["@cloudflare/workers-types"],
214
+ allowImportingTsExtensions: true,
215
+ jsx: "react-jsx",
216
+ },
217
+ include: [
218
+ "vite/*.ts",
219
+ "src/**/*.ts",
220
+ "src/**/*.tsx",
221
+ "src/env.d.ts",
222
+ ],
223
+ references: props.references?.map((path) => ({ path })),
224
+ }, null, 2)),
225
+ ]);
226
+ }
227
+ }
228
+ });
@@ -0,0 +1,29 @@
1
+ import type { Context } from "../context";
2
+ import { Resource } from "../resource";
3
+ export interface ShadcnComponentProps {
4
+ /**
5
+ * The name of the component to install
6
+ */
7
+ name: string;
8
+ /**
9
+ * The working directory where the component should be installed
10
+ */
11
+ cwd: string;
12
+ /**
13
+ * Force overwrite of existing component
14
+ * @default false
15
+ */
16
+ force?: boolean;
17
+ /**
18
+ * Mute output
19
+ * @default false
20
+ */
21
+ silent?: boolean;
22
+ }
23
+ export interface ShadcnComponent extends ShadcnComponentProps, Resource {
24
+ /**
25
+ * The name of the installed component
26
+ */
27
+ name: string;
28
+ }
29
+ export declare const ShadcnComponent: ((this: Context<ShadcnComponent>, id: string, props: ShadcnComponentProps) => Promise<ShadcnComponent>) | (((this: any, id: string, props: ShadcnComponentProps) => never) & (new (_: never) => never));
@@ -0,0 +1,31 @@
1
+ import { exec } from "child_process";
2
+ import fs from "fs/promises";
3
+ import path from "path";
4
+ import { promisify } from "util";
5
+ import { Resource } from "../resource";
6
+ const execAsync = promisify(exec);
7
+ export const ShadcnComponent = Resource("project::ShadcnComponent", async function (id, props) {
8
+ if (this.phase === "delete") {
9
+ const componentPath = path.join(props.cwd, "src", "components", "ui", `${props.name}.tsx`);
10
+ try {
11
+ await fs.unlink(componentPath);
12
+ }
13
+ catch (error) {
14
+ if (error.code !== "ENOENT") {
15
+ throw error;
16
+ }
17
+ }
18
+ return this.destroy();
19
+ }
20
+ // Build the shadcn add command with options
21
+ const addCommand = [
22
+ "bunx --bun shadcn@latest add",
23
+ props.name,
24
+ props.silent && "-s",
25
+ ]
26
+ .filter(Boolean)
27
+ .join(" ");
28
+ // Execute the command in the specified directory
29
+ await execAsync(addCommand, { cwd: props.cwd });
30
+ return this(props);
31
+ });
@@ -0,0 +1 @@
1
+ export * from "./component";
@@ -0,0 +1 @@
1
+ export * from "./component";
@@ -0,0 +1,90 @@
1
+ import type { Context } from "../context";
2
+ import { Resource } from "../resource";
3
+ type ViteTemplate = "vanilla" | "vanilla-ts" | "vue" | "vue-ts" | "react" | "react-ts" | "react-swc" | "react-swc-ts" | "preact" | "preact-ts" | "lit" | "lit-ts" | "svelte" | "svelte-ts" | "solid" | "solid-ts" | "qwik" | "qwik-ts";
4
+ export interface ViteProjectProps {
5
+ /**
6
+ * The name/path of the project
7
+ */
8
+ name: string;
9
+ /**
10
+ * The Vite template to use
11
+ */
12
+ template: ViteTemplate;
13
+ /**
14
+ * The extends to add to the tsconfig.json file
15
+ */
16
+ extends?: string;
17
+ /**
18
+ * The references to add to the tsconfig.json file
19
+ */
20
+ references?: string[];
21
+ /**
22
+ * Add Tailwind CSS to the project
23
+ * @default false
24
+ */
25
+ tailwind?: boolean;
26
+ /**
27
+ * Add Tanstack Router to the project
28
+ * @default false
29
+ */
30
+ tanstack?: boolean;
31
+ /**
32
+ * Add Shadcn UI to the project
33
+ * @default false
34
+ */
35
+ shadcn?: {
36
+ /**
37
+ * The base color to use
38
+ * @default "neutral"
39
+ */
40
+ baseColor?: "neutral" | "gray" | "zinc" | "stone" | "slate";
41
+ /**
42
+ * Use default configuration
43
+ * @default false
44
+ */
45
+ defaults?: boolean;
46
+ /**
47
+ * Force overwrite of existing configuration
48
+ * @default false
49
+ */
50
+ force?: boolean;
51
+ /**
52
+ * The working directory
53
+ * @default current directory
54
+ */
55
+ cwd?: string;
56
+ /**
57
+ * Mute output
58
+ * @default false
59
+ */
60
+ silent?: boolean;
61
+ /**
62
+ * Use the src directory when creating a new project
63
+ * @default false
64
+ */
65
+ srcDir?: boolean;
66
+ /**
67
+ * Use css variables for theming
68
+ * @default true
69
+ */
70
+ cssVariables?: boolean;
71
+ /**
72
+ * The components to add
73
+ */
74
+ components?: string[];
75
+ };
76
+ /**
77
+ * Force overwrite the project config files during the update phase
78
+ *
79
+ * @default false
80
+ */
81
+ overwrite?: boolean;
82
+ }
83
+ export interface ViteProject extends ViteProjectProps, Resource {
84
+ /**
85
+ * The name/path of the project
86
+ */
87
+ name: string;
88
+ }
89
+ export declare const ViteProject: ((this: Context<ViteProject>, id: string, props: ViteProjectProps) => Promise<ViteProject>) | (((this: any, id: string, props: ViteProjectProps) => never) & (new (_: never) => never));
90
+ export {};