70bf21e064
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Prerelease / build (push) Has been skipped
Handle Changesets / Handle Changesets (push) Has been cancelled
Semgrep OSS scan / semgrep-oss (push) Has been cancelled
133 lines
3.5 KiB
TypeScript
133 lines
3.5 KiB
TypeScript
import { logRaw, updateStatus } from "@cloudflare/cli-shared-helpers";
|
|
import { blue, brandColor, dim } from "@cloudflare/cli-shared-helpers/colors";
|
|
import { transformFile } from "@cloudflare/codemod";
|
|
import { runFrameworkGenerator } from "frameworks/index";
|
|
import { usesTypescript } from "helpers/files";
|
|
import { detectPackageManager } from "helpers/packageManagers";
|
|
import { installPackages } from "helpers/packages";
|
|
import * as recast from "recast";
|
|
import type { TemplateConfig } from "../../../src/templates";
|
|
import type { C3Context, PackageJson } from "types";
|
|
|
|
const { npm } = detectPackageManager();
|
|
|
|
const generate = async (ctx: C3Context) => {
|
|
await runFrameworkGenerator(ctx, ["create", ctx.project.name]);
|
|
|
|
logRaw("");
|
|
};
|
|
|
|
const configure = async (ctx: C3Context) => {
|
|
// Install the adapter
|
|
const pkg = `@sveltejs/adapter-cloudflare`;
|
|
await installPackages([pkg], {
|
|
dev: true,
|
|
startText: "Adding the Cloudflare adapter",
|
|
doneText: `${brandColor(`installed`)} ${dim(pkg)}`,
|
|
});
|
|
|
|
updateViteConfig(ctx);
|
|
updateTypeDefinitions(ctx);
|
|
};
|
|
|
|
const updateViteConfig = (ctx: C3Context) => {
|
|
// As of `sv` 0.16, the adapter is configured in the Vite config rather than
|
|
// in `svelte.config.js`, so all we need to do is change the adapter import
|
|
// statement there.
|
|
const configFile = usesTypescript(ctx) ? "vite.config.ts" : "vite.config.js";
|
|
updateStatus(`Changing adapter in ${blue(configFile)}`);
|
|
|
|
transformFile(configFile, {
|
|
visitImportDeclaration: function (n) {
|
|
// importSource is the `x` in `import y from "x"`
|
|
const importSource = n.value.source;
|
|
if (importSource.value === "@sveltejs/adapter-auto") {
|
|
importSource.value = "@sveltejs/adapter-cloudflare";
|
|
}
|
|
|
|
// stop traversing this node
|
|
return false;
|
|
},
|
|
});
|
|
};
|
|
|
|
const updateTypeDefinitions = (ctx: C3Context) => {
|
|
if (!usesTypescript(ctx)) {
|
|
return;
|
|
}
|
|
|
|
updateStatus(`Updating global type definitions in ${blue("app.d.ts")}`);
|
|
|
|
const b = recast.types.builders;
|
|
|
|
transformFile("src/app.d.ts", {
|
|
visitTSModuleDeclaration(n) {
|
|
if (n.value.id.name === "App" && n.node.body) {
|
|
const moduleBlock = n.node
|
|
.body as recast.types.namedTypes.TSModuleBlock;
|
|
|
|
const platformInterface = b.tsInterfaceDeclaration(
|
|
b.identifier("Platform"),
|
|
b.tsInterfaceBody([
|
|
b.tsPropertySignature(
|
|
b.identifier("env"),
|
|
b.tsTypeAnnotation(b.tsTypeReference(b.identifier("Env")))
|
|
),
|
|
b.tsPropertySignature(
|
|
b.identifier("cf"),
|
|
b.tsTypeAnnotation(
|
|
b.tsTypeReference(b.identifier("CfProperties"))
|
|
)
|
|
),
|
|
b.tsPropertySignature(
|
|
b.identifier("ctx"),
|
|
b.tsTypeAnnotation(
|
|
b.tsTypeReference(b.identifier("ExecutionContext"))
|
|
)
|
|
),
|
|
])
|
|
);
|
|
|
|
moduleBlock.body.unshift(platformInterface);
|
|
}
|
|
|
|
this.traverse(n);
|
|
},
|
|
});
|
|
};
|
|
|
|
const typesPath = "./src/worker-configuration.d.ts";
|
|
const config: TemplateConfig = {
|
|
configVersion: 1,
|
|
id: "svelte",
|
|
frameworkCli: "sv",
|
|
displayName: "SvelteKit",
|
|
platform: "workers",
|
|
copyFiles: {
|
|
path: "./templates",
|
|
},
|
|
path: "templates/svelte/workers",
|
|
generate,
|
|
configure,
|
|
transformPackageJson: async (original: PackageJson, ctx: C3Context) => {
|
|
let scripts: Record<string, string> = {
|
|
preview: `${npm} run build && wrangler dev`,
|
|
deploy: `${npm} run build && wrangler deploy`,
|
|
};
|
|
|
|
if (usesTypescript(ctx)) {
|
|
scripts = {
|
|
...scripts,
|
|
"cf-typegen": `wrangler types ${typesPath}`,
|
|
};
|
|
}
|
|
|
|
return { scripts };
|
|
},
|
|
devScript: "dev",
|
|
deployScript: "deploy",
|
|
previewScript: "preview",
|
|
typesPath,
|
|
};
|
|
export default config;
|