Skip to content

fix(scripts): generate snippets.json files [skip-bc] #4354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 13, 2025
Merged
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
8 changes: 4 additions & 4 deletions scripts/specs/__tests__/snippets.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { describe, expect, it } from 'vitest';

import { parseCodeSamples } from '../snippets.ts';
import { generateSnippetsJSON } from '../snippets.ts';
import type { CodeSamples } from '../types.ts';

describe('init', () => {
it('parses a multi line import', () => {
expect(
JSON.stringify(
parseCodeSamples({
generateSnippetsJSON({
foo: {
default: `
// Initialize the client
Expand Down Expand Up @@ -56,7 +56,7 @@ var response = await client.CreateConfigAsync(
it('parses a single line import', () => {
expect(
JSON.stringify(
parseCodeSamples({
generateSnippetsJSON({
foo: {
default: `
// Initialize the client
Expand Down Expand Up @@ -105,7 +105,7 @@ describe('initialize', () => {
it("doesn't stop at `client`", () => {
expect(
JSON.stringify(
parseCodeSamples({
generateSnippetsJSON({
foo: {
default: `
// Initialize the client foo bar BAAAAAAAAAAAAAAAAAAAAAZ
Expand Down
4 changes: 1 addition & 3 deletions scripts/specs/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { GENERATORS, run, toAbsolutePath } from '../common.ts';
import { createSpinner } from '../spinners.ts';
import type { Spec } from '../types.ts';

import { getCodeSampleLabel, parseCodeSamples, transformGeneratedSnippetsToCodeSamples } from './snippets.ts';
import { getCodeSampleLabel, transformGeneratedSnippetsToCodeSamples } from './snippets.ts';

export async function lintCommon(useCache: boolean): Promise<void> {
const spinner = createSpinner('linting common spec');
Expand Down Expand Up @@ -53,8 +53,6 @@ export async function bundleSpecsForDoc(bundledPath: string, clientName: string)
const tagsDefinitions = bundledSpec.tags;
const codeSamples = await transformGeneratedSnippetsToCodeSamples(clientName);

parseCodeSamples(JSON.parse(JSON.stringify(codeSamples)));

for (const [pathKey, pathMethods] of Object.entries(bundledSpec.paths)) {
for (const [method, specMethod] of Object.entries(pathMethods)) {
if (specMethod['x-helper']) {
Expand Down
8 changes: 7 additions & 1 deletion scripts/specs/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function getCodeSampleLabel(language: Language): OpenAPICodeSample['label
}

// Iterates over the result of `transformSnippetsToCodeSamples` in order to generate a JSON file for the doc to consume.
export function parseCodeSamples(codeSamples: CodeSamples): CodeSamples {
export function generateSnippetsJSON(codeSamples: CodeSamples): CodeSamples {
for (const [language, operationWithSamples] of Object.entries(codeSamples)) {
for (const [operation, samples] of Object.entries(operationWithSamples)) {
if (operation === 'import') {
Expand Down Expand Up @@ -110,5 +110,11 @@ export async function transformGeneratedSnippetsToCodeSamples(clientName: string
}
}

const jsonSnippets = generateSnippetsJSON(JSON.parse(JSON.stringify(codeSamples)));
await fsp.writeFile(
toAbsolutePath(`docs/bundled/${clientName}-snippets.json`),
JSON.stringify(jsonSnippets, null, 2),
);

return codeSamples;
}