Skip to content
This repository has been archived by the owner on Oct 30, 2020. It is now read-only.

Commit

Permalink
feat: build and install as a scripting library, expose runCommand as a
Browse files Browse the repository at this point in the history
library's handler
  • Loading branch information
phuctm97 committed Apr 26, 2020
1 parent 734432c commit af6afab
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import main from '@bin/main';
import runCommand from '@bin/runCommand';

/**
* System exit codes.
Expand Down Expand Up @@ -37,3 +38,19 @@ scpt.run = (args) => {
$.exit(ExitCodes.FAILURE);
}
};

/**
* The JXAX scripting library's `runCommand` handler.
*
* @description
* This fucntion is exported as `runCommand` handler in `JXAX` scripting library which is
* accessible to all OSA scripting components via `Library` function or equivalences.
*
* @example
* const jxax = Library('JXAX');
* jxax.runCommand('desktops.changePicture', {picture: 'Catalina Rock'});
*
* @param {string} uses The command to use.
* @param {object} args The arguments to supply to the `Command`.
*/
scpt.runCommand = runCommand;
2 changes: 1 addition & 1 deletion src/bin/listCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function shortFormat(commands) {
}

/**
* List all available `Command`(s).
* List all JXAX `Command`(s).
*
* @param {string} format Output format (`short`/`long`).
*/
Expand Down
17 changes: 17 additions & 0 deletions src/bin/runCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { has } from 'lodash';
import { runCommand as run } from '@core/workflow';
import library from '@bin/library';

/**
* Run a JXAX `Command`.
*
* @param {string} uses The command to use.
* @param {object} args The arguments to supply to the `Command`.
*/
export default function runCommand(uses, args) {
if (!has(library, uses)) {
throw new Error(`unknown command '${uses}'`);
}

run(library[uses], args);
}
35 changes: 32 additions & 3 deletions src/core/workflow/command.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { omit } from 'lodash';
import {
isNil, isEmpty, isArray, every, omit, has,
} from 'lodash';
import { validate } from '@utils';

/**
Expand Down Expand Up @@ -29,7 +31,34 @@ export function validateCommandArgs(command, args) {

/**
* Run a `Command`.
*
* @param {Command} command The command.
* @param {object} args The arguments.
*/
export function runCommand() {
throw new Error('Not implemented');
export function runCommand(command, args) {
// Run validation.
const errors = validateCommandArgs(command, args);
if (!isNil(errors)) { // Validation failed.
const errorMsgs = [];
Object.entries(errors).forEach(([arg, errs]) => {
errorMsgs.push(...errs.map((err) => `${arg} ${err}`));
});

throw new Error(errorMsgs.join(', '));
}

// Run command.
const results = command.run(args);

// Parse resutls.
if (isArray(results) && !isEmpty(results)
&& every(results, (detail) => has(detail, 'step') && has(detail, 'succeeded'))) {
// Parse result details and get error messages (if any).
const errorMsgs = results.filter((detail) => !detail.succeeded)
.map((detail) => `${detail.step} ${detail.error}`);

if (!isEmpty(errorMsgs)) { // Error occurred.
throw new Error(errorMsgs.join(', '));
}
}
}
2 changes: 2 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ module.exports = (env) => ({
banner: [
'const scpt=this;',
'this.run=function(args){};',
'this.runCommand=function(command, args){};',
'function run(args){return this.run(args);}',
'function runCommand(command, args){return this.runCommand(command, args);}',
].join(''),
raw: true,
}),
Expand Down

0 comments on commit af6afab

Please sign in to comment.