@jsnix/pastel
Version:
Framework for effortlessly building Ink apps
71 lines • 2.52 kB
JavaScript
import { fileURLToPath } from 'node:url';
import process from 'node:process';
import { Command } from 'commander';
import { readPackageUp } from 'read-package-up';
import generateCommand from './generate-command.js';
import readCommands from './read-commands.js';
import generateCommands from './generate-commands.js';
import App from './_app.js';
import readCustomApp from './read-custom-app.js';
export default class Pastel {
options;
#render;
constructor(options) {
this.options = options;
}
get renderOptions() {
return this.options.renderOptions;
}
set render(render) {
this.#render = render;
}
get render() {
return this.#render;
}
/**
* Run the app.
*/
async run(argv = process.argv) {
const commandsDirectory = fileURLToPath(new URL('commands', this.options.importMeta.url));
const appComponent = (await readCustomApp(commandsDirectory)) ?? App;
const program = new Command();
const commands = await readCommands(commandsDirectory);
const indexCommand = commands.get('index');
if (indexCommand) {
generateCommand(program, indexCommand, { appComponent, app: this });
commands.delete('index');
}
generateCommands(program, commands, { appComponent, app: this });
if (this.options.name) {
program.name(this.options.name);
}
const package_ = await readPackageUp();
const version = this.options.version ?? package_?.packageJson.version;
if (version) {
program.version(version, '-v, --version', 'Show version number');
}
const description = indexCommand?.description ??
this.options.description ??
package_?.packageJson.description ??
'';
program.description(description);
program.helpOption('-h, --help', 'Show help');
await program.parseAsync(argv);
return this.#render;
}
}
/**
* Set additional metadata for an option. Must be used as an argument to `describe` function from Zod.
*/
export function option(config) {
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) {
return `__pastel_argument_config__${JSON.stringify(config)}`;
}
export * from './types.js';
export { readCommands };
//# sourceMappingURL=index.js.map