Skip to content

Commit 8553e4d

Browse files
authored
fix(scripts): generate snippets.json files [skip-bc] (#4354)
1 parent 21d4511 commit 8553e4d

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

scripts/specs/__tests__/snippets.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { describe, expect, it } from 'vitest';
22

3-
import { parseCodeSamples } from '../snippets.ts';
3+
import { generateSnippetsJSON } from '../snippets.ts';
44
import type { CodeSamples } from '../types.ts';
55

66
describe('init', () => {
77
it('parses a multi line import', () => {
88
expect(
99
JSON.stringify(
10-
parseCodeSamples({
10+
generateSnippetsJSON({
1111
foo: {
1212
default: `
1313
// Initialize the client
@@ -56,7 +56,7 @@ var response = await client.CreateConfigAsync(
5656
it('parses a single line import', () => {
5757
expect(
5858
JSON.stringify(
59-
parseCodeSamples({
59+
generateSnippetsJSON({
6060
foo: {
6161
default: `
6262
// Initialize the client
@@ -105,7 +105,7 @@ describe('initialize', () => {
105105
it("doesn't stop at `client`", () => {
106106
expect(
107107
JSON.stringify(
108-
parseCodeSamples({
108+
generateSnippetsJSON({
109109
foo: {
110110
default: `
111111
// Initialize the client foo bar BAAAAAAAAAAAAAAAAAAAAAZ

scripts/specs/format.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { GENERATORS, run, toAbsolutePath } from '../common.ts';
1010
import { createSpinner } from '../spinners.ts';
1111
import type { Spec } from '../types.ts';
1212

13-
import { getCodeSampleLabel, parseCodeSamples, transformGeneratedSnippetsToCodeSamples } from './snippets.ts';
13+
import { getCodeSampleLabel, transformGeneratedSnippetsToCodeSamples } from './snippets.ts';
1414

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

56-
parseCodeSamples(JSON.parse(JSON.stringify(codeSamples)));
57-
5856
for (const [pathKey, pathMethods] of Object.entries(bundledSpec.paths)) {
5957
for (const [method, specMethod] of Object.entries(pathMethods)) {
6058
if (specMethod['x-helper']) {

scripts/specs/snippets.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function getCodeSampleLabel(language: Language): OpenAPICodeSample['label
1919
}
2020

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

113+
const jsonSnippets = generateSnippetsJSON(JSON.parse(JSON.stringify(codeSamples)));
114+
await fsp.writeFile(
115+
toAbsolutePath(`docs/bundled/${clientName}-snippets.json`),
116+
JSON.stringify(jsonSnippets, null, 2),
117+
);
118+
113119
return codeSamples;
114120
}

0 commit comments

Comments
 (0)