@ohos/hpm-cli
Version:
CLI for HarmonyOS package manager
182 lines (181 loc) • 9.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.cacheExpiration = cacheExpiration;
exports.getRootBundlePath = exports.getGlobalRepo = exports.getCurrentPathLength = exports.getConfig = void 0;
exports.httpProxyDecrypt = httpProxyDecrypt;
exports.ignoreLicenseCheck = ignoreLicenseCheck;
exports.initConfig = void 0;
exports.isPrivateSupport = isPrivateSupport;
exports.isStrictSsl = isStrictSsl;
exports.publishGate = publishGate;
exports.restoreCodeSegment = restoreCodeSegment;
var _fs = _interopRequireDefault(require("fs"));
var _path = _interopRequireDefault(require("path"));
var _rcParser2 = _interopRequireDefault(require("../core/rc-parser"));
var _helpers = require("./helpers");
var _constant = require("../constant/constant");
var _config = _interopRequireDefault(require("../config.json"));
var _i18n = _interopRequireWildcard(require("../i18n"));
var _crypto = _interopRequireDefault(require("../core/crypto"));
var _log = _interopRequireDefault(require("../core/log"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, 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); } /*
* 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 defaultConfig = _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({
registry: _config["default"].registry,
login: _config["default"].login,
globalRepo: _constant.DEFAULT_GLOBAL_DIR,
strictSsl: _config["default"].strictSsl,
publishGate: false
}, (0, _i18n.getI18nMessage)('config.https_proxy_note'), ''), (0, _i18n.getI18nMessage)('config.http_proxy_note'), ''), "restoreCodeSegment", true), "privateSupport", false), "ignoreLicenseCheck", false), "OSPlatform", 'Auto'), "batchDownloadLimit", 4), "ignoreBundles", ''), "modelRepository", _constant.DEFAULT_CONFIG_DIR), 'cache-expiration', _config["default"]['cache-expiration']);
if ((0, _helpers.isWindows)()) {
defaultConfig.shellPath = process.env.COMSPEC;
} else {
defaultConfig.shellPath = _config["default"].unixShell;
}
var initConfig = exports.initConfig = function initConfig() {
if (!_fs["default"].existsSync(_constant.DEFAULT_CONFIG_DIR)) {
(0, _helpers.runShellCmd)(function (shell) {
return shell.mkdir('-p', _constant.DEFAULT_CONFIG_DIR);
});
}
var configPath = _constant.DEFAULT_CONFIG_FILE;
var config = null;
// init hpmrc
if (!_fs["default"].existsSync(configPath)) {
var rcParser = _rcParser2["default"].saveJsonToFile(defaultConfig, configPath);
config = rcParser.toJson();
} else {
var _rcParser = _rcParser2["default"].fromPath(configPath);
var json = _rcParser.toJson();
config = _objectSpread(_objectSpread({}, defaultConfig), json);
if (config.shellPath && !_fs["default"].existsSync(config.shellPath)) {
_i18n["default"].log('config.shellPathNotFound', {
shellPath: config.shellPath
});
_i18n["default"].log('common.modifyRc', {
filePath: _constant.DEFAULT_CONFIG_FILE
});
process.exit(-1);
}
}
// init proxy key pairs
var publicPath = _path["default"].resolve(_constant.DEFAULT_CONFIG_DIR, 'key', 'publicKey_proxy.pem');
var privatePath = _path["default"].resolve(_constant.DEFAULT_CONFIG_DIR, 'key', 'privateKey_proxy.pem');
if (!_fs["default"].existsSync(publicPath) && !_fs["default"].existsSync(privatePath)) {
_crypto["default"].generateKeyPair({
user: 'proxy',
isLog: false,
isAesGcm: true
});
}
// init global bundle json
if (!_fs["default"].existsSync(config.globalRepo)) {
(0, _helpers.runShellCmd)(function (shell) {
return shell.mkdir('-p', config.globalRepo);
});
_fs["default"].writeFileSync(_path["default"].join(config.globalRepo, 'bundle.json'), JSON.stringify({
name: 'global',
version: '0.0.1'
}, null, 4));
}
// write config to process.env
for (var key in config) {
if (config[key]) {
if (key.toLowerCase() === 'path') {
var pathArr = config[key].split(_path["default"].delimiter).concat(process.env.path.split(_path["default"].delimiter));
process.env.path = pathArr.join(_path["default"].delimiter);
} else {
if (['http_proxy', 'https_proxy'].includes(key)) {
try {
process.env[key] = _crypto["default"].proxyDecrypt(config[key]);
} catch (error) {
_log["default"].warnConsole((0, _i18n.getI18nMessage)('config.proxyDecryptedError'));
}
} else {
process.env[key] = config[key];
}
}
}
}
};
var getConfig = exports.getConfig = function getConfig() {
var configPath = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _constant.DEFAULT_CONFIG_FILE;
if (_fs["default"].existsSync(configPath)) {
var rcParser = _rcParser2["default"].fromPath(configPath);
var config = rcParser.toJson();
return config;
}
return defaultConfig;
};
var getGlobalRepo = exports.getGlobalRepo = function getGlobalRepo() {
return _path["default"].join(process.env.globalRepo, _constant.DEP_FOLDER_NAME);
};
var getRootBundlePath = exports.getRootBundlePath = function getRootBundlePath(isGlobal) {
return isGlobal ? process.env.globalRepo : process.cwd();
};
var getCurrentPathLength = exports.getCurrentPathLength = function getCurrentPathLength() {
return _fs["default"].readdirSync(process.cwd()).length;
};
var toBoolean = function toBoolean(value, defaultVal) {
if (typeof value === 'string') {
return value.trim().toLowerCase() === 'true';
}
return defaultVal;
};
function isStrictSsl() {
return toBoolean(process.env.strictSsl, false);
}
function isPrivateSupport() {
return toBoolean(process.env.privateSupport, false);
}
function restoreCodeSegment() {
return toBoolean(process.env.restoreCodeSegment, true);
}
function ignoreLicenseCheck() {
return toBoolean(process.env.ignoreLicenseCheck, false);
}
function cacheExpiration() {
var cacheTime = parseInt(process.env['cache-expiration']);
if (!!cacheTime || cacheTime < 0) {
return _config["default"]['cache-expiration'] * 1000;
}
return cacheTime * 1000;
}
function httpProxyDecrypt() {
var proxy = {
https_proxy: '',
http_proxy: ''
};
if (process.env.https_proxy) {
proxy.https_proxy = process.env.https_proxy;
}
if (process.env.http_proxy) {
proxy.http_proxy = process.env.http_proxy;
}
return proxy;
}
function publishGate() {
return toBoolean(process.env.publishGate, false);
}