Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
vadimdemedes committed May 20, 2023
1 parent 09cb44e commit 194dde1
Show file tree
Hide file tree
Showing 31 changed files with 91 additions and 31 deletions.
9 changes: 9 additions & 0 deletions source/index.ts
Expand Up @@ -34,6 +34,9 @@ export type Options = {
export default class Pastel {
constructor(private readonly options: Options) {}

/**
* Run the app.
*/
async run(argv: string[] = process.argv) {
const commandsDirectory = fileURLToPath(
new URL('commands', this.options.importMeta.url),
Expand Down Expand Up @@ -76,10 +79,16 @@ export default class Pastel {
}
}

/**
* Set additional metadata for an option. Must be used as an argument to `describe` function from Zod.
*/
export function option(config: CommandOptionConfig) {
return `__pastel_option_config__${JSON.stringify(config)}`;
}

/**
* Set additional metadata for an argument. Must be used as an argument to `describe` function from Zod.
*/
export function argument(config: CommandArgumentConfig) {
return `__pastel_argument_config__${JSON.stringify(config)}`;
}
Expand Down
51 changes: 51 additions & 0 deletions source/types.ts
@@ -1,25 +1,76 @@
import type {ComponentType} from 'react';

export type AppProps = {
/**
* Command's component.
*/
Component: ComponentType<{
options: Record<string, unknown>;
args: unknown[];
}>;

/**
* Props to pass to command's component.
*/
commandProps: {
/**
* Options.
*/
options: Record<string, unknown>;

/**
* Arguments.
*/
args: unknown[];
};
};

/**
* Additional metadata for an option.
*/
export type CommandOptionConfig = {
/**
* Description. If description is missing, option won't appear in the "Options" section of the help message.
*/
description?: string;

/**
* Description of a default value.
*
* @default JSON.stringify(defaultValue)
*/
defaultValueDescription?: string;

/**
* Description of a value. Replaces "value" in `--flag <value>` in help message.
*
* @default "value"
*/
valueDescription?: string;

/**
* Alias. Usually a first letter of the full option name.
*/
alias?: string;
};

export type CommandArgumentConfig = {
/**
* Argument's name. Displayed in "Usage" part of the help message. Doesn't affect how argument is parsed.
*
* @default "arg"
*/
name?: string;

/**
* Description of an argument. If description is missing, argument won't appear in the "Arguments" section of the help message.
*/
description?: string;

/**
* Description of a default value.
*
* @default JSON.stringify(defaultValue)
*/
defaultValueDescription?: string;
};
2 changes: 1 addition & 1 deletion test/fixtures/all-optional-options/commands/index.tsx
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import {Text} from 'ink';
import zod from 'zod';

export const options = z
export const options = zod
.object({
name: zod.string().describe('Name'),
})
Expand Down
Expand Up @@ -4,7 +4,7 @@ import zod from 'zod';
import {option} from '../../../../../source/index.js';

export const options = zod.object({
tag: z
tag: zod
.array(zod.string())
.default(['A', 'B'])
.describe(
Expand Down
Expand Up @@ -3,7 +3,7 @@ import {Text} from 'ink';
import zod from 'zod';
import {argument} from '../../../../../source/index.js';

export const args = z
export const args = zod
.array(zod.enum(['macOS', 'Ubuntu', 'Debian', 'Windows']))
.default(['macOS', 'Windows'])
.describe(
Expand Down
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import {Text} from 'ink';
import zod from 'zod';

export const args = z
export const args = zod
.array(zod.enum(['macOS', 'Ubuntu', 'Debian', 'Windows']))
.default(['macOS', 'Windows'])
.describe('os');
Expand Down
Expand Up @@ -3,7 +3,7 @@ import {Text} from 'ink';
import zod from 'zod';
import {argument} from '../../../../../source/index.js';

export const args = z
export const args = zod
.array(zod.enum(['Ubuntu', 'Debian']))
.describe(argument({name: 'os', description: 'Operating systems'}));

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/enum-argument/array-name/commands/index.tsx
Expand Up @@ -3,7 +3,7 @@ import {Text} from 'ink';
import zod from 'zod';
import {argument} from '../../../../../source/index.js';

export const args = z
export const args = zod
.array(zod.enum(['Ubuntu', 'Debian']))
.describe(argument({name: 'os'}));

Expand Down
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import {Text} from 'ink';
import zod from 'zod';

export const args = z
export const args = zod
.array(zod.enum(['Ubuntu', 'Debian']))
.optional()
.describe('os');
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/enum-argument/variadic/commands/index.tsx
Expand Up @@ -4,7 +4,7 @@ import zod from 'zod';

const os = zod.enum(['macOS', 'Ubuntu', 'Debian', 'Windows']);

export const args = z
export const args = zod
.tuple([os.describe('first'), os.describe('second')])
.rest(os.describe('rest'));

Expand Down
Expand Up @@ -4,7 +4,7 @@ import zod from 'zod';
import {option} from '../../../../../source/index.js';

export const options = zod.object({
os: z
os: zod
.enum(['Ubuntu', 'Debian'])
.default('Ubuntu')
.describe(
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/enum-option/default-value/commands/index.tsx
Expand Up @@ -3,7 +3,7 @@ import {Text} from 'ink';
import zod from 'zod';

export const options = zod.object({
os: z
os: zod
.enum(['Ubuntu', 'Debian'])
.default('Ubuntu')
.describe('Operating system'),
Expand Down
Expand Up @@ -3,7 +3,7 @@ import {Text} from 'ink';
import zod from 'zod';
import {argument} from '../../../../../source/index.js';

export const args = z
export const args = zod
.array(zod.number())
.default([128, 256])
.describe(
Expand Down
Expand Up @@ -3,7 +3,7 @@ import {Text} from 'ink';
import zod from 'zod';
import {argument} from '../../../../../source/index.js';

export const args = z
export const args = zod
.array(zod.number())
.describe(argument({name: 'number', description: 'Numbers'}));

Expand Down
Expand Up @@ -4,7 +4,7 @@ import zod from 'zod';
import {argument} from '../../../../../source/index.js';

export const args = zod.tuple([
z
zod
.number()
.optional()
.describe(
Expand All @@ -13,7 +13,7 @@ export const args = zod.tuple([
description: 'First',
}),
),
z
zod
.number()
.default(256)
.describe(
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/number-argument/variadic/commands/index.tsx
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import {Text} from 'ink';
import zod from 'zod';

export const args = z
export const args = zod
.tuple([zod.number().describe('first'), zod.number().describe('second')])
.rest(zod.number().describe('rest'));

Expand Down
Expand Up @@ -4,7 +4,7 @@ import zod from 'zod';
import {option} from '../../../../../source/index.js';

export const options = zod.object({
size: z
size: zod
.number()
.default(128)
.describe(
Expand Down
Expand Up @@ -4,7 +4,7 @@ import zod from 'zod';
import {option} from '../../../../../source/index.js';

export const options = zod.object({
tag: z
tag: zod
.set(zod.string())
.default(new Set(['A', 'B']))
.describe(
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/set-option/default-value/commands/index.tsx
Expand Up @@ -3,7 +3,7 @@ import {Text} from 'ink';
import zod from 'zod';

export const options = zod.object({
tag: z
tag: zod
.set(zod.string())
.default(new Set(['A', 'B']))
.describe('Tags'),
Expand Down
Expand Up @@ -3,7 +3,7 @@ import {Text} from 'ink';
import zod from 'zod';
import {argument} from '../../../../../source/index.js';

export const args = z
export const args = zod
.array(zod.string())
.default(['Jane', 'Hopper'])
.describe(
Expand Down
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import {Text} from 'ink';
import zod from 'zod';

export const args = z
export const args = zod
.array(zod.string())
.default(['Jane', 'Hopper'])
.describe('traits');
Expand Down
Expand Up @@ -3,7 +3,7 @@ import {Text} from 'ink';
import zod from 'zod';
import {argument} from '../../../../../source/index.js';

export const args = z
export const args = zod
.array(zod.string())
.describe(argument({name: 'traits', description: 'Traits'}));

Expand Down
Expand Up @@ -4,7 +4,7 @@ import zod from 'zod';
import {argument} from '../../../../../source/index.js';

export const args = zod.tuple([
z
zod
.string()
.optional()
.describe(
Expand All @@ -13,7 +13,7 @@ export const args = zod.tuple([
description: 'Name',
}),
),
z
zod
.string()
.default('Hopper')
.describe(
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/string-argument/variadic/commands/index.tsx
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import {Text} from 'ink';
import zod from 'zod';

export const args = z
export const args = zod
.tuple([zod.string().describe('name'), zod.string().describe('surname')])
.rest(zod.string().describe('traits'));

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/string-option/alias/commands/index.tsx
Expand Up @@ -4,7 +4,7 @@ import zod from 'zod';
import {option} from '../../../../../source/index.js';

export const options = zod.object({
name: z
name: zod
.string()
.regex(/[a-z]+/i, {
message: 'Invalid value',
Expand Down
Expand Up @@ -4,7 +4,7 @@ import zod from 'zod';
import {option} from '../../../../../source/index.js';

export const options = zod.object({
name: z
name: zod
.string()
.regex(/[a-z]+/i, {
message: 'Invalid value',
Expand Down
Expand Up @@ -3,7 +3,7 @@ import {Text} from 'ink';
import zod from 'zod';

export const options = zod.object({
name: z
name: zod
.string()
.regex(/[a-z]+/i, {
message: 'Invalid value',
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/string-option/description/commands/index.tsx
Expand Up @@ -4,7 +4,7 @@ import zod from 'zod';
import {option} from '../../../../../source/index.js';

export const options = zod.object({
name: z
name: zod
.string()
.regex(/[a-z]+/i, {
message: 'Invalid value',
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/string-option/optional/commands/index.tsx
Expand Up @@ -3,7 +3,7 @@ import {Text} from 'ink';
import zod from 'zod';

export const options = zod.object({
name: z
name: zod
.string()
.regex(/[a-z]+/i, {
message: 'Invalid value',
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/string-option/required/commands/index.tsx
Expand Up @@ -3,7 +3,7 @@ import {Text} from 'ink';
import zod from 'zod';

export const options = zod.object({
name: z
name: zod
.string()
.regex(/[a-z]+/i, {
message: 'Invalid value',
Expand Down
Expand Up @@ -4,7 +4,7 @@ import zod from 'zod';
import {option} from '../../../../../source/index.js';

export const options = zod.object({
name: z
name: zod
.string()
.regex(/[a-z]+/i, {
message: 'Invalid value',
Expand Down

0 comments on commit 194dde1

Please sign in to comment.