Skip to content

Commit

Permalink
feat(package/cli): graceful user exit
Browse files Browse the repository at this point in the history
  • Loading branch information
vicary committed Feb 10, 2025
1 parent 879f605 commit 718707a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-roses-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gqty/cli': patch
---

graceful user exit
59 changes: 39 additions & 20 deletions packages/cli/src/commands/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,12 @@ export const addCommand = (command: Command) => {
};

const promptEndpoints = async (defaultEndpoint?: string) => {
const endpoints = await inquirer.input({
message: 'Where is your GraphQL endpoint or schema files?',
default: defaultEndpoint,
});
const endpoints = await inquirer
.input({
message: 'Where is your GraphQL endpoint or schema files?',
default: defaultEndpoint,
})
.catch(terminateOnInterrupt);

return endpoints
.split(/[,\s+]/)
Expand All @@ -353,41 +355,58 @@ const promptEndpoints = async (defaultEndpoint?: string) => {
};

const promptTarget = async (defaultTarget: string) => {
const target = await inquirer.input({
message: 'Where should the client be generated?',
default: defaultTarget,
});
const target = await inquirer
.input({
message: 'Where should the client be generated?',
default: defaultTarget,
})
.catch(terminateOnInterrupt);

return target;
};

const promptFrameworks = async () => {
const frameworks = await inquirer.checkbox({
message: `Pick the frontend frameworks in use:`,
choices: [{ value: 'react' }, { value: 'solid-js' }],
});
const frameworks = await inquirer
.checkbox({
message: `Pick the frontend frameworks in use:`,
choices: [{ value: 'react' }, { value: 'solid-js' }],
})
.catch(terminateOnInterrupt);

return frameworks as SupportedFrameworks[];
};

const promptSubscriptions = async (defaultValue?: string) => {
const subscriptions = await inquirer.input({
message: 'Do you need a subscription client? (Enter "-" to skip)',
default: defaultValue?.trim() || undefined,
});
const subscriptions = await inquirer
.input({
message: 'Do you need a subscription client? (Enter "-" to skip)',
default: defaultValue?.trim() || undefined,
})
.catch(terminateOnInterrupt);

return subscriptions?.trim().replace(/^-$/, '') || false;
};

const promptTypescript = async (defaultValue: boolean) => {
const typescript = await inquirer.confirm({
message: 'Do you want a TypeScript client over vanilla.js?',
default: defaultValue,
});
const typescript = await inquirer
.confirm({
message: 'Do you want a TypeScript client over vanilla.js?',
default: defaultValue,
})
.catch(terminateOnInterrupt);

return typescript;
};

const terminateOnInterrupt = (e: unknown) => {
if (e instanceof Error && e.name === 'ExitPromptError') {
logger.info('Goodbye 👋');
process.exit();
}

throw e;
};

const terminateWithError = (e: unknown) => {
if (e instanceof Error) {
logger.error(e.message);
Expand Down

0 comments on commit 718707a

Please sign in to comment.