@porosys/pss
Version:
Porosys Server Setup (pss): General-purpose server setup and automation tool (including Netdata management)
54 lines (42 loc) • 1.33 kB
JavaScript
import { promises as fs } from 'node:fs';
const rgxFrom = /(?<=from )['|"](.*)['|"]/gm;
const rgxDynamicImport = /import\(['|"](.*?)['|"]\)/gm;
async function main() {
const tsconfig = JSON.parse(
(await fs.readFile('./tsconfig.json')).toString(),
);
return await lsdir(tsconfig.compilerOptions.outDir, async (f) => {
if (!f.endsWith('.js')) return;
let code = (await fs.readFile(f)).toString();
let modified = false;
// Static imports
code = code.replace(rgxFrom, (_, p) => {
if (!(p.startsWith('./') || p.startsWith('../')) || p.endsWith('.js'))
return `'${p}'`;
modified = true;
return `'${p}.js'`;
});
// Dynamic imports
code = code.replace(rgxDynamicImport, (match, p) => {
if (!(p.startsWith('./') || p.startsWith('../')) || p.endsWith('.js'))
return match;
modified = true;
return `import('${p}.js')`;
});
if (!modified) return '';
await fs.writeFile(f, code);
return f;
});
}
main();
async function lsdir(dir, fn) {
const tree = await fs.readdir(dir, { withFileTypes: true });
const tasks = tree.map(async (entity) => {
const pth = `${dir}/${entity.name}`;
if (entity.isDirectory()) {
return lsdir(pth, fn);
}
return fn(pth);
});
return (await Promise.all(tasks)).flat();
}