kinde-cli
Version:
Kinde cli for managing your business integration, users, roles and permissions etc
129 lines (128 loc) • 4.84 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearGlobalConfig = exports.writeGlobalConfig = exports.readGlobalConfig = exports.createRootDirectory = exports.isValidPath = exports.rootDirectoryPath = void 0;
const node_fs_1 = __importDefault(require("node:fs"));
const node_os_1 = __importDefault(require("node:os"));
const node_path_1 = __importDefault(require("node:path"));
// export const isValidConfig = (data: string) => {
// try {
// let config = JSON.parse(data);
// return true;
// } catch (e) {
// return false;
// }
// };
const rootDirectoryPath = () => {
let dirName = ".kinde";
let rootDirectory = node_path_1.default.join(node_os_1.default.homedir(), dirName);
return rootDirectory;
};
exports.rootDirectoryPath = rootDirectoryPath;
function getGlobalConfigPath() {
return __awaiter(this, void 0, void 0, function* () {
return node_path_1.default.join((0, exports.rootDirectoryPath)(), "config.json");
});
}
function isValidPath(path) {
if (node_fs_1.default.existsSync(path)) {
return true;
}
else {
return false;
}
}
exports.isValidPath = isValidPath;
function createRootDirectory() {
return __awaiter(this, void 0, void 0, function* () {
let rootDirectory = (0, exports.rootDirectoryPath)();
try {
// if directory does not exist, create directory
if (!node_fs_1.default.existsSync(rootDirectory)) {
rootDirectory = yield new Promise((resolve, reject) => {
return node_fs_1.default.mkdir(node_path_1.default.join(rootDirectory), (err) => {
if (err)
return reject(null);
return resolve(rootDirectory);
});
});
}
return rootDirectory;
}
catch (err) {
return null;
}
});
}
exports.createRootDirectory = createRootDirectory;
function readGlobalConfig() {
return __awaiter(this, void 0, void 0, function* () {
try {
const configPath = yield getGlobalConfigPath();
let configFile = yield new Promise((resolve, reject) => {
node_fs_1.default.readFile(node_path_1.default.join(configPath), (err, data) => {
if (err) {
return reject(err);
}
return resolve(data.toString("utf8"));
});
});
return configFile;
}
catch (err) {
return null;
}
});
}
exports.readGlobalConfig = readGlobalConfig;
function writeGlobalConfig(data) {
return __awaiter(this, void 0, void 0, function* () {
try {
let rootDirectory = (0, exports.rootDirectoryPath)();
let config = yield new Promise((resolve, reject) => {
node_fs_1.default.writeFile(node_path_1.default.join(rootDirectory, "config.json"), JSON.stringify(data), { encoding: "utf-8" }, (err) => {
if (err) {
return reject(false);
}
return resolve(true);
});
});
return config;
}
catch (err) {
return null;
}
});
}
exports.writeGlobalConfig = writeGlobalConfig;
function clearGlobalConfig() {
return __awaiter(this, void 0, void 0, function* () {
try {
const configPath = yield getGlobalConfigPath();
let deleted = yield new Promise((resolve, reject) => {
node_fs_1.default.unlink(configPath, (err) => {
if (err) {
return reject(false);
}
return resolve(true);
});
});
return deleted;
}
catch (err) {
return null;
}
});
}
exports.clearGlobalConfig = clearGlobalConfig;