comty/scripts/utils/getPackages.js

49 lines
1.3 KiB
JavaScript

const fs = require("fs")
const path = require("path")
const rootPath = process.cwd()
const packagesPath = path.resolve(rootPath, "packages")
const excludedPackages = ["comty.js"]
function filterPackages(packages) {
const gitIgnore = fs.readFileSync(path.resolve(rootPath, ".gitignore"), "utf-8")
// create a regex to match all packages that are in the gitignore file
const gitIgnoreRegex = gitIgnore.split("\n").map((line) => {
// remove comments
if (line.startsWith("#")) return
return line.replace(/(\/)/g, "\\/").replace(/(\*)/g, "(.*)")
}).filter((line) => line)
// filter packages that are in the gitignore file
packages = packages.filter((packageName) => {
// filter excluded packages
if (excludedPackages.includes(packageName)) {
return false
}
const resolvedPath = path.resolve(packagesPath, packageName)
return !gitIgnoreRegex.some((regex) => {
return resolvedPath.match(regex)
})
})
packages = packages.filter((packageName) => {
return fs.statSync(path.resolve(packagesPath, packageName)).isDirectory()
})
return packages
}
async function getPackages() {
let packages = await fs.promises.readdir(packagesPath)
packages = filterPackages(packages)
return packages
}
module.exports = getPackages