-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathbefore-generate.js
74 lines (62 loc) · 2.8 KB
/
before-generate.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const fs = require("fs");
const path = require("path");
// before-generate.js
//
// re-writes all exampleInfo.tsx files to replace import exampleImage from "./my-image.jpg" to
// const exampleImage = "my-image.jpg"
//
// Solves compilation issue with importing image files in ExampleInfo.tsx when running generateSitemap.ts and generateFooter.ts
const getFilesInDirectory = async (pathUrl) => {
const entries = await fs.promises.readdir(pathUrl, { withFileTypes: true });
// Get files within the current directory and add a path key to the file objects
const files = entries
.filter((file) => !file.isDirectory())
.map((file) => ({
fileName: file.name,
fileDir: pathUrl,
filePath: path.join(pathUrl, file.name),
}));
// Get folders within the current directory
const folders = entries.filter((folder) => folder.isDirectory());
// Add the found files within the subdirectory to the files array
for (const folder of folders) files.push(...(await getFilesInDirectory(path.join(pathUrl, folder.name))));
return files;
};
function writeFile(targetFileName, fileText, platform) {
if (!["win32", "darwin"].includes(platform)) {
throw Error(`Platform ${platform} is not supported. Please run this script on Windows or macOS`);
}
fs.writeFileSync(targetFileName, fileText);
}
function replaceImportWithConst(targetFileName, exampleInfoText, platform) {
if (!["win32", "darwin"].includes(platform)) {
throw Error(`Platform ${platform} is not supported. Please run this script on Windows or macOS`);
}
console.log(`before-generate: Updating file ${targetFileName}`);
exampleInfoText = exampleInfoText.replace("import exampleImage from \"./", "const exampleImage = \"");
writeFile(targetFileName, exampleInfoText, platform);
}
(async function () {
const examplesPath = path.join(__dirname, "src", "components", "Examples");
const files = await getFilesInDirectory(examplesPath);
const exampleDefinitionFiles = files.filter((f) => f.fileName === "exampleInfo.tsx");
const platform = process.platform;
// Iterate through all exampleInfo.tsx.
// Replace
// "import exampleImage from "./someimage.jpg";
// with
// "const exampleImage = "someimage.jpg";
exampleDefinitionFiles.forEach((f) => {
// Read the file and store this for later
const exampleInfoText = fs.readFileSync(f.filePath, "utf-8");
// Update the file and save
replaceImportWithConst(f.filePath, exampleInfoText, platform);
});
// // Generate the sitemap. Fails if file contains import images
// await generateSitemap();
//
// // Undo find-replace
// exampleDefinitionFiles.forEach((f) => {
// writeFile(f.filePath, map[f.filePath], platform);
// });
})();