@storm-software/cloudflare-tools
Version:
A Nx plugin package that contains various executors, generators, and utilities that assist in managing Cloudflare services.
270 lines (262 loc) • 7.72 kB
JavaScript
import {
generator_default
} from "./chunk-R5EXW4CZ.mjs";
import {
findWorkspaceRoot,
getConfig
} from "./chunk-JOBVIJOH.mjs";
import {
brandIcon,
getStopwatch,
writeDebug,
writeError,
writeFatal,
writeInfo,
writeTrace
} from "./chunk-OBUVLRI3.mjs";
import {
__dirname
} from "./chunk-W6EAOXRO.mjs";
// src/generators/worker/generator.ts
import {
convertNxGenerator,
ensurePackage,
formatFiles,
generateFiles,
joinPathFragments,
names,
readProjectConfiguration,
runTasksInSerial,
updateJson,
updateProjectConfiguration
} from "@nx/devkit";
import { determineProjectNameAndRootOptions } from "@nx/devkit/src/generators/project-name-and-root-utils";
import { applicationGenerator as nodeApplicationGenerator } from "@nx/node";
import { nxVersion } from "@nx/node/src/utils/versions";
import { join } from "path";
// src/generators/worker/libs/get-account-id.ts
function getAccountId(accountId) {
return `account_id = "${accountId}"`;
}
// src/generators/worker/libs/vitest-imports.ts
var vitestImports = `import { describe, expect, it, beforeAll, afterAll } from 'vitest';`;
// src/generators/worker/libs/vitest-script.ts
var vitestScript = `"test": "vitest run"`;
// src/generators/worker/generator.ts
async function applicationGenerator(tree, schema) {
const stopwatch = getStopwatch("Storm Worker generator");
let config;
try {
const workspaceRoot = findWorkspaceRoot();
config = await getConfig(workspaceRoot);
writeInfo(
`${brandIcon(config)} Running the Storm Worker generator...
`,
config
);
writeDebug(
`Loading the Storm Config from environment variables and storm.json file...
- workspaceRoot: ${workspaceRoot}`,
config
);
writeTrace(
`Loaded Storm config into env:
${Object.keys(process.env).map((key) => ` - ${key}=${JSON.stringify(process.env[key])}`).join("\n")}`,
config
);
const options = await normalizeOptions(tree, schema, config);
const tasks = [];
tasks.push(
await generator_default(tree, {
...options,
skipFormat: true
})
);
tasks.push(
await nodeApplicationGenerator(tree, {
...options,
framework: "none",
skipFormat: true,
unitTestRunner: options.unitTestRunner == "vitest" ? "none" : options.unitTestRunner,
e2eTestRunner: "none",
name: schema.name
})
);
if (options.unitTestRunner === "vitest") {
const { vitestGenerator, createOrEditViteConfig } = ensurePackage(
"@nx/vite",
nxVersion
);
const vitestTask = await vitestGenerator(tree, {
project: options.name,
uiFramework: "none",
coverageProvider: "v8",
skipFormat: true,
testEnvironment: "node"
});
tasks.push(vitestTask);
createOrEditViteConfig(
tree,
{
project: options.name,
includeLib: false,
includeVitest: true,
testEnvironment: "node"
},
true
);
}
addCloudflareFiles(tree, options);
updateTsAppConfig(tree, options);
addTargets(tree, options);
if (options.unitTestRunner === "none") {
removeTestFiles(tree, options);
}
if (!options.skipFormat) {
await formatFiles(tree);
}
if (options.template === "hono") {
tasks.push(() => {
const packageJsonPath = joinPathFragments(
options.directory ?? "",
"package.json"
);
if (tree.exists(packageJsonPath)) {
updateJson(tree, packageJsonPath, (json) => ({
...json,
dependencies: {
hono: "4.4.0",
...json?.dependencies
}
}));
}
});
}
return runTasksInSerial(...tasks);
} catch (error) {
return () => {
writeFatal(
"A fatal error occurred while running the generator - the process was forced to terminate",
config
);
writeError(
`An exception was thrown in the generator's process
- Details: ${error.message}
- Stacktrace: ${error.stack}`,
config
);
};
} finally {
stopwatch();
}
}
function updateTsAppConfig(tree, options) {
updateJson(tree, join(options.appProjectRoot, "tsconfig.app.json"), (json) => {
json.compilerOptions = {
...json.compilerOptions,
esModuleInterop: true,
target: "es2021",
lib: ["es2021"],
module: "es2022",
moduleResolution: "node",
resolveJsonModule: true,
allowJs: true,
checkJs: false,
noEmit: true,
isolatedModules: true,
allowSyntheticDefaultImports: true,
forceConsistentCasingInFileNames: true,
strict: true,
skipLibCheck: true
};
json.compilerOptions.types = [
...json.compilerOptions.types,
"@cloudflare/workers-types"
];
return json;
});
}
function addCloudflareFiles(tree, options) {
tree.delete(join(options.appProjectRoot, "src/main.ts"));
generateFiles(
tree,
join(__dirname, "./files/common"),
options.appProjectRoot,
{
...options,
tmpl: "",
name: options.name,
accountId: options.accountId ? getAccountId(options.accountId) : "",
vitestScript: options.unitTestRunner === "vitest" ? vitestScript : ""
}
);
if (options.template && options.template !== "none") {
generateFiles(
tree,
join(__dirname, `./files/${options.template}`),
join(options.appProjectRoot, "src"),
{
...options,
tmpl: "",
name: options.name,
accountId: options.accountId ? getAccountId(options.accountId) : "",
vitestScript: options.unitTestRunner === "vitest" ? vitestScript : "",
vitestImports: options.unitTestRunner === "vitest" ? vitestImports : ""
}
);
}
}
function addTargets(tree, options) {
try {
const projectConfiguration = readProjectConfiguration(tree, options.name);
projectConfiguration.targets = {
...projectConfiguration.targets ?? {},
serve: {
executor: "@storm-software/cloudflare-tools:serve",
options: {
port: options.port
}
},
"nx-release-publish": {
executor: "@storm-software/cloudflare-tools:cloudflare-publish"
}
};
if (projectConfiguration.targets.build) {
delete projectConfiguration.targets.build;
}
updateProjectConfiguration(tree, options.name, projectConfiguration);
} catch (e) {
console.error(e);
}
}
function removeTestFiles(tree, options) {
tree.delete(join(options.appProjectRoot, "src", "index.test.ts"));
}
async function normalizeOptions(host, options, config) {
const { projectName: appProjectName, projectRoot: appProjectRoot } = await determineProjectNameAndRootOptions(host, {
name: options.name,
projectType: "application",
directory: options.directory,
rootProject: options.rootProject
});
options.rootProject = appProjectRoot === ".";
return {
addPlugin: process.env.NX_ADD_PLUGINS !== "false",
accountId: process.env.STORM_BOT_CLOUDFLARE_ACCOUNT,
...options,
name: names(appProjectName).fileName,
frontendProject: options.frontendProject ? names(options.frontendProject).fileName : void 0,
appProjectRoot,
unitTestRunner: options.unitTestRunner ?? "vitest",
rootProject: options.rootProject ?? false,
template: options.template ?? "fetch-handler",
port: options.port ?? 3e3
};
}
var generator_default2 = applicationGenerator;
var applicationSchematic = convertNxGenerator(applicationGenerator);
export {
applicationGenerator,
generator_default2 as generator_default,
applicationSchematic
};