@storm-software/config-tools
Version:
A package containing various utilities to support custom workspace configurations and environment management for Storm Software projects, including configuration file handling, environment variable management, and logging utilities.
48 lines (38 loc) • 2.37 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 _chunkXPQXNNGZcjs = require('./chunk-XPQXNNGZ.cjs');
// src/utilities/toml.ts
var _jtoml = require('@ltd/j-toml'); var _jtoml2 = _interopRequireDefault(_jtoml);
function parseCargoTomlWithTree(tree, projectRoot, projectName) {
const cargoTomlString = _optionalChain([tree, 'access', _ => _.read, 'call', _2 => _2(`${projectRoot}/Cargo.toml`), 'optionalAccess', _3 => _3.toString, 'call', _4 => _4()]);
if (!cargoTomlString) {
_chunkXPQXNNGZcjs.writeError.call(void 0, `Cannot find a Cargo.toml file in the ${projectName}`);
throw new Error();
}
return parseCargoToml(cargoTomlString);
}
function parseCargoToml(cargoString) {
if (!cargoString) {
throw new Error("Cargo.toml is empty");
}
return _jtoml2.default.parse(cargoString, {
x: { comment: true }
});
}
function stringifyCargoToml(cargoToml) {
const tomlString = _jtoml2.default.stringify(cargoToml, {
newlineAround: "section"
});
if (Array.isArray(tomlString)) {
return tomlString.join("\n");
}
return tomlString;
}
function modifyCargoTable(toml, section, key, value) {
toml[section] ??= _jtoml2.default.Section({});
toml[section][key] = typeof value === "object" && !Array.isArray(value) ? _jtoml2.default.inline(value) : typeof value === "function" ? value() : value;
}
function modifyCargoNestedTable(toml, section, key, value) {
toml[section] ??= {};
toml[section][key] = _jtoml2.default.Section(value);
}
exports.parseCargoTomlWithTree = parseCargoTomlWithTree; exports.parseCargoToml = parseCargoToml; exports.stringifyCargoToml = stringifyCargoToml; exports.modifyCargoTable = modifyCargoTable; exports.modifyCargoNestedTable = modifyCargoNestedTable;