Skip to content

Commit

Permalink
feature(terminal-run) add
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed May 10, 2019
1 parent c629f8e commit 139936c
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 49 deletions.
12 changes: 8 additions & 4 deletions .cloudcmd.menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ module.exports = {
const {element} = DOM.CurrentInfo;
DOM.renameCurrent(element);
},
'D - Build Dev': async ({DOM, tryToCatch}) => {
return 'npm run build:client:dev';
'D - Build Dev': async ({CloudCmd}) => {
CloudCmd.TerminalRun.show({
command: 'npm run build:client:dev',
});
},
'P - Build Prod': () => {
return 'npm run build:client';
'P - Build Prod': ({CloudCmd}) => {
CloudCmd.TerminalRun.show({
command: 'npm run build:client',
});
},
};

1 change: 1 addition & 0 deletions .webpack/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ module.exports = {
[modules + '/operation']: `${dirModules}/operation/index.js`,
[modules + '/konsole']: `${dirModules}/konsole.js`,
[modules + '/terminal']: `${dirModules}/terminal.js`,
[modules + '/terminal-run']: `${dirModules}/terminal-run.js`,
[modules + '/cloud']: `${dirModules}/cloud.js`,
[modules + '/user-menu']: `${dirModules}/user-menu.js`,
[modules + '/polyfill']: `${dirModules}/polyfill.js`,
Expand Down
138 changes: 138 additions & 0 deletions client/modules/terminal-run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
'use strict';

/* global CloudCmd, gritty */

const {promisify} = require('es6-promisify');
const tryToCatch = require('try-to-catch/legacy');

require('../../css/terminal.css');

const exec = require('execon');
const load = require('load.js');
const DOM = require('../dom');
const Images = require('../dom/images');

const loadParallel = promisify(load.parallel);

const {Dialog} = DOM;
const {
Key,
config,
} = CloudCmd;

CloudCmd.TerminalRun = exports;

let Loaded;
let Terminal;
let Socket;

const loadAll = async () => {
const {prefix} = CloudCmd;

const prefixGritty = getPrefix();
const js = `${prefixGritty}/gritty.js`;
const css = `${prefix}/dist/terminal.css`;

const [e] = await tryToCatch(loadParallel, [js, css]);

if (e) {
const src = e.target.src.replace(window.location.href, '');
return Dialog.alert(`file ${src} could not be loaded`);
}

Loaded = true;
};

module.exports.init = async () => {
if (!config('terminal'))
return;

Images.show.load('top');

await CloudCmd.View();
await loadAll();
};

module.exports.show = show;
module.exports.hide = hide;

function hide () {
CloudCmd.View.hide();
}

function getPrefix() {
return CloudCmd.prefix + '/gritty';
}

function getPrefixSocket() {
return CloudCmd.prefixSocket + '/gritty';
}

function getEnv() {
return {
ACTIVE_DIR: DOM.getCurrentDirPath,
PASSIVE_DIR: DOM.getNotCurrentDirPath,
CURRENT_NAME: DOM.getCurrentName,
CURRENT_PATH: DOM.getCurrentPath,
};
}

function create({command}) {
const options = {
env: getEnv(),
prefix: getPrefixSocket(),
socketPath: CloudCmd.prefix,
fontFamily: 'Droid Sans Mono',
command,
autoRestart: false,
};

let commandExit = false;

const {socket, terminal} = gritty(document.body, options);

Socket = socket;
Terminal = terminal;

Terminal.on('key', (char, {keyCode, shiftKey}) => {
if (commandExit)
hide();

if (shiftKey && keyCode === Key.ESC) {
hide();
}
});

Socket.on('exit', () => {
terminal.write('\nPress any key to close Terminal...');
commandExit = true;
});

Socket.on('connect', exec.with(authCheck, socket));
}

function authCheck(spawn) {
spawn.emit('auth', config('username'), config('password'));

spawn.on('reject', () => {
Dialog.alert('Wrong credentials!');
});
}

async function show(options = {}) {
if (!Loaded)
return;

if (!config('terminal'))
return;

const {command} = options;
await create({command});

CloudCmd.View.show(Terminal.element, {
afterShow: () => {
Terminal.focus();
},
});
}

14 changes: 7 additions & 7 deletions client/modules/terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ CloudCmd.Terminal = exports;

let Loaded;
let Terminal;
let Socket;

const loadAll = async () => {
const {prefix} = CloudCmd;
Expand Down Expand Up @@ -84,17 +85,19 @@ function create() {
socketPath: CloudCmd.prefix,
fontFamily: 'Droid Sans Mono',
};

const {socket, terminal} = gritty(document.body, options);

Socket = socket;
Terminal = terminal;

terminal.on('key', (char, {keyCode, shiftKey}) => {
Terminal.on('key', (char, {keyCode, shiftKey}) => {
if (shiftKey && keyCode === Key.ESC) {
hide();
}
});

socket.on('connect', exec.with(authCheck, socket));
Socket.on('connect', exec.with(authCheck, socket));
}

function authCheck(spawn) {
Expand All @@ -105,7 +108,7 @@ function authCheck(spawn) {
});
}

function show(callback) {
function show() {
if (!Loaded)
return;

Expand All @@ -114,10 +117,7 @@ function show(callback) {

CloudCmd.View.show(Terminal.element, {
afterShow: () => {
if (Terminal)
Terminal.focus();

exec(callback);
Terminal.focus();
},
});
}
Expand Down
23 changes: 3 additions & 20 deletions client/modules/user-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ const Name = 'UserMenu';
CloudCmd[Name] = module.exports;

const {Key} = CloudCmd;
const {stringify} = JSON;

const userMenuUrl = '/api/v1/user-menu';

module.exports.init = async () => {
await Promise.all([
Expand Down Expand Up @@ -58,7 +55,7 @@ async function show() {
Images.show.load('top');

const {dirPath} = CurrentInfo;
const res = await fetch(`${userMenuUrl}?dir=${dirPath}`);
const res = await fetch(`/api/v1/user-menu?dir=${dirPath}`);
const userMenu = getUserMenu(await res.text());
const options = Object.keys(userMenu);

Expand Down Expand Up @@ -114,25 +111,11 @@ const onKeyDown = currify(async (keys, options, userMenu, e) => {

hide();

const cmd = await userMenu[value]({
await userMenu[value]({
DOM,
CloudCmd,
});

if (!cmd)
return;

const res = await fetch(userMenuUrl, {
method: 'PUT',
body: stringify({
cmd,
}),
});

const data = await res.text();

if (data)
DOM.Dialog.alert(data);

CloudCmd.refresh();
});

1 change: 1 addition & 0 deletions json/modules.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"operation",
"konsole",
"terminal",
"terminal-run",
"cloud",
"user-menu"
],
Expand Down
18 changes: 0 additions & 18 deletions server/user-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
const {homedir} = require('os');
const fs = require('fs');
const {join} = require('path');
const {exec} = require('child_process');

const tryToCatch = require('try-to-catch');
const pullout = require('pullout');
const {promisify} = require('util');
const {parse} = JSON;

const readFile = promisify(fs.readFile);
const execute = promisify(exec);
const menuName = '.cloudcmd.menu.js';
const userMenuPath = join(homedir(), menuName);

Expand All @@ -24,10 +20,6 @@ module.exports = async (req, res, next) => {
if (method === 'GET')
return onGET(req.query, res);

if (method === 'PUT') {
return onPUT(await pullout(req), res);
}

next();
};

Expand Down Expand Up @@ -56,13 +48,3 @@ async function onGET({dir}, res) {
.send(e.message);
}

async function onPUT(str, res) {
const {cmd} = parse(str);
const [e, data] = await tryToCatch(execute, cmd);

if (e)
return res.send(e.message);

return res.send(data);
}

0 comments on commit 139936c

Please sign in to comment.