UNPKG

xrefcli

Version:

CLI command for the searching through OpenEdge XREF

130 lines 5.06 kB
"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 child_process = __importStar(require("child_process")); const fs = __importStar(require("fs")); class Editor { constructor(config) { this.config = config; } open(files) { const editconfig = this.config.data.editor; if (editconfig === undefined) { return; } this.validateConfig(editconfig).then(validationOk => { if (!validationOk) { return; } // add source root to files const repo = this.config.getRepo(); if (!this.validateFiles(files)) { console.error('error opening files'); return; } let params = editconfig.open || '%s'; params = params.replace('%s', files.join(' ')); if (repo.srcroot !== undefined) { params = params.replace('%r', repo.srcroot); } try { if (editconfig.type === 'cli') { const editor = editconfig.executable || ''; const child = child_process.spawnSync(editor, [params], { stdio: 'inherit' }); if (child.error) { console.log(child.error); } } else { const editor = '\"' + editconfig.executable + '\"'; const executeString = `${editor} ${params}`; const child = child_process.execSync(executeString); } } catch (err) { // hide further errors } }); } openContent(content) { const editconfig = this.config.data.editor; if (editconfig === undefined) { return; } this.validateConfig(editconfig).then(validationOk => { if (!validationOk) { return; } const filename = this.config.writeTmpFile(content, '.json'); let params = editconfig.open || '%s'; params = params.replace('%r', ''); params = params.replace('%s', `${filename}`); if (editconfig.type === 'cli') { const child = child_process.spawnSync(editconfig.executable || 'view', [filename], { stdio: 'inherit' }); if (child.error) { console.log(child.error); } } else { child_process.execSync(`"${editconfig.executable}" ${params}`); } }); } validateFiles(files) { let validationOk = true; files.forEach(file => { if (!fs.existsSync(file)) { console.error(`"${file}" could not be found`); validationOk = false; } }); return validationOk; } validateConfig(editconfig) { return __awaiter(this, void 0, void 0, function* () { const promise = new Promise((resolve) => __awaiter(this, void 0, void 0, function* () { let validationOk = true; if (editconfig === undefined || !editconfig.executable || !editconfig.open) { console.error('editor not configured'); if (editconfig && !editconfig.executable) { console.error(' set "executable" property'); } if (editconfig && !editconfig.open) { console.error(' set "open" property'); } validationOk = false; } if (validationOk) { yield this.checkEditor(); } resolve(validationOk); })); return promise; }); } checkEditor() { return __awaiter(this, void 0, void 0, function* () { yield this.config.askEditorType(this.config.data); }); } } exports.Editor = Editor; //# sourceMappingURL=editor.js.map