Skip to content

Commit

Permalink
Merge pull request #9 from daltonmenezes/feature/allowed-shells
Browse files Browse the repository at this point in the history
Feature/allowed shells
  • Loading branch information
daltonmenezes authored Aug 24, 2020
2 parents ded26b5 + 4098950 commit d66cc42
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
- [init](#init)
- [Rules](#rules)
- [Commands](#commands)
- [Allowed Shells](#allowed-shells)
- [clearCommand](#clearcommand)
- [commandSeparator](#commandseparator)
- [Contributing](#contributing)
Expand Down Expand Up @@ -70,7 +71,8 @@ All you have to do to get started is to create an array of objects called `init`
init: [
{
rule: 'once',
commands: ['cd ~/Desktop', 'ls']
commands: ['cd ~/Desktop', 'ls'],
allowedShells: ['zsh', 'bash']
}
]
```
Expand All @@ -84,7 +86,8 @@ module.exports = {
init: [
{
rule: 'once',
commands: ['cd ~/Desktop', 'ls']
commands: ['cd ~/Desktop', 'ls'],
allowedShells: ['zsh', 'bash']
},
{
rule: 'windows',
Expand Down Expand Up @@ -122,6 +125,14 @@ Example:
commands: ['cd ~/Desktop', 'ls']
```

#### Allowed Shells
An array of allowed shells to restrict the commands to be executed.

Example:
```js
allowedShells: ['zsh', 'bash']
```
> You can omit this property or let the array empty if you would like to allow the commands run for all shells.
### clearCommand

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hyper-init",
"version": "1.4.0",
"version": "1.5.0",
"description": "The ultimate and most complete extension to initialize commands before and after Hyper terminal starts",
"main": "index.js",
"scripts": {
Expand Down
14 changes: 14 additions & 0 deletions src/is/is-allowed-shell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { getShellName } = require('../get-shell-name')

exports.isAllowedShell = (props) => {
const { init, key, app, uid } = props

const shellName = getShellName({ browserWindow: app, uid })
const restrictions = init[key].allowedShells
const isEmptyArray = restrictions && restrictions.length === 0

const shouldAllow =
!restrictions || isEmptyArray || restrictions.includes(shellName)

return shouldAllow ? true : false
}
3 changes: 3 additions & 0 deletions src/rules/rules-handler.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const rules = require('./index')
const { isRule } = require('../is/is-rule')
const { isAllowedShell } = require('../is/is-allowed-shell')

exports.rulesHandler = (...[props]) => {
const rule = props.init[props.key].rule

if (!isAllowedShell(props)) return

if (Array.isArray(rule)) {
return rule.map((rule) => (isRule(rule) ? rules[rule](props) : ''))
}
Expand Down

0 comments on commit d66cc42

Please sign in to comment.