Skip to content

Commit

Permalink
master:Added open editor, substituted input of path for switch
Browse files Browse the repository at this point in the history
  • Loading branch information
DKunin committed May 4, 2017
1 parent ceb879d commit 8e5cff8
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 48 deletions.
Binary file added .DS_Store
Binary file not shown.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ Barebones simple server, to open files in your favorite editor from whereever po
![screen](./assets/images/screen-1.png)
![screen](./assets/images/screen-2.png)

### Set path to your favorite editor.
### Select your favorite editor:

- Sublime
- Atom
- VScode
- Webstorm
- Textmate

Now set your tools (extentions and stuff) to call:

Expand All @@ -26,4 +32,5 @@ Now set your tools (extentions and stuff) to call:
And it will open your editor on specified line and column.

### Changelog
2.0.0 Support for different major editors, no need to fiddle with editor path
1.1.1 Updated tray icon for retina
Binary file added assets/.DS_Store
Binary file not shown.
Binary file modified assets/images/screen-2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
{
"name": "menubar-helper",
"version": "1.1.1",
"version": "2.0.0",
"description": "Simple menubar app to help open files in editor",
"author": "Dmitri Kunin",
"license": "MIT",
"repository": "dkunin/menubar-helper",
"main": "./src/main.js",
"dependencies": {
"electron-config": "0.2.1",
"execa": "0.6.3",
"express": "4.15.2"
"env-editor": "0.1.0",
"express": "4.15.2",
"line-column-path": "1.0.0",
"open-editor": "1.0.1",
"opn": "5.0.0"
},
"devDependencies": {
"electron": "^1.4.10",
Expand Down
59 changes: 30 additions & 29 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,61 +29,62 @@
padding: 5px;
margin: 10px 0;
}
.editor {
display: block;
width: 100%;
font-size: 16px;
}
.version {
color: white;
position: absolute;
top: 5px;
right: 5px;
font-size: 8px;
}
</style>
<link rel="shortcut icon" href="../assets/images/favicon.png" type="image/png">
</head>
<body>
<div class="version"></div>
<form>
<label for="port">
<span>Port:</span><input class="port" type="number" placeholder="port"/></label>
<label for="port">
<span>Editor path:</span><input class="editorPath" type="text" placeholder="editor path"/></label>
<span>Port:</span><input class="port" type="number" placeholder="port" /></label>
<label for="editor">
<select value="sublime" id="editor" class="editor">
<option value="sublime">Sublime</option>
<option value="atom">Atom</option>
<option value="vscode">VScode</option>
<option value="webstorm">Webstorm</option>
<option value="textmate">Textmate</option>
</select>
</label>
<button>Save</button>
<button class="close-btn" type="button">Close</button>
</form>
<script>
const electron = require('electron');
const remote = electron.remote;
const { clipboard } = electron;
const { Menu, MenuItem } = remote;

window.addEventListener('contextmenu', (e) => {
e.preventDefault();
menu.popup(remote.getCurrentWindow());
}, false);
const port = document.querySelector('.port');
const editorPath = document.querySelector('.editorPath');
const editor = document.querySelector('.editor');
const closeBtn = document.querySelector('.close-btn');
const versionLabel = document.querySelector('.version');
const config = remote.getGlobal('config');
const restartServer = remote.getGlobal('restartServer');
const pkg = require('../package.json');
versionLabel.innerText = pkg.version;

function onSubmit(event) {
event ? event.preventDefault() : null;
config.set({
port: port.value,
editorPath: escape(editorPath.value)
editor: editor.value
});
event ? restartServer(true) : null;
}

const menu = new Menu();
menu.append(new MenuItem({label: 'paste', click() {
editorPath.value = clipboard.readText();
onSubmit();
}}));

document.addEventListener('keydown', (event) => {
if (event.key === 'v' && event.metaKey) {
editorPath.value = clipboard.readText();
onSubmit();
}
});

port.value = config.get('port');
editorPath.value = config.get('editorPath');
closeBtn.addEventListener('click', () => {
restartServer();
});
editor.value = config.get('editor');
closeBtn.addEventListener('click', restartServer);
document.querySelector('form').addEventListener('submit', onSubmit);
</script>

Expand Down
4 changes: 2 additions & 2 deletions src/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const serverObject = require('./server');
let server;

process.on('message', message => {
let [command, port, editorPath] = message.split(' ');
let [command, port, editor] = message.split(' ');

if (command === 'launch') {
server = serverObject.app.listen(port);
serverObject.updatePath(editorPath);
serverObject.updateEditor(editor);
}
if (command === 'kill') {
server.close();
Expand Down
6 changes: 3 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ const fork = require('child_process').fork;
// var shell = require('electron').shell;

let port = DEFAULT_PORT;
let editorPath = '';
let editor = 'sublime';

const config = new Config({
defaults: {
editorPath,
editor,
port
}
});
Expand All @@ -37,7 +37,7 @@ let contextMenu = null;

function startServer() {
server = fork(path.resolve(__dirname, './launch.js'));
server.send(`launch ${config.get('port')} ${config.get('editorPath')}`);
server.send(`launch ${config.get('port')} ${config.get('editor')}`);
// if (appIcon) {
// appIcon.setImage(iconPath.active);
// }
Expand Down
77 changes: 77 additions & 0 deletions src/open-editor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';
// Copy of https://github.com/sindresorhus/open-editor, but fiddled with a bit
const childProcess = require('child_process');
const envEditor = require('env-editor');
const lineColumnPath = require('line-column-path');
const opn = require('opn');

const make = (files, opts) => {
if (!Array.isArray(files)) {
throw new TypeError(`Expected an \`Array\`, got ${typeof files}`);
}

opts = Object.assign({}, opts);

const editor = envEditor.get(opts.editor);

const args = [];

if (editor.id === 'vscode') {
args.push('--goto');
}

for (const file of files) {
const parsed = lineColumnPath.parse(file);

if (['sublime', 'atom', 'vscode'].indexOf(editor.id) !== -1) {
args.push(lineColumnPath.stringify(parsed));
continue;
}

if (editor.id === 'webstorm') {
args.push(lineColumnPath.stringify(parsed, { column: false }));
continue;
}

if (editor.id === 'textmate') {
args.push(
'--line',
lineColumnPath.stringify(parsed, {
file: false,
column: false
}),
parsed.file
);
continue;
}

args.push(parsed.file);
}

return {
bin: editor.bin,
args
};
};

module.exports = (files, opts) => {
const result = make(files, opts);

const cp = childProcess.spawn(result.bin, result.args, {
detached: true,
stdio: 'ignore'
});

// Fallback
cp.on('error', () => {
const result = make(files, Object.assign({}, opts, { editor: '' }));

for (const file of result.args) {
opn(file, { wait: false });
}
});

cp.unref();
};

module.exports.make = make;
15 changes: 6 additions & 9 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';

const express = require('express');
const execa = require('execa');
const path = require('path');
const app = express();
let editorPath = '';
const openEditor = require('./open-editor');
let editor = 'sublime';

app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
Expand All @@ -17,15 +16,13 @@ app.use(function(req, res, next) {

app.get('/openeditor', function(req, res) {
let { options = '' } = req.query;
options = options.replace(/\s/g, '\\ ');
execa.shell(path.normalize(editorPath) + ' ' + (options ? options : ''))
.then(result => res.send(result.stdout))
.catch(error => res.send(error));
openEditor([ options ], { editor });
res.send('ok');
});

module.exports = {
app,
updatePath: function(newPath) {
editorPath = unescape(newPath).replace(/\s/g, '\\ ');
updateEditor(newEditor) {
editor = newEditor;
}
};
2 changes: 1 addition & 1 deletion src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

const serverObject = require('./server');
serverObject.app.listen(7288);
serverObject.updatePath('/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl');
// serverObject.updateEditor('atom');

0 comments on commit 8e5cff8

Please sign in to comment.