UNPKG

pricing4ts

Version:

![NPM Version](https://img.shields.io/npm/v/pricing4ts) Pricing4TS is a TypeScript-based toolkit designed to enhance the server-side functionality of a pricing-driven SaaS by enabling the seamless integration of pricing plans into the application logic. T

54 lines (53 loc) 2.69 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.retrievePricingFromPath = retrievePricingFromPath; exports.writePricingToYaml = writePricingToYaml; exports.writePricingWithErrorToYaml = writePricingWithErrorToYaml; var js_yaml_1 = __importDefault(require("js-yaml")); var fs_1 = __importDefault(require("fs")); var pricing_parser_1 = require("../../main/utils/pricing-formatting/pricing-parser"); var version_manager_1 = require("./version-manager"); var pricing_serializer_1 = require("../../main/utils/pricing-formatting/pricing-serializer"); function retrievePricingFromPath(yamlPath) { var absolutePath = fs_1.default.realpathSync(yamlPath); var fileContent; try { fileContent = fs_1.default.readFileSync(absolutePath, 'utf-8'); } catch (_error) { throw new Error("The file at path ".concat(yamlPath, " could not be found. Please check that the path is correct and that the file exists.")); } var extractedPricing = js_yaml_1.default.load(fileContent); if (extractedPricing === null || extractedPricing === undefined) { throw new Error("The file at path ".concat(yamlPath, " does not contain valid YAML content.")); } (0, version_manager_1.update)(extractedPricing, true, absolutePath); var pricing = (0, pricing_parser_1.parsePricing)(extractedPricing); return pricing; } function writePricingToYaml(pricing, yamlPath) { try { var absolutePath = fs_1.default.realpathSync(yamlPath); var pricingToBeWritten = (0, pricing_serializer_1.serializePricing)(pricing); var yamlString = js_yaml_1.default.dump(pricingToBeWritten); fs_1.default.writeFileSync(absolutePath, yamlString); } catch (_error) { console.log(_error); throw new Error("Failed to write the file at path ".concat(yamlPath, ". Please check that the path is correct and that the file exists.")); } } function writePricingWithErrorToYaml(pricing, yamlPath) { try { var absolutePath = fs_1.default.realpathSync(yamlPath); var pricingToBeWritten = (0, pricing_serializer_1.serializePricing)(pricing); var yamlString = js_yaml_1.default.dump(pricingToBeWritten); fs_1.default.writeFileSync(absolutePath, yamlString); } catch (_error) { throw new Error("Failed to write the file at path '".concat(yamlPath, "' after failing in the version update process. Please check that the path is correct and that the file exists. Received pricing: \n ").concat(pricing)); } }