Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/wide-ghosts-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'astro': minor
---

Updates `astro add cloudflare` to scaffold more configuration files

Running `astro add cloudflare` will now emit `wrangler.jsonc` and `public/.assetsignore`, allowing your Astro project to work out of the box as a worker.
115 changes: 95 additions & 20 deletions packages/astro/src/cli/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ export default async function seed() {
// TODO
}
`,
CLOUDFLARE_WRANGLER_CONFIG: (name: string) => `\
{
"main": "dist/_worker.js/index.js",
"name": ${JSON.stringify(name)},
"compatibility_date": ${JSON.stringify(new Date().toISOString().slice(0,10))},
"assets": {
"binding": "ASSETS",
"directory": "./dist"
}
}`,
CLOUDFLARE_ASSETSIGNORE: `_worker.js\n_routes.json`,
};

const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record<string, string> = {
Expand Down Expand Up @@ -146,8 +157,89 @@ export async function add(names: string[], { flags }: AddOptions) {
// Append forward slash to compute relative paths
root.href = appendForwardSlash(root.href);

const rawConfigPath = await resolveConfigPath({
root: rootPath,
configFile: inlineConfig.configFile,
fs: fsMod,
});
let configURL = rawConfigPath ? pathToFileURL(rawConfigPath) : undefined;

if (configURL) {
logger.debug('add', `Found config at ${configURL}`);
} else {
logger.info('add', `Unable to locate a config file, generating one for you.`);
configURL = new URL('./astro.config.mjs', root);
await fs.writeFile(fileURLToPath(configURL), STUBS.ASTRO_CONFIG, { encoding: 'utf-8' });
}

let packageJson:
| { type: 'exists'; data: { name: string; dependencies?: any; devDependencies?: any } }
| { type: 'unknown' }
| { type: 'does-not-exist' } = { type: 'unknown' };

async function getPackageJson() {
if (packageJson.type === 'exists') {
return packageJson.data;
}

if (packageJson.type === 'does-not-exist') {
return null;
}

const pkgURL = new URL('./package.json', configURL);
if (existsSync(pkgURL)) {
packageJson = {
type: 'exists',
data: await fs.readFile(fileURLToPath(pkgURL)).then((res) => JSON.parse(res.toString())),
};
return packageJson.data;
}

packageJson = { type: 'does-not-exist' };
return null;
}

switch (installResult) {
case UpdateResult.updated: {
if (integrations.find((integration) => integration.id === 'cloudflare')) {
const wranglerConfigURL = new URL('./wrangler.jsonc', configURL);
if (!existsSync(wranglerConfigURL)) {
logger.info(
'SKIP_FORMAT',
`\n ${magenta(`Astro will scaffold ${green('./wrangler.jsonc')}.`)}\n`,
);

if (await askToContinue({ flags })) {
const data = await getPackageJson();

await fs.writeFile(
wranglerConfigURL,
STUBS.CLOUDFLARE_WRANGLER_CONFIG(data?.name ?? 'example'),
'utf-8',
);
}
} else {
logger.debug('add', 'Using existing wrangler configuration');
}

const dir = new URL(userConfig.publicDir ?? './public/', root);
const assetsignore = new URL('./.assetsignore', dir);
if (!existsSync(assetsignore)) {
logger.info(
'SKIP_FORMAT',
`\n ${magenta(`Astro will scaffold ${green('./public/.assetsignore')}.`)}\n`,
);

if (await askToContinue({ flags })) {
if (!existsSync(dir)) {
await fs.mkdir(dir);
}
await fs.writeFile(assetsignore, STUBS.CLOUDFLARE_ASSETSIGNORE, 'utf-8');
}
} else {
logger.debug('add', `Using existing .assetsignore`);
}
}
if (integrations.find((integration) => integration.id === 'tailwind')) {
const dir = new URL('./styles/', new URL(userConfig.srcDir ?? './src/', root));
const styles = new URL('./global.css', dir);
Expand Down Expand Up @@ -245,21 +337,6 @@ export async function add(names: string[], { flags }: AddOptions) {
break;
}

const rawConfigPath = await resolveConfigPath({
root: rootPath,
configFile: inlineConfig.configFile,
fs: fsMod,
});
let configURL = rawConfigPath ? pathToFileURL(rawConfigPath) : undefined;

if (configURL) {
logger.debug('add', `Found config at ${configURL}`);
} else {
logger.info('add', `Unable to locate a config file, generating one for you.`);
configURL = new URL('./astro.config.mjs', root);
await fs.writeFile(fileURLToPath(configURL), STUBS.ASTRO_CONFIG, { encoding: 'utf-8' });
}

let mod: ProxifiedModule<any> | undefined;
try {
mod = await loadFile(fileURLToPath(configURL));
Expand Down Expand Up @@ -328,11 +405,9 @@ export async function add(names: string[], { flags }: AddOptions) {
break;
}
case UpdateResult.none: {
const pkgURL = new URL('./package.json', configURL);
if (existsSync(fileURLToPath(pkgURL))) {
const { dependencies = {}, devDependencies = {} } = await fs
.readFile(fileURLToPath(pkgURL))
.then((res) => JSON.parse(res.toString()));
const data = await getPackageJson();
if (data) {
const { dependencies = {}, devDependencies = {} } = data;
const deps = Object.keys(Object.assign(dependencies, devDependencies));
const missingDeps = integrations.filter(
(integration) => !deps.includes(integration.packageName),
Expand Down
Loading