@storm-software/cloudflare-tools
Version:
A Nx plugin package that contains various executors, generators, and utilities that assist in managing Cloudflare services.
210 lines (188 loc) • 8.93 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 _chunkJ6WBZNBTjs = require('./chunk-J6WBZNBT.js');
var _chunkNU34IEWNjs = require('./chunk-NU34IEWN.js');
var _chunkYMATJZEAjs = require('./chunk-YMATJZEA.js');
var _chunkIATTWS2Ijs = require('./chunk-IATTWS2I.js');
// src/executors/r2-upload-publish/executor.ts
var _clients3 = require('@aws-sdk/client-s3');
var _devkit = require('@nx/devkit');
var _glob = require('glob');
var _child_process = require('child_process');
var _promises = require('fs/promises');
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 || !_optionalChain([context, 'access', _ => _.projectsConfigurations, 'optionalAccess', _2 => _2.projects]) || !context.projectsConfigurations.projects[context.projectName] || !_optionalChain([context, 'access', _3 => _3.projectsConfigurations, 'access', _4 => _4.projects, 'access', _5 => _5[context.projectName], 'optionalAccess', _6 => _6.root])) {
throw new Error("The executor requires projectsConfigurations.");
}
try {
const workspaceRoot = _chunkIATTWS2Ijs.findWorkspaceRoot.call(void 0, );
const config = await _chunkYMATJZEAjs.getConfig.call(void 0, workspaceRoot);
const sourceRoot = _nullishCoalesce(_optionalChain([context, 'access', _7 => _7.projectsConfigurations, 'access', _8 => _8.projects, 'access', _9 => _9[context.projectName], 'optionalAccess', _10 => _10.sourceRoot]), () => ( workspaceRoot));
const projectName = _nullishCoalesce(_optionalChain([context, 'access', _11 => _11.projectsConfigurations, 'access', _12 => _12.projects, 'access', _13 => _13[context.projectName], 'optionalAccess', _14 => _14.name]), () => ( context.projectName));
const projectDetails = _chunkNU34IEWNjs.getPackageInfo.call(void 0,
context.projectsConfigurations.projects[context.projectName]
);
if (!_optionalChain([projectDetails, 'optionalAccess', _15 => _15.content])) {
throw new Error(
`Could not find the project details for ${context.projectName}`
);
}
const args = _chunkNU34IEWNjs.createCliOptions.call(void 0, { ...options });
if (isDryRun) {
args.push("--dry-run");
}
const cloudflareAccountId = process.env.STORM_BOT_CLOUDFLARE_ACCOUNT;
if (!_optionalChain([options, 'optionalAccess', _16 => _16.registry]) && !cloudflareAccountId) {
throw new Error(
"The Storm Registry URL is not set in the Storm config. Please set either the `extensions.cyclone.registry` or `config.extensions.cyclone.accountId` property in the Storm config."
);
}
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY) {
throw new Error(
"The AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables are not set. Please set these environment variables to upload to the Cyclone Registry."
);
}
const endpoint = _optionalChain([options, 'optionalAccess', _17 => _17.registry]) ? options.registry : `https://${cloudflareAccountId}.r2.cloudflarestorage.com`;
let projectGraph;
try {
projectGraph = _devkit.readCachedProjectGraph.call(void 0, );
} catch (e) {
await _devkit.createProjectGraphAsync.call(void 0, );
projectGraph = _devkit.readCachedProjectGraph.call(void 0, );
}
if (!projectGraph) {
throw new Error(
"The executor failed because the project graph is not available. Please run the build command again."
);
}
_chunkIATTWS2Ijs.writeInfo.call(void 0,
`Publishing ${context.projectName} to the Storm Registry at ${endpoint}`
);
const s3Client = new (0, _clients3.S3)({
region: "auto",
endpoint,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
}
});
const version = _optionalChain([projectDetails, 'access', _18 => _18.content, 'optionalAccess', _19 => _19.version]);
_chunkIATTWS2Ijs.writeInfo.call(void 0, `Generated component version: ${version}`);
const files = await _glob.glob.call(void 0, _devkit.joinPathFragments.call(void 0, sourceRoot, "**/*"), {
ignore: "**/{*.stories.tsx,*.stories.ts,*.spec.tsx,*.spec.ts}"
});
const projectPath = `registry/${context.projectName}`;
const internalDependencies = await _chunkJ6WBZNBTjs.getInternalDependencies.call(void 0,
context.projectName,
projectGraph
);
const dependencies = internalDependencies.filter(
(projectNode) => !projectNode.data.tags || projectNode.data.tags.every((tag) => tag.toLowerCase() !== "component")
).reduce((ret, dep) => {
if (!ret[dep.name]) {
ret[dep.name] = "latest";
}
return ret;
}, _nullishCoalesce(projectDetails.content.dependencies, () => ( {})));
const release = _nullishCoalesce(options.tag, () => ( _child_process.execSync.call(void 0, "npm config get tag").toString().trim()));
_chunkIATTWS2Ijs.writeInfo.call(void 0, `Clearing out existing items in ${projectPath}`);
if (!isDryRun) {
const response = await s3Client.listObjects({
Bucket: options.bucketId,
Prefix: projectPath
});
if (_optionalChain([response, 'optionalAccess', _20 => _20.Contents]) && response.Contents.length > 0) {
_chunkIATTWS2Ijs.writeDebug.call(void 0,
`Deleting the following existing items from the component registry: ${response.Contents.map((item) => item.Key).join(", ")}`
);
await Promise.all(
response.Contents.map(
(item) => s3Client.deleteObjects({
Bucket: options.bucketId,
Delete: {
Objects: [
{
Key: item.Key
}
],
Quiet: false
}
})
)
);
} else {
_chunkIATTWS2Ijs.writeDebug.call(void 0,
`No existing items to delete in the component registry path ${projectPath}`
);
}
} else {
_chunkIATTWS2Ijs.writeWarning.call(void 0, "[Dry run]: skipping upload to the Cyclone Registry.");
}
const meta = {
name: context.projectName,
version,
release,
description: projectDetails.content.description,
tags: projectDetails.content.keywords,
dependencies,
devDependencies: null,
internalDependencies: internalDependencies.filter(
(projectNode) => projectNode.data.tags && projectNode.data.tags.some((tag) => tag.toLowerCase() === "component")
).map((dep) => dep.name)
};
if (projectDetails.type === "package.json") {
meta.devDependencies = projectDetails.content.devDependencies;
}
const metaJson = JSON.stringify(meta);
_chunkIATTWS2Ijs.writeInfo.call(void 0, `Generating meta.json file:
${metaJson}`);
await _chunkJ6WBZNBTjs.r2UploadFile.call(void 0,
s3Client,
options.bucketId,
projectPath,
"meta.json",
version,
metaJson,
"application/json",
isDryRun
);
await Promise.all(
files.map((file) => {
const fileName = file.replaceAll("\\", "/").replace(sourceRoot.replaceAll("\\", "/"), "");
return _promises.readFile.call(void 0, file, { encoding: "utf8" }).then(
(fileContent) => _chunkJ6WBZNBTjs.r2UploadFile.call(void 0,
s3Client,
options.bucketId,
projectPath,
fileName,
version,
fileContent,
"text/plain",
isDryRun
)
);
})
);
_chunkIATTWS2Ijs.writeSuccess.call(void 0,
`Successfully uploaded the ${projectName} component to the Cyclone Registry`,
config
);
return {
success: true
};
} catch (error) {
console.error("Failed to publish to Cloudflare Workers Registry");
console.error(error);
console.log("");
return {
success: false
};
}
}
exports.runExecutor = runExecutor;