Skip to content

Commit 1b562fa

Browse files
committed
chore: add script for use when testing local json-schemas
1 parent 1f0e0a4 commit 1b562fa

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

v1-draft-0/typescript-definitions/scripts/generate-types.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async function main() {
1515
* and run 'npm run generate-types' to regenerate this file.
1616
*/`,
1717
customName: (schema, name) => {
18-
console.log("name", name, schema);
18+
// console.log("name", name, schema);
1919
return undefined;
2020
},
2121
};
@@ -41,6 +41,11 @@ async function main() {
4141
async function saveFile(savePath, contents) {
4242
// Ensure the folder exists:
4343
await fs.promises.mkdir(path.dirname(savePath), { recursive: true });
44+
45+
// Replace contents in case of using localhost during testing:
46+
47+
contents = contents.replaceAll('HttpLocalhost8080', 'HttpsOgrafEbuIo')
48+
4449
await fs.promises.writeFile(savePath, contents);
4550
}
4651

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
/*
5+
*************************************************************************************************
6+
7+
This scripts replaces URLs in the manifest with local paths, for testing purposes.
8+
Usage:
9+
* `node scripts/local-testing.js` to replace URLs with local paths
10+
* `node scripts/local-testing.js --restore` to restore the original URLs
11+
*
12+
13+
This is useful when testing the json-manifests locally.
14+
One simple way to serve the files locally is:
15+
* `cd ograf`
16+
* `npm install -g http-server`
17+
* `http-server -p 8080` // serves the files on http://localhost:8080
18+
19+
*************************************************************************************************
20+
*/
21+
22+
let restore = false;
23+
process.argv.forEach((arg) => {
24+
if (arg === "--restore") restore = true;
25+
});
26+
27+
let replacements = [
28+
{
29+
from: "https://ograf.ebu.io/",
30+
to: "http://localhost:8080/",
31+
},
32+
];
33+
34+
if (restore) {
35+
replacements = replacements.map((r) => {
36+
return {
37+
from: r.to,
38+
to: r.from,
39+
};
40+
});
41+
}
42+
43+
let updateCount = 0;
44+
45+
async function replaceInAllFiles(folderPath) {
46+
const files = await fs.promises.readdir(folderPath);
47+
48+
for (const file of files) {
49+
if (file === "node_modules") continue;
50+
if (file === "local-testing.js") continue;
51+
// Only process these file types:
52+
53+
const filePath = path.join(folderPath, file);
54+
55+
// is dir?
56+
if ((await fs.promises.stat(filePath)).isDirectory()) {
57+
await replaceInAllFiles(filePath);
58+
} else {
59+
// Only process these file types:
60+
if (
61+
!file.endsWith(".ts") &&
62+
!file.endsWith(".js") &&
63+
!file.endsWith(".json")
64+
)
65+
continue;
66+
67+
const fileContents = await fs.promises.readFile(filePath, "utf-8");
68+
69+
let newContents = fileContents;
70+
71+
for (const replacement of replacements) {
72+
newContents = newContents.replaceAll(replacement.from, replacement.to);
73+
}
74+
75+
if (newContents !== fileContents) {
76+
await fs.promises.writeFile(filePath, newContents);
77+
console.log(`Updated ${filePath}`);
78+
updateCount++;
79+
}
80+
}
81+
}
82+
}
83+
84+
const basePath = path.resolve(__dirname, "../..");
85+
replaceInAllFiles(basePath)
86+
.then(() => {
87+
if (!restore) {
88+
console.log(`Updated ${updateCount} files.`);
89+
console.log("To restore, run `node scripts/local-testing.js --restore`");
90+
} else {
91+
console.log(`${updateCount} Files restored`);
92+
}
93+
})
94+
.catch(console.error);

0 commit comments

Comments
 (0)