Skip to content

Commit

Permalink
feat: added yes options to automatically answer to optional prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
mgordel committed Aug 29, 2024
1 parent 93dfff7 commit c71c0f1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/new/new.action.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NewOptions, newProjectNameError, newProjectNameRegEx } from "./new.options";
import { newDefaultOptions, NewOptions, newProjectNameError, newProjectNameRegEx } from "./new.options";

import { prompt } from "enquirer";
import { join } from "path";
Expand Down Expand Up @@ -29,16 +29,20 @@ async function getName(providedName: string): Promise<string> {
return providedName;
}

async function getVersion(providedVersion?: string): Promise<string> {
async function getVersion(providedVersion?: string, useDefault = false): Promise<string> {
if (typeof providedVersion === "string") {
return providedVersion;
}

if (useDefault) {
return newDefaultOptions.appVersion;
}

const result = (await prompt({
type: "input",
name: "version",
message: "Project version",
initial: "1.0.0",
initial: newDefaultOptions.appVersion,
})) as { version: string };

return result.version;
Expand Down Expand Up @@ -74,16 +78,20 @@ async function getTemplate(providedTemplate?: string): Promise<string> {
return providedTemplate;
}

async function getDescription(providedDescription?: string): Promise<string> {
async function getDescription(providedDescription?: string, useDefault = false): Promise<string> {
if (typeof providedDescription === "string") {
return providedDescription;
}

if (useDefault) {
return newDefaultOptions.description;
}

const result = (await prompt({
type: "input",
name: "description",
message: "Project description",
initial: "An unique and awesome application that runs on Golem Network",
initial: newDefaultOptions.description,
})) as { description: string };

return result.description;
Expand Down Expand Up @@ -168,8 +176,8 @@ export async function newAction(providedName: string, options: NewOptions) {
process.exit(1);
}

const description = await getDescription(options.description);
const version = await getVersion(options.appVersion);
const description = await getDescription(options.description, options.yes);
const version = await getVersion(options.appVersion, options.yes);
const author = options.author;

console.log(`Creating a new Golem app in ${projectPath}.`);
Expand Down
1 change: 1 addition & 0 deletions src/new/new.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ newCommand
.option("-d, --description <text>", "Description of the project.")
.option("-a, --author <name>", "Author of the project.") // TODO: try to read it from git config?
.option("-v, --app-version <version>", "Version of the project.")
.option("-y, --yes", 'Automatically answer "yes" to optional prompts that installer might print on the command line')
.option("--skip-install", "Do not install dependencies.")
// TODO: implement list-templates?
// .option("-l, --list-templates", "List available project templates.")
Expand Down
6 changes: 6 additions & 0 deletions src/new/new.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ export interface NewOptions {
template?: string;
appVersion?: string;
skipInstall?: boolean;
yes: boolean;
}

export const newProjectNameRegEx = /^[a-z0-9-_]+$/;
export const newProjectNameError =
"Project name may only contain lower case letters, numbers, hyphens and underscores.";

export const newDefaultOptions = {
description: "An unique and awesome application that runs on Golem Network",
appVersion: "1.0.0",
};

0 comments on commit c71c0f1

Please sign in to comment.