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";
const root = process.argv[2]; function allFilesInDir(directory): string[] {
const typeRe = /(make(Enum|MessageType))\(\n\s+".*",/g; let results: string[] = [];
const list = fs.readdirSync(directory);
fs.readdirSync(root, { withFileTypes: true }).forEach(dirEnt => { list.forEach(function(file) {
const dirPath = path.join(root, dirEnt.name); file = path.join(directory, file);
const stat = fs.statSync(file);
if (dirEnt.isDirectory()) { if (stat && stat.isDirectory()) {
fs.readdirSync(dirPath).forEach(fileName => { results = results.concat(allFilesInDir(file));
if (fileName.endsWith(".js")) { } else {
const file = path.join(dirPath, fileName); results.push(file);
}
});
return results;
}
function adjustFiles() {
const root = process.argv[2];
const typeRe = /(make(Enum|MessageType))\(\n\s+".*",/g;
let jsFiles = allFilesInDir(root).filter(f => f.endsWith(".js"));
for (const file of jsFiles) {
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();