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

chore: updated types for sites commands #6879

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
28 changes: 10 additions & 18 deletions src/commands/sites/sites-create-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,19 @@ import { configureRepo } from '../../utils/init/config.js'
import { createRepo, getTemplatesFromGitHub, validateTemplate } from '../../utils/sites/utils.js'
import { track } from '../../utils/telemetry/index.js'
import BaseCommand from '../base-command.js'

import { RepoFromGithub, Template } from '../../utils/types.js'
import { getSiteNameInput } from './sites-create.js'

// @ts-expect-error TS(7006) FIXME: Parameter 'token' implicitly has an 'any' type.
export const fetchTemplates = async (token) => {
const templatesFromGithubOrg = await getTemplatesFromGitHub(token)
export const fetchTemplates = async (token: string): Promise<Template[]> => {
const templatesFromGithubOrg: RepoFromGithub[] = await getTemplatesFromGitHub(token)

return (
// @ts-expect-error TS(18046) - 'templatesFromGithubOrg' if of type 'unknown'
templatesFromGithubOrg
// @ts-expect-error TS(7006) FIXME: Parameter 'repo' implicitly has an 'any' type.
.filter((repo) => !repo.archived && !repo.disabled)
// @ts-expect-error TS(7006) FIXME: Parameter 'template' implicitly has an 'any' type.
.map((template) => ({
name: template.name,
sourceCodeUrl: template.html_url,
slug: template.full_name,
}))
)
return templatesFromGithubOrg
.filter((repo: RepoFromGithub) => !repo.archived && !repo.disabled)
.map((template: RepoFromGithub) => ({
name: template.name,
sourceCodeUrl: template.html_url,
slug: template.full_name,
}))
}

// @ts-expect-error TS(7031) FIXME: Binding element 'ghToken' implicitly has an 'any' ... Remove this comment to see the full error message
Expand All @@ -55,7 +49,6 @@ const getTemplateName = async ({ ghToken, options, repository }) => {
type: 'list',
name: 'templateName',
message: 'Template:',
// @ts-expect-error TS(7006) FIXME: Parameter 'template' implicitly has an 'any' type.
choices: templates.map((template) => ({
value: template.slug,
name: template.name,
Expand Down Expand Up @@ -221,7 +214,6 @@ export const sitesCreateTemplate = async (repository: string, options: OptionVal

if (options.withCi) {
log('Configuring CI')
// @ts-expect-error TS(2345) FIXME: Argument of type '{ workingDir: any; }' is not ass... Remove this comment to see the full error message
const repoData = await getRepoData({ workingDir: command.workingDir })
// @ts-expect-error TS(2532) FIXME: Object is possibly 'undefined'.
await configureRepo({ command, siteId: site.id, repoData, manual: options.manual })
Expand Down
23 changes: 10 additions & 13 deletions src/commands/sites/sites-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { configureRepo } from '../../utils/init/config.js'
import { track } from '../../utils/telemetry/index.js'
import BaseCommand from '../base-command.js'
import { link } from '../link/link.js'
import { Account } from '../../utils/types.js'

// @ts-expect-error TS(7006) FIXME: Parameter 'name' implicitly has an 'any' type.
export const getSiteNameInput = async (name) => {
export const getSiteNameInput = async (name: string | undefined): Promise<{ name: string }> => {
if (!name) {
const { name: nameInput } = await inquirer.prompt([
{
Expand All @@ -22,7 +22,7 @@ export const getSiteNameInput = async (name) => {
/^[a-zA-Z\d-]+$/.test(input || undefined) || 'Only alphanumeric characters and hyphens are allowed',
},
])
name = nameInput || ''
name = typeof nameInput === 'string' ? nameInput : ''
}

return { name }
Expand All @@ -33,16 +33,17 @@ export const sitesCreate = async (options: OptionValues, command: BaseCommand) =

await command.authenticate()

const accounts = await api.listAccountsForUser()
const accounts: Account[] = await api.listAccountsForUser()

let { accountSlug } = options
let { accountSlug }: { accountSlug?: string } = options
if (!accountSlug) {
const { accountSlug: accountSlugInput } = await inquirer.prompt([
const { accountSlug: accountSlugInput }: { accountSlug: string } = await inquirer.prompt<
Promise<{ accountSlug: string }>
>([
{
type: 'list',
name: 'accountSlug',
message: 'Team:',
// @ts-expect-error TS(7006) FIXME: Parameter 'account' implicitly has an 'any' type.
choices: accounts.map((account) => ({
value: account.slug,
name: account.name,
Expand All @@ -55,13 +56,11 @@ export const sitesCreate = async (options: OptionValues, command: BaseCommand) =
let site

// Allow the user to reenter site name if selected one isn't available
// @ts-expect-error TS(7006) FIXME: Parameter 'name' implicitly has an 'any' type.
const inputSiteName = async (name) => {
const inputSiteName = async (name?: string) => {
const { name: siteName } = await getSiteNameInput(name)

const body = {}
const body: { name?: string } = {}
if (typeof siteName === 'string') {
// @ts-expect-error TS(2339) FIXME: Property 'name' does not exist on type '{}'.
body.name = siteName.trim()
}
try {
Expand All @@ -73,7 +72,6 @@ export const sitesCreate = async (options: OptionValues, command: BaseCommand) =
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error_.status === 422) {
warn(`${siteName}.netlify.app already exists. Please try a different slug.`)
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
await inputSiteName()
} else {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
Expand Down Expand Up @@ -109,7 +107,6 @@ export const sitesCreate = async (options: OptionValues, command: BaseCommand) =

if (options.withCi) {
log('Configuring CI')
// @ts-expect-error TS(2345) FIXME: Argument of type '{ workingDir: any; }' is not ass... Remove this comment to see the full error message
const repoData = await getRepoData({ workingDir: command.workingDir })
// @ts-expect-error TS(2532) FIXME: Object is possibly 'undefined'.
await configureRepo({ command, siteId: site.id, repoData, manual: options.manual })
Expand Down
4 changes: 2 additions & 2 deletions src/utils/get-repo-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import { log } from './command-helpers.js'
* @param {string} config.workingDir
* @returns
*/
// @ts-expect-error TS(7031) FIXME: Binding element 'remoteName' implicitly has an 'an... Remove this comment to see the full error message
const getRepoData = async function ({ remoteName, workingDir }) {

const getRepoData = async function ({ remoteName, workingDir }: { remoteName?: string; workingDir: string }) {
try {
const [gitConfig, gitDirectory] = await Promise.all([
util.promisify(gitconfiglocal)(workingDir),
Expand Down
26 changes: 16 additions & 10 deletions src/utils/sites/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fetch from 'node-fetch'
import { error } from '../../utils/command-helpers.js'
import { RepoFromGithub } from '../../utils/types.js'

// @ts-expect-error TS(7006) FIXME: Parameter 'token' implicitly has an 'any' type.
export const getTemplatesFromGitHub = async (token) => {
export const getTemplatesFromGitHub = async (token: string): Promise<RepoFromGithub[]> => {
const getPublicGitHubReposFromOrg = new URL(`https://api.github.com/orgs/netlify-templates/repos`)
// GitHub returns 30 by default and we want to avoid our limit
// due to our archived repositories at any given time
Expand All @@ -12,14 +13,19 @@ export const getTemplatesFromGitHub = async (token) => {
// @ts-expect-error TS(2345) FIXME: Argument of type 'number' is not assignable to par... Remove this comment to see the full error message
getPublicGitHubReposFromOrg.searchParams.set('per_page', REPOS_PER_PAGE)

const templates = await fetch(getPublicGitHubReposFromOrg, {
method: 'GET',
headers: {
Authorization: `token ${token}`,
},
})
const allTemplates = await templates.json()

let allTemplates: RepoFromGithub[] = []
try {
const templates = await fetch(getPublicGitHubReposFromOrg, {
method: 'GET',
headers: {
Authorization: `token ${token}`,
},
})
allTemplates = (await templates.json()) as RepoFromGithub[]
} catch (error_) {
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
error(error_)
}
return allTemplates
}

Expand Down
42 changes: 42 additions & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,45 @@ export interface Request extends IncomingMessage {
}

export type Rewriter = (req: Request) => Match | null

export interface Account {
id: string
name: string
slug: string
type: string
capabilities: {
sites: {
included: number
used: number
}
collaborators: {
included: number
used: number
}
}
billing_name: string
billing_email: string
billing_details: string
billing_period: string
payment_method_id: string
type_name: string
type_id: string
owner_ids: string[]
roles_allowed: string[]
created_at: string
updated_at: string
}

export interface RepoFromGithub {
benhancock marked this conversation as resolved.
Show resolved Hide resolved
name: string
html_url: string
full_name: string
archived: boolean
disabled: boolean
}

export interface Template {
name: string
sourceCodeUrl: string
slug: string
}