UNPKG

@ohos/hpm-cli

Version:

CLI for HarmonyOS package manager

160 lines (159 loc) 7.13 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); } 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 fs = require('fs'); var path = require('path'); var _require = require('../constant/constant'), DEFAULT_CONFIG_DIR = _require.DEFAULT_CONFIG_DIR; var _require2 = require('../utils/helpers'), runShellCmd = _require2.runShellCmd; var _json = /*#__PURE__*/new WeakMap(); var Preference = /*#__PURE__*/function () { function Preference() { _classCallCheck(this, Preference); _classPrivateFieldInitSpec(this, _json, void 0); if (!fs.existsSync(DEFAULT_CONFIG_DIR)) { runShellCmd(function (shell) { return shell.mkdir('-p', DEFAULT_CONFIG_DIR); }); } this.path = path.join(DEFAULT_CONFIG_DIR, 'preference.json'); if (!fs.existsSync(this.path)) { fs.writeFileSync(this.path, JSON.stringify({ recentProject: [], disabledPlugins: [], darkMode: true, lang: 'en' })); } _classPrivateFieldSet(_json, this, this.readJsonFromFile()); if (!(_classPrivateFieldGet(_json, this)['recentProject'] && _classPrivateFieldGet(_json, this)['disabledPlugins'] && _classPrivateFieldGet(_json, this)['lang'] && _classPrivateFieldGet(_json, this)['darkMode'] !== undefined)) { _classPrivateFieldGet(_json, this)['recentProject'] = _classPrivateFieldGet(_json, this)['recentProject'] || []; _classPrivateFieldGet(_json, this)['disabledPlugins'] = _classPrivateFieldGet(_json, this)['disabledPlugins'] || []; _classPrivateFieldGet(_json, this)['lang'] = _classPrivateFieldGet(_json, this)['lang'] || 'en'; _classPrivateFieldGet(_json, this)['darkMode'] = _classPrivateFieldGet(_json, this)['darkMode'] || true; this.save(); } } return _createClass(Preference, [{ key: "readJsonFromFile", value: function readJsonFromFile() { return JSON.parse(fs.readFileSync(this.path).toString()); } }, { key: "set", value: function set(key, value, save) { _classPrivateFieldGet(_json, this)[key] = value; save && this.save(); } }, { key: "get", value: function get(key) { return _classPrivateFieldGet(_json, this)[key]; } }, { key: "save", value: function save() { fs.writeFileSync(this.path, JSON.stringify(_classPrivateFieldGet(_json, this), null, 4)); } }, { key: "getRecentProject", value: function getRecentProject() { return _classPrivateFieldGet(_json, this).recentProject; } }, { key: "setRecentProject", value: function setRecentProject(name, projectPath, isDelete) { var recentProject = _classPrivateFieldGet(_json, this).recentProject || []; recentProject = recentProject.filter(function (p) { return p.path !== projectPath; }); if (isDelete) { recentProject = recentProject.filter(function (p) { return p.path !== projectPath; }); } else { recentProject.unshift({ name: name, path: projectPath, time: new Date().toLocaleString(undefined, { hour12: false }) }); } if (recentProject.length > 10) { recentProject.pop(); } this.set('recentProject', recentProject); this.save(); } }, { key: "addDisabledPlugin", value: function addDisabledPlugin(name) { var disabledPlugins = _classPrivateFieldGet(_json, this).disabledPlugins || []; disabledPlugins = disabledPlugins.filter(function (pluginName) { return pluginName !== name; }); disabledPlugins.push(name); this.set('disabledPlugins', disabledPlugins); this.save(); } }, { key: "removeDisabledPlugin", value: function removeDisabledPlugin(name) { var disabledPlugins = _classPrivateFieldGet(_json, this).disabledPlugins || []; disabledPlugins = disabledPlugins.filter(function (pluginName) { return pluginName !== name; }); this.set('disabledPlugins', disabledPlugins); this.save(); } }, { key: "getDisabledPlugins", value: function getDisabledPlugins() { return _classPrivateFieldGet(_json, this).disabledPlugins; } }, { key: "getThemeOrLang", value: function getThemeOrLang(key) { var keys = ['darkMode', 'lang']; if (keys.includes(key)) { return _classPrivateFieldGet(_json, this)[key]; } } }, { key: "setThemeOrLang", value: function setThemeOrLang(key, value) { var keys = ['darkMode', 'lang']; if (keys.includes(key)) { this.set(key, value); this.save(); } } }]); }(); var preference = new Preference(); module.exports = preference;