@salesforce/core
Version:
Core libraries to interact with SFDX projects, orgs, and APIs.
130 lines • 4.77 kB
JavaScript
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Global = exports.Mode = void 0;
const fs = __importStar(require("node:fs"));
const os = __importStar(require("node:os"));
const path = __importStar(require("node:path"));
const kit_1 = require("@salesforce/kit");
const sfError_1 = require("./sfError");
/**
* Represents an environment mode. Supports `production`, `development`, `demo`, and `test`
* with the default mode being `production`.
*
* To set the mode, `export SFDX_ENV=<mode>` in your current environment.
*/
var Mode;
(function (Mode) {
Mode["PRODUCTION"] = "production";
Mode["DEVELOPMENT"] = "development";
Mode["DEMO"] = "demo";
Mode["TEST"] = "test";
})(Mode || (exports.Mode = Mode = {}));
/**
* Global constants, methods, and configuration.
*/
class Global {
/**
* Enable interoperability between `.sfdx` and `.sf`.
*
* When @salesforce/core@v2 is deprecated and no longer used, this can be removed.
*/
static SFDX_INTEROPERABILITY = kit_1.env.getBoolean('SF_SFDX_INTEROPERABILITY', true);
/**
* The global folder in which sfdx state is stored.
*/
static SFDX_STATE_FOLDER = '.sfdx';
/**
* The global folder in which sf state is stored.
*/
static SF_STATE_FOLDER = '.sf';
/**
* The preferred global folder in which state is stored.
*/
static STATE_FOLDER = Global.SFDX_STATE_FOLDER;
/**
* The full system path to the global sfdx state folder.
*
* **See** {@link Global.SFDX_STATE_FOLDER}
*/
static get SFDX_DIR() {
return path.join(os.homedir(), Global.SFDX_STATE_FOLDER);
}
/**
* The full system path to the global sf state folder.
*
* **See** {@link Global.SF_STATE_FOLDER}
*/
static get SF_DIR() {
return path.join(os.homedir(), Global.SF_STATE_FOLDER);
}
/**
* The full system path to the preferred global state folder
*/
static get DIR() {
return path.join(os.homedir(), Global.SFDX_STATE_FOLDER);
}
/**
* Gets the current mode environment variable as a {@link Mode} instance.
*
* ```
* console.log(Global.getEnvironmentMode() === Mode.PRODUCTION);
* ```
*/
static getEnvironmentMode() {
const envValue = kit_1.env.getString('SF_ENV') ?? kit_1.env.getString('SFDX_ENV', Mode.PRODUCTION);
return envValue in Mode || envValue.toUpperCase() in Mode
? Mode[envValue.toUpperCase()]
: Mode.PRODUCTION;
}
/**
* Creates a directory within {@link Global.SFDX_DIR}, or {@link Global.SFDX_DIR} itself if the `dirPath` param
* is not provided. This is resolved or rejected when the directory creation operation has completed.
*
* @param dirPath The directory path to be created within {@link Global.SFDX_DIR}.
*/
static async createDir(dirPath) {
const resolvedPath = dirPath ? path.join(Global.SFDX_DIR, dirPath) : Global.SFDX_DIR;
try {
if (process.platform === 'win32') {
await fs.promises.mkdir(resolvedPath, { recursive: true });
}
else {
await fs.promises.mkdir(resolvedPath, { recursive: true, mode: 0o700 });
}
}
catch (error) {
throw new sfError_1.SfError(`Failed to create directory or set permissions for: ${resolvedPath}`);
}
}
}
exports.Global = Global;
//# sourceMappingURL=global.js.map
;