Skip to content

Commit

Permalink
[cli] Use getters for Now props that mirror Client (vercel#6838)
Browse files Browse the repository at this point in the history
This will prevent any issues where the `Now` instance is out-of-sync with the `Client` instance, for example, during a re-auth where a new auth token is issued. Also reduces a bit of code which is nice.
  • Loading branch information
TooTallNate authored Oct 13, 2021
1 parent e01a1ce commit b8f8289
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 76 deletions.
10 changes: 1 addition & 9 deletions packages/cli/src/commands/billing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ const help = () => {
};

let argv;
let debug;
let apiUrl;
let subcommand;

export default async client => {
Expand All @@ -65,8 +63,6 @@ export default async client => {

argv._ = argv._.slice(1);

debug = argv['--debug'];
apiUrl = client.apiUrl;
subcommand = argv._[0];

if (argv['--help'] || !subcommand) {
Expand All @@ -76,17 +72,13 @@ export default async client => {

const {
output,
authConfig: { token },
config: { currentTeam },
} = client;

const start = new Date();
const creditCards = new NowCreditCards({
apiUrl,
token,
debug,
client,
currentTeam,
output,
});

let contextName = null;
Expand Down
15 changes: 3 additions & 12 deletions packages/cli/src/commands/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ import { Output } from '../../util/output';
import { help } from './args';

export default async (client: Client) => {
const {
apiUrl,
output,
authConfig: { token },
} = client;
const { output } = client;

let argv = null;

Expand Down Expand Up @@ -157,10 +153,8 @@ export default async (client: Client) => {
}
}

const { log, debug, error, warn } = output;
const debugEnabled = argv['--debug'];
const { log, debug, error, warn, isTTY } = output;

const { isTTY } = process.stdout;
const quiet = !isTTY;

// check paths
Expand Down Expand Up @@ -437,11 +431,8 @@ export default async (client: Client) => {

const currentTeam = org?.type === 'team' ? org.id : undefined;
const now = new Now({
apiUrl,
token,
debug: debugEnabled,
client,
currentTeam,
output,
});
let deployStamp = stamp();
let deployment = null;
Expand Down
15 changes: 3 additions & 12 deletions packages/cli/src/commands/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,8 @@ export default async function main(client: Client) {
return 1;
}

const {
authConfig: { token },
output,
apiUrl,
config,
} = client;

const debugEnabled = argv['--debug'];
const { output, config } = client;

const { print, log, error, note, debug, spinner } = output;

if (argv._.length > 2) {
Expand Down Expand Up @@ -126,10 +120,7 @@ export default async function main(client: Client) {
spinner(`Fetching deployments in ${chalk.bold(contextName)}`);

const now = new Now({
apiUrl,
token,
debug: debugEnabled,
output,
client,
currentTeam,
});
const start = Date.now();
Expand Down
7 changes: 1 addition & 6 deletions packages/cli/src/commands/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ export default async function main(client: Client) {
argv._ = argv._.slice(1);

const {
apiUrl,
authConfig: { token },
output,
config: { currentTeam },
} = client;
Expand Down Expand Up @@ -245,11 +243,8 @@ export default async function main(client: Client) {
}

const now = new Now({
apiUrl,
token,
debug: argv['--debug'],
client,
currentTeam,
output,
});
const start = Date.now();

Expand Down
11 changes: 3 additions & 8 deletions packages/cli/src/commands/secrets.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ const help = () => {

// Options
let argv;
let debug;
let apiUrl;
let subcommand;
let nextTimestamp;

Expand All @@ -90,8 +88,6 @@ const main = async client => {

argv._ = argv._.slice(1);

debug = argv.debug;
apiUrl = client.apiUrl;
subcommand = argv._[0];
nextTimestamp = argv.next;

Expand All @@ -101,7 +97,6 @@ const main = async client => {
}

const {
authConfig: { token },
output,
config: { currentTeam },
} = client;
Expand All @@ -118,7 +113,7 @@ const main = async client => {
throw err;
}

return run({ output, token, contextName, currentTeam, client });
return run({ output, contextName, currentTeam, client });
};

export default async client => {
Expand All @@ -130,8 +125,8 @@ export default async client => {
}
};

async function run({ output, token, contextName, currentTeam, client }) {
const secrets = new NowSecrets({ apiUrl, token, debug, currentTeam, output });
async function run({ output, contextName, currentTeam, client }) {
const secrets = new NowSecrets({ client, currentTeam });
const args = argv._.slice(1);
const start = Date.now();
const { 'test-warning': testWarningFlag } = argv;
Expand Down
39 changes: 21 additions & 18 deletions packages/cli/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,21 @@ import chalk from 'chalk';
import ua from './ua';
import processDeployment from './deploy/process-deployment';
import highlight from './output/highlight';
import createOutput, { Output } from './output';
import { responseError } from './error';
import stamp from './output/stamp';
import { APIError, BuildError } from './errors-ts';
import printIndications from './print-indications';
import { Org } from '../types';
import { VercelConfig } from './dev/types';
import { FetchOptions, isJSONObject } from './client';
import Client, { FetchOptions, isJSONObject } from './client';
import { Dictionary } from '@vercel/client';

export interface NowOptions {
apiUrl: string;
token?: string;
client: Client;
url?: string | null;
currentTeam?: string | null;
output: Output;
forceNew?: boolean;
withCache?: boolean;
debug?: boolean;
}

export interface CreateOptions {
Expand Down Expand Up @@ -66,39 +62,46 @@ export interface ListOptions {
export default class Now extends EventEmitter {
url: string | null;
currentTeam: string | null;
_apiUrl: string;
_token?: string;
_debug: boolean;
_client: Client;
_forceNew: boolean;
_withCache: boolean;
_output: Output;
_syncAmount?: number;
_files?: any[];
_missing?: string[];

constructor({
apiUrl,
token,
client,
url = null,
currentTeam = null,
forceNew = false,
withCache = false,
debug = false,
output = createOutput({ debug }),
}: NowOptions) {
super();

this.url = url;
this._token = token;
this._debug = debug;
this._client = client;
this._forceNew = forceNew;
this._withCache = withCache;
this._output = output;
this._apiUrl = apiUrl;
this._onRetry = this._onRetry.bind(this);
this.currentTeam = currentTeam;
}

get _apiUrl() {
return this._client.apiUrl;
}

get _token() {
return this._client.authConfig.token;
}

get _output() {
return this._client.output;
}

get _debug() {
return this._client.output.isDebugEnabled();
}

async create(
paths: string[],
{
Expand Down
13 changes: 2 additions & 11 deletions packages/cli/src/util/link/setup-and-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,7 @@ export default async function setupAndLink(
projectName,
}: SetupAndLinkOptions
): Promise<ProjectLinkResult> {
const {
authConfig: { token },
localConfig,
apiUrl,
output,
config,
} = client;
const { localConfig, output, config } = client;
const debug = output.isDebugEnabled();

const isFile = !isDirectory(path);
Expand Down Expand Up @@ -153,10 +147,7 @@ export default async function setupAndLink(

if (isZeroConfig) {
const now = new Now({
apiUrl,
token,
debug,
output,
client,
currentTeam: config.currentTeam,
});
const createArgs: CreateOptions = {
Expand Down

0 comments on commit b8f8289

Please sign in to comment.