zephyr-agent
Version:
Zephyr plugin agent
82 lines (81 loc) • 3.6 kB
JavaScript
import "node:module";
import { createApplicationUid, flatCreateSnapshotId } from "zephyr-edge-contract";
import { applyBaseHrefToAssets, applyBaseHrefToPath, normalizeBasePath } from "./ze-basehref-handler.mjs";
import { ZeErrors, ZephyrError } from "../errors/index.mjs";
import { getZephyrAgentVersion } from "../version/zephyr-agent-version.mjs";
import { posix, win32 } from "node:path";
async function createSnapshot(zephyr_engine, { mfConfig, mfConfigs, assets, snapshotType, entrypoint }) {
const buildId = await zephyr_engine.build_id;
if (!buildId) {
await zephyr_engine.build_id;
throw new ZephyrError(ZeErrors.ERR_DEPLOY_LOCAL_BUILD, {
message: 'Cannot create snapshot before getting buildId'
});
}
const options = {
git_branch: zephyr_engine.gitProperties.git.branch,
buildId,
username: (await zephyr_engine.application_configuration).username,
email: (await zephyr_engine.application_configuration).email,
applicationProperties: zephyr_engine.applicationProperties,
edge_url: (await zephyr_engine.application_configuration).EDGE_URL,
gitProperties: zephyr_engine.gitProperties,
mfConfig: mfConfig,
mfConfigs: mfConfigs
};
const version_postfix = zephyr_engine.env.isCI ? `${options.git_branch}.${options.buildId}` : `${options.username}.${options.buildId}`;
const basedAssets = applyBaseHrefToAssets(assets, zephyr_engine.buildProperties.baseHref);
const normalizedBaseHref = normalizeBasePath(zephyr_engine.buildProperties.baseHref);
const basedEntrypoint = entrypoint && ('ssr' === snapshotType || zephyr_engine.env.ssr) ? applyBaseHrefToPath(entrypoint, normalizedBaseHref) : entrypoint;
const snapshot = {
application_uid: createApplicationUid(options.applicationProperties),
version: `${options.applicationProperties.version}-${version_postfix}`,
snapshot_id: flatCreateSnapshotId(Object.assign({}, options.applicationProperties, {
buildId: options.buildId,
username: options.username
})),
domain: options.edge_url,
target: zephyr_engine.env.target ?? 'web',
uid: {
build: options.buildId,
app_name: options.applicationProperties.name,
repo: options.applicationProperties.project,
org: options.applicationProperties.org
},
git: options.gitProperties.git,
creator: {
name: options.username,
email: options.email
},
createdAt: Date.now(),
mfConfig: options.mfConfig,
mfConfigs: options.mfConfigs,
builder: zephyr_engine.builder,
plugin_version: getZephyrAgentVersion(),
assets: Object.keys(basedAssets).reduce((memo, hash)=>{
const asset = basedAssets[hash];
const { path, extname, size } = asset;
const normalizedPath = normalizePathSeparators(path);
memo[normalizedPath] = {
path: normalizedPath,
extname,
hash: asset.hash,
size
};
return memo;
}, {}),
...snapshotType && {
type: snapshotType
},
...basedEntrypoint && {
entrypoint: basedEntrypoint
}
};
if (zephyr_engine.env.ssr) snapshot.type = 'ssr';
return snapshot;
}
function normalizePathSeparators(path) {
return path.split(win32.sep).join(posix.sep);
}
export { createSnapshot };
//# sourceMappingURL=ze-build-snapshot.mjs.map