Skip to content

Commit cbe8a73

Browse files
committed
refactor(cli): remove paths from getProjectId()
Remove the `paths` var from `getProjectId()` function. This means we can keep track of which call is the first/second call, without needing a `paths` var (useful when we support diagrams without a path). Intead we use `Cache.previousSelectedProjectId` and `Cache.usePreviousSelectedProjectId`.
1 parent 8a6e3a6 commit cbe8a73

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

packages/cli/src/commander.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,22 @@ function linkCmd() {
172172
const linkedDiagram = await link(existingFile, client, {
173173
cache: linkCache,
174174
title: path,
175-
async getProjectId(cache) {
175+
async getProjectId(cache, documentTitle) {
176+
if (cache.previousSelectedProjectId !== undefined) {
177+
if (cache.usePreviousSelectedProjectId === undefined) {
178+
cache.usePreviousSelectedProjectId = confirm({
179+
message: `Would you like to upload all diagrams to this project?`,
180+
default: true,
181+
});
182+
}
183+
if (await cache.usePreviousSelectedProjectId) {
184+
return cache.previousSelectedProjectId;
185+
}
186+
}
187+
176188
cache.projects = cache.projects ?? client.getProjects();
177189
const projectId = await select({
178-
message: `Select a project to upload ${path} to`,
190+
message: `Select a project to upload ${documentTitle} to`,
179191
choices: [
180192
...(await cache.projects).map((project) => {
181193
return {
@@ -189,15 +201,7 @@ function linkCmd() {
189201
],
190202
});
191203

192-
if (path === paths[0] && paths.length > 1) {
193-
const useProjectIdForAllDiagrams = await confirm({
194-
message: `Would you like to upload all ${paths.length} diagrams to this project?`,
195-
default: true,
196-
});
197-
if (useProjectIdForAllDiagrams) {
198-
cache.selectedProjectId = projectId;
199-
}
200-
}
204+
cache.previousSelectedProjectId = projectId;
201205

202206
return projectId;
203207
},

packages/cli/src/methods.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@ import { extractFrontMatter, injectFrontMatter, removeFrontMatterKeys } from './
66
/**
77
* Cached data to use when pulling/pushing/linking multiple files at once.
88
*/
9-
interface Cache {
9+
export interface Cache {
1010
/**
11-
* If set, the user has said to use the projectId to create all documents
11+
* If `true`, the user has said to use the projectId to create all documents
1212
* in.
13+
*
14+
* If `undefined`, ask the user if they want to use their first chosen project
15+
* id for every other document.
16+
*
17+
* If `false`, don't ask the user.
1318
*/
14-
selectedProjectId?: string;
19+
usePreviousSelectedProjectId?: Promise<boolean>;
20+
/**
21+
* Previously selected project ID.
22+
* Will be reused if {@link usePreviousSelectedProjectId} is `true`.
23+
*/
24+
previousSelectedProjectId?: string;
1525
/**
1626
* Cached response from {@link MermaidChart.getProjects}.
1727
*/
@@ -25,8 +35,8 @@ interface CommonOptions {
2535

2636
interface LinkOptions extends CommonOptions {
2737
/** Function that asks the user which project id they want to upload a diagram to */
28-
getProjectId: (cache: LinkOptions['cache']) => Promise<string>;
29-
// cache to be shared between link calls
38+
getProjectId: (cache: LinkOptions['cache'], documentTitle: string) => Promise<string>;
39+
// cache to be shared between link calls. This object may be modified between calls.
3040
cache: Cache;
3141
}
3242

@@ -48,7 +58,7 @@ export async function link(diagram: string, client: MermaidChart, options: LinkO
4858

4959
const { title, getProjectId, cache } = options;
5060

51-
const projectId = cache.selectedProjectId ?? (await getProjectId(cache));
61+
const projectId = await getProjectId(cache, title);
5262

5363
const createdDocument = await client.createDocument(projectId);
5464

0 commit comments

Comments
 (0)