Fix @__PURE__ replacement not matching multiple times

Also handle subfolders
This commit is contained in:
Damien Elmes 2023-07-10 11:02:11 +10:00
parent 0d7c34dace
commit 17051c2b81

View file

@ -4,21 +4,35 @@
import * as fs from "fs"; import * as fs from "fs";
import * as path from "path"; import * as path from "path";
function allFilesInDir(directory): string[] {
let results: string[] = [];
const list = fs.readdirSync(directory);
list.forEach(function(file) {
file = path.join(directory, file);
const stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
results = results.concat(allFilesInDir(file));
} else {
results.push(file);
}
});
return results;
}
function adjustFiles() {
const root = process.argv[2]; const root = process.argv[2];
const typeRe = /(make(Enum|MessageType))\(\n\s+".*",/g; const typeRe = /(make(Enum|MessageType))\(\n\s+".*",/g;
fs.readdirSync(root, { withFileTypes: true }).forEach(dirEnt => { let jsFiles = allFilesInDir(root).filter(f => f.endsWith(".js"));
const dirPath = path.join(root, dirEnt.name); for (const file of jsFiles) {
if (dirEnt.isDirectory()) {
fs.readdirSync(dirPath).forEach(fileName => {
if (fileName.endsWith(".js")) {
const file = path.join(dirPath, fileName);
let contents = fs.readFileSync(file, "utf8"); let contents = fs.readFileSync(file, "utf8");
// allow tree shaking on proto messages // allow tree shaking on proto messages
contents = contents.replace( contents = contents.replace(
"= proto3.make", /= proto3.make/g,
"= /* @__PURE__ */ proto3.make", "= /* @__PURE__ */ proto3.make",
); );
@ -29,6 +43,6 @@ fs.readdirSync(root, { withFileTypes: true }).forEach(dirEnt => {
fs.writeFileSync(file, contents, "utf8"); fs.writeFileSync(file, contents, "utf8");
} }
});
} }
});
adjustFiles();