@kadena/kadena-cli
Version:
Kadena CLI tool to interact with the Kadena blockchain (manage keys, transactions, etc.)
118 lines • 5.44 kB
JavaScript
import { getNetworkFiles, networkDefaults, } from '../../../constants/networks.js';
import { formatZodError, mergeConfigs, notEmpty, sanitizeFilename, } from '../../../utils/globalHelpers.js';
import { getDefaultNetworkName } from '../../../utils/helpers.js';
import yaml from 'js-yaml';
import path from 'path';
import { z } from 'zod';
import { services } from '../../../services/index.js';
import { getNetworkDirectory, getNetworksSettingsFilePath, } from './networkPath.js';
const networkSchema = z.object({
network: z.string(),
networkId: z.string(),
networkHost: z.string().url(),
networkExplorerUrl: z.string().optional(),
});
/**
* Writes the given network setting to the networks folder
*
* @param {TNetworksCreateOptions} options - The set of configuration options.
* @param {string} options.network - The network (e.g., 'mainnet', 'testnet') or custom network.
* @param {string} options.networkId - The ID representing the network.
* @param {string} options.networkHost - The hostname for the network.
* @param {string} options.networkExplorerUrl - The URL for the network explorer.
* @returns {void} - No return value; the function writes directly to a file.
*/
export async function writeNetworks(kadenaDir, options) {
const { network } = options;
const sanitizedNetwork = sanitizeFilename(network).toLowerCase();
const networkFilePath = path.join(kadenaDir, 'networks', `${sanitizedNetwork}.yaml`);
const validation = networkSchema.safeParse(options);
if (!validation.success) {
throw new Error(`Failed to write network config: ${formatZodError(validation.error)}`);
}
await services.filesystem.ensureDirectoryExists(path.dirname(networkFilePath));
await services.filesystem.writeFile(networkFilePath, yaml.dump(options));
}
/**
* Removes the given network setting from the networks folder
*
* @param {Pick<INetworkCreateOptions, 'network'>} options - The set of configuration options.
* @param {string} options.network - The network (e.g., 'mainnet', 'testnet') or custom network.
*/
export async function removeNetwork(options) {
const networkDir = getNetworkDirectory();
const { network } = options;
const sanitizedNetwork = sanitizeFilename(network).toLowerCase();
const networkFilePath = path.join(networkDir, `${sanitizedNetwork}.yaml`);
await services.filesystem.deleteFile(networkFilePath);
}
export async function removeDefaultNetwork() {
const defaultNetworksSettingsFilePath = getNetworksSettingsFilePath();
if (await services.filesystem.fileExists(defaultNetworksSettingsFilePath)) {
await services.filesystem.deleteFile(defaultNetworksSettingsFilePath);
}
}
export async function loadNetworkConfig(network) {
const networkDir = getNetworkDirectory();
const networkFilePath = path.join(networkDir, `${network}.yaml`);
const file = await services.filesystem.readFile(networkFilePath);
if (file === null) {
throw new Error('Network configuration file not found.');
}
return yaml.load(file);
}
export async function getNetworks() {
const networkDir = getNetworkDirectory();
const files = await services.filesystem.readDir(networkDir);
const networks = (await Promise.all(files.map(async (file) => {
const networkFilePath = path.join(networkDir, file);
const content = await services.filesystem.readFile(networkFilePath);
if (content === null)
return null;
const parsed = networkSchema.safeParse(yaml.load(content));
return parsed.success ? parsed.data : null;
}))).filter(notEmpty);
return networks;
}
export async function ensureNetworksConfiguration(kadenaDir) {
await services.filesystem.ensureDirectoryExists(path.join(kadenaDir, 'networks'));
const networkFiles = getNetworkFiles(kadenaDir);
for (const [network, filePath] of Object.entries(networkFiles)) {
if (!(await services.filesystem.fileExists(filePath))) {
await writeNetworks(kadenaDir, networkDefaults[network]);
}
}
}
export async function getNetworksInOrder(networks) {
const defaultNetworkName = await getDefaultNetworkName();
const partitionedNetworks = networks.reduce((acc, obj) => {
var _a;
const networkKey = (_a = obj.network) !== null && _a !== void 0 ? _a : obj.name;
if (networkKey === defaultNetworkName) {
acc.defaultNetworks.push(obj);
}
else {
acc.remainingNetworks.push(obj);
}
return acc;
}, { defaultNetworks: [], remainingNetworks: [] });
return [
...partitionedNetworks.defaultNetworks,
...partitionedNetworks.remainingNetworks,
];
}
export async function mergeNetworkConfig(kadenaDir, network, options) {
const sanitizedNetwork = sanitizeFilename(network).toLowerCase();
const networkFilePath = path.join(kadenaDir, 'networks', `${sanitizedNetwork}.yaml`);
let existingConfig = typeof networkDefaults[network] !== 'undefined'
? { ...networkDefaults[network] }
: { ...networkDefaults.other };
if (await services.filesystem.fileExists(networkFilePath)) {
const content = await services.filesystem.readFile(networkFilePath);
if (content !== null) {
existingConfig = yaml.load(content);
}
}
return mergeConfigs(existingConfig, options);
}
//# sourceMappingURL=networkHelpers.js.map