UNPKG

@kui-shell/core

Version:

An Electron-based shell for cloud-native development

62 lines (60 loc) 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.expandHomeDir = exports.default = exports.cwd = void 0; exports.fallbackCWD = fallbackCWD; var _path = require("path"); var _os = require("os"); var _capabilities = require("../core/capabilities"); /* * Copyright 2019 The Kubernetes Authors * * 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. */ const _homedir = (0, _os.homedir)(); const expandHomeDir = function (path) { const homedir = _homedir === '/' && process.env.HOME || _homedir; if (!path || !path.slice) { return path; } else if (path === '~') { return homedir; } else if (path.slice(0, 2) !== '~/' && path.slice(0, 2) !== '~\\') { return path; } else { return (0, _path.join)(homedir, path.slice(2)); } }; exports.expandHomeDir = expandHomeDir; var _default = expandHomeDir; /** In case of error, e.g. removed CWD, this is our fallback plan */ exports.default = _default; function fallbackCWD(cwd) { if (cwd) { return (0, _path.dirname)(cwd); } else { return process.env.HOME || _homedir || '/'; } } const cwd = () => { try { return process.env.VIRTUAL_CWD || ((0, _capabilities.inBrowser)() ? process.env.PWD || process.env.HOME || '/' : process.cwd().slice(0)); } catch (err) { // it could be that the underlying directory disappeared // see https://github.com/kubernetes-sigs/kui/issues/8160 console.error('Using fallback plan due to error', err); // fallback plan: return fallbackCWD(); } }; exports.cwd = cwd;