Skip to content

Commit 3d77bed

Browse files
committed
fix: set target branch
1 parent 2c6c824 commit 3d77bed

File tree

3 files changed

+71
-20
lines changed

3 files changed

+71
-20
lines changed

.github/scripts/add-code-samples.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,32 @@ function mergeSamplesIntoSchema(
310310
/**
311311
* Write updated OpenAPI schemas back to files
312312
*/
313-
function writeOpenAPISchemas(schemas: Array<{ path: string; schema: any }>): void {
313+
function writeOpenAPISchemas(schemas: Array<{ path: string; schema: any }>, outputPath?: string): void {
314314
console.log(`💾 Writing ${schemas.length} updated schema(s)...`);
315315

316316
for (const schemaInfo of schemas) {
317317
const content = JSON.stringify(schemaInfo.schema, null, 2);
318-
fs.writeFileSync(schemaInfo.path, content, 'utf-8');
319-
console.log(` ✓ Updated ${path.basename(schemaInfo.path)}`);
318+
319+
// Determine output file path
320+
let outputFilePath: string;
321+
if (outputPath) {
322+
// If output path is provided, save files there
323+
const fileName = path.basename(schemaInfo.path);
324+
325+
// Ensure output directory exists
326+
if (!fs.existsSync(outputPath)) {
327+
fs.mkdirSync(outputPath, { recursive: true });
328+
}
329+
330+
outputFilePath = path.join(outputPath, fileName);
331+
console.log(` ✅ Writing ${fileName} to ${outputPath}`);
332+
} else {
333+
// Otherwise, save in place
334+
outputFilePath = schemaInfo.path;
335+
console.log(` ✅ Updated ${path.basename(schemaInfo.path)}`);
336+
}
337+
338+
fs.writeFileSync(outputFilePath, content, 'utf-8');
320339
}
321340

322341
console.log('✅ All schemas updated successfully');
@@ -341,35 +360,43 @@ function cleanup(dir: string): void {
341360
/**
342361
* Main execution function
343362
*/
344-
export async function main(directoryPath?: string, pattern?: string): Promise<number> {
363+
export async function main(directoryPath?: string, pattern?: string, outputPath?: string): Promise<number> {
345364
// Get command line arguments if not provided
346365
let dir = directoryPath;
347366
let pat = pattern;
367+
let outPath = outputPath;
348368

349369
if (!dir || !pat) {
350370
const args = process.argv.slice(2);
351371

352372
if (args.length < 2) {
353373
console.error('❌ Error: Missing required arguments');
354374
console.error('');
355-
console.error('Usage: cd .github/scripts && npm run add-sdk-samples -- <directory> <pattern>');
375+
console.error('Usage: cd .github/scripts && npm run add-sdk-samples -- <directory> <pattern> [output]');
356376
console.error('');
357377
console.error('Arguments:');
358378
console.error(' <directory> - Path to directory containing OpenAPI JSON files');
359379
console.error(' <pattern> - Regex pattern to match filenames');
380+
console.error(' [output] - Optional output directory (if not provided, files are saved in place)');
360381
console.error('');
361382
console.error('Examples:');
362383
console.error(' cd .github/scripts && npm run add-sdk-samples -- "../../openapi" "openapi.*\\.json"');
384+
console.error(' cd .github/scripts && npm run add-sdk-samples -- "../../openapi" "openapi.*\\.json" "../../output"');
363385
return 1;
364386
}
365387

366388
dir = args[0];
367389
pat = args[1];
390+
outPath = args[2]; // Optional third argument
368391
}
369392

370393
console.log('🚀 Starting multi-SDK sample extraction...\n');
371394
console.log(`Searching in directory: ${dir}`);
372-
console.log(`Pattern: ${pat}\n`);
395+
console.log(`Pattern: ${pat}`);
396+
if (outPath) {
397+
console.log(`Output directory: ${outPath}`);
398+
}
399+
console.log();
373400

374401
// Find matching OpenAPI files
375402
const openapiFiles = findMatchingFiles(dir, pat);
@@ -490,7 +517,7 @@ export async function main(directoryPath?: string, pattern?: string): Promise<nu
490517
}
491518

492519
// Step 4: Write updated schemas
493-
writeOpenAPISchemas(schemas);
520+
writeOpenAPISchemas(schemas, outPath);
494521
console.log();
495522

496523
// Summary

.github/scripts/replace-links.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function replaceLinks(content: string, oldUrl: string, newUrl: string): string {
1414
/**
1515
* Process a single OpenAPI file
1616
*/
17-
function processFile(filePath: string, oldUrl: string, newUrl: string): void {
17+
function processFile(filePath: string, oldUrl: string, newUrl: string, outputPath?: string): void {
1818
try {
1919
console.log(`Processing: ${filePath}`);
2020

@@ -48,10 +48,27 @@ function processFile(filePath: string, oldUrl: string, newUrl: string): void {
4848
// Replace links
4949
const updatedContent = replaceLinks(content, oldUrl, newUrl);
5050

51-
// Write back to file
52-
fs.writeFileSync(filePath, updatedContent, 'utf-8');
51+
// Determine output file path
52+
let outputFilePath: string;
53+
if (outputPath) {
54+
// If output path is provided, save files there
55+
const fileName = path.basename(filePath);
56+
57+
// Ensure output directory exists
58+
if (!fs.existsSync(outputPath)) {
59+
fs.mkdirSync(outputPath, { recursive: true });
60+
}
61+
62+
outputFilePath = path.join(outputPath, fileName);
63+
console.log(` ✅ Replaced ${count} occurrence(s), saved to ${outputFilePath}`);
64+
} else {
65+
// Otherwise, save in place
66+
outputFilePath = filePath;
67+
console.log(` ✅ Replaced ${count} occurrence(s) in ${filePath}`);
68+
}
5369

54-
console.log(` ✅ Replaced ${count} occurrence(s) in ${filePath}`);
70+
// Write to file
71+
fs.writeFileSync(outputFilePath, updatedContent, 'utf-8');
5572
} catch (e) {
5673
console.error(` ❌ Error processing ${filePath}:`, (e as Error).message);
5774
process.exit(1);
@@ -68,27 +85,34 @@ export function main(): number {
6885
if (args.length < 4) {
6986
console.error('❌ Error: Missing required arguments');
7087
console.error('');
71-
console.error('Usage: cd .github/scripts && npm run replace-links -- <directory> <pattern> <old_url> <new_url>');
88+
console.error('Usage: cd .github/scripts && npm run replace-links -- <directory> <pattern> <old_url> <new_url> [output]');
7289
console.error('');
7390
console.error('Arguments:');
7491
console.error(' <directory> - Path to directory containing JSON files');
7592
console.error(' <pattern> - Regex pattern to match filenames');
7693
console.error(' <old_url> - URL to replace');
7794
console.error(' <new_url> - Replacement URL');
95+
console.error(' [output] - Optional output directory (if not provided, files are saved in place)');
7896
console.error('');
7997
console.error('Examples:');
80-
console.error(' cd .github/scripts && npm run replace-links -- "openapi" "openapi.*\\.json" "https://developer.box.com" "https://ja.developer.box.com"');
98+
console.error(' cd .github/scripts && npm run replace-links -- "../../openapi" "openapi.*\\.json" "https://developer.box.com" "https://ja.developer.box.com"');
99+
console.error(' cd .github/scripts && npm run replace-links -- "../../openapi" "openapi.*\\.json" "https://developer.box.com" "https://ja.developer.box.com" "output"');
81100
return 1;
82101
}
83102

84103
const directoryPath = args[0];
85104
const pattern = args[1];
86105
const oldUrl = args[2];
87106
const newUrl = args[3];
107+
const outputPath = args[4]; // Optional fifth argument
88108

89109
console.log(`Replacing "${oldUrl}" with "${newUrl}"`);
90110
console.log(`Searching in directory: ${directoryPath}`);
91-
console.log(`Pattern: ${pattern}\n`);
111+
console.log(`Pattern: ${pattern}`);
112+
if (outputPath) {
113+
console.log(`Output directory: ${outputPath}`);
114+
}
115+
console.log();
92116

93117
// Find matching files
94118
const filePaths = findMatchingFiles(directoryPath, pattern);
@@ -104,7 +128,7 @@ export function main(): number {
104128

105129
// Process each file
106130
for (const filePath of filePaths) {
107-
processFile(filePath, oldUrl, newUrl);
131+
processFile(filePath, oldUrl, newUrl, outputPath);
108132
}
109133

110134
console.log('\n✅ All files processed successfully!');

.github/workflows/en-api-docs.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- name: Check out scripts from main branch
2626
uses: actions/checkout@v4
2727
with:
28-
ref: refs/heads/main
28+
ref: refs/heads/ws-11970-clean
2929
sparse-checkout: |
3030
.github/scripts
3131
token: ${{ secrets.GITHUB_TOKEN }}
@@ -48,12 +48,12 @@ jobs:
4848

4949
- name: Run add-code-samples script
5050
working-directory: .github/scripts
51-
run: npm run add-code-samples -- "../../openapi" "openapi.*\\.json"
51+
run: npm run add-code-samples -- "../../openapi" "openapi.*\\.json" "../../output"
5252

5353
- name: Push openapi directory to en-api-docs branch
5454
5555
env:
5656
REPO: self
57-
BRANCH: refs/heads/en-api-docs
58-
FOLDER: openapi
59-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
57+
BRANCH: en-api-docs
58+
FOLDER: output
59+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)