UNPKG

@ohos/hpm-cli

Version:

CLI for HarmonyOS package manager

143 lines (142 loc) 6.31 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _fs = _interopRequireDefault(require("fs")); var _os = _interopRequireDefault(require("os")); var _i18n = require("../i18n"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); } function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); } function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); } function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; } function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); } /* * Copyright (c) 2020-2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var _path = /*#__PURE__*/new WeakMap(); var _contentList = /*#__PURE__*/new WeakMap(); var _config = /*#__PURE__*/new WeakMap(); var RcParser = exports["default"] = /*#__PURE__*/function () { function RcParser(filePath) { _classCallCheck(this, RcParser); _classPrivateFieldInitSpec(this, _path, void 0); _classPrivateFieldInitSpec(this, _contentList, []); _classPrivateFieldInitSpec(this, _config, {}); _classPrivateFieldSet(_path, this, filePath); } return _createClass(RcParser, [{ key: "parse", value: function parse() { var _this = this; if (!_fs["default"].existsSync(_classPrivateFieldGet(_path, this))) { throw (0, _i18n.getI18nMessage)('parser.noFile'); } var content = _fs["default"].readFileSync(_classPrivateFieldGet(_path, this), 'utf-8'); var lines = content.split(_os["default"].EOL); lines.forEach(function (line) { if (line.startsWith('#')) { _classPrivateFieldGet(_contentList, _this).push(line); } else { var index = line.indexOf('='); if (index >= 0) { var key = line.substr(0, index).trim(); var value = line.substr(index + 1).trim(); _classPrivateFieldGet(_contentList, _this).push(key); _classPrivateFieldGet(_config, _this)[key] = value; } } }); } }, { key: "get", value: function get(key) { return _classPrivateFieldGet(_config, this)[key]; } }, { key: "set", value: function set(key, value) { _classPrivateFieldGet(_config, this)[key] = value; if (!_classPrivateFieldGet(_contentList, this).includes(key)) { _classPrivateFieldGet(_contentList, this).push(key); } } }, { key: "delete", value: function _delete(key) { delete _classPrivateFieldGet(_config, this)[key]; var index = _classPrivateFieldGet(_contentList, this).indexOf(key); if (index >= 0) { _classPrivateFieldGet(_contentList, this).splice(index, 1); } } }, { key: "setJson", value: function setJson(json) { var _this2 = this; if (!json) { throw (0, _i18n.getI18nMessage)('parser.invalidFormat'); } Object.keys(json).forEach(function (key) { _this2.set(key, json[key]); }); } }, { key: "toJson", value: function toJson() { return _classPrivateFieldGet(_config, this); } }, { key: "toString", value: function toString() { var _this3 = this; var content = _classPrivateFieldGet(_contentList, this).map(function (c) { if (!c.startsWith('#')) { if (_classPrivateFieldGet(_config, _this3)[c] !== undefined) { return "".concat(c, "=").concat(_classPrivateFieldGet(_config, _this3)[c]); } return ''; } return c; }); return content.join(_os["default"].EOL); } }, { key: "save", value: function save() { _fs["default"].writeFileSync(_classPrivateFieldGet(_path, this), this.toString()); } }]); }(); RcParser.fromJson = function (json, filePath) { var parser = new RcParser(filePath); parser.setJson(json); return parser; }; RcParser.fromPath = function (filePath) { var parser = new RcParser(filePath); parser.parse(); return parser; }; RcParser.saveJsonToFile = function (json, filePath) { var parser = RcParser.fromJson(json, filePath); parser.save(); return parser; };