Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-bash

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-bash - npm Package Compare versions

Comparing version 1.3.0 to 1.4.0

es/parser.js

98

es/bash.js

@@ -19,2 +19,6 @@ 'use strict';

var _parser = require('./parser');
var BashParser = _interopRequireWildcard(_parser);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

@@ -35,5 +39,15 @@

/*
* This parses and executes the given <input> and returns an updated
* state object.
*
* @param {string} input - the user input
* @param {Object} state - the current terminal state
* @returns {Object} the new terminal state
*/
_createClass(Bash, [{
key: 'execute',
value: function execute(input, state) {
value: function execute(input, currentState) {
this.prevCommands.push(input);

@@ -43,5 +57,5 @@ this.prevCommandsIndex = this.prevCommands.length;

// Append input to history
var newState = Object.assign({}, state, {
history: state.history.concat({
cwd: state.cwd,
var newState = Object.assign({}, currentState, {
history: currentState.history.concat({
cwd: currentState.cwd,
value: input

@@ -51,50 +65,47 @@ })

var _parseInput = this.parseInput(input);
var commandList = BashParser.parse(input);
return this.runCommands(commandList, newState);
}
var command = _parseInput.command;
var args = _parseInput.args;
/*
* This function executes a list of command lists. The outer list
* is a dependency list parsed from the `&&` operator. The inner lists
* are groups of commands parsed from the `;` operator. If any given
* command fails, the outer list will stop executing.
*
* @param {Array} commands - the commands to run
* @param {Object} state - the terminal state
* @returns {Object} the new terminal state
*/
if (command === '') {
return newState;
} else if (this.commands[command]) {
return this.commands[command].exec(newState, args);
} else {
return Util.reportError(newState, _const.Errors.COMMAND_NOT_FOUND, input);
}
}
}, {
key: 'parseInput',
value: function parseInput(input) {
var tokens = input.trim().split(/ +/);
var command = tokens.shift();
key: 'runCommands',
value: function runCommands(commands, state) {
var _this = this;
// Short circuit if command doesn't exist
if (!this.commands[command]) return { command: command };
var errorOccurred = false;
var aliases = this.commands[command].aliases || {};
var args = {};
var anonArgPos = 0;
function parseToken(token) {
if (token[0] === '-') {
if (aliases[token]) {
var argTokens = [].concat(aliases[token]);
argTokens.forEach(parseToken);
return;
}
if (token[1] === '-') {
args[token.substr(2)] = true;
} else {
var next = tokens.shift();
args[token.substr(1)] = next;
}
/*
* This function executes a single command and marks whether an error
* occurred. If an error occurs, the following dependent commands should
* not be run.
*/
var reducer = function reducer(newState, command) {
if (command.name === '') {
return newState;
} else if (_this.commands[command.name]) {
var nextState = _this.commands[command.name].exec(newState, command);
errorOccurred = errorOccurred || nextState && nextState.error;
return nextState;
} else {
args[anonArgPos++] = token;
errorOccurred = true;
return Util.appendError(newState, _const.Errors.COMMAND_NOT_FOUND, command.name);
}
}
};
while (tokens.length > 0) {
parseToken(tokens.shift());
while (!errorOccurred && commands.length) {
var dependentCommands = commands.shift();
state = dependentCommands.reduce(reducer, state);
}
return { command: command, args: args };
return state;
}

@@ -111,3 +122,2 @@

* @param {string} state.cwd - the current working directory
*
* @returns {?string} a suggested autocomplete for the <input>

@@ -114,0 +124,0 @@ */

@@ -6,3 +6,3 @@ 'use strict';

});
exports.pwd = exports.cd = exports.mkdir = exports.cat = exports.ls = exports.clear = exports.help = undefined;
exports.printenv = exports.echo = exports.pwd = exports.cd = exports.mkdir = exports.cat = exports.ls = exports.clear = exports.help = undefined;

@@ -19,15 +19,13 @@ var _util = require('./util');

var helpCommands = ['clear', 'ls', 'cat', 'mkdir', 'cd', 'pwd'];
var helpCommands = ['clear', 'ls', 'cat', 'mkdir', 'cd', 'pwd', 'echo', 'printenv'];
var help = exports.help = {
exec: function exec(_ref) {
var history = _ref.history;
var structure = _ref.structure;
var cwd = _ref.cwd;
exec: function exec(state) {
var _state$history;
return { structure: structure, cwd: cwd,
history: history.concat.apply(history, [{ value: 'React-bash:' }, { value: 'These shell commands are defined internally. Type \'help\' to see this list.' }].concat(_toConsumableArray(helpCommands.map(function (value) {
return Object.assign({}, state, {
history: (_state$history = state.history).concat.apply(_state$history, [{ value: 'React-bash:' }, { value: 'These shell commands are defined internally. Type \'help\' to see this list.' }].concat(_toConsumableArray(helpCommands.map(function (value) {
return { value: value };
}))))
};
});
}

@@ -37,7 +35,4 @@ };

var clear = exports.clear = {
exec: function exec(_ref2) {
var structure = _ref2.structure;
var cwd = _ref2.cwd;
return { structure: structure, cwd: cwd, history: [] };
exec: function exec(state) {
return Object.assign({}, state, { history: [] });
}

@@ -47,17 +42,10 @@ };

var ls = exports.ls = {
aliases: {
'-l': '--long',
'-la': ['--long', '--all'],
'-al': ['--long', '--all'],
'-a': '--all'
},
exec: function exec(state, args) {
var history = state.history;
var structure = state.structure;
var cwd = state.cwd;
exec: function exec(state, _ref) {
var flags = _ref.flags;
var args = _ref.args;
var path = args[0] || '';
var fullPath = Util.extractPath(path, cwd);
var fullPath = Util.extractPath(path, state.cwd);
var _Util$getDirectoryByP = Util.getDirectoryByPath(structure, fullPath);
var _Util$getDirectoryByP = Util.getDirectoryByPath(state.structure, fullPath);

@@ -69,6 +57,6 @@ var err = _Util$getDirectoryByP.err;

if (err) {
return Util.reportError(state, err, path);
return Util.appendError(state, err, path);
} else {
var content = Object.keys(dir);
if (!args.all) {
if (!flags.a) {
content = content.filter(function (name) {

@@ -78,12 +66,12 @@ return name[0] !== '.';

}
if (args.long) {
return { structure: structure, cwd: cwd,
history: history.concat(content.map(function (value) {
if (flags.l) {
return Object.assign({}, state, {
history: state.history.concat(content.map(function (value) {
return { value: value };
}))
};
});
} else {
return { structure: structure, cwd: cwd,
history: history.concat({ value: content.join(' ') })
};
return Object.assign({}, state, {
history: state.history.concat({ value: content.join(' ') })
});
}

@@ -95,6 +83,4 @@ }

var cat = exports.cat = {
exec: function exec(state, args) {
var history = state.history;
var structure = state.structure;
var cwd = state.cwd;
exec: function exec(state, _ref2) {
var args = _ref2.args;

@@ -104,5 +90,5 @@ var path = args[0];

var fileName = relativePath.pop();
var fullPath = Util.extractPath(relativePath.join('/'), cwd);
var fullPath = Util.extractPath(relativePath.join('/'), state.cwd);
var _Util$getDirectoryByP2 = Util.getDirectoryByPath(structure, fullPath);
var _Util$getDirectoryByP2 = Util.getDirectoryByPath(state.structure, fullPath);

@@ -113,13 +99,13 @@ var err = _Util$getDirectoryByP2.err;

if (err) {
return Util.reportError(state, err, path);
return Util.appendError(state, err, path);
} else if (!dir[fileName]) {
return Util.reportError(state, _const.Errors.NO_SUCH_FILE, path);
return Util.appendError(state, _const.Errors.NO_SUCH_FILE, path);
} else if (!dir[fileName].hasOwnProperty('content')) {
return Util.reportError(state, _const.Errors.IS_A_DIRECTORY, path);
return Util.appendError(state, _const.Errors.IS_A_DIRECTORY, path);
} else {
return { cwd: cwd, structure: structure,
history: history.concat({
return Object.assign({}, state, {
history: state.history.concat({
value: dir[fileName].content
})
};
});
}

@@ -130,6 +116,4 @@ }

var mkdir = exports.mkdir = {
exec: function exec(state, args) {
var history = state.history;
var structure = state.structure;
var cwd = state.cwd;
exec: function exec(state, _ref3) {
var args = _ref3.args;

@@ -139,4 +123,4 @@ var path = args[0];

var newDirectory = relativePath.pop();
var fullPath = Util.extractPath(relativePath.join('/'), cwd);
var deepCopy = JSON.parse(JSON.stringify(structure));
var fullPath = Util.extractPath(relativePath.join('/'), state.cwd);
var deepCopy = JSON.parse(JSON.stringify(state.structure));

@@ -149,6 +133,6 @@ var _Util$getDirectoryByP3 = Util.getDirectoryByPath(deepCopy, fullPath);

if (dir[newDirectory]) {
return Util.reportError(state, _const.Errors.FILE_EXISTS, path);
return Util.appendError(state, _const.Errors.FILE_EXISTS, path);
} else {
dir[newDirectory] = {};
return { cwd: cwd, history: history, structure: deepCopy };
return Object.assign({}, state, { structure: deepCopy });
}

@@ -159,15 +143,13 @@ }

var cd = exports.cd = {
exec: function exec(state, args) {
var history = state.history;
var structure = state.structure;
var cwd = state.cwd;
exec: function exec(state, _ref4) {
var args = _ref4.args;
var path = args[0];
if (!path || path === '/') {
return { structure: structure, history: history, cwd: '' };
return Object.assign({}, state, { cwd: '' });
}
var fullPath = Util.extractPath(path, cwd);
var fullPath = Util.extractPath(path, state.cwd);
var _Util$getDirectoryByP4 = Util.getDirectoryByPath(structure, fullPath);
var _Util$getDirectoryByP4 = Util.getDirectoryByPath(state.structure, fullPath);

@@ -178,5 +160,5 @@ var err = _Util$getDirectoryByP4.err;

if (err) {
return Util.reportError(state, err, path);
return Util.appendError(state, err, path);
} else {
return { history: history, structure: structure, cwd: fullPath };
return Object.assign({}, state, { cwd: fullPath });
}

@@ -188,13 +170,34 @@ }

exec: function exec(state) {
var history = state.history;
var structure = state.structure;
var cwd = state.cwd;
var directory = '/' + state.cwd;
return Object.assign({}, state, {
history: state.history.concat({ value: directory })
});
}
};
var directory = '/' + cwd;
var echo = exports.echo = {
exec: function exec(state, _ref5) {
var input = _ref5.input;
return {
cwd: cwd, structure: structure,
history: history.concat({ value: directory })
};
var ECHO_LENGTH = 'echo '.length;
var envVariables = Util.getEnvVariables(state);
var value = input.slice(ECHO_LENGTH).replace(/(\$\w+)/g, function (key) {
return envVariables[key.slice(1)] || '';
});
return Object.assign({}, state, {
history: state.history.concat({ value: value })
});
}
};
var printenv = exports.printenv = {
exec: function exec(state) {
var envVariables = Util.getEnvVariables(state);
var values = Object.keys(envVariables).map(function (key) {
return { value: key + '=' + envVariables[key] };
});
return Object.assign({}, state, {
history: state.history.concat(values)
});
}
};

@@ -50,2 +50,3 @@ 'use strict';

var extensions = _ref.extensions;
var prefix = _ref.prefix;

@@ -59,2 +60,3 @@ _classCallCheck(this, Terminal);

_this.state = {
settings: { user: { username: prefix.split('@')[1] } },
history: history.slice(),

@@ -61,0 +63,0 @@ structure: Object.assign({}, structure),

@@ -6,2 +6,4 @@ 'use strict';

});
var IS_SERVER = exports.IS_SERVER = typeof window === 'undefined';
var BACK_REGEX = exports.BACK_REGEX = /\/?\.?[\w-_]+\/\.\./;

@@ -15,2 +17,24 @@

IS_A_DIRECTORY: 'cat: $1: Is a directory'
};
var EnvVariables = exports.EnvVariables = {
TERM_PROGRAM: 'ReactBash.app',
TERM: 'reactbash-256color',
TERM_PROGRAM_VERSION: '1.4.0',
TERM_SESSION_ID: 'w0t0p1:37842145-87D9-4768-BEC3-3684BAF3A964',
USER: function USER(state) {
return state.settings.user.username;
},
PATH: '/',
PWD: function PWD(state) {
return '/' + state.cwd;
},
LANG: function LANG() {
return !IS_SERVER ? navigator.language.replace('-', '_') + '.UTF-8' : 'en_US.UTF-8';
},
HOME: '/',
LOGNAME: function LOGNAME(state) {
return state.settings.user.username;
},
OLDPWD: '/'
};

@@ -10,8 +10,17 @@ 'use strict';

exports.trim = trim;
exports.reportError = reportError;
exports.appendError = appendError;
exports.extractPath = extractPath;
exports.getDirectoryByPath = getDirectoryByPath;
exports.getEnvVariables = getEnvVariables;
var _const = require('./const');
/*
* This is a utility method for trimming the beginning
* and ending of a string of any given char.
*
* @param {string} str - the string the trim
* @param {string} char - the char to remove
* @returns {string} the trimmed string
*/
function trim(str, char) {

@@ -27,4 +36,14 @@ if (str[0] === char) {

function reportError(state, error, command) {
/*
* This is a utility method for appending an error
* message to the current state.
*
* @param {Object} state - the terminal state
* @param {string} error - the error to interpolate
* @param {string} command - the string to insert
* @returns {Object} the new terminal state
*/
function appendError(state, error, command) {
return Object.assign({}, state, {
error: true,
history: state.history.concat({

@@ -37,5 +56,8 @@ value: error.replace('$1', command)

/*
* This is a utility method for chaining the relativePath
* onto the rootPath. It includes backwards movement and
* normalization.
* This is a utility method for appending a relative path
* to a root path. Handles trimming and backtracking.
*
* @param {string} relativePath
* @param {string} rootPath
* @returns {string} the combined path
*/

@@ -60,4 +82,8 @@ function extractPath(relativePath, rootPath) {

/*
* This is a utility method for traversing the <structure>
* down the '/' separated <relativePath>
* This is a utility method for traversing the structure
* down the relative path.
*
* @param {Object} structure - the terminal file structure
* @param {string} relativePath - the path of the directory
* @returns {Object} the directory or error
*/

@@ -87,2 +113,17 @@ function getDirectoryByPath(structure, relativePath) {

return { dir: dir };
}
/*
* This is a utility method for getting the environment
* variables with the dynamic values updated with state.
*
* @param {Object} state - the terminal state
* @returns {Object} the updated env variables
*/
function getEnvVariables(state) {
return Object.keys(_const.EnvVariables).reduce(function (envVars, key) {
var value = _const.EnvVariables[key];
envVars[key] = typeof value === 'function' ? value(state) : value;
return envVars;
}, {});
}

@@ -19,2 +19,6 @@ 'use strict';

var _parser = require('./parser');
var BashParser = _interopRequireWildcard(_parser);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

@@ -35,5 +39,15 @@

/*
* This parses and executes the given <input> and returns an updated
* state object.
*
* @param {string} input - the user input
* @param {Object} state - the current terminal state
* @returns {Object} the new terminal state
*/
_createClass(Bash, [{
key: 'execute',
value: function execute(input, state) {
value: function execute(input, currentState) {
this.prevCommands.push(input);

@@ -43,5 +57,5 @@ this.prevCommandsIndex = this.prevCommands.length;

// Append input to history
var newState = Object.assign({}, state, {
history: state.history.concat({
cwd: state.cwd,
var newState = Object.assign({}, currentState, {
history: currentState.history.concat({
cwd: currentState.cwd,
value: input

@@ -51,50 +65,47 @@ })

var _parseInput = this.parseInput(input);
var commandList = BashParser.parse(input);
return this.runCommands(commandList, newState);
}
var command = _parseInput.command;
var args = _parseInput.args;
/*
* This function executes a list of command lists. The outer list
* is a dependency list parsed from the `&&` operator. The inner lists
* are groups of commands parsed from the `;` operator. If any given
* command fails, the outer list will stop executing.
*
* @param {Array} commands - the commands to run
* @param {Object} state - the terminal state
* @returns {Object} the new terminal state
*/
if (command === '') {
return newState;
} else if (this.commands[command]) {
return this.commands[command].exec(newState, args);
} else {
return Util.reportError(newState, _const.Errors.COMMAND_NOT_FOUND, input);
}
}
}, {
key: 'parseInput',
value: function parseInput(input) {
var tokens = input.trim().split(/ +/);
var command = tokens.shift();
key: 'runCommands',
value: function runCommands(commands, state) {
var _this = this;
// Short circuit if command doesn't exist
if (!this.commands[command]) return { command: command };
var errorOccurred = false;
var aliases = this.commands[command].aliases || {};
var args = {};
var anonArgPos = 0;
function parseToken(token) {
if (token[0] === '-') {
if (aliases[token]) {
var argTokens = [].concat(aliases[token]);
argTokens.forEach(parseToken);
return;
}
if (token[1] === '-') {
args[token.substr(2)] = true;
} else {
var next = tokens.shift();
args[token.substr(1)] = next;
}
/*
* This function executes a single command and marks whether an error
* occurred. If an error occurs, the following dependent commands should
* not be run.
*/
var reducer = function reducer(newState, command) {
if (command.name === '') {
return newState;
} else if (_this.commands[command.name]) {
var nextState = _this.commands[command.name].exec(newState, command);
errorOccurred = errorOccurred || nextState && nextState.error;
return nextState;
} else {
args[anonArgPos++] = token;
errorOccurred = true;
return Util.appendError(newState, _const.Errors.COMMAND_NOT_FOUND, command.name);
}
}
};
while (tokens.length > 0) {
parseToken(tokens.shift());
while (!errorOccurred && commands.length) {
var dependentCommands = commands.shift();
state = dependentCommands.reduce(reducer, state);
}
return { command: command, args: args };
return state;
}

@@ -111,3 +122,2 @@

* @param {string} state.cwd - the current working directory
*
* @returns {?string} a suggested autocomplete for the <input>

@@ -114,0 +124,0 @@ */

@@ -6,3 +6,3 @@ 'use strict';

});
exports.pwd = exports.cd = exports.mkdir = exports.cat = exports.ls = exports.clear = exports.help = undefined;
exports.printenv = exports.echo = exports.pwd = exports.cd = exports.mkdir = exports.cat = exports.ls = exports.clear = exports.help = undefined;

@@ -19,15 +19,13 @@ var _util = require('./util');

var helpCommands = ['clear', 'ls', 'cat', 'mkdir', 'cd', 'pwd'];
var helpCommands = ['clear', 'ls', 'cat', 'mkdir', 'cd', 'pwd', 'echo', 'printenv'];
var help = exports.help = {
exec: function exec(_ref) {
var history = _ref.history;
var structure = _ref.structure;
var cwd = _ref.cwd;
exec: function exec(state) {
var _state$history;
return { structure: structure, cwd: cwd,
history: history.concat.apply(history, [{ value: 'React-bash:' }, { value: 'These shell commands are defined internally. Type \'help\' to see this list.' }].concat(_toConsumableArray(helpCommands.map(function (value) {
return Object.assign({}, state, {
history: (_state$history = state.history).concat.apply(_state$history, [{ value: 'React-bash:' }, { value: 'These shell commands are defined internally. Type \'help\' to see this list.' }].concat(_toConsumableArray(helpCommands.map(function (value) {
return { value: value };
}))))
};
});
}

@@ -37,7 +35,4 @@ };

var clear = exports.clear = {
exec: function exec(_ref2) {
var structure = _ref2.structure;
var cwd = _ref2.cwd;
return { structure: structure, cwd: cwd, history: [] };
exec: function exec(state) {
return Object.assign({}, state, { history: [] });
}

@@ -47,17 +42,10 @@ };

var ls = exports.ls = {
aliases: {
'-l': '--long',
'-la': ['--long', '--all'],
'-al': ['--long', '--all'],
'-a': '--all'
},
exec: function exec(state, args) {
var history = state.history;
var structure = state.structure;
var cwd = state.cwd;
exec: function exec(state, _ref) {
var flags = _ref.flags;
var args = _ref.args;
var path = args[0] || '';
var fullPath = Util.extractPath(path, cwd);
var fullPath = Util.extractPath(path, state.cwd);
var _Util$getDirectoryByP = Util.getDirectoryByPath(structure, fullPath);
var _Util$getDirectoryByP = Util.getDirectoryByPath(state.structure, fullPath);

@@ -69,6 +57,6 @@ var err = _Util$getDirectoryByP.err;

if (err) {
return Util.reportError(state, err, path);
return Util.appendError(state, err, path);
} else {
var content = Object.keys(dir);
if (!args.all) {
if (!flags.a) {
content = content.filter(function (name) {

@@ -78,12 +66,12 @@ return name[0] !== '.';

}
if (args.long) {
return { structure: structure, cwd: cwd,
history: history.concat(content.map(function (value) {
if (flags.l) {
return Object.assign({}, state, {
history: state.history.concat(content.map(function (value) {
return { value: value };
}))
};
});
} else {
return { structure: structure, cwd: cwd,
history: history.concat({ value: content.join(' ') })
};
return Object.assign({}, state, {
history: state.history.concat({ value: content.join(' ') })
});
}

@@ -95,6 +83,4 @@ }

var cat = exports.cat = {
exec: function exec(state, args) {
var history = state.history;
var structure = state.structure;
var cwd = state.cwd;
exec: function exec(state, _ref2) {
var args = _ref2.args;

@@ -104,5 +90,5 @@ var path = args[0];

var fileName = relativePath.pop();
var fullPath = Util.extractPath(relativePath.join('/'), cwd);
var fullPath = Util.extractPath(relativePath.join('/'), state.cwd);
var _Util$getDirectoryByP2 = Util.getDirectoryByPath(structure, fullPath);
var _Util$getDirectoryByP2 = Util.getDirectoryByPath(state.structure, fullPath);

@@ -113,13 +99,13 @@ var err = _Util$getDirectoryByP2.err;

if (err) {
return Util.reportError(state, err, path);
return Util.appendError(state, err, path);
} else if (!dir[fileName]) {
return Util.reportError(state, _const.Errors.NO_SUCH_FILE, path);
return Util.appendError(state, _const.Errors.NO_SUCH_FILE, path);
} else if (!dir[fileName].hasOwnProperty('content')) {
return Util.reportError(state, _const.Errors.IS_A_DIRECTORY, path);
return Util.appendError(state, _const.Errors.IS_A_DIRECTORY, path);
} else {
return { cwd: cwd, structure: structure,
history: history.concat({
return Object.assign({}, state, {
history: state.history.concat({
value: dir[fileName].content
})
};
});
}

@@ -130,6 +116,4 @@ }

var mkdir = exports.mkdir = {
exec: function exec(state, args) {
var history = state.history;
var structure = state.structure;
var cwd = state.cwd;
exec: function exec(state, _ref3) {
var args = _ref3.args;

@@ -139,4 +123,4 @@ var path = args[0];

var newDirectory = relativePath.pop();
var fullPath = Util.extractPath(relativePath.join('/'), cwd);
var deepCopy = JSON.parse(JSON.stringify(structure));
var fullPath = Util.extractPath(relativePath.join('/'), state.cwd);
var deepCopy = JSON.parse(JSON.stringify(state.structure));

@@ -149,6 +133,6 @@ var _Util$getDirectoryByP3 = Util.getDirectoryByPath(deepCopy, fullPath);

if (dir[newDirectory]) {
return Util.reportError(state, _const.Errors.FILE_EXISTS, path);
return Util.appendError(state, _const.Errors.FILE_EXISTS, path);
} else {
dir[newDirectory] = {};
return { cwd: cwd, history: history, structure: deepCopy };
return Object.assign({}, state, { structure: deepCopy });
}

@@ -159,15 +143,13 @@ }

var cd = exports.cd = {
exec: function exec(state, args) {
var history = state.history;
var structure = state.structure;
var cwd = state.cwd;
exec: function exec(state, _ref4) {
var args = _ref4.args;
var path = args[0];
if (!path || path === '/') {
return { structure: structure, history: history, cwd: '' };
return Object.assign({}, state, { cwd: '' });
}
var fullPath = Util.extractPath(path, cwd);
var fullPath = Util.extractPath(path, state.cwd);
var _Util$getDirectoryByP4 = Util.getDirectoryByPath(structure, fullPath);
var _Util$getDirectoryByP4 = Util.getDirectoryByPath(state.structure, fullPath);

@@ -178,5 +160,5 @@ var err = _Util$getDirectoryByP4.err;

if (err) {
return Util.reportError(state, err, path);
return Util.appendError(state, err, path);
} else {
return { history: history, structure: structure, cwd: fullPath };
return Object.assign({}, state, { cwd: fullPath });
}

@@ -188,13 +170,34 @@ }

exec: function exec(state) {
var history = state.history;
var structure = state.structure;
var cwd = state.cwd;
var directory = '/' + state.cwd;
return Object.assign({}, state, {
history: state.history.concat({ value: directory })
});
}
};
var directory = '/' + cwd;
var echo = exports.echo = {
exec: function exec(state, _ref5) {
var input = _ref5.input;
return {
cwd: cwd, structure: structure,
history: history.concat({ value: directory })
};
var ECHO_LENGTH = 'echo '.length;
var envVariables = Util.getEnvVariables(state);
var value = input.slice(ECHO_LENGTH).replace(/(\$\w+)/g, function (key) {
return envVariables[key.slice(1)] || '';
});
return Object.assign({}, state, {
history: state.history.concat({ value: value })
});
}
};
var printenv = exports.printenv = {
exec: function exec(state) {
var envVariables = Util.getEnvVariables(state);
var values = Object.keys(envVariables).map(function (key) {
return { value: key + '=' + envVariables[key] };
});
return Object.assign({}, state, {
history: state.history.concat(values)
});
}
};

@@ -50,2 +50,3 @@ 'use strict';

var extensions = _ref.extensions;
var prefix = _ref.prefix;

@@ -59,2 +60,3 @@ _classCallCheck(this, Terminal);

_this.state = {
settings: { user: { username: prefix.split('@')[1] } },
history: history.slice(),

@@ -61,0 +63,0 @@ structure: Object.assign({}, structure),

@@ -6,2 +6,4 @@ 'use strict';

});
var IS_SERVER = exports.IS_SERVER = typeof window === 'undefined';
var BACK_REGEX = exports.BACK_REGEX = /\/?\.?[\w-_]+\/\.\./;

@@ -15,2 +17,24 @@

IS_A_DIRECTORY: 'cat: $1: Is a directory'
};
var EnvVariables = exports.EnvVariables = {
TERM_PROGRAM: 'ReactBash.app',
TERM: 'reactbash-256color',
TERM_PROGRAM_VERSION: '1.4.0',
TERM_SESSION_ID: 'w0t0p1:37842145-87D9-4768-BEC3-3684BAF3A964',
USER: function USER(state) {
return state.settings.user.username;
},
PATH: '/',
PWD: function PWD(state) {
return '/' + state.cwd;
},
LANG: function LANG() {
return !IS_SERVER ? navigator.language.replace('-', '_') + '.UTF-8' : 'en_US.UTF-8';
},
HOME: '/',
LOGNAME: function LOGNAME(state) {
return state.settings.user.username;
},
OLDPWD: '/'
};

@@ -10,8 +10,17 @@ 'use strict';

exports.trim = trim;
exports.reportError = reportError;
exports.appendError = appendError;
exports.extractPath = extractPath;
exports.getDirectoryByPath = getDirectoryByPath;
exports.getEnvVariables = getEnvVariables;
var _const = require('./const');
/*
* This is a utility method for trimming the beginning
* and ending of a string of any given char.
*
* @param {string} str - the string the trim
* @param {string} char - the char to remove
* @returns {string} the trimmed string
*/
function trim(str, char) {

@@ -27,4 +36,14 @@ if (str[0] === char) {

function reportError(state, error, command) {
/*
* This is a utility method for appending an error
* message to the current state.
*
* @param {Object} state - the terminal state
* @param {string} error - the error to interpolate
* @param {string} command - the string to insert
* @returns {Object} the new terminal state
*/
function appendError(state, error, command) {
return Object.assign({}, state, {
error: true,
history: state.history.concat({

@@ -37,5 +56,8 @@ value: error.replace('$1', command)

/*
* This is a utility method for chaining the relativePath
* onto the rootPath. It includes backwards movement and
* normalization.
* This is a utility method for appending a relative path
* to a root path. Handles trimming and backtracking.
*
* @param {string} relativePath
* @param {string} rootPath
* @returns {string} the combined path
*/

@@ -60,4 +82,8 @@ function extractPath(relativePath, rootPath) {

/*
* This is a utility method for traversing the <structure>
* down the '/' separated <relativePath>
* This is a utility method for traversing the structure
* down the relative path.
*
* @param {Object} structure - the terminal file structure
* @param {string} relativePath - the path of the directory
* @returns {Object} the directory or error
*/

@@ -87,2 +113,17 @@ function getDirectoryByPath(structure, relativePath) {

return { dir: dir };
}
/*
* This is a utility method for getting the environment
* variables with the dynamic values updated with state.
*
* @param {Object} state - the terminal state
* @returns {Object} the updated env variables
*/
function getEnvVariables(state) {
return Object.keys(_const.EnvVariables).reduce(function (envVars, key) {
var value = _const.EnvVariables[key];
envVars[key] = typeof value === 'function' ? value(state) : value;
return envVars;
}, {});
}
{
"name": "react-bash",
"version": "1.3.0",
"version": "1.4.0",
"description": "A configurable/extendable bash terminal component",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -29,3 +29,3 @@ # \<Terminal /\>

export const clear = {
exec: ({ structure, history, cwd }, args) => {
exec: ({ structure, history, cwd }, command) => {
return { structure, cwd, history: [] };

@@ -39,11 +39,14 @@ },

Each command is given the `state` and a parsed `args` object. Some commands can use optional or required arguments. ReactBash uses the [yargs](https://www.npmjs.com/package/yargs) approach. There are three types of arguments: `anonymous` args, `boolean` args (--), and `named` args (-). You can also alias commands for shorthands or multiple ways of writing the same argument (see the ls command for an example). To see how ReactBash parses the input, check out this fictional example that utilizes all three in order.
Each command is given the `state` and a parsed `command` object. The command object provides the `name`, `input`, `args`, and `flags` corresponding to the input. Some commands can use optional or required arguments. There are three types of args: `anonymous` args, `named` args (--), and `flag` args (-). To see how ReactBash parses the input.
For the input `foo some/path --bar -hello world`, ReactBash would parse the input as:
For the input `foo some/path -baz --hello world`, ReactBash would parse the input as:
```js
command = 'foo'
args = {
{
command = 'foo'
input: 'foo some/path -baz --hello world',
args: {
0: 'some/path',
bar: true,
hello: 'world'
hello: 'world',
},
flags: { b: true, a: true, z: true },
}

@@ -50,0 +53,0 @@ ```

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc