UNPKG

az-deployment-denoise

Version:
54 lines 2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.readFile = readFile; exports.readOperationResult = readOperationResult; exports.readConfig = readConfig; exports.readPackageInfo = readPackageInfo; const path = require("node:path"); const fs = require("node:fs"); const Either_1 = require("fp-ts/Either"); const function_1 = require("fp-ts/function"); const Either_2 = require("fp-ts/Either"); const YAML = require("yaml"); const whatif_1 = require("./types/whatif"); const config_1 = require("./types/config"); async function readFile(path) { const stream = path ? fs.createReadStream(path) : process.stdin; const chunks = []; for await (const chunk of stream) { chunks.push(chunk); } return Buffer.concat(chunks).toString('utf8'); } const getPaths = (v) => { return (0, function_1.pipe)(v, (0, Either_2.fold)((errors) => errors.map((error) => error.context.map(({ key }) => key).join('.')), () => ['no errors'])); }; async function readOperationResult(path) { const content = await readFile(path); const decoded = whatif_1.OperationResultT.decode(JSON.parse(content)); if ((0, Either_1.isLeft)(decoded)) { throw Error(`invalid what-if operation result at [${getPaths(decoded).join(', ')}]`); } return decoded.right; } async function readConfig(path) { const content = await readFile(path); let configObject; if (/\.ya?ml$/.test(path)) { configObject = YAML.parse(content); } else { configObject = JSON.parse(content); } const decoded = config_1.ConfigT.decode(configObject); if ((0, Either_1.isLeft)(decoded)) { throw Error(`invalid config at [${getPaths(decoded).join(', ')}]`); } return decoded.right; } async function readPackageInfo() { const packageJsonPath = path.join(__dirname, '..', 'package.json'); const content = await readFile(packageJsonPath); return JSON.parse(content); } //# sourceMappingURL=reader.js.map