Skip to content

Commit e6a4f59

Browse files
authoredDec 18, 2024··
fix: remove extra call to accounts endpoint across cli (#6947)
* fix: remove second call to accounts to make status a little faster * chore: remove temp files * fix: don't await all promises when there's just one * chore: add types for accounts * fix: remove unused ts ignore * fix: use types we already have * chore: prettier * fix: remove accounts api extra call from sites:create * fix: remove extra call to accounts endpoint from sites:create-template
1 parent a355ab7 commit e6a4f59

File tree

7 files changed

+14
-25
lines changed

7 files changed

+14
-25
lines changed
 

‎bin/run.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { argv } from 'process'
33

44
import updateNotifier from 'update-notifier'
55

6-
import { runProgram } from '../dist/utils/run-program.js'
6+
import { createMainCommand } from '../dist/commands/main.js'
77
import { error } from '../dist/utils/command-helpers.js'
88
import getPackageJson from '../dist/utils/get-package-json.js'
9-
import { createMainCommand } from '../dist/commands/main.js'
9+
import { runProgram } from '../dist/utils/run-program.js'
1010

1111
// 12 hours
1212
const UPDATE_CHECK_INTERVAL = 432e5

‎src/commands/base-command.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ export default class BaseCommand extends Command {
596596
token,
597597
...apiUrlOpts,
598598
})
599-
const { buildDir, config, configPath, repositoryRoot, siteInfo } = cachedConfig
599+
const { accounts, buildDir, config, configPath, repositoryRoot, siteInfo } = cachedConfig
600600
let { env } = cachedConfig
601601
if (flags.offlineEnv) {
602602
env = {}
@@ -642,6 +642,7 @@ export default class BaseCommand extends Command {
642642
const configFilePath = configPath || join(this.workingDir, 'netlify.toml')
643643

644644
actionCommand.netlify = {
645+
accounts,
645646
// api methods
646647
api,
647648
apiOpts,

‎src/commands/sites/sites-create-template.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import path from 'node:path'
2+
import { fileURLToPath } from 'node:url'
3+
14
import { OptionValues } from 'commander'
25
import inquirer from 'inquirer'
36
import pick from 'lodash/pick.js'
47
import { render } from 'prettyjson'
58
import { v4 as uuid } from 'uuid'
6-
import path from 'node:path'
7-
import { fileURLToPath } from 'node:url'
89

910
import {
1011
chalk,
@@ -30,7 +31,7 @@ import BaseCommand from '../base-command.js'
3031
import { getSiteNameInput } from './sites-create.js'
3132

3233
export const sitesCreateTemplate = async (repository: string, options: OptionValues, command: BaseCommand) => {
33-
const { api } = command.netlify
34+
const { accounts, api } = command.netlify
3435
await command.authenticate()
3536

3637
const { globalConfig } = command.netlify
@@ -52,7 +53,6 @@ export const sitesCreateTemplate = async (repository: string, options: OptionVal
5253
error(`${getTerminalLink(chalk.bold(templateName), githubLink)} is not a valid GitHub template`)
5354
return
5455
}
55-
const accounts = await api.listAccountsForUser()
5656

5757
let { accountSlug } = options
5858

‎src/commands/sites/sites-create.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ export const getSiteNameInput = async (name: string | undefined): Promise<{ name
2929
}
3030

3131
export const sitesCreate = async (options: OptionValues, command: BaseCommand) => {
32-
const { api } = command.netlify
32+
const { accounts, api } = command.netlify
3333

3434
await command.authenticate()
3535

36-
const accounts: Account[] = await api.listAccountsForUser()
37-
3836
let { accountSlug }: { accountSlug?: string } = options
3937
if (!accountSlug) {
4038
const { accountSlug: accountSlugInput }: { accountSlug: string } = await inquirer.prompt<

‎src/commands/status/status.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { chalk, error, exit, getToken, log, logJson, warn, APIError } from '../.
66
import BaseCommand from '../base-command.js'
77

88
export const status = async (options: OptionValues, command: BaseCommand) => {
9-
const { api, globalConfig, site, siteInfo } = command.netlify
9+
const { accounts, api, globalConfig, site, siteInfo } = command.netlify
1010
const current = globalConfig.get('userId')
1111
const [accessToken] = await getToken()
1212

@@ -23,12 +23,10 @@ export const status = async (options: OptionValues, command: BaseCommand) => {
2323
Current Netlify User │
2424
──────────────────────┘`)
2525

26-
let accounts
2726
let user
2827

2928
try {
30-
// eslint-disable-next-line @typescript-eslint/no-extra-semi
31-
;[accounts, user] = await Promise.all([api.listAccountsForUser(), api.getCurrentUser()])
29+
user = await api.getCurrentUser()
3230
} catch (error_) {
3331
if ((error_ as APIError).status === 401) {
3432
error('Your session has expired. Please try to re-authenticate by running `netlify logout` and `netlify login`.')
@@ -45,7 +43,6 @@ export const status = async (options: OptionValues, command: BaseCommand) => {
4543
}
4644
const teamsData = {}
4745

48-
// @ts-expect-error TS(7006) FIXME: Parameter 'team' implicitly has an 'any' type.
4946
accounts.forEach((team) => {
5047
// @ts-expect-error TS(7053) FIXME: Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
5148
teamsData[team.name] = team.roles_allowed.join(' ')

‎src/commands/types.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { NetlifyAPI } from 'netlify'
44

55
import type { FrameworksAPIPaths } from "../utils/frameworks-api.ts";
66
import StateConfig from '../utils/state-config.js'
7+
import { Account } from "../utils/types.ts";
78

89

910
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -57,6 +58,7 @@ export type EnvironmentVariables = Record<string, { sources: EnvironmentVariable
5758
* The netlify object inside each command with the state
5859
*/
5960
export type NetlifyOptions = {
61+
accounts: Account[]
6062
// poorly duck type the missing api functions
6163
api: NetlifyAPI & Record<string, (...args: $TSFixMe) => Promise<$TSFixMe>>
6264
apiOpts: $TSFixMe

‎src/utils/types.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,7 @@ export interface Account {
168168
name: string
169169
slug: string
170170
type: string
171-
capabilities: {
172-
sites: {
173-
included: number
174-
used: number
175-
}
176-
collaborators: {
177-
included: number
178-
used: number
179-
}
180-
}
171+
capabilities: Record<string, { included: string; used: string }>
181172
billing_name: string
182173
billing_email: string
183174
billing_details: string

1 commit comments

Comments
 (1)

github-actions[bot] commented on Dec 18, 2024

@github-actions[bot]

📊 Benchmark results

  • Dependency count: 1,227
  • Package size: 318 MB
  • Number of ts-expect-error directives: 875
Please sign in to comment.