alchemy 0.2.0 → 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.
- package/README.md +506 -0
- package/lib/alchemy.js +10 -2
- package/lib/apply.js +86 -77
- package/lib/cloudflare/index.d.ts +2 -0
- package/lib/cloudflare/index.js +2 -0
- package/lib/cloudflare/static-site.js +9 -9
- package/lib/cloudflare/worker.js +1 -1
- package/lib/cloudflare/wrangler.json.d.ts +243 -0
- package/lib/cloudflare/wrangler.json.js +35 -0
- package/lib/cloudflare/zone-settings.d.ts +301 -0
- package/lib/cloudflare/zone-settings.js +1 -0
- package/lib/cloudflare/zone.d.ts +196 -0
- package/lib/cloudflare/zone.js +175 -0
- package/lib/destroy.js +1 -1
- package/lib/project/vite.d.ts +90 -0
- package/lib/project/vite.js +228 -0
- package/lib/scope.d.ts +2 -0
- package/lib/scope.js +20 -1
- package/lib/shadcn/component.d.ts +29 -0
- package/lib/shadcn/component.js +31 -0
- package/lib/shadcn/index.d.ts +1 -0
- package/lib/shadcn/index.js +1 -0
- package/lib/state.js +6 -1
- package/lib/util/rm.d.ts +1 -0
- package/lib/util/rm.js +11 -0
- package/lib/vite/index.d.ts +1 -0
- package/lib/vite/index.js +1 -0
- package/lib/vite/vite.d.ts +90 -0
- package/lib/vite/vite.js +228 -0
- package/package.json +5 -3
- package/src/alchemy.ts +9 -2
- package/src/apply.ts +100 -94
- package/src/cloudflare/index.ts +2 -0
- package/src/cloudflare/static-site.ts +11 -11
- package/src/cloudflare/worker.ts +1 -1
- package/src/cloudflare/wrangler.json.ts +309 -0
- package/src/cloudflare/zone-settings.ts +368 -0
- package/src/cloudflare/zone.ts +491 -0
- package/src/destroy.ts +1 -1
- package/src/project/vite.ts +406 -0
- package/src/scope.ts +25 -1
- package/src/shadcn/component.ts +80 -0
- package/src/shadcn/index.ts +1 -0
- package/src/state.ts +9 -1
- package/src/util/rm.ts +11 -0
- package/src/vite/index.ts +1 -0
- package/src/vite/vite.ts +406 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import type { Context } from "../context";
|
|
2
|
+
import { Resource } from "../resource";
|
|
3
|
+
import type { AlwaysUseHTTPSValue, AutomaticHTTPSRewritesValue, BrotliValue, DevelopmentModeValue, EarlyHintsValue, EmailObfuscationValue, HTTP2Value, HTTP3Value, HotlinkProtectionValue, IPv6Value, MinTLSVersionValue, SSLValue, TLS13Value, WebSocketsValue, ZeroRTTValue } from "./zone-settings";
|
|
4
|
+
/**
|
|
5
|
+
* Properties for creating or updating a Zone
|
|
6
|
+
*/
|
|
7
|
+
export interface ZoneProps {
|
|
8
|
+
/**
|
|
9
|
+
* The domain name for the zone
|
|
10
|
+
*/
|
|
11
|
+
name: string;
|
|
12
|
+
/**
|
|
13
|
+
* Account ID to use for the zone
|
|
14
|
+
* If not provided, will use the default account
|
|
15
|
+
*/
|
|
16
|
+
accountId?: string;
|
|
17
|
+
/**
|
|
18
|
+
* The type of zone to create
|
|
19
|
+
* "full" - Full zone implies that DNS is hosted with Cloudflare
|
|
20
|
+
* "partial" - Partial zone is typically a partner-hosted zone or a CNAME setup
|
|
21
|
+
* "secondary" - Secondary zone is a zone that mirrors the primary zone
|
|
22
|
+
* @default "full"
|
|
23
|
+
*/
|
|
24
|
+
type?: "full" | "partial" | "secondary";
|
|
25
|
+
/**
|
|
26
|
+
* Whether to jump start the zone
|
|
27
|
+
* When enabled, Cloudflare will attempt to fetch existing DNS records
|
|
28
|
+
* @default true
|
|
29
|
+
*/
|
|
30
|
+
jumpStart?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Settings to apply to the zone
|
|
33
|
+
*/
|
|
34
|
+
settings?: {
|
|
35
|
+
/**
|
|
36
|
+
* Enable SSL/TLS encryption for the zone
|
|
37
|
+
* "off" - SSL disabled
|
|
38
|
+
* "flexible" - Encrypts traffic between browser and Cloudflare
|
|
39
|
+
* "full" - Encrypts traffic between browser and server, allows self-signed certs
|
|
40
|
+
* "strict" - Encrypts traffic between browser and server, requires valid cert
|
|
41
|
+
*/
|
|
42
|
+
ssl?: SSLValue;
|
|
43
|
+
/**
|
|
44
|
+
* Enable Always Use HTTPS
|
|
45
|
+
* Redirects all HTTP traffic to HTTPS
|
|
46
|
+
* @default "off"
|
|
47
|
+
*/
|
|
48
|
+
alwaysUseHttps?: AlwaysUseHTTPSValue;
|
|
49
|
+
/**
|
|
50
|
+
* Enable Automatic HTTPS Rewrites
|
|
51
|
+
* Automatically rewrites HTTP URLs to HTTPS
|
|
52
|
+
* @default "off"
|
|
53
|
+
*/
|
|
54
|
+
automaticHttpsRewrites?: AutomaticHTTPSRewritesValue;
|
|
55
|
+
/**
|
|
56
|
+
* Enable TLS 1.3
|
|
57
|
+
* Enables the latest version of TLS encryption
|
|
58
|
+
* @default "off"
|
|
59
|
+
*/
|
|
60
|
+
tls13?: TLS13Value;
|
|
61
|
+
/**
|
|
62
|
+
* Enable Early Hints
|
|
63
|
+
* Speeds up page loads by serving Link headers
|
|
64
|
+
* @default "off"
|
|
65
|
+
*/
|
|
66
|
+
earlyHints?: EarlyHintsValue;
|
|
67
|
+
/**
|
|
68
|
+
* Enable Email Obfuscation
|
|
69
|
+
* Obfuscates email addresses on the site
|
|
70
|
+
* @default "off"
|
|
71
|
+
*/
|
|
72
|
+
emailObfuscation?: EmailObfuscationValue;
|
|
73
|
+
/**
|
|
74
|
+
* Enable Browser Cache TTL
|
|
75
|
+
* Sets the browser cache TTL in seconds
|
|
76
|
+
*/
|
|
77
|
+
browserCacheTtl?: number;
|
|
78
|
+
/**
|
|
79
|
+
* Enable Development Mode
|
|
80
|
+
* Disables caching and enables real-time updates
|
|
81
|
+
* @default "off"
|
|
82
|
+
*/
|
|
83
|
+
developmentMode?: DevelopmentModeValue;
|
|
84
|
+
/**
|
|
85
|
+
* Enable HTTP/2
|
|
86
|
+
* @default "on"
|
|
87
|
+
*/
|
|
88
|
+
http2?: HTTP2Value;
|
|
89
|
+
/**
|
|
90
|
+
* Enable HTTP/3
|
|
91
|
+
* @default "on"
|
|
92
|
+
*/
|
|
93
|
+
http3?: HTTP3Value;
|
|
94
|
+
/**
|
|
95
|
+
* Enable IPv6
|
|
96
|
+
* @default "on"
|
|
97
|
+
*/
|
|
98
|
+
ipv6?: IPv6Value;
|
|
99
|
+
/**
|
|
100
|
+
* Enable WebSockets
|
|
101
|
+
* @default "on"
|
|
102
|
+
*/
|
|
103
|
+
websockets?: WebSocketsValue;
|
|
104
|
+
/**
|
|
105
|
+
* Enable Zero-RTT
|
|
106
|
+
* @default "off"
|
|
107
|
+
*/
|
|
108
|
+
zeroRtt?: ZeroRTTValue;
|
|
109
|
+
/**
|
|
110
|
+
* Enable Brotli compression
|
|
111
|
+
* @default "on"
|
|
112
|
+
*/
|
|
113
|
+
brotli?: BrotliValue;
|
|
114
|
+
/**
|
|
115
|
+
* Enable Hotlink Protection
|
|
116
|
+
* @default "off"
|
|
117
|
+
*/
|
|
118
|
+
hotlinkProtection?: HotlinkProtectionValue;
|
|
119
|
+
/**
|
|
120
|
+
* Minimum TLS Version
|
|
121
|
+
* @default "1.0"
|
|
122
|
+
*/
|
|
123
|
+
minTlsVersion?: MinTLSVersionValue;
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Output returned after Zone creation/update
|
|
128
|
+
*/
|
|
129
|
+
export interface Zone extends Resource<"cloudflare::Zone"> {
|
|
130
|
+
/**
|
|
131
|
+
* The ID of the zone
|
|
132
|
+
*/
|
|
133
|
+
id: string;
|
|
134
|
+
/**
|
|
135
|
+
* The domain name for the zone
|
|
136
|
+
*/
|
|
137
|
+
name: string;
|
|
138
|
+
/**
|
|
139
|
+
* The type of zone
|
|
140
|
+
*/
|
|
141
|
+
type: "full" | "partial" | "secondary";
|
|
142
|
+
/**
|
|
143
|
+
* The status of the zone
|
|
144
|
+
*/
|
|
145
|
+
status: string;
|
|
146
|
+
/**
|
|
147
|
+
* Whether the zone is paused
|
|
148
|
+
*/
|
|
149
|
+
paused: boolean;
|
|
150
|
+
/**
|
|
151
|
+
* The account ID the zone belongs to
|
|
152
|
+
*/
|
|
153
|
+
accountId: string;
|
|
154
|
+
/**
|
|
155
|
+
* The nameservers assigned to the zone
|
|
156
|
+
*/
|
|
157
|
+
nameservers: string[];
|
|
158
|
+
/**
|
|
159
|
+
* The original nameservers for the zone
|
|
160
|
+
*/
|
|
161
|
+
originalNameservers: string[] | null;
|
|
162
|
+
/**
|
|
163
|
+
* Time at which the zone was created
|
|
164
|
+
*/
|
|
165
|
+
createdAt: number;
|
|
166
|
+
/**
|
|
167
|
+
* Time at which the zone was last modified
|
|
168
|
+
*/
|
|
169
|
+
modifiedAt: number;
|
|
170
|
+
/**
|
|
171
|
+
* Time at which the zone was activated
|
|
172
|
+
*/
|
|
173
|
+
activatedAt: number | null;
|
|
174
|
+
/**
|
|
175
|
+
* The zone's current settings
|
|
176
|
+
*/
|
|
177
|
+
settings: {
|
|
178
|
+
ssl: SSLValue;
|
|
179
|
+
alwaysUseHttps: AlwaysUseHTTPSValue;
|
|
180
|
+
automaticHttpsRewrites: AutomaticHTTPSRewritesValue;
|
|
181
|
+
tls13: TLS13Value;
|
|
182
|
+
earlyHints: EarlyHintsValue;
|
|
183
|
+
emailObfuscation: EmailObfuscationValue;
|
|
184
|
+
browserCacheTtl: number;
|
|
185
|
+
developmentMode: DevelopmentModeValue;
|
|
186
|
+
http2: HTTP2Value;
|
|
187
|
+
http3: HTTP3Value;
|
|
188
|
+
ipv6: IPv6Value;
|
|
189
|
+
websockets: WebSocketsValue;
|
|
190
|
+
zeroRtt: ZeroRTTValue;
|
|
191
|
+
brotli: BrotliValue;
|
|
192
|
+
hotlinkProtection: HotlinkProtectionValue;
|
|
193
|
+
minTlsVersion: MinTLSVersionValue;
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
export declare const Zone: ((this: Context<Zone>, id: string, props: ZoneProps) => Promise<Zone>) | (((this: any, id: string, props: ZoneProps) => never) & (new (_: never) => never));
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { Resource } from "../resource";
|
|
2
|
+
import { createCloudflareApi } from "./api";
|
|
3
|
+
export const Zone = Resource("cloudflare::Zone", async function (id, props) {
|
|
4
|
+
// Create Cloudflare API client with automatic account discovery
|
|
5
|
+
const api = await createCloudflareApi({
|
|
6
|
+
accountId: props.accountId,
|
|
7
|
+
});
|
|
8
|
+
if (this.phase === "delete") {
|
|
9
|
+
if (this.output?.id) {
|
|
10
|
+
// Delete zone
|
|
11
|
+
const deleteResponse = await api.delete(`/zones/${this.output.id}`);
|
|
12
|
+
if (!deleteResponse.ok && deleteResponse.status !== 404) {
|
|
13
|
+
const errorData = await deleteResponse.json().catch(() => ({
|
|
14
|
+
errors: [{ message: deleteResponse.statusText }],
|
|
15
|
+
}));
|
|
16
|
+
throw new Error(`Error deleting zone '${props.name}': ${errorData.errors?.[0]?.message || deleteResponse.statusText}`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
console.warn(`Zone '${props.name}' not found, skipping delete`);
|
|
21
|
+
}
|
|
22
|
+
return this.destroy();
|
|
23
|
+
}
|
|
24
|
+
if (this.phase === "update" && this.output?.id) {
|
|
25
|
+
// Get zone details to verify it exists
|
|
26
|
+
const response = await api.get(`/zones/${this.output.id}`);
|
|
27
|
+
if (!response.ok) {
|
|
28
|
+
throw new Error(`Error getting zone '${props.name}': ${response.statusText}`);
|
|
29
|
+
}
|
|
30
|
+
const result = (await response.json());
|
|
31
|
+
const zoneData = result.result;
|
|
32
|
+
// Update zone settings if provided
|
|
33
|
+
if (props.settings) {
|
|
34
|
+
await updateZoneSettings(api, this.output.id, props.settings);
|
|
35
|
+
// Add a small delay to ensure settings are propagated
|
|
36
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
37
|
+
}
|
|
38
|
+
return this({
|
|
39
|
+
id: zoneData.id,
|
|
40
|
+
name: zoneData.name,
|
|
41
|
+
type: zoneData.type,
|
|
42
|
+
status: zoneData.status,
|
|
43
|
+
paused: zoneData.paused,
|
|
44
|
+
accountId: zoneData.account.id,
|
|
45
|
+
nameservers: zoneData.name_servers,
|
|
46
|
+
originalNameservers: zoneData.original_name_servers,
|
|
47
|
+
createdAt: new Date(zoneData.created_on).getTime(),
|
|
48
|
+
modifiedAt: new Date(zoneData.modified_on).getTime(),
|
|
49
|
+
activatedAt: zoneData.activated_on
|
|
50
|
+
? new Date(zoneData.activated_on).getTime()
|
|
51
|
+
: null,
|
|
52
|
+
settings: await getZoneSettings(api, zoneData.id),
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
// Create new zone
|
|
57
|
+
const response = await api.post("/zones", {
|
|
58
|
+
name: props.name,
|
|
59
|
+
type: props.type || "full",
|
|
60
|
+
jump_start: props.jumpStart !== false,
|
|
61
|
+
account: {
|
|
62
|
+
id: api.accountId,
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
const body = await response.text();
|
|
66
|
+
if (!response.ok) {
|
|
67
|
+
if (response.status === 400 && body.includes("already exists")) {
|
|
68
|
+
// TODO: should we error?
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
throw new Error(`Error creating zone '${props.name}': ${response.statusText}\n${body}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const zoneData = JSON.parse(body).result;
|
|
75
|
+
// Update zone settings if provided
|
|
76
|
+
if (props.settings) {
|
|
77
|
+
await updateZoneSettings(api, zoneData.id, props.settings);
|
|
78
|
+
// Add a small delay to ensure settings are propagated
|
|
79
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
80
|
+
}
|
|
81
|
+
return this({
|
|
82
|
+
id: zoneData.id,
|
|
83
|
+
name: zoneData.name,
|
|
84
|
+
type: zoneData.type,
|
|
85
|
+
status: zoneData.status,
|
|
86
|
+
paused: zoneData.paused,
|
|
87
|
+
accountId: zoneData.account.id,
|
|
88
|
+
nameservers: zoneData.name_servers,
|
|
89
|
+
originalNameservers: zoneData.original_name_servers,
|
|
90
|
+
createdAt: new Date(zoneData.created_on).getTime(),
|
|
91
|
+
modifiedAt: new Date(zoneData.modified_on).getTime(),
|
|
92
|
+
activatedAt: zoneData.activated_on
|
|
93
|
+
? new Date(zoneData.activated_on).getTime()
|
|
94
|
+
: null,
|
|
95
|
+
settings: await getZoneSettings(api, zoneData.id),
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
/**
|
|
100
|
+
* Helper function to update zone settings
|
|
101
|
+
*/
|
|
102
|
+
async function updateZoneSettings(api, zoneId, settings) {
|
|
103
|
+
if (!settings)
|
|
104
|
+
return;
|
|
105
|
+
const settingsMap = {
|
|
106
|
+
ssl: "ssl",
|
|
107
|
+
alwaysUseHttps: "always_use_https",
|
|
108
|
+
automaticHttpsRewrites: "automatic_https_rewrites",
|
|
109
|
+
tls13: "tls_1_3",
|
|
110
|
+
earlyHints: "early_hints",
|
|
111
|
+
emailObfuscation: "email_obfuscation",
|
|
112
|
+
browserCacheTtl: "browser_cache_ttl",
|
|
113
|
+
developmentMode: "development_mode",
|
|
114
|
+
http2: "http2",
|
|
115
|
+
http3: "http3",
|
|
116
|
+
ipv6: "ipv6",
|
|
117
|
+
websockets: "websockets",
|
|
118
|
+
zeroRtt: "0rtt",
|
|
119
|
+
brotli: "brotli",
|
|
120
|
+
hotlinkProtection: "hotlink_protection",
|
|
121
|
+
minTlsVersion: "min_tls_version",
|
|
122
|
+
};
|
|
123
|
+
await Promise.all(Object.entries(settings)
|
|
124
|
+
.filter(([_, value]) => value !== undefined)
|
|
125
|
+
.map(async ([key, value]) => {
|
|
126
|
+
const settingId = settingsMap[key];
|
|
127
|
+
if (!settingId)
|
|
128
|
+
return;
|
|
129
|
+
const response = await api.patch(`/zones/${zoneId}/settings/${settingId}`, {
|
|
130
|
+
value,
|
|
131
|
+
});
|
|
132
|
+
if (!response.ok) {
|
|
133
|
+
const data = await response.text();
|
|
134
|
+
if (response.status === 400 && data.includes("already enabled")) {
|
|
135
|
+
console.warn(`Warning: Setting '${key}' already enabled`);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
throw new Error(`Failed to update zone setting ${key}: ${response.statusText}`);
|
|
139
|
+
}
|
|
140
|
+
}));
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Helper function to get current zone settings
|
|
144
|
+
*/
|
|
145
|
+
async function getZoneSettings(api, zoneId) {
|
|
146
|
+
const settingsResponse = await api.get(`/zones/${zoneId}/settings`);
|
|
147
|
+
if (!settingsResponse.ok) {
|
|
148
|
+
throw new Error(`Failed to fetch zone settings: ${settingsResponse.status} ${settingsResponse.statusText}`);
|
|
149
|
+
}
|
|
150
|
+
const result = (await settingsResponse.json());
|
|
151
|
+
const settingsData = result.result;
|
|
152
|
+
// Helper to get setting value with default
|
|
153
|
+
const getSetting = (id, defaultValue) => {
|
|
154
|
+
const setting = settingsData.find((s) => s.id === id);
|
|
155
|
+
return setting?.value ?? defaultValue;
|
|
156
|
+
};
|
|
157
|
+
return {
|
|
158
|
+
ssl: getSetting("ssl", "off"),
|
|
159
|
+
alwaysUseHttps: getSetting("always_use_https", "off"),
|
|
160
|
+
automaticHttpsRewrites: getSetting("automatic_https_rewrites", "off"),
|
|
161
|
+
tls13: getSetting("tls_1_3", "off"),
|
|
162
|
+
earlyHints: getSetting("early_hints", "off"),
|
|
163
|
+
emailObfuscation: getSetting("email_obfuscation", "off"),
|
|
164
|
+
browserCacheTtl: getSetting("browser_cache_ttl", 14400),
|
|
165
|
+
developmentMode: getSetting("development_mode", "off"),
|
|
166
|
+
http2: getSetting("http2", "on"),
|
|
167
|
+
http3: getSetting("http3", "on"),
|
|
168
|
+
ipv6: getSetting("ipv6", "on"),
|
|
169
|
+
websockets: getSetting("websockets", "on"),
|
|
170
|
+
zeroRtt: getSetting("0rtt", "off"),
|
|
171
|
+
brotli: getSetting("brotli", "on"),
|
|
172
|
+
hotlinkProtection: getSetting("hotlink_protection", "off"),
|
|
173
|
+
minTlsVersion: getSetting("min_tls_version", "1.0"),
|
|
174
|
+
};
|
|
175
|
+
}
|
package/lib/destroy.js
CHANGED
|
@@ -67,7 +67,7 @@ export async function destroy(...args) {
|
|
|
67
67
|
// BUG: this does not restore persisted scope
|
|
68
68
|
await alchemy.run(instance.ID, async (scope) => {
|
|
69
69
|
nestedScope = scope;
|
|
70
|
-
return Provider.handler.bind(ctx)(instance.ID, state.props);
|
|
70
|
+
return await Provider.handler.bind(ctx)(instance.ID, state.props);
|
|
71
71
|
});
|
|
72
72
|
}
|
|
73
73
|
catch (err) {
|
|
@@ -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 {};
|
|
@@ -0,0 +1,228 @@
|
|
|
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
|
+
import { ShadcnComponent } from "../shadcn/component";
|
|
7
|
+
import { rm } from "../util/rm";
|
|
8
|
+
const execAsync = promisify(exec);
|
|
9
|
+
export const ViteProject = Resource("project::ViteProject", {
|
|
10
|
+
alwaysUpdate: true,
|
|
11
|
+
}, async function (id, props) {
|
|
12
|
+
const phase = this.phase;
|
|
13
|
+
if (this.phase === "delete") {
|
|
14
|
+
try {
|
|
15
|
+
if (await fs.exists(props.name)) {
|
|
16
|
+
// TODO: OS agnostic - fs.rm is slow to delete node_modules/
|
|
17
|
+
await execAsync("rm -rf " + props.name);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
console.error(`Error deleting project ${id}:`, error);
|
|
22
|
+
}
|
|
23
|
+
return this.destroy();
|
|
24
|
+
}
|
|
25
|
+
if (this.phase === "update") {
|
|
26
|
+
if (props.overwrite) {
|
|
27
|
+
await modifyConfig(props);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
console.warn("ViteProject does not support updates - the project must be recreated to change the template");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
await execAsync(`bun create vite ${id} --template ${props.template}`);
|
|
35
|
+
await modifyConfig(props);
|
|
36
|
+
}
|
|
37
|
+
return this(props);
|
|
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";
|
|
70
|
+
import react from "@vitejs/plugin-react";
|
|
71
|
+
import { defineConfig } from "vite";
|
|
72
|
+
${tailwind ? "import tailwindcss from '@tailwindcss/vite';" : ""}
|
|
73
|
+
${tanstack ? 'import { TanStackRouterVite } from "@tanstack/router-plugin/vite";' : ""}
|
|
74
|
+
|
|
75
|
+
// https://vite.dev/config/
|
|
76
|
+
export default defineConfig({
|
|
77
|
+
plugins: [${plugins.join(", ")}],
|
|
78
|
+
resolve: {
|
|
79
|
+
alias: {
|
|
80
|
+
"@": path.resolve(__dirname, "./src"),
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
});`);
|
|
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
|
+
}
|
|
90
|
+
}
|
|
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
|
+
}
|
|
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
|
+
});
|