Starting Statocyst
Kicking off our Typescript Linux interface lib
Today we're starting Statocyst, which we'll use for our project Squid. Statocyst is going to provide a typescript api for interacting with underlying Linux tools via their command line interface. First thing we'll do is create a new repo https://github.com/joyautomation/statocyst:
gh create repo joyautomation/statocyst --public --clone
which will creeate a folder for us to start in so we'll cd statocyst into that empty folder.
After that we run deno init to create amain.ts, main.test.ts, an initial deno.json. I like to name my entry point index.ts for libraries (instead of main.ts which I reserver for applications) so we'll:
mv main.ts index.ts
rm main.test.ts
> index.ts # clear out the contents of main.ts, we don't need them 🤗
The first thing we're going to need is a command.ts which is where we'll build our Linux command handler, which looks like this:
/* command.ts */
import { createErrorString, createFail, createSuccess, Result } from "@joyautomation/dark-matter";
export const runCommand = async (...args: ConstructorParameters<typeof Deno.Command>): Promise<Result<string>> => {
const cmd = new Deno.Command(...args);
try {
const result = await cmd.output();
const textDecoder = new TextDecoder();
if (result.stderr.length > 0) {
return createFail(createErrorString(textDecoder.decode(result.stderr)));
}
return createSuccess(textDecoder.decode(result.stdout));
} catch (error) {
return createFail(createErrorString(error));
}
}
We're using our Dark Matter library, which provides us with some nice functional programming error handling utilities. The runCommand function will run a Linux command and return a Result with stdout or a failure with a message from stderr. This will be a great froundation for us to build our integrations.