Skip to content

Commit 9811ecb

Browse files
committed
feat(cli): support multiple files on pull/push
Support pulling/pushing multiple files at once with the `@mermaidchart/cli` CLI, e.g. like `mermaid-chart pull file1 file2`.
1 parent 3211343 commit 9811ecb

File tree

2 files changed

+58
-36
lines changed

2 files changed

+58
-36
lines changed

packages/cli/src/commander.test.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,13 @@ describe('link', () => {
218218

219219
describe('pull', () => {
220220
const diagram = 'test/output/connected-diagram.mmd';
221+
const diagram2 = 'test/output/connected-diagram-2.mmd';
221222

222223
beforeEach(async () => {
223-
await copyFile('test/fixtures/connected-diagram.mmd', diagram);
224+
await Promise.all([
225+
copyFile('test/fixtures/connected-diagram.mmd', diagram),
226+
copyFile('test/fixtures/connected-diagram.mmd', diagram2),
227+
]);
224228
});
225229

226230
it('should fail if MermaidChart document has not yet been linked', async () => {
@@ -243,7 +247,7 @@ describe('pull', () => {
243247
).rejects.toThrowError(`Diagram at ${diagram} has no code`);
244248
});
245249

246-
it('should pull document and add a `id:` field to frontmatter', async () => {
250+
it('should pull documents and add a `id:` field to frontmatter', async () => {
247251
const { program } = mockedProgram();
248252

249253
const mockedDiagram = {
@@ -255,22 +259,30 @@ title: My cool flowchart
255259
A[I've been updated!]`,
256260
};
257261

258-
vi.mocked(MermaidChart.prototype.getDocument).mockResolvedValueOnce(mockedDiagram);
262+
vi.mocked(MermaidChart.prototype.getDocument).mockResolvedValue(mockedDiagram);
259263

260-
await program.parseAsync(['--config', CONFIG_AUTHED, 'pull', diagram], { from: 'user' });
264+
await program.parseAsync(['--config', CONFIG_AUTHED, 'pull', diagram, diagram2], {
265+
from: 'user',
266+
});
261267

262-
const diagramContents = await readFile(diagram, { encoding: 'utf8' });
268+
for (const file of [diagram, diagram2]) {
269+
const diagramContents = await readFile(file, { encoding: 'utf8' });
263270

264-
expect(diagramContents).toContain(`id: ${mockedDiagram.documentID}`);
265-
expect(diagramContents).toContain("flowchart TD\n A[I've been updated!]");
271+
expect(diagramContents).toContain(`id: ${mockedDiagram.documentID}`);
272+
expect(diagramContents).toContain("flowchart TD\n A[I've been updated!]");
273+
}
266274
});
267275
});
268276

269277
describe('push', () => {
270278
const diagram = 'test/output/connected-diagram.mmd';
279+
const diagram2 = 'test/output/connected-diagram-2.mmd';
271280

272281
beforeEach(async () => {
273-
await copyFile('test/fixtures/connected-diagram.mmd', diagram);
282+
await Promise.all([
283+
copyFile('test/fixtures/connected-diagram.mmd', diagram),
284+
copyFile('test/fixtures/connected-diagram.mmd', diagram2),
285+
]);
274286
});
275287

276288
it('should fail if MermaidChart document has not yet been linked', async () => {
@@ -283,16 +295,18 @@ describe('push', () => {
283295
).rejects.toThrowError('Diagram at test/fixtures/unsynced.mmd has no id');
284296
});
285297

286-
it('should push document and remove the `id:` field front frontmatter', async () => {
298+
it('should push documents and remove the `id:` field front frontmatter', async () => {
287299
const { program } = mockedProgram();
288300

289-
vi.mocked(MermaidChart.prototype.getDocument).mockResolvedValueOnce(mockedEmptyDiagram);
301+
vi.mocked(MermaidChart.prototype.getDocument).mockResolvedValue(mockedEmptyDiagram);
290302

291303
await expect(readFile(diagram, { encoding: 'utf8' })).resolves.not.toContain(/^id:/);
292304

293-
await program.parseAsync(['--config', CONFIG_AUTHED, 'push', diagram], { from: 'user' });
305+
await program.parseAsync(['--config', CONFIG_AUTHED, 'push', diagram, diagram2], {
306+
from: 'user',
307+
});
294308

295-
expect(vi.mocked(MermaidChart.prototype.setDocument)).toHaveBeenCalledOnce();
309+
expect(vi.mocked(MermaidChart.prototype.setDocument)).toHaveBeenCalledTimes(2);
296310
expect(vi.mocked(MermaidChart.prototype.setDocument)).toHaveBeenCalledWith(
297311
expect.objectContaining({
298312
code: expect.not.stringContaining('id:'),

packages/cli/src/commander.ts

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -196,40 +196,48 @@ function linkCmd() {
196196

197197
function pullCmd() {
198198
return createCommand('pull')
199-
.description('Pulls a document from from Mermaid Chart')
200-
.addArgument(new Argument('<path>', 'The path of the file to pull.'))
201-
.option('--check', 'Check whether the local file would be overwrited')
202-
.action(async (path, options, command) => {
199+
.description('Pulls documents from Mermaid Chart')
200+
.addArgument(new Argument('<path...>', 'The paths of the files to pull.'))
201+
.option('--check', 'Check whether the local files would be overwrited')
202+
.action(async (paths, options, command) => {
203203
const optsWithGlobals = command.optsWithGlobals<CommonOptions>();
204204
const client = await createClient(optsWithGlobals);
205-
const text = await readFile(path, { encoding: 'utf8' });
206-
207-
const newFile = await pull(text, client, { title: path });
208-
209-
if (text === newFile) {
210-
console.log(`✅ - ${path} is up to date`);
211-
} else {
212-
if (options['check']) {
213-
console.log(`❌ - ${path} would be updated`);
214-
process.exitCode = 1;
215-
} else {
216-
await writeFile(path, newFile, { encoding: 'utf8' });
217-
console.log(`✅ - ${path} was updated`);
218-
}
219-
}
205+
await Promise.all(
206+
paths.map(async (path) => {
207+
const text = await readFile(path, { encoding: 'utf8' });
208+
209+
const newFile = await pull(text, client, { title: path });
210+
211+
if (text === newFile) {
212+
console.log(`✅ - ${path} is up to date`);
213+
} else {
214+
if (options['check']) {
215+
console.log(`❌ - ${path} would be updated`);
216+
process.exitCode = 1;
217+
} else {
218+
await writeFile(path, newFile, { encoding: 'utf8' });
219+
console.log(`✅ - ${path} was updated`);
220+
}
221+
}
222+
}),
223+
);
220224
});
221225
}
222226

223227
function pushCmd() {
224228
return createCommand('push')
225-
.description('Push a local diagram to Mermaid Chart')
226-
.addArgument(new Argument('<path>', 'The path of the file to push.'))
227-
.action(async (path, _options, command) => {
229+
.description('Push local diagrams to Mermaid Chart')
230+
.addArgument(new Argument('<path...>', 'The paths of the files to push.'))
231+
.action(async (paths, _options, command) => {
228232
const optsWithGlobals = command.optsWithGlobals<CommonOptions>();
229233
const client = await createClient(optsWithGlobals);
230-
const text = await readFile(path, { encoding: 'utf8' });
234+
await Promise.all(
235+
paths.map(async (path) => {
236+
const text = await readFile(path, { encoding: 'utf8' });
231237

232-
await push(text, client, { title: path });
238+
await push(text, client, { title: path });
239+
}),
240+
);
233241
});
234242
}
235243

0 commit comments

Comments
 (0)