Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add validation for readOptions with zod #826

Merged
merged 21 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"octokit": "^3.1.0",
"prettier": "^3.0.2",
"replace-in-file": "^7.0.1",
"title-case": "^3.0.3"
"title-case": "^3.0.3",
"zod": "^3.22.2"
},
"devDependencies": {
"@octokit/request-error": "^5.0.0",
Expand Down
4 changes: 3 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions src/shared/options/augmentOptionsWithExcludes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as prompts from "@clack/prompts";

import { filterPromptCancel } from "../prompts.js";
import { Options } from "../types.js";

type Base = "everything" | "minimum" | "prompt";
import { InputBase, Options } from "../types.js";

const exclusionDescriptions = {
excludeCompliance: {
Expand Down Expand Up @@ -85,7 +83,7 @@ export async function augmentOptionsWithExcludes(

const base =
options.base ??
filterPromptCancel<Base | symbol>(
filterPromptCancel<InputBase | symbol>(
await prompts.select({
message: `How much tooling would you like the template to set up for you?`,
options: [
Expand Down
144 changes: 113 additions & 31 deletions src/shared/options/readOptions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { parseArgs } from "node:util";
import { titleCase } from "title-case";
import { z } from "zod";

import { withSpinner } from "../cli/spinners.js";
import { InputBase, Options } from "../types.js";
import { Options } from "../types.js";
import { allArgOptions } from "./args.js";
import { augmentOptionsWithExcludes } from "./augmentOptionsWithExcludes.js";
import { ensureRepositoryExists } from "./ensureRepositoryExists.js";
Expand Down Expand Up @@ -35,42 +36,55 @@ export async function readOptions(args: string[]): Promise<OptionsParseResult> {
tokens: true,
});

const options = {
author: values.author as string | undefined,
base: values.base as InputBase,
createRepository: values["create-repository"] as boolean | undefined,
description: values.description as string | undefined,
email: values.email as string | undefined,
excludeCompliance: values["exclude-compliance"] as boolean | undefined,
excludeContributors: values["exclude-contributors"] as boolean | undefined,
excludeLintJson: values["exclude-lint-json"] as boolean | undefined,
excludeLintKnip: values["exclude-lint-knip"] as boolean | undefined,
excludeLintMd: values["exclude-lint-md"] as boolean | undefined,
excludeLintPackageJson: values["exclude-lint-package-json"] as
| boolean
| undefined,
excludeLintPackages: values["exclude-lint-packages"] as boolean | undefined,
excludeLintPerfectionist: values["exclude-lint-perfectionist"] as
| boolean
| undefined,
excludeLintSpelling: values["exclude-lint-spelling"] as boolean | undefined,
excludeLintYml: values["exclude-lint-yml"] as boolean | undefined,
excludeReleases: values["exclude-releases"] as boolean | undefined,
excludeRenovate: values["exclude-renovate"] as boolean | undefined,
excludeTests: values["unit-tests"] as boolean | undefined,
funding: values.funding as string | undefined,
owner: values.owner as string | undefined,
repository: values.repository as string | undefined,
const mappedOptions = {
author: values.author,
base: values.base,
createRepository: values["create-repository"],
description: values.description,
email: values.email,
excludeCompliance: values["exclude-compliance"],
excludeContributors: values["exclude-contributors"],
excludeLintJson: values["exclude-lint-json"],
excludeLintKnip: values["exclude-lint-knip"],
excludeLintMd: values["exclude-lint-md"],
excludeLintPackageJson: values["exclude-lint-package-json"],
excludeLintPackages: values["exclude-lint-packages"],
excludeLintPerfectionist: values["exclude-lint-perfectionist"],
excludeLintSpelling: values["exclude-lint-spelling"],
excludeLintYml: values["exclude-lint-yml"],
excludeReleases: values["exclude-releases"],
excludeRenovate: values["exclude-renovate"],
excludeTests: values["unit-tests"],
funding: values.funding,
owner: values.owner,
repository: values.repository,
skipGitHubApi: !!values["skip-github-api"],
skipInstall: !!values["skip-install"],
skipRemoval: !!values["skip-removal"],
skipRestore: values["skip-restore"] as boolean | undefined,
skipRestore: values["skip-restore"],
skipUninstall: !!values["skip-uninstall"],
title: values.title as string | undefined,
title: values.title,
};

const optionsParseResult = optionsSchema.safeParse(mappedOptions);

if (!optionsParseResult.success) {
return {
cancelled: true,
options: Object.entries(mappedOptions).reduce(
(acc, [key, value]) => ({
...acc,
[key]: typeof value === "string" ? undefined : value,
}),
{},
),
nowyDEV marked this conversation as resolved.
Show resolved Hide resolved
};
}

const options = optionsParseResult.data;

options.owner ??= await getPrefillOrPromptedOption(
values.owner as string | undefined,
options.owner,
"What organization or user will the repository be under?",
defaults.owner,
);
Expand Down Expand Up @@ -130,10 +144,13 @@ export async function readOptions(args: string[]): Promise<OptionsParseResult> {
const augmentedOptions = await augmentOptionsWithExcludes({
...options,
author: options.author ?? (await defaults.owner()),
description: options.description,
email: options.email ?? (await defaults.email()),
funding: options.funding ?? (await defaults.funding()),
owner: options.owner,
repository,
} as Options);
title: options.title,
});

if (!augmentedOptions) {
return {
Expand All @@ -148,3 +165,68 @@ export async function readOptions(args: string[]): Promise<OptionsParseResult> {
options: augmentedOptions,
};
}

export const optionsSchema = z
.object({
author: z.string().optional(),
base: z
.union([
z.literal("everything"),
z.literal("minimum"),
z.literal("prompt"),
])
.optional(),
createRepository: z.boolean().optional(),
description: z.string().optional(),
email: z.string().email().optional(),
excludeCompliance: z.boolean().optional(),
excludeContributors: z.boolean().optional(),
excludeLintJson: z.boolean().optional(),
excludeLintKnip: z.boolean().optional(),
excludeLintMd: z.boolean().optional(),
excludeLintPackageJson: z.boolean().optional(),
excludeLintPackages: z.boolean().optional(),
excludeLintPerfectionist: z.boolean().optional(),
excludeLintSpelling: z.boolean().optional(),
excludeLintYml: z.boolean().optional(),
excludeReleases: z.boolean().optional(),
excludeRenovate: z.boolean().optional(),
excludeTests: z.boolean().optional(),
funding: z.string().optional(),
owner: z.string().optional(),
repository: z.string().optional(),
skipGitHubApi: z.boolean(),
skipInstall: z.boolean(),
skipRemoval: z.boolean(),
skipRestore: z.boolean().optional(),
skipUninstall: z.boolean(),
title: z.string().optional(),
})
// This is necessary since we want to require schema fields, but allow values to be undefined
// https://stackoverflow.com/questions/71477015/specify-a-zod-schema-with-a-non-optional-but-possibly-undefined-field
.transform((o) => ({
nowyDEV marked this conversation as resolved.
Show resolved Hide resolved
...o,
author: o.author,
base: o.base,
createRepository: o.createRepository,
description: o.description,
email: o.email,
excludeCompliance: o.excludeCompliance,
excludeContributors: o.excludeContributors,
excludeLintJson: o.excludeLintJson,
excludeLintKnip: o.excludeLintKnip,
excludeLintMd: o.excludeLintMd,
excludeLintPackageJson: o.excludeLintPackageJson,
excludeLintPackages: o.excludeLintPackages,
excludeLintPerfectionist: o.excludeLintPerfectionist,
excludeLintSpelling: o.excludeLintSpelling,
excludeLintYml: o.excludeLintYml,
excludeReleases: o.excludeReleases,
excludeRenovate: o.excludeRenovate,
excludeTests: o.excludeTests,
funding: o.funding,
owner: o.owner,
repository: o.repository,
skipRestore: o.skipRestore,
title: o.title,
}));
1 change: 1 addition & 0 deletions src/steps/uninstallPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export async function uninstallPackages() {
"prettier",
"replace-in-file",
"title-case",
"zod",
],
packageData.dependencies,
);
Expand Down
Loading