Created
March 21, 2023 09:27
-
-
Save zihotki/c2a47dcd5ecdbb40eab4253c3bb8675b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import FileHound from "filehound"; | |
import { promises as fs } from "fs"; | |
import path from "path"; | |
import { promisify } from "util"; | |
// | |
// const packages = await FileHound.create() | |
// .paths(`packages`) | |
// .directory() | |
// .depth(1) | |
// .find(); | |
const buildPaths = ['esnext'] | |
for (const buildPath of buildPaths) { | |
const filePaths = await FileHound.create() | |
.paths(buildPath) | |
.discard("node_modules") | |
.ext("js") | |
.find() | |
await Promise.all( | |
filePaths.map( | |
async filePath => { | |
const initialContents = await fs.readFile( | |
filePath, | |
"utf-8" | |
); | |
const statements = initialContents.match(/(?:(?:import|export)(?: .+ from)? ["'].+["'];|(?:import\(["'].+["']\)))/g); | |
if (!statements) { | |
return; | |
} | |
const importMap = process.env.IMPORT_MAP ? JSON.parse(await fs.readFile(process.env.IMPORT_MAP, "utf-8")) : undefined; | |
const contents = await statements.reduce( | |
async (contentsPromise, statement) => { | |
const contents = await contentsPromise; | |
const url = statement.match(/["'](.+)["']/)[1]; | |
if (importMap?.imports?.[url]) { | |
const replacement = importMap.imports[url]; | |
if (!replacement.includes("./src")) { | |
return contents.replace( | |
statement, | |
statement.replace(url, replacement) | |
); | |
} | |
const shift = filePath | |
.split("/") | |
// Skip top folder + file | |
.slice(2) | |
// Replace with shift up directory | |
.map(() => "..") | |
.join("/"); | |
return contents.replace( | |
statement, | |
statement.replace(url, replacement.replace("./src", shift).replace(/\.tsx?$/, ".js")) | |
); | |
} else { | |
return contents.replace( | |
statement, | |
await getReplacement(url) | |
); | |
} | |
async function getReplacement(url) { | |
const [stat, indexStat] = await Promise.all([ | |
fs.stat(path.resolve(path.dirname(filePath), url + ".js")).catch(() => {}), | |
fs.stat(path.resolve(path.dirname(filePath), url + "/index.js")).catch(() => {}) | |
]); | |
if (stat && stat.isFile()) { | |
return statement.replace(url, url + ".js"); | |
} else if (indexStat && indexStat.isFile()) { | |
return statement.replace(url, url + "/index.js"); | |
} | |
return statement; | |
} | |
}, | |
Promise.resolve(initialContents) | |
); | |
await fs.writeFile(filePath, contents, "utf-8"); | |
} | |
) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment