-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathfixSamples.mjs
51 lines (45 loc) · 1.5 KB
/
fixSamples.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import * as fs from "node:fs/promises";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const baseDir = path.join(__dirname, "..", "samples");
async function findJsFiles(dir) {
let results = [];
const entries = await fs.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
results = results.concat(await findJsFiles(fullPath));
} else if (
entry.isFile() &&
entry.name.endsWith(".js") &&
fullPath.includes(path.join("javascript", ""))
) {
results.push(fullPath);
}
}
return results;
}
async function processFile(filePath) {
try {
const content = await fs.readFile(filePath, "utf-8");
// Regex to match: require("@azure/openai/types"); using either ' or " for the quotes.
const regex = /require\((['"])@azure\/openai\/types\1\);?\s*\n?/g;
const newContent = content.replace(regex, "\n");
if (newContent !== content) {
await fs.writeFile(filePath, newContent, "utf-8");
console.log(`Updated: ${filePath}`);
}
} catch (error) {
console.error(`Error processing ${filePath}:`, error);
}
}
async function main() {
const jsFiles = await findJsFiles(baseDir);
console.log(`Found ${jsFiles.length} JavaScript file(s) in samples/**/javascript/.`);
for (const filePath of jsFiles) {
await processFile(filePath);
}
}
main();