nodejs
import * as fs from "node:fs";
const convertFileCode = function (path) {
if (!path.endsWith(".vue")) {
return;
}
const res = fs.readFileSync(path, "utf-8");
const sign = `\<\/template\>`;
const endIndex = res.lastIndexOf(sign);
// 后面的内容应该是script </template>
const endCode = res.substring(endIndex + 11).trimStart();
if (!endCode.startsWith(`\<script\>`)) {
return;
}
const templateCode = res.substring(0, endIndex + 11);
const retTempCode = templateCode.replace(/this\./g, "");
const ret = retTempCode + res.substring(endIndex + 11);
fs.writeFileSync(path, ret, function (err) {
console.log(err);
});
console.log("--end");
};
const convertCode = function (dir) {
fs.readdir(dir, (err, files) => {
console.log(files);
files.forEach((file) => {
let fullPath = path.join(dir, file);
fs.stat(fullPath, (err, stat) => {
if (err) {
console.error('Error getting file status:', err);
return;
}
if (stat.isDirectory()) {
convertCode(fullPath);
}
else {
convertFileCode(fullPath);
}
});
})
});
};