Wrangling the wrangler
This commit is contained in:
@@ -2,10 +2,10 @@ import { existsSync, readFileSync } from 'node:fs';
|
||||
import { spawn } from 'node:child_process';
|
||||
import { basename } from 'node:path';
|
||||
|
||||
const [command, envFile, ...args] = process.argv.slice(2);
|
||||
const [command, envFile] = process.argv.slice(2);
|
||||
|
||||
if (!['build', 'deploy'].includes(command) || !envFile) {
|
||||
console.error('Usage: node scripts/cloudflare-pages.mjs <build|deploy> <env-file> [--branch name] [--project name]');
|
||||
console.error('Usage: node scripts/cloudflare-worker.mjs <build|deploy> <env-file>');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
@@ -14,20 +14,6 @@ if (!existsSync(envFile)) {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function parseArgs(values) {
|
||||
const options = {};
|
||||
|
||||
for (let index = 0; index < values.length; index += 1) {
|
||||
const value = values[index];
|
||||
if (value === '--branch' || value === '--project') {
|
||||
options[value.slice(2)] = values[index + 1];
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
function parseEnvFile(path) {
|
||||
const entries = {};
|
||||
const lines = readFileSync(path, 'utf8').split(/\r?\n/);
|
||||
@@ -57,9 +43,7 @@ function parseEnvFile(path) {
|
||||
|
||||
function applyEnv(entries) {
|
||||
for (const [key, value] of Object.entries(entries)) {
|
||||
if (process.env[key] === undefined) {
|
||||
process.env[key] = value;
|
||||
}
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,9 +70,10 @@ function run(name, commandName, commandArgs) {
|
||||
const envEntries = parseEnvFile(envFile);
|
||||
applyEnv(envEntries);
|
||||
|
||||
const options = parseArgs(args);
|
||||
const projectName = options.project ?? process.env.CF_PAGES_PROJECT_NAME;
|
||||
const branch = options.branch ?? process.env.CF_PAGES_BRANCH;
|
||||
const workerName = process.env.CF_WORKER_NAME;
|
||||
const workerRoute = process.env.CF_WORKER_ROUTE;
|
||||
const workerDomain = process.env.CF_WORKER_DOMAIN;
|
||||
const compatibilityDate = process.env.CF_WORKER_COMPATIBILITY_DATE;
|
||||
|
||||
console.log(`Using ${basename(envFile)} for ${command}.`);
|
||||
console.log(`PUBLIC_SITE_URL=${process.env.PUBLIC_SITE_URL ?? '<unset>'}`);
|
||||
@@ -97,15 +82,24 @@ console.log(`DIRECTUS_URL=${process.env.DIRECTUS_URL ?? '<unset>'}`);
|
||||
await run('Astro build', 'npm', ['run', 'build']);
|
||||
|
||||
if (command === 'deploy') {
|
||||
if (!projectName) {
|
||||
console.error('Missing CF_PAGES_PROJECT_NAME. Set it in the env file or pass --project <name>.');
|
||||
if (!workerName) {
|
||||
console.error('Missing CF_WORKER_NAME. Set it in the env file.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const wranglerArgs = ['wrangler', 'pages', 'deploy', 'dist', '--project-name', projectName];
|
||||
if (branch) {
|
||||
wranglerArgs.push('--branch', branch);
|
||||
const wranglerArgs = ['wrangler', 'deploy', 'dist', '--name', workerName];
|
||||
|
||||
if (compatibilityDate) {
|
||||
wranglerArgs.push('--compatibility-date', compatibilityDate);
|
||||
}
|
||||
|
||||
await run('Cloudflare Pages deploy', 'npx', wranglerArgs);
|
||||
if (workerRoute) {
|
||||
wranglerArgs.push('--route', workerRoute);
|
||||
}
|
||||
|
||||
if (workerDomain) {
|
||||
wranglerArgs.push('--domain', workerDomain);
|
||||
}
|
||||
|
||||
await run('Cloudflare Worker deploy', 'npx', wranglerArgs);
|
||||
}
|
||||
Reference in New Issue
Block a user