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

Added support for object variables. Renamed withUUID to withActionOutput #14

Merged
merged 5 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Adding a new Shortcut action is relatively straightforward.
3. Move it into a `/shortcuts` directory in the root of this project.
4. Run `node scripts/parse.js` and select your Shortcut and action. This will log out the parsed Shortcut data.
5. Copy this data into a new file in `src/actions`, optionally using an existing action as a template. The file (and action) you're creating should be the name of the action as it appears in the Shortcuts app, camelCased. For example, if you were adding the 'Run Home Scene' action, the file would be named `runHomeScene.ts` and it would export a function named `runHomeScene`. If in any doubt, use the already implemented actions as guides.
6. Connect up all Shortcut options to the function parameters. Try to be as thorough as possible and make sure you cover all possible options. You'll likely need to add some new types to `src/interfaces/WF` and `src/interfaces/WF/WFWorkflowActionParameters.ts`. You'll also want to check whether or not the action can be used as a 'Magic Variable'. If it can, don't forget to wrap the exported function with the `withUUID` function (see `src/actions/number.ts` as an example).
6. Connect up all Shortcut options to the function parameters. Try to be as thorough as possible and make sure you cover all possible options. You'll likely need to add some new types to `src/interfaces/WF` and `src/interfaces/WF/WFWorkflowActionParameters.ts`. You'll also want to check whether or not the action can be used as a 'Magic Variable'. If it can, don't forget to wrap the exported function with the `withActionOutput` function (see `src/actions/number.ts` as an example).
7. Export the new file from `src/actions/index.ts`.
8. Add tests for the new action to `__tests__/actions`. Again, try to be as thorough as possible and use another action's tests as a starting point.
9. Make sure all tests pass, and that the new action is documented with JSDoc in the same way all other actions are.
Expand Down
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ npm install @joshfarrant/shortcuts-js
const fs = require('fs');

const {
actionOutput,
buildShortcut,
withVariables,
} = require('@joshfarrant/shortcuts-js');
Expand All @@ -44,7 +45,7 @@ const {
} = require('@joshfarrant/shortcuts-js/actions');

// We'll use this later to reference the output of a calculation
let calcId;
let calcVar = actionOutput();
xAlien95 marked this conversation as resolved.
Show resolved Hide resolved

// Define our list of actions
const actions = [
Expand All @@ -57,16 +58,13 @@ const actions = [
calculate({
operand: 3,
operation: '/',
}, (id) => {
// We'll use this again in a moment
calcId = id;
}),
}, calcVar),
showResult({
/**
* We can use the result of the calculation in this Shortcuts's input
* by passing the string to the 'withVariables' tag function
*/
text: withVariables`Total is ${calcId}!`,
text: withVariables`Total is ${calcVar}!`,
}),
];

Expand Down Expand Up @@ -98,6 +96,7 @@ For brevity, these examples omit the code for writing the file to disk and just

```js
const {
actionOutput,
buildShortcut,
withVariables,
} = require('@joshfarrant/shortcuts-js');
Expand All @@ -108,12 +107,10 @@ const {
showResult,
} = require('@joshfarrant/shortcuts-js/actions');

let batteryLevel;
const batteryLevel = actionOutput();

const actions = [
getBatteryLevel({}, (id) => {
batteryLevel = id;
}),
getBatteryLevel({}, batteryLevel),
conditional({
input: '<',
value: 20,
Expand Down
13 changes: 9 additions & 4 deletions __tests__/actions/addToVariable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { addToVariable } from '../../src/actions';
import { variable } from '../../src/utils/variables';

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

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

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

const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.appendvariable',
WFWorkflowActionParameters: {
WFVariableName: name,
WFVariableName: name.VariableName,
},
};
const actual = addToVariable({ name });

const actual = addToVariable({
variable: name,
});

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

import WFSerialization from '../../src/interfaces/WF/WFSerialization';
import {
actionOutput,
variable,
} from '../../src/utils/variables';

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

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

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

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

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

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',
};
it('builds a getVariable action when a magic variable is passed', () => {
const magic = actionOutput();
magic.OutputUUID = 'b74c81a8-192a-463f-a0a6-2d327963714f';

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

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

expect(actual).toEqual(expected);
});
Expand Down
13 changes: 9 additions & 4 deletions __tests__/actions/setVariable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { setVariable } from '../../src/actions';
import { variable } from '../../src/utils/variables';

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

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

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

const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.setvariable',
WFWorkflowActionParameters: {
WFVariableName: name,
WFVariableName: name.VariableName,
},
};
const actual = setVariable({ name });

const actual = setVariable({
variable: name,
});

expect(actual).toEqual(expected);
});
Expand Down
22 changes: 5 additions & 17 deletions __tests__/utils/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import {
buildShortcut,
buildShortcutTemplate,
encodeShortcut,
testUUID,
withUUID,
withActionOutput,
withVariables,
} from '../../src/utils';

Expand All @@ -17,11 +16,8 @@ import {
encodeShortcut as encodeShortcutDirect,
} from '../../src/utils/encodeShortcut';
import {
testUUID as testUUIDDirect,
} from '../../src/utils/testUUID';
import {
withUUID as withUUIDDirect,
} from '../../src/utils/withUUID';
withActionOutput as withUUIDDirect,
xAlien95 marked this conversation as resolved.
Show resolved Hide resolved
} from '../../src/utils/withActionOutput';
import {
withVariables as withVariablesDirect,
} from '../../src/utils/withVariables';
Expand Down Expand Up @@ -52,17 +48,9 @@ describe('util index.ts file', () => {
expect(actual).toEqual(expected);
});

it('exports testUUID as a named function', () => {

const actual = testUUID.toString();
const expected = testUUIDDirect.toString();

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

it('exports withUUID as a named function', () => {
it('exports withActionOutput as a named function', () => {

const actual = withUUID.toString();
const actual = withActionOutput.toString();
const expected = withUUIDDirect.toString();
xAlien95 marked this conversation as resolved.
Show resolved Hide resolved

expect(actual).toEqual(expected);
Expand Down
18 changes: 0 additions & 18 deletions __tests__/utils/testUUID.spec.ts

This file was deleted.

104 changes: 104 additions & 0 deletions __tests__/utils/variables.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import Attachment from '../../src/interfaces/WF/Attachment';
import {
actionOutput,
askWhenRun,
clipboard,
currentDate,
shortcutInput,
variable,
} from '../../src/utils/variables';

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

it('returns an ActionOutput attachment object when passed nothing', () => {
const uuid = '70a08121-f08d-46e4-a41f-102912750b11';

const actual = actionOutput();
actual.OutputUUID = uuid;

const expected: Attachment = {
OutputUUID: uuid,
Type: 'ActionOutput',
};

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

it('returns a custom named ActionOutput attachment object when passed a string', () => {
const uuid = 'dd05b2e3-6069-4c4a-900a-79ae837795d6';

const actual = actionOutput('My Result');
actual.OutputUUID = uuid;

const expected: Attachment = {
OutputName: 'My Result',
OutputUUID: uuid,
Type: 'ActionOutput',
};

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

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

it('returns a Variable attachment object when passed a string', () => {
const actual = variable('My Variable');
const expected: Attachment = {
VariableName: 'My Variable',
Type: 'Variable',
};

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

describe('askWhenRun global variable', () => {
it('is an Ask attachment', () => {
const actual = askWhenRun;
const expected: Attachment = {
Type: 'Ask',
};

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

describe('clipboard global variable', () => {
it('is an Ask attachment', () => {
const actual = clipboard;
const expected: Attachment = {
Type: 'Clipboard',
};

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

describe('currentDate global variable', () => {
it('is an Ask attachment', () => {
const actual = currentDate;
const expected: Attachment = {
Type: 'CurrentDate',
};

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

describe('shortcutInput global variable', () => {
it('is an Ask attachment', () => {
const actual = shortcutInput;
const expected: Attachment = {
Type: 'ExtensionInput',
};

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