@@ -5,12 +5,12 @@ import {
5
5
InvalidArgumentError ,
6
6
} from '@commander-js/extra-typings' ;
7
7
import { readFile , writeFile } from 'fs/promises' ;
8
- import { extractFrontMatter , removeFrontMatterKeys , injectFrontMatter } from './frontmatter.js' ;
9
8
import { MermaidChart } from '@mermaidchart/sdk' ;
10
9
11
10
import input from '@inquirer/input' ;
12
11
import select , { Separator } from '@inquirer/select' ;
13
12
import { type Config , defaultConfigPath , readConfig , writeConfig } from './config.js' ;
13
+ import { link , pull , push } from './methods.js' ;
14
14
15
15
/**
16
16
* Global configuration option for the root Commander Command.
@@ -156,58 +156,45 @@ function logout() {
156
156
} ) ;
157
157
}
158
158
159
- function link ( ) {
159
+ function linkCmd ( ) {
160
160
return createCommand ( 'link' )
161
161
. description ( 'Link the given Mermaid diagram to Mermaid Chart' )
162
162
. addArgument ( new Argument ( '<path>' , 'The path of the file to link.' ) )
163
163
. action ( async ( path , _options , command ) => {
164
164
const optsWithGlobals = command . optsWithGlobals < CommonOptions > ( ) ;
165
165
const client = await createClient ( optsWithGlobals ) ;
166
+ const linkCache = { } ;
167
+
166
168
const existingFile = await readFile ( path , { encoding : 'utf8' } ) ;
167
- const frontmatter = extractFrontMatter ( existingFile ) ;
168
-
169
- if ( frontmatter . metadata . id ) {
170
- throw new CommanderError (
171
- /*exitCode=*/ 1 ,
172
- 'EALREADY_LINKED' ,
173
- 'This document already has an `id` field' ,
174
- ) ;
175
- }
176
169
177
- const projects = await client . getProjects ( ) ;
178
-
179
- const projectId = await select ( {
180
- message : 'Select a project to upload your document to' ,
181
- choices : [
182
- ...projects . map ( ( project ) => {
183
- return {
184
- name : project . title ,
185
- value : project . id ,
186
- } ;
187
- } ) ,
188
- new Separator (
189
- `Or go to ${ new URL ( '/app/projects' , client . baseURL ) } to create a new project` ,
190
- ) ,
191
- ] ,
170
+ const linkedDiagram = await link ( existingFile , client , {
171
+ cache : linkCache ,
172
+ title : path ,
173
+ async getProjectId ( cache ) {
174
+ cache . projects = cache . projects ?? client . getProjects ( ) ;
175
+ const projectId = await select ( {
176
+ message : `Select a project to upload ${ path } to` ,
177
+ choices : [
178
+ ...( await cache . projects ) . map ( ( project ) => {
179
+ return {
180
+ name : project . title ,
181
+ value : project . id ,
182
+ } ;
183
+ } ) ,
184
+ new Separator (
185
+ `Or go to ${ new URL ( '/app/projects' , client . baseURL ) } to create a new project` ,
186
+ ) ,
187
+ ] ,
188
+ } ) ;
189
+ return projectId ;
190
+ } ,
192
191
} ) ;
193
192
194
- const createdDocument = await client . createDocument ( projectId ) ;
195
-
196
- const code = injectFrontMatter ( existingFile , { id : createdDocument . documentID } ) ;
197
-
198
- await Promise . all ( [
199
- writeFile ( path , code , { encoding : 'utf8' } ) ,
200
- client . setDocument ( {
201
- projectID : createdDocument . projectID ,
202
- documentID : createdDocument . documentID ,
203
- title : path ,
204
- code : existingFile ,
205
- } ) ,
206
- ] ) ;
193
+ await writeFile ( path , linkedDiagram , { encoding : 'utf8' } ) ;
207
194
} ) ;
208
195
}
209
196
210
- function pull ( ) {
197
+ function pullCmd ( ) {
211
198
return createCommand ( 'pull' )
212
199
. description ( 'Pulls a document from from Mermaid Chart' )
213
200
. addArgument ( new Argument ( '<path>' , 'The path of the file to pull.' ) )
@@ -216,21 +203,8 @@ function pull() {
216
203
const optsWithGlobals = command . optsWithGlobals < CommonOptions > ( ) ;
217
204
const client = await createClient ( optsWithGlobals ) ;
218
205
const text = await readFile ( path , { encoding : 'utf8' } ) ;
219
- const frontmatter = extractFrontMatter ( text ) ;
220
206
221
- if ( frontmatter . metadata . id === undefined ) {
222
- throw new Error ( 'Diagram has no id, have you run `link` yet?' ) ;
223
- }
224
-
225
- const uploadedFile = await client . getDocument ( {
226
- documentID : frontmatter . metadata . id ,
227
- } ) ;
228
-
229
- if ( uploadedFile . code === undefined ) {
230
- throw new Error ( 'Diagram has no code, please use push first' ) ;
231
- }
232
-
233
- const newFile = injectFrontMatter ( uploadedFile . code , { id : frontmatter . metadata . id } ) ;
207
+ const newFile = await pull ( text , client , { title : path } ) ;
234
208
235
209
if ( text === newFile ) {
236
210
console . log ( `✅ - ${ path } is up to date` ) ;
@@ -246,38 +220,16 @@ function pull() {
246
220
} ) ;
247
221
}
248
222
249
- function push ( ) {
223
+ function pushCmd ( ) {
250
224
return createCommand ( 'push' )
251
225
. description ( 'Push a local diagram to Mermaid Chart' )
252
226
. addArgument ( new Argument ( '<path>' , 'The path of the file to push.' ) )
253
227
. action ( async ( path , _options , command ) => {
254
228
const optsWithGlobals = command . optsWithGlobals < CommonOptions > ( ) ;
255
229
const client = await createClient ( optsWithGlobals ) ;
256
230
const text = await readFile ( path , { encoding : 'utf8' } ) ;
257
- const frontmatter = extractFrontMatter ( text ) ;
258
-
259
- if ( frontmatter . metadata . id === undefined ) {
260
- throw new Error ( 'Diagram has no id, have you run `link` yet?' ) ;
261
- }
262
-
263
- // TODO: check if file has changed since last push and print a warning
264
- const existingDiagram = await client . getDocument ( {
265
- documentID : frontmatter . metadata . id ,
266
- } ) ;
267
231
268
- // due to MC-1056, try to remove YAML frontmatter if we can
269
- const diagramToUpload = removeFrontMatterKeys ( text , new Set ( [ 'id' ] ) ) ;
270
-
271
- if ( existingDiagram . code === diagramToUpload ) {
272
- console . log ( `✅ - ${ path } is up to date` ) ;
273
- } else {
274
- await client . setDocument ( {
275
- projectID : existingDiagram . projectID ,
276
- documentID : existingDiagram . documentID ,
277
- code : diagramToUpload ,
278
- } ) ;
279
- console . log ( `✅ - ${ path } was pushed` ) ;
280
- }
232
+ await push ( text , client , { title : path } ) ;
281
233
} ) ;
282
234
}
283
235
@@ -297,7 +249,7 @@ export function createCommanderCommand() {
297
249
. addCommand ( whoami ( ) )
298
250
. addCommand ( login ( ) )
299
251
. addCommand ( logout ( ) )
300
- . addCommand ( link ( ) )
301
- . addCommand ( pull ( ) )
302
- . addCommand ( push ( ) ) ;
252
+ . addCommand ( linkCmd ( ) )
253
+ . addCommand ( pullCmd ( ) )
254
+ . addCommand ( pushCmd ( ) ) ;
303
255
}
0 commit comments