@storm-software/cloudflare-tools
Version:
A Nx plugin package that contains various executors, generators, and utilities that assist in managing Cloudflare services.
85 lines (82 loc) • 2.63 kB
JavaScript
import {
createCliOptions
} from "./chunk-PH3DHY4Q.mjs";
import {
__require
} from "./chunk-B7N6WOYD.mjs";
// src/executors/cloudflare-publish/executor.ts
import { joinPathFragments } from "@nx/devkit";
import { fork } from "node:child_process";
async function runExecutor(options, context) {
const isDryRun = process.env.NX_DRY_RUN === "true" || options.dryRun || false;
if (!context.projectName) {
throw new Error("The executor requires a projectName.");
}
console.info(
`\u{1F680} Running Storm Cloudflare Publish executor on the ${context.projectName} worker`
);
if (!context.projectName || !context.projectsConfigurations?.projects || !context.projectsConfigurations.projects[context.projectName] || !context.projectsConfigurations.projects[context.projectName]?.root) {
throw new Error("The executor requires projectsConfigurations.");
}
const packageRoot = joinPathFragments(
context.root,
context.projectsConfigurations.projects[context.projectName]?.root
);
try {
const args = createCliOptions({ ...options });
if (isDryRun) {
args.push("--dry-run");
}
console.log("");
console.log(`Running "wrangler deploy ${args.join(" ")}"...`);
console.log("");
let proc;
try {
fork(__require.resolve("wrangler/bin/wrangler"), ["deploy", ...args], {
env: {
CLOUDFLARE_ACCOUNT_ID: process.env.STORM_BOT_CLOUDFLARE_ACCOUNT,
CLOUDFLARE_API_TOKEN: process.env.STORM_BOT_CLOUDFLARE_TOKEN,
WRANGLER_LOG: "debug",
...process.env,
FORCE_COLOR: "true"
},
cwd: packageRoot,
stdio: ["pipe", "pipe", "pipe", "ipc"]
});
} catch (e) {
console.error(e);
throw new Error(
"Unable to run Wrangler. Please ensure Wrangler is installed."
);
}
proc?.stdout?.on("data", (message) => {
process.stdout.write(message);
});
proc?.stderr?.on("data", (message) => {
process.stderr.write(message);
});
return new Promise((resolve) => {
proc?.on("close", (code) => {
console.log("");
if (isDryRun) {
console.log(
"Would publish to Cloudflare Workers Registry, but [dry-run] was set"
);
} else {
console.log("Published to Cloudflare Workers Registry");
}
return resolve({ success: code === 0 });
});
});
} catch (error) {
console.error("Failed to publish to Cloudflare Workers Registry");
console.error(error);
console.log("");
return {
success: false
};
}
}
export {
runExecutor
};