@ima/cli
Version:
IMA.js CLI tool to build, develop and work with IMA.js applications.
62 lines (61 loc) • 2.27 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.openEditorMiddleware = openEditorMiddleware;
const logger_1 = require("@ima/dev-utils/logger");
const chalk_1 = __importDefault(require("chalk"));
const env_editor_1 = __importDefault(require("env-editor"));
const open_editor_1 = __importDefault(require("open-editor"));
/**
* Return preferred editor identifier
*/
function getEditor() {
// Prioritize explicit config
if (process.env.IMA_EDITOR) {
return process.env.IMA_EDITOR;
}
// Re-use create-react-app editor
if (process.env.REACT_EDITOR) {
return process.env.REACT_EDITOR;
}
return process.env.EDITOR;
}
/**
* Open code editor at given file, line and column, if supported.
* If there are no editor env variables defined, it prints an
* instructions with stepts to setup this functionality.
*/
function launchEditor(fileName, line, column) {
const editor = getEditor();
if (!editor) {
const supportedEditors = env_editor_1.default.allEditors().map(editor => editor.id);
logger_1.logger.error(`${chalk_1.default.underline('Unable to open:')} ${chalk_1.default.cyan('$IMA_EDITOR')}, ${chalk_1.default.cyan('$REACT_EDITOR')} or ${chalk_1.default.cyan('$EDITOR')} env variables are empty.\n` +
'Set it to the command/path of your editor in ~/.zshenv or ~/.bashrc\n\n' +
`${chalk_1.default.magenta('Supported values:')} ${supportedEditors.join(', ')}\n`);
return;
}
(0, open_editor_1.default)([
{
file: fileName,
line,
column,
},
], { editor });
}
function openEditorMiddleware() {
return async (req, res) => {
const { fileName, line, column } = req.query;
if (!fileName) {
return res.status(500).end();
}
try {
launchEditor(fileName?.toString(), line ? parseInt(line.toString()) : 1, column ? parseInt(column.toString()) : 1);
}
catch (error) {
logger_1.logger.error(error);
}
return res.status(200).end();
};
}