Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cli-out-dirs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@stats-forge/github-stats-forge-cli': patch
---

fix: create the folders `--out` and a saved card's path name, rather than failing on a folder that is not there yet
2 changes: 1 addition & 1 deletion apps/docs/src/content/docs/docs/usage/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ the prompt, which hides what you type.
| Flag | What it does |
| ------------------- | --------------------------------------------------------------------------------------------------------------- |
| `-c, --card <id>` | Skip the first prompt: `stats`, `top-langs`, `pin`, `org`, `org-activity`, `contributed-to`, `gist`, `wakatime` |
| `-o, --out <file>` | Where to write the SVG; the default is named after the card and its subject |
| `-o, --out <file>` | Where to write the SVG, creating its folders; the default is named after the card and its subject |
| `--config <file>` | A saved card to load, and where "Save these options" writes |
| `--options <query>` | Options as a query string, layered over `--config` |
| `-g, --generate` | Render what `--config` holds and exit, without opening the menu |
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
import { existsSync } from 'node:fs';
import { readFile, writeFile } from 'node:fs/promises';
import { relative, resolve } from 'node:path';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { dirname, relative, resolve } from 'node:path';
import { formatWithOptions, parseArgs } from 'node:util';

import type { CardKind } from '@stats-forge/github-stats-forge-catalog';
Expand Down Expand Up @@ -149,6 +149,7 @@ const renderAndWrite = async (
}

const file = outFile(card, query, out);
await mkdir(dirname(file), { recursive: true });
await writeFile(file, rendered.content, 'utf8');
const written = relative(process.cwd(), file);
process.stdout.write(`Wrote ${written}\n`);
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/saved-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*/

import { existsSync } from 'node:fs';
import { readFile, writeFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { dirname, resolve } from 'node:path';

import type { CardKind } from '@stats-forge/github-stats-forge-catalog';
import {
Expand Down Expand Up @@ -105,6 +105,7 @@ export const writeSavedCard = async (
options: Record<string, string>,
): Promise<string> => {
const file = resolve(process.cwd(), path);
await mkdir(dirname(file), { recursive: true });
await writeFile(file, savedCardText(card, options), 'utf8');
return file;
};
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ describe('the command line', () => {
expect(prompts.navigateOptions).not.toHaveBeenCalled();
});

it("creates --out's folders when they are not there yet", async () => {
render.mockResolvedValue(DRAWN);
const out = join(dir, 'cards', 'dark', 'card.svg');

const result = await run('--config', savedGist, '--generate', '--pat', 'ghp_x', '--out', out);

await expect(readFile(out, 'utf8')).resolves.toBe('<svg>the card</svg>');
expect(result.code).toBeUndefined();
});

it('reports a render that failed, writes nothing, and exits 1', async () => {
render.mockResolvedValue(REFUSED);
const out = join(dir, 'card.svg');
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/tests/saved-card.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ describe(writeSavedCard, () => {
show_icons: 'true',
});
});

it('creates the folders the path names', async () => {
const file = join(dir(), 'cards', 'card.json');

await writeSavedCard(file, card('stats'), { username: 'anuraghazra' });

await expect(readFile(file, 'utf8')).resolves.toContain('"username": "anuraghazra"');
});
});

describe(readSavedCard, () => {
Expand Down
Loading