@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
102 lines (100 loc) • 3.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Common = exports.ResolveState = void 0;
const fs = require('fs');
const fse = require('fs-extra');
/**
* Error resolution states
*/
var ResolveState;
(function (ResolveState) {
ResolveState["Resolved"] = "RESOLVED";
ResolveState["Unresolved"] = "UNRESOLVED";
})(ResolveState = exports.ResolveState || (exports.ResolveState = {}));
class Common {
static cliRootPath = __dirname.substring(0, __dirname.length - 3);
static moduleTemplatePath = __dirname.substring(0, __dirname.length - 11) + 'module-template\\';
static _rootPath;
/**
* Gets the root path for the current repository
* @returns {string} Root path for the current repository
*/
static get rootPath() {
return Common._rootPath;
}
/**
* Sets the root path for the current repository
* @param {string} path Root path for the current repository
*/
static set rootPath(path) {
Common._rootPath = path;
}
/**
* Retrieves content of given file
* @param {string} filePath Target file path
* @returns {string | null} String representation of file content or null if file was not found
*/
static readFileData(filePath) {
if (!fs.statSync(filePath).isDirectory()) {
if (!fse.existsSync(filePath)) {
return null;
}
return fse.readFileSync(filePath, 'utf8');
}
return null;
}
/**
* Writes to the content of given file
* @param {string} filePath Target file path
* @returns {boolean} indicates whether the file was written successfully.
*/
static writeFileData(filePath, newContent) {
if (!fs.statSync(filePath).isDirectory()) {
if (!fse.existsSync(filePath)) {
return false;
}
fse.writeFileSync(filePath, newContent, 'utf8');
return true;
}
return false;
}
/**
* Creates the file with content.
* @param {string} filePath Target file path
* @param {string} newContent Content to write to the file
*/
static async writeFile(filePath, newContent) {
await fs.writeFile(filePath, newContent, (err) => {
if (err) {
console.error('Error creating file:', err);
return;
}
});
}
/**
* Retrieves content of given JSON file and parses into an object
* @param {string} filePath Target file path
* @returns {any | null} Parsed JSON object or null if file was not found
*/
static readFileJSON(filePath) {
if (!fs.statSync(filePath).isDirectory()) {
if (!fse.existsSync(filePath)) {
return null;
}
return fse.readJSONSync(filePath);
}
return null;
}
/**
* Replaces part of target string with a new string using given regex
* @param {string} originalString Target string
* @param {RegExp} regexToReplace Regex to apply to target string
* @param {string} newString String that will replace regex matches
* @returns {string} New string created from replacing regex matches or original string if no matches were found
*/
static replaceInString(originalString, regexToReplace, newString) {
return originalString.replace(regexToReplace, newString);
}
}
exports.Common = Common;
//# sourceMappingURL=common.js.map