Skip to content

Instantly share code, notes, and snippets.

View john-osullivan's full-sized avatar
Focusing

John O'Sullivan john-osullivan

Focusing
View GitHub Profile
@john-osullivan
john-osullivan / no-stutter.ts
Created September 16, 2019 23:32
Dev Diaries #2: Demonstration of stuttering
// Stuttering
const args: AuthMethods.LoginMethod.LoginArgs = { ... };
// No stuttering
const args: Auth.Login.Args = { ... };
@john-osullivan
john-osullivan / easy-imports.ts
Last active September 17, 2019 02:04
Dev Diaries #2 - Module layout for easy imports
// The type reference is so wordy! This isn't convenient.
import Types from '@eximchain/dappbot-types';
const args: Types.Methods.Auth.Login.Args = { ... };
// We can make the reference more compact by importing a
// more specific slice of the overall types.
import Auth from '@eximchain/dappbot-types/spec/methods';
const args: Auth.Login.Args = { ... };
@john-osullivan
john-osullivan / yargs-command.ts
Created November 6, 2019 21:27
Dev Diaries #3 - yargs.command()
yargs.command(
'lorem <arg1> <arg2>',
'This cmd description is ipsum.',
function(yargs) {
// Builder: Perform addt'l command configuration
},
function(argv) {
// Handler: When user correctly calls this cmd, this fxn will receive the parsed opstring
}
);
@john-osullivan
john-osullivan / yargs-module.ts
Created November 6, 2019 21:33
Dev Diaries #3 - yargs module for cmd config
export const command = 'lorem <arg1> <arg2>';
export const desc = 'This cmd description is ipsum';
export function builder(yargs) {
// Add addt'l command configuration
}
export function handler(argv) {
// Use parsed argv to perform any actions desired
@john-osullivan
john-osullivan / project-structure.md
Last active November 7, 2019 01:36
Dev Diaries #3 - Project Structure

/src
└── cli.tsx: Calls "yargs.commandDir('./rootCmds')" to configure top-level command
└── /rootCmds: 
|   └── api.tsx: This cmd calls `yargs.commandDir()` on each of the dirs below
|   └── /authCmds
|   |   └── beginPassReset.tsx: "dappbot api auth/beginPassReset" will send a password reset request
|   |   └── ...
|   └── /privateCmds
| | └── createDapp.tsx: "dappbot api private/createDapp <...args>" will create a Dapp
@john-osullivan
john-osullivan / goto-handler.tsx
Created November 7, 2019 04:51
Dev Diaries #3 - CLI goto handler
...
export function handler(args:ArgShape<DappNameArg>) {
const { DappName, hubUrl } = args;
const url = `${hubUrl}/${DappName}`;
// Each and every handler function has a
// render call like this one.
render(
@john-osullivan
john-osullivan / package-for-custom-name.json
Created November 7, 2019 05:02
Dev Diaries #3 - package.json "bin" syntaxes
{
"name": "@eximchain/dappbot-cli",
"bin": {
"dappbot": "build/cli.js"
},
"..." : "..."
}
@john-osullivan
john-osullivan / commandConfig.tsx
Created November 7, 2019 06:46
Dev Diary #3 - CLI File Loader
/**
* Middlware: Listen for any options whose name ends in "Path", and if found,
* read the file's contents as a string and add it as an argument
* whose name ends in "File". For instance, "authPath" will yield
* an "authFile" string, which can be JSON.parse()d to get the
* actual authData.
* @param args
*/
export function loadFileFromPath(args:ArgShape): ArgShape {
const pathKeys = Object.keys(args).filter(key => key.indexOf('Path') > -1);
@john-osullivan
john-osullivan / goto-handler.tsx
Last active November 7, 2019 06:51
Dev Diaries #3 - CLI goto handler
import { SuccessBox } from '../ui';
export function handler(args:ArgShape<DappNameArg>) {
const { DappName, hubUrl } = args;
const url = `${hubUrl}/${DappName}`;
render(
<SuccessBox result={{
message: `Opening ${url} now!`
}} />
@john-osullivan
john-osullivan / authStorage.ts
Created November 7, 2019 06:56
Dev Diary #3 - CLI authStorage
import { AuthData, newAuthData } from '@eximchain/dappbot-types/spec/user';
export const AUTH_FILENAME = 'dappbotAuthData.json';
export const AUTH_FILE_PATH = path.resolve(__dirname, `./${AUTH_FILENAME}`);
export function initAuthFile() {
if (!fs.existsSync(AUTH_FILE_PATH)) {
fs.writeFileSync(AUTH_FILE_PATH, JSON.stringify(newAuthData(), null, 2));
}
}