Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Support Named Variables. Add appendVariable, getVariable, runSshScript, vibrate, waitToReturn actions. #8

Merged
merged 7 commits into from
Nov 21, 2018
21 changes: 21 additions & 0 deletions __tests__/actions/appendVariable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { appendVariable } from '../../src/actions';

describe('appendVariable function', () => {

it('is a function', () => {
expect(typeof appendVariable).toBe('function');
});

it('builds a appendVariable action when a name is passed', () => {
const name = 'Test Variable';
const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.appendvariable',
WFWorkflowActionParameters: {
WFVariableName: name,
},
};
const actual = appendVariable({ name });

expect(actual).toEqual(expected);
});
});
91 changes: 91 additions & 0 deletions __tests__/actions/getVariable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { getVariable } from '../../src/actions';

import WFSerialization from '../../src/interfaces/WF/WFSerialization';

describe('getVariable function', () => {

it('is a function', () => {
expect(typeof getVariable).toBe('function');
});

it('builds a getVariable action when no variable is passed', () => {
const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.getvariable',
WFWorkflowActionParameters: {},
};
const actual = getVariable({});

expect(actual).toEqual(expected);
});

it('builds a getVariable action when a variable name is passed', () => {
const name = 'variable';

const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.getvariable',
WFWorkflowActionParameters: {
WFVariable: {
Value: {
Type: 'Variable',
VariableName: name,
},
WFSerializationType: 'WFTextTokenAttachment',
},
},
};

const actual = getVariable({ variable: name });

expect(actual).toEqual(expected);
});

it('builds a getVariable action when a variable UUID is passed', () => {
const uuid = 'b74c81a8-192a-463f-a0a6-2d327963714f';

const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.getvariable',
WFWorkflowActionParameters: {
WFVariable: {
Value: {
OutputUUID: uuid,
Type: 'ActionOutput',
},
WFSerializationType: 'WFTextTokenAttachment',
},
},
};

const actual = getVariable({ variable: uuid });

expect(actual).toEqual(expected);
});

it('builds a getVariable action when a variable object is passed', () => {
const uuid = 'b74c81a8-192a-463f-a0a6-2d327963714f';

const variableObject: WFSerialization = {
Value: {
OutputUUID: uuid,
Type: 'ActionOutput',
},
WFSerializationType: 'WFTextTokenAttachment',
};

const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.getvariable',
WFWorkflowActionParameters: {
WFVariable: {
Value: {
OutputUUID: uuid,
Type: 'ActionOutput',
},
WFSerializationType: 'WFTextTokenAttachment',
},
},
};

const actual = getVariable({ variable: variableObject });

expect(actual).toEqual(expected);
});
});
36 changes: 36 additions & 0 deletions __tests__/actions/runSshScript.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { runSshScript } from '../../src/actions';

describe('runSshScript function', () => {

it('is a function', () => {
expect(typeof runSshScript).toBe('function');
});

it('builds a runSshScript action when options are passed', () => {
const host = '192.168.1.1';
const password = 'root';
const port = '22';
const script = 'uptime';
const user = 'root';

const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.runsshscript',
WFWorkflowActionParameters: {
WFSSHHost: host,
WFSSHPassword: password,
WFSSHPort: port,
WFSSHScript: script,
WFSSHUser: user,
},
};
const actual = runSshScript({
host,
password,
port,
script,
user,
});

expect(actual).toEqual(expected);
});
});
19 changes: 19 additions & 0 deletions __tests__/actions/vibrate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { vibrate } from '../../src/actions';

describe('vibrate function', () => {

it('is a function', () => {
expect(typeof vibrate).toBe('function');
});

it('builds a vibrate action when no options are passed', () => {
const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.vibrate',
WFWorkflowActionParameters: {},
};
const actual = vibrate({});

expect(actual).toEqual(expected);
});

});
19 changes: 19 additions & 0 deletions __tests__/actions/waitToReturn.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { waitToReturn } from '../../src/actions';

describe('waitToReturn function', () => {

it('is a function', () => {
expect(typeof waitToReturn).toBe('function');
});

it('builds a waitToReturn action when no options are passed', () => {
const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.waittoreturn',
WFWorkflowActionParameters: {},
};
const actual = waitToReturn({});

expect(actual).toEqual(expected);
});

});
25 changes: 25 additions & 0 deletions src/actions/appendVariable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/** @module actions */

import WFWorkflowAction from '../interfaces/WF/WFWorkflowAction';

type AppendVariableOptions = {
name: string;
};

/**
* Add to Variable Action. Appends this action's input to the specified variable, creating the variable if it does not exist.
* @param {Object} options
* @param {string} options.name
*/
const appendVariable = (
{
name,
}: AppendVariableOptions,
): WFWorkflowAction => ({
WFWorkflowActionIdentifier: 'is.workflow.actions.appendvariable',
WFWorkflowActionParameters: {
WFVariableName: name,
},
});

export default appendVariable;
55 changes: 55 additions & 0 deletions src/actions/getVariable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/** @module actions */

import WFWorkflowAction from '../interfaces/WF/WFWorkflowAction';
import WFSerialization from '../interfaces/WF/WFSerialization';

import { testUUID } from '../utils';

type GetVariableOptions = {
variable: WFSerialization | string;
};

const formatSerialization = (variable: WFSerialization | string): WFSerialization => {
// Already serialized
if (typeof(variable) !== 'string') {
Archez marked this conversation as resolved.
Show resolved Hide resolved
return variable;
}

// Magic variable
if (testUUID(variable)) {
return {
Value: {
OutputUUID: variable,
Type: 'ActionOutput',
},
WFSerializationType: 'WFTextTokenAttachment',
}
// Static variable
} else {
return {
Value: {
Type: 'Variable',
VariableName: variable,
},
WFSerializationType: 'WFTextTokenAttachment'
}
}
};

/**
* Get Variable Action. Gets the value of the specified variable and passes it to the next action.
* @param {Object} options
* @param {string} options.variable
*/
const getVariable = (
{
variable,
}: GetVariableOptions,
): WFWorkflowAction => ({
WFWorkflowActionIdentifier: 'is.workflow.actions.getvariable',
WFWorkflowActionParameters: {
WFVariable: formatSerialization(variable)
}
});

export default getVariable;
10 changes: 10 additions & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import appendVariable from './appendVariable';
import ask from './ask';
import calculate from './calculate';
import chooseFromMenu from './chooseFromMenu';
Expand All @@ -11,9 +12,11 @@ import getContentsOfUrl from './getContentsOfUrl';
import getDictionaryValue from './getDictionaryValue';
import getName from './getName';
import getType from './getType';
import getVariable from './getVariable';
import nothing from './nothing';
import number from './number';
import runShortcut from './runShortcut';
import runSshScript from './runSshScript';
import setAirplaneMode from './setAirplaneMode';
import setBrightness from './setBrightness';
import setLowPowerMode from './setLowPowerMode';
Expand All @@ -23,10 +26,13 @@ import setVariable from './setVariable';
import showResult from './showResult';
import text from './text';
import url from './url';
import vibrate from './vibrate';
import viewContentGraph from './viewContentGraph';
import wait from './wait';
import waitToReturn from './waitToReturn';

export {
appendVariable,
ask,
calculate,
chooseFromMenu,
Expand All @@ -40,9 +46,11 @@ export {
getDictionaryValue,
getName,
getType,
getVariable,
nothing,
number,
runShortcut,
runSshScript,
setAirplaneMode,
setBrightness,
setLowPowerMode,
Expand All @@ -52,6 +60,8 @@ export {
showResult,
text,
url,
vibrate,
viewContentGraph,
wait,
waitToReturn,
};
42 changes: 42 additions & 0 deletions src/actions/runSshScript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/** @module actions */

import WFSerialization from '../interfaces/WF/WFSerialization';
import WFWorkflowAction from '../interfaces/WF/WFWorkflowAction';

type RunSshScriptOptions = {
host: WFSerialization | string;
password: WFSerialization | string;
port: WFSerialization | string;
script: WFSerialization | string;
user: WFSerialization | string;
};

/**
* Run SSH Script Action. Runs a script on a remote computer over SSH.
* @param {Object} options
* @param {string} options.host
* @param {string} options.password
* @param {string} options.port
* @param {string} options.script
* @param {string} options.user
*/
const runSshScript = (
{
host,
password,
port,
script,
user,
}: RunSshScriptOptions,
): WFWorkflowAction => ({
WFWorkflowActionIdentifier: 'is.workflow.actions.runsshscript',
WFWorkflowActionParameters: {
WFSSHHost: host,
WFSSHPassword: password,
WFSSHPort: port,
WFSSHScript: script,
WFSSHUser: user,
},
});

export default runSshScript;
18 changes: 18 additions & 0 deletions src/actions/vibrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @module actions */

import WFWorkflowAction from '../interfaces/WF/WFWorkflowAction';

type VibrateOptions = {};

/**
* Vibrate Action. Vibrates the device for a short amount of time.
* @param {Object} [options]
*/
const vibrate = (
{}: VibrateOptions,
): WFWorkflowAction => ({
WFWorkflowActionIdentifier: 'is.workflow.actions.vibrate',
WFWorkflowActionParameters: {},
});

export default vibrate;
Loading