Handling file opening (#329)

* Add function to escape a shell argument.

* Keep track of the last used window.

* Implement support for the open-file event.

* The shell escape regex needs parentheses to be captured.

* Removed lastWindow and added getLastFocusedWindow method to app

* Create window create new session only if no callback passed

* Added app.windowCallback to handle open-file before "ready"

* Small lint fixes

* Differeent open file handling: check if it is directory, and if the file is executale or not

* Small fixed and added some comments

* Added php-escape-shell module in place of custom escapeShellArgument()

* Fixed wrong code and added detailed comment

* Aliased php_escapeshellcmd import

* Rebased; added error handling in OpenFile

* changed string contatenation with template literal

* Pinned php-escape-shell dependency

* Typo fix

* getLastdFocusedWindow now uses Array.prototype.reduce()

* Notify users of unexisting path while open file
This commit is contained in:
Lorenzo Zottar 2016-08-02 00:52:21 +02:00 committed by James Hall
parent 3198a90823
commit 7e941b0451
9 changed files with 106 additions and 14 deletions

View file

@ -1,7 +1,5 @@
import { requestSession } from './sessions';
import rpc from '../rpc';
export function init () {
return (dispatch) => {
dispatch(requestSession());
};
rpc.emit('init');
}

View file

@ -21,14 +21,6 @@ import {
export function addSession (uid, shell, pid) {
return (dispatch, getState) => {
const { sessions } = getState();
// normally this would be encoded as an effect
// but the `SESSION_ADD` action is pretty expensive
// and we want to get this out as soon as possible
const initial = null == sessions.activeUid;
if (initial) rpc.emit('init');
dispatch({
type: SESSION_ADD,
uid,

View file

@ -1,6 +1,9 @@
import * as shellEscape from 'php-escape-shell';
import { setActiveSession } from './sessions';
import { keys } from '../utils/object';
import { last } from '../utils/array';
import { isExecutable } from '../utils/file';
import notify from '../utils/notify';
import rpc from '../rpc';
import {
requestSession,
@ -16,9 +19,12 @@ import {
UI_MOVE_RIGHT,
UI_MOVE_TO,
UI_SHOW_PREFERENCES,
UI_WINDOW_MOVE
UI_WINDOW_MOVE,
UI_OPEN_FILE
} from '../constants/ui';
const { stat } = window.require('fs');
export function increaseFontSize () {
return (dispatch, getState) => {
dispatch({
@ -172,3 +178,35 @@ export function windowMove () {
});
};
}
export function openFile (path) {
return (dispatch, getState) => {
dispatch({
type: UI_OPEN_FILE,
effect () {
stat(path, (err, stats) => {
if (err) {
console.error(err.stack);
notify('Unable to open path', `"${path}" doesn't exist.`);
} else {
// We need to use 'php-escape-shell' property this way
// until this eslint issue will be fixed:
// https://github.com/eslint/eslint/issues/6755
let command = shellEscape.php_escapeshellcmd(path);
if (stats.isDirectory()) {
command = `cd ${command}\n`;
} else if (stats.isFile() && isExecutable(stats)) {
command += '\n';
}
rpc.once('session add', ({ uid }) => {
rpc.once('session data', () => {
dispatch(sendSessionData(uid, command));
});
});
}
dispatch(requestSession());
});
}
});
};
}

View file

@ -8,3 +8,4 @@ export const UI_MOVE_RIGHT = 'UI_MOVE_RIGHT';
export const UI_MOVE_TO = 'UI_MOVE_TO';
export const UI_SHOW_PREFERENCES = 'UI_SHOW_PREFERENCES';
export const UI_WINDOW_MOVE = 'UI_WINDOW_MOVE';
export const UI_OPEN_FILE = 'UI_OPEN_FILE';

View file

@ -100,6 +100,10 @@ rpc.on('preferences', () => {
store_.dispatch(uiActions.showPreferences());
});
rpc.on('open file', ({ path }) => {
store_.dispatch(uiActions.openFile(path));
});
rpc.on('update available', ({ releaseName, releaseNotes }) => {
store_.dispatch(updaterActions.updateAvailable(releaseName, releaseNotes));
});

17
lib/utils/file.js Normal file
View file

@ -0,0 +1,17 @@
// Based on https://github.com/kevva/executable
// Since this module doesn't expose the function to check stat mode only,
// his logic is pasted here.
// Opened an issue and a pull request about it,
// to maybe switch to module in the future:
// Issue: https://github.com/kevva/executable/issues/9
// PR: https://github.com/kevva/executable/pull/10
export function isExecutable (fileStat) {
if (process.platform === 'win32') return true;
return Boolean(
(fileStat['mode'] & parseInt('0001', 8)) ||
(fileStat['mode'] & parseInt('0010', 8)) ||
(fileStat['mode'] & parseInt('0100', 8))
);
}