@copado/copado-cli
Version:
Copado Developer CLI
294 lines • 10.8 kB
JavaScript
;
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.CopadoFiles = exports.FILE_DIR_PATHS = exports.DEFAULT_FORCEIGNORE = exports.SFDC_API_VERSION = exports.COPADO_NAMESPACE = exports.DOT_SFDX_DIR = void 0;
const core_1 = require("@salesforce/core");
const internal_1 = require("@salesforce/core/lib/util/internal");
const fse = __importStar(require("fs-extra"));
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const readline = __importStar(require("readline"));
// tslint:disable-next-line:no-var-requires
require('dotenv').config({ path: path.resolve(__dirname, '../../.env') });
const LOCAL_DIR = process.cwd();
const GLOBAL_DIR = os.homedir();
const DOT_COPADO_DIR = '.copado';
const FORCEIGNORE_FILE = '.forceignore';
const COPADO_CONFIG_FILE = 'copado-config.json';
const EXTENSION_CONFIG_FILE = 'CliConfiguration.json';
exports.DOT_SFDX_DIR = '.sfdx';
exports.COPADO_NAMESPACE = process.env.COPADO_NAMESPACE;
exports.SFDC_API_VERSION = process.env.SFDC_API_VERSION;
exports.DEFAULT_FORCEIGNORE = ['**/jsconfig.json', '**/.eslintrc.json', '**/__tests__/**', 'package.xml', '**/*-meta.xml'];
exports.FILE_DIR_PATHS = {
LOCAL_DIR,
GLOBAL_DIR,
DOT_COPADO_DIR,
COPADO_CONFIG_FILE,
EXTENSION_CONFIG_FILE
};
core_1.Messages.importMessagesDirectory(__dirname);
const messages = core_1.Messages.loadMessages('@copado/copado-cli', 'copado');
/**
* Main class for filesystem methods. Here are all method related to file handling.
* @export
* @class CopadoFiles
*/
class CopadoFiles {
static async deleteFromUserConfig(copadousername, propertyKey) {
const userConfig = await this.getUserConfig(copadousername);
userConfig.unset(propertyKey);
(await this.getUserConfig(copadousername)).writeSync(userConfig.getContents());
return userConfig;
}
/**
* Get an ConfigFile instance of: copado-config.json or copado-cli.log
* Supported files are: copado-config.json or copado-cli.log
* @static
* @param {string} folder
* @returns instance of {ConfigFile<ConfigFile.Options>}
* @memberof CopadoFiles
*/
static async getCopadoConfigFile(folder) {
const configObj = {
rootFolder: folder,
filename: COPADO_CONFIG_FILE,
isGlobal: false,
isState: false,
filePath: DOT_COPADO_DIR
};
return core_1.ConfigFile.create(configObj);
}
/**
* Create or update Config file.
* @static
* @param {ConfigFile<ConfigFile.Options>} copadoConfig
* @param {Map<string,string>} valuesMap
* @memberof CopadoFiles
*/
static async setInConfigFile(copadoConfig, valuesMap) {
valuesMap.forEach((value, key) => {
copadoConfig.set(key, value);
});
await copadoConfig.write();
}
static async setInGroup(copadoConfig, valuesMap, group) {
if (group && copadoConfig.get(group)) {
const groupValues = copadoConfig.get(group);
valuesMap.forEach((value, key) => {
groupValues[key] = value;
});
copadoConfig.set(group, groupValues);
}
else {
copadoConfig.set(group, Object.fromEntries(valuesMap));
}
await copadoConfig.write();
}
/**
* Get an ConfigGroup instance of given file at global directory
* @static
* @param {string} username
* @returns instance of {ConfigGroup<ConfigGroup.Options>}
* @memberof CopadoFiles
*/
static async getUserConfig(username) {
if (!username) {
username = await this.getCurrentUser();
}
if (!username && !await this.verifyFilesExists(username)) {
throw new core_1.SfError(messages.getMessage('copadoUserNotSet'));
}
const configObj = {
rootFolder: GLOBAL_DIR,
filename: `${username}.json`,
isGlobal: false,
isState: false,
filePath: DOT_COPADO_DIR
};
return core_1.ConfigFile.create(configObj);
}
/**
* Get an ConfigGroup instance of config file at local directory
* @static
* @param {string} defaultGroup
* @returns instance of {ConfigGroup<ConfigGroup.Options>}
* @memberof CopadoFiles
*/
static async getLocalConfig() {
const configObj = {
rootFolder: LOCAL_DIR,
filename: COPADO_CONFIG_FILE,
isGlobal: false,
isState: false,
filePath: DOT_COPADO_DIR
};
return core_1.ConfigFile.create(configObj);
}
/**
* Sets current user to 'copado-config.json' at global file
* @static
* @param {string} copadousername
* @memberof CopadoFiles
*/
static async setCurrentUser(copadousername) {
const copadoConfig = await this.getCopadoConfigFile(GLOBAL_DIR);
await this.setInConfigFile(copadoConfig, new Map([['currentuser', copadousername]]));
}
/**
* Gets current user from global file 'copado-config.json'
* @static
* @returns
* @memberof CopadoFiles
*/
static async getCurrentUser() {
return this.getCopadoConfigFile(GLOBAL_DIR).then(content => content.getContents().currentuser);
}
/**
* Reads User Story Id from user@domain.com.json configuration file.
* @static
* @returns
* @memberof CopadoFiles
*/
static async getCopadoUserStoryId() {
let config = (await this.getLocalConfig()).get('work');
let userStoryId = config ? config.copadouserstoryid : null;
if (!userStoryId) {
config = await this.getUserConfig();
userStoryId = config ? config.copadouserstoryid : null;
}
if (!userStoryId) {
throw new core_1.SfError(messages.getMessage('copadoUserStoryNotSet'));
}
return userStoryId;
}
/**
* Verifies if a file exists or not within pathToCheck
* @static
* @param {string} file
* @returns {boolean}
* @memberof CopadoFiles
*/
static async verifyFilesExists(file, pathToCheck) {
pathToCheck = !pathToCheck ? path.join(GLOBAL_DIR, DOT_COPADO_DIR) : pathToCheck;
return fs.existsSync(path.join(pathToCheck, `${file}.json`));
}
/**
* Gets sfdx-project.json
*/
static async getProjectJson() {
let projectjson;
if (await this.existProjectJson()) {
projectjson = await core_1.SfProject.resolve().then(project => project.resolveProjectConfig()).then(config => JSON.stringify(config))
.catch(err => { throw new core_1.SfError(err.message); });
}
return projectjson;
}
/**
*
* Reads .forceignore file and create an array of patterns
* @static
* @returns Array of patterns. If There's no .forceignore file a default set of patterns will be applied.
* @memberof CopadoFiles
*/
static async getForceIgnoreFile() {
let patternList = [];
const forceignorePath = await this.existForceIgnoreFile();
if (forceignorePath) {
const filename = path.join(forceignorePath, FORCEIGNORE_FILE);
patternList = await this.getPattern(filename);
}
else {
// This is hard-coded because in some IDEs there is no default way to control what you are pushing.
patternList = exports.DEFAULT_FORCEIGNORE;
}
return patternList;
}
static async deleteFromLocalConfig(propertyKey) {
const config = await this.getCopadoConfigFile(LOCAL_DIR);
config.unset(propertyKey);
(await this.getCopadoConfigFile(LOCAL_DIR)).writeSync(config.getContents());
return config;
}
/**
* In order to read .forceignore file this method generates new Promise to read and build the content in a variable.
* @private
* @static
* @param {*} filename path where .forceignore file is stored.
* @returns .forceignore file into a variable.
* @memberof CopadoFiles
*/
static async getPattern(filename) {
let patternList = [];
return new Promise(resolve => {
readline.createInterface({
input: fse.createReadStream(filename),
terminal: false
}).on('line', line => {
if (!line.includes('#') && line) {
patternList.push(line);
}
}).on('close', () => {
resolve(patternList);
});
});
}
/**
* Checks if sfdx project json exists or not.
* @private
* @static
* @returns a path if project json exists or undefined if not.
* @memberof CopadoFiles
*/
static async existProjectJson() {
if (fs.existsSync(path.join(LOCAL_DIR, internal_1.SFDX_PROJECT_JSON)))
return LOCAL_DIR;
return undefined;
}
/**
* Checks if .forceignore file exists or not.
* @private
* @static
* @returns a path if .forceignore file exists or undefined if not.
* @memberof CopadoFiles
*/
static async existForceIgnoreFile() {
if (fs.existsSync(path.join(LOCAL_DIR, FORCEIGNORE_FILE)))
return LOCAL_DIR;
return undefined;
}
/**
* Creates an instance of CopadoFiles.
* Checks if Global directory exists, if not it creates the directory
* @constructor
* @memberof CopadoFiles
*/
constructor() {
fse.ensureDirSync(path.join(GLOBAL_DIR, DOT_COPADO_DIR));
}
}
exports.CopadoFiles = CopadoFiles;
//# sourceMappingURL=filesystem.js.map