xrefcli
Version:
CLI command for the searching through OpenEdge XREF
175 lines • 6.54 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const tmp = __importStar(require("tmp"));
const inquirer = __importStar(require("inquirer"));
class Config {
constructor() {
this.data = new ConfigData();
this.configRootDir = '';
this.reposDir = '';
this.configFile = '';
this.tmpDir = '';
this.currentCommand = '';
}
initialize(args) {
return __awaiter(this, void 0, void 0, function* () {
this.currentCommand = args.command;
const promise = new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
this.checkDirs();
const value = yield this.loadConfig();
this.data = value;
resolve();
}));
return promise;
});
}
checkDirs() {
const userDir = require('os').homedir();
this.configRootDir = userDir + path.sep + '.xrefcli';
this.reposDir = this.configRootDir + path.sep + 'repos';
this.tmpDir = this.configRootDir + path.sep + 'tmp';
if (!fs.existsSync(this.configRootDir)) {
fs.mkdirSync(this.configRootDir);
}
if (!fs.existsSync(this.reposDir)) {
fs.mkdirSync(this.reposDir);
}
if (!fs.existsSync(this.tmpDir)) {
fs.mkdirSync(this.tmpDir);
}
}
loadConfig() {
return __awaiter(this, void 0, void 0, function* () {
const promise = new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
this.configFile = this.configRootDir + path.sep + 'xrefconfig.json';
let config = new ConfigData();
if (fs.existsSync(this.configFile)) {
config = Object.assign(new ConfigData(), require(this.configFile));
}
const validationOk = yield this.validateConfig(config);
if (validationOk) {
resolve(config);
}
else {
reject();
}
}));
return promise;
});
}
validateConfig(config) {
return __awaiter(this, void 0, void 0, function* () {
// stripped validation for now
const promise = new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
resolve(true);
}));
return promise;
});
}
askEditorType(config) {
return __awaiter(this, void 0, void 0, function* () {
const promise = new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
const type = config.editor.type;
if (type !== 'gui' && type !== 'cli') {
const prompt = inquirer.createPromptModule();
const answer = yield prompt({
name: 'type',
type: 'list',
message: 'Editor type is not specified, what UI do you want?',
choices: ['gui', 'cli']
});
config.editor.type = answer.type;
this.saveConfig();
}
resolve();
}));
return promise;
});
}
saveConfig() {
fs.writeFileSync(this.configFile, JSON.stringify(this.data, null, 2));
}
addRepo(repo) {
if (this.data.repos.findIndex(item => item.name === repo.name) === -1) {
this.data.repos.push(repo);
}
}
removeRepo(reponame) {
reponame = reponame.toLowerCase();
if (this.data.repos.findIndex(item => item.name === reponame) === -1) {
console.error(`Error: repo '${reponame}' not found`);
process.exit(1);
}
const repofile = this.getRepoFilename(reponame);
fs.unlinkSync(repofile);
this.data.repos = this.data.repos.filter(item => item.name !== reponame);
}
repoExists(reponame) {
return (this.data.repos.findIndex(item => item.name === reponame) !== -1);
}
writeRepoData(reponame, xrefdata) {
const repofilename = this.getRepoFilename(reponame);
fs.writeFileSync(repofilename, JSON.stringify(xrefdata, undefined, 2));
return true;
}
getRepo(reponame = '') {
if (reponame === '') {
reponame = this.data.current;
}
const repo = this.data.repos.filter(item => item.name === reponame.toLowerCase())[0];
return repo;
}
getRepoFilename(reponame) {
const repofilename = this.reposDir + path.sep + reponame + '.json';
return repofilename;
}
loadRepo(reponame) {
const repofile = this.getRepoFilename(reponame);
const xreffile = require(repofile);
return xreffile;
}
writeTmpFile(content, postfix = '.tmp') {
const tmpFilename = tmp.tmpNameSync({
dir: this.tmpDir,
postfix: postfix
});
fs.writeFileSync(tmpFilename, content);
return tmpFilename;
}
}
exports.Config = Config;
class ConfigData {
constructor() {
this.current = '';
this.editor = new EditorConfig();
this.repos = [];
}
}
exports.ConfigData = ConfigData;
class EditorConfig {
constructor() {
this.name = '';
this.type = '';
this.executable = '';
this.open = '';
}
}
exports.EditorConfig = EditorConfig;
//# sourceMappingURL=config.js.map