Skip to content

Commit

Permalink
fix: improve env types
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Aug 31, 2021
1 parent c58be32 commit e7a7c1d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { Hook, Hooks } from '@oclif/core/lib/interfaces/hooks';
import { cli } from 'cli-ux';
import { Duration, env } from '@salesforce/kit';
import { Deployer } from './deployer';
import { EnvList, EnvDisplay, JsonObject, Deploy, Login, TableObject } from './types';
import { EnvList, EnvDisplay, JsonObject, Deploy, Login } from './types';

interface SfHooks<T = unknown> extends Hooks {
'sf:env:list': EnvList.HookMeta<T & TableObject>;
'sf:env:list': EnvList.HookMeta<T & JsonObject>;
'sf:env:display': EnvDisplay.HookMeta<T & JsonObject>;
'sf:deploy': Deploy.HookMeta<T & Deployer>;
'sf:login': Login.HookMeta;
Expand Down
53 changes: 43 additions & 10 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,65 @@ export type JsonObject = {
};

/**
* Describes an object that is intended to be rendered in a table, which requires
* that no value is an array.
* By default `sf env display` will display every key/value returned by the hook.
* It will also Title Case every key for readability. To overwrite this behavior,
* you can specify how a key should be displayed to the user.
*
* Example:
* `{ data: { theURL: 'https://example.com' } }
* Renders as:
* Key Value
* The URL https://example.com
*
* Example:
* `{ keys: { theURL: 'Url' }, data: { theURL: 'https://example.com' } }
* Renders as:
* Key Value
* Url https://example.com
*/
export type TableObject = {
[key: string]: string | number | boolean | null | undefined;
};

export namespace EnvDisplay {
type Keys<T> = Record<keyof T, string>;

export type HookMeta<T extends JsonObject> = {
options: { targetEnv: string };
return: T;
return: { data: T; keys?: Keys<T> };
};
}

/**
* By default `sf env list` will render a table with all the data provided by the hook.
* The columns of the table are derived from the keys of the provided data. These column
* headers are Title Cased for readability. To overwrite a column name specifiy it the `keys`
* property.
*
* Example:
* `{ title: 'My Envs', data: { username: 'foo', theURL: 'https://example.com' } }
* Renders as:
* My Envs
* ================================
* | Username | The URL
* | foo | https://example.com
*
* Example:
* `{ keys: { theURL: 'Url', username: 'Name' }, data: { username: 'foo', theURL: 'https://example.com' } }
* Renders as:
* My Envs
* ============================
* | Name | Url
* | foo | https://example.com
*/
export namespace EnvList {
type Table<T extends TableObject> = {
type Table<T extends JsonObject> = {
data: T[];
columns: Array<keyof T>;
keys?: Record<keyof T, string>;
title: string;
};

type Options = {
all: boolean;
};

export type HookMeta<T extends TableObject> = {
export type HookMeta<T extends JsonObject> = {
options: Options;
return: Array<Table<T>>;
};
Expand Down

0 comments on commit e7a7c1d

Please sign in to comment.