@@ -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!' ) ;
0 commit comments