Skip to content

Commit

Permalink
feat: handle gracefully ctrl+c (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker authored Mar 30, 2024
1 parent cd85dd7 commit 0b85496
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const run = async () => {
const template = await promptTemplate(action);
const starter = action === 'app' ? await promptStarter() : null;
const name = await promptProjectName();

await generate({
action,
name,
Expand Down
11 changes: 10 additions & 1 deletion src/services/generate.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {red} from 'kleur';
import path from 'node:path';
import prompts from 'prompts';
import {populate} from '../utils/populate.utils';
import {assertAnswerCtrlC} from '../utils/prompts.utils';

interface Template {
key: string;
Expand All @@ -25,6 +26,8 @@ export const promptTemplate = async (type: 'app' | 'website'): Promise<Template>
choices: collection.map(({title, key}) => ({title, value: key}))
});

assertAnswerCtrlC(template);

const item = collection.find(({key}) => key === template);

if (isNullish(item)) {
Expand All @@ -51,15 +54,21 @@ export const promptStarter = async () => {
}
]
});

assertAnswerCtrlC(starter);

return starter;
};

export const promptProjectName = async () => {
export const promptProjectName = async (): Promise<string> => {
const {name}: {name: string} = await prompts({
type: 'text',
name: 'name',
message: 'What is the name of your project?'
});

assertAnswerCtrlC(name);

return name;
};

Expand Down
1 change: 1 addition & 0 deletions src/utils/populate.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const getGitHubFiles = async (templatePath: string): Promise<FileDescriptor[]> =
};

const createDirectory = async (where: string | null) => {
// Where equals null means "create in current directory"
if (where === null) {
return;
}
Expand Down

0 comments on commit 0b85496

Please sign in to comment.