@storm-software/cloudflare-tools
Version:
A Nx plugin package that contains various executors, generators, and utilities that assist in managing Cloudflare services.
267 lines (232 loc) • 9.04 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
var _chunkLSDIMDO6js = require('./chunk-LSDIMDO6.js');
var _chunkWROOA5AXjs = require('./chunk-WROOA5AX.js');
var _chunkOZPXCCZBjs = require('./chunk-OZPXCCZB.js');
// src/generators/worker/generator.ts
var _devkit = require('@nx/devkit');
var _projectnameandrootutils = require('@nx/devkit/src/generators/project-name-and-root-utils');
var _node = require('@nx/node');
var _versions = require('@nx/node/src/utils/versions');
var _path = require('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 = _chunkOZPXCCZBjs.getStopwatch.call(void 0, "Storm Worker generator");
let config;
try {
const workspaceRoot = _chunkWROOA5AXjs.findWorkspaceRoot.call(void 0, );
config = await _chunkWROOA5AXjs.getConfig.call(void 0, workspaceRoot);
_chunkOZPXCCZBjs.writeInfo.call(void 0,
`${_chunkOZPXCCZBjs.brandIcon.call(void 0, config)} Running the Storm Worker generator...
`,
config
);
_chunkOZPXCCZBjs.writeDebug.call(void 0,
`Loading the Storm Config from environment variables and storm.json file...
- workspaceRoot: ${workspaceRoot}`,
config
);
_chunkOZPXCCZBjs.writeTrace.call(void 0,
`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 _chunkLSDIMDO6js.generator_default.call(void 0, tree, {
...options,
skipFormat: true
})
);
tasks.push(
await _node.applicationGenerator.call(void 0, 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 } = _devkit.ensurePackage.call(void 0,
"@nx/vite",
_versions.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 _devkit.formatFiles.call(void 0, tree);
}
if (options.template === "hono") {
tasks.push(() => {
const packageJsonPath = _devkit.joinPathFragments.call(void 0,
_nullishCoalesce(options.directory, () => ( "")),
"package.json"
);
if (tree.exists(packageJsonPath)) {
_devkit.updateJson.call(void 0, tree, packageJsonPath, (json) => ({
...json,
dependencies: {
hono: "4.4.0",
..._optionalChain([json, 'optionalAccess', _ => _.dependencies])
}
}));
}
});
}
return _devkit.runTasksInSerial.call(void 0, ...tasks);
} catch (error) {
return () => {
_chunkOZPXCCZBjs.writeFatal.call(void 0,
"A fatal error occurred while running the generator - the process was forced to terminate",
config
);
_chunkOZPXCCZBjs.writeError.call(void 0,
`An exception was thrown in the generator's process
- Details: ${error.message}
- Stacktrace: ${error.stack}`,
config
);
};
} finally {
stopwatch();
}
}
function updateTsAppConfig(tree, options) {
_devkit.updateJson.call(void 0, tree, _path.join.call(void 0, 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(_path.join.call(void 0, options.appProjectRoot, "src/main.ts"));
_devkit.generateFiles.call(void 0,
tree,
_path.join.call(void 0, __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") {
_devkit.generateFiles.call(void 0,
tree,
_path.join.call(void 0, __dirname, `./files/${options.template}`),
_path.join.call(void 0, 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 = _devkit.readProjectConfiguration.call(void 0, tree, options.name);
projectConfiguration.targets = {
..._nullishCoalesce(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;
}
_devkit.updateProjectConfiguration.call(void 0, tree, options.name, projectConfiguration);
} catch (e) {
console.error(e);
}
}
function removeTestFiles(tree, options) {
tree.delete(_path.join.call(void 0, options.appProjectRoot, "src", "index.test.ts"));
}
async function normalizeOptions(host, options, config) {
const { projectName: appProjectName, projectRoot: appProjectRoot } = await _projectnameandrootutils.determineProjectNameAndRootOptions.call(void 0, 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: _devkit.names.call(void 0, appProjectName).fileName,
frontendProject: options.frontendProject ? _devkit.names.call(void 0, options.frontendProject).fileName : void 0,
appProjectRoot,
unitTestRunner: _nullishCoalesce(options.unitTestRunner, () => ( "vitest")),
rootProject: _nullishCoalesce(options.rootProject, () => ( false)),
template: _nullishCoalesce(options.template, () => ( "fetch-handler")),
port: _nullishCoalesce(options.port, () => ( 3e3))
};
}
var generator_default2 = applicationGenerator;
var applicationSchematic = _devkit.convertNxGenerator.call(void 0, applicationGenerator);
exports.applicationGenerator = applicationGenerator; exports.generator_default = generator_default2; exports.applicationSchematic = applicationSchematic;