Skip to content

Commit 418d364

Browse files
committed
perf(cli): move function def out of loop
Micro-op, I'm mainly doing this so the code is cleaner, not so that the code is faster!
1 parent cbe8a73 commit 418d364

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

packages/cli/src/commander.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import confirm from '@inquirer/confirm';
1111
import input from '@inquirer/input';
1212
import select, { Separator } from '@inquirer/select';
1313
import { type Config, defaultConfigPath, readConfig, writeConfig } from './config.js';
14-
import { link, pull, push } from './methods.js';
14+
import { link, type LinkOptions, pull, push } from './methods.js';
1515

1616
/**
1717
* Global configuration option for the root Commander Command.
@@ -164,15 +164,9 @@ function linkCmd() {
164164
.action(async (paths, _options, command) => {
165165
const optsWithGlobals = command.optsWithGlobals<CommonOptions>();
166166
const client = await createClient(optsWithGlobals);
167-
const linkCache = {};
168-
169-
for (const path of paths) {
170-
const existingFile = await readFile(path, { encoding: 'utf8' });
171167

172-
const linkedDiagram = await link(existingFile, client, {
173-
cache: linkCache,
174-
title: path,
175-
async getProjectId(cache, documentTitle) {
168+
// Ask the user which project they want to upload each diagram to
169+
const getProjectId: LinkOptions['getProjectId'] = async (cache, documentTitle) => {
176170
if (cache.previousSelectedProjectId !== undefined) {
177171
if (cache.usePreviousSelectedProjectId === undefined) {
178172
cache.usePreviousSelectedProjectId = confirm({
@@ -204,7 +198,17 @@ function linkCmd() {
204198
cache.previousSelectedProjectId = projectId;
205199

206200
return projectId;
207-
},
201+
};
202+
203+
const linkCache = {};
204+
205+
for (const path of paths) {
206+
const existingFile = await readFile(path, { encoding: 'utf8' });
207+
208+
const linkedDiagram = await link(existingFile, client, {
209+
cache: linkCache,
210+
title: path,
211+
getProjectId,
208212
});
209213

210214
await writeFile(path, linkedDiagram, { encoding: 'utf8' });

packages/cli/src/methods.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ interface CommonOptions {
3333
title: string;
3434
}
3535

36-
interface LinkOptions extends CommonOptions {
36+
/**
37+
* Options to pass to {@link link}.
38+
*/
39+
export interface LinkOptions extends CommonOptions {
3740
/** Function that asks the user which project id they want to upload a diagram to */
3841
getProjectId: (cache: LinkOptions['cache'], documentTitle: string) => Promise<string>;
3942
// cache to be shared between link calls. This object may be modified between calls.

0 commit comments

Comments
 (0)