Skip to content

Node.js Cody Tutorial Exercise I

Alexander Miertsch edited this page Feb 14, 2021 · 3 revisions

In the tutorial preparation we've set up folder structure, exercises, InspectIO tutorial board and Cody. Your folder structure should look like this:

cody-tutorial
|__ cody-bot
|__ exercises

The Cody Server should be running and listen on http://localhost:3311:

cd cody-tutorial/cody-bot
docker-compose ps

   Name                 Command               State           Ports         
----------------------------------------------------------------------------
iio_cody_1   docker-entrypoint.sh npm r ...   Up      0.0.0.0:3311->3000/tcp

Note: A file watcher restarts the server on changes. If something does not work as expected try to restart the server manually and take a look at the logs: docker-compose stop && docker-compose up -d && docker-compose logs -f

Test-Driven Exercises

Navigate to the tutorial project in a second console cd cody-tutorial/exercises and run the first exercise using docker-compose run --rm exercises npm run exercise1

Of course the test case is failing. it is looking for a Command called AddBuilding and that's the first file we want to generate from an InspectIO event map. You might have noticed the commented hooks in codyconfig.ts. What we need is a onCommandHook, so let's create one!

Create Command Hook

# current dir: cody-tutorial/cody-bot

# Create interface for Context object
echo -e "export interface Context {\n  srcFolder: string;\n}\n" >> src/hooks/Context.ts

# Create onCommandHook.ts
touch src/hooks/onCommandHook.ts

Open src/hooks/onCommandHook.ts in your favorite editor and copy this content into it:

import {CodyHook} from "../board/code";
import {Node} from "../board/graph";
import {CodyResponse, isCodyError} from "../general/response";
import {nodeNameToPascalCase} from "../utils/string";
import {Context} from "./Context";
import {writeFileSync} from "../utils/filesystem";

/**
 * onCommandHook
 *
 * @param {Node}    command  Information about command card received from InspectIO
 * @param {Context} ctx      Context object populated in codyconfig.ts
 * @returns Promise<CodyResponse>
 */
export const onCommandHook: CodyHook<Context> = async (command: Node, ctx: Context): Promise<CodyResponse> => {
  // Cody ships with some util functions for common tasks
  const cmdName = nodeNameToPascalCase(command);
  const cmdFilename = cmdName+'.ts';
  // ctx.srcFolder is set in codyconfig.ts
  const cmdFile = ctx.srcFolder + `/Command/${cmdFilename}`;
  let successDetails = 'Checklist\n\n';

  const content = `
export interface ${cmdName} {}  
`;

  // Util functions return Cody-Error-Responses in case something went wrong
  const writeFileErr = writeFileSync(cmdFile, content);

  if(isCodyError(writeFileErr)) {
    return writeFileErr;
  }

  successDetails = successDetails + `✔️ Command file ${cmdFile} written\n`;

  // Cody responses can be formatted similar to browser console formatting
  // @see https://developers.google.com/web/tools/chrome-devtools/console/console-write#styling_console_output_with_css
  return {
    cody: `Wasn't easy, but command ${cmdName} should work now!`,
    details: ['%c'+successDetails, 'color: #73dd8e;font-weight: bold'],
  }
}

Register Hook in codyconfig

To activate the hook, we need to register it in codyconfig.ts:

import {onCommandHook} from "./src/hooks/onCommandHook";

const CodyConfig = {
  context: {
    srcFolder: '/exercises/src',
  },
  hooks: {
    onCommand: onCommandHook,
//    onAggregate: onAggregateHook,
//    onEvent: onEventHook,
//    onPolicy: onPolicyHook,
//    onDocument: onDocumentHook,
  }
};

module.exports = CodyConfig;

Modeling On Event Map

Cody is now able to turn information from command stickies (on an InspectIO event map) into Typescript interfaces. Switch to the Cody Tutorial board in InspectIO and add a command sticky (blue one) with label AddBuilding. Right click on the newly created sticky and choose Trigger Cody from context menu. In the Cody Console you can check the response. Cody should tell you: Wasn't easy, but command AddBuilding should work now!.

Awesome, it works! Rerun docker-compose run --rm exercises npm run exercise1 in cody-tutorial/exercises. It should turn green now. The test verifies that a cody-tutorial/exercises/src/Command/AddBuilding.ts has been generated by Cody.

Recap Exercise I

In exercise I we implemented our first Cody Hook and registered it in codyconfig.ts. The hook is called with information received from an InspectIO event map and with a user defined context object, which can be used to pass configuration options (like a source path) to each hook. We can trigger the hook from InspectIO by selecting an appropriate sticky on an event map and Trigger Cody from context menu.

Next - Exercise II

Clone this wiki locally