helpers.mjs 904 B

123456789101112131415161718192021222324252627282930313233343536
  1. import { pathToFileURL } from 'node:url'
  2. import path from 'node:path'
  3. import glob from 'glob'
  4. export function getPaths (globPath) {
  5. return new Promise((resolve, reject) => {
  6. glob(globPath, (error, paths) => {
  7. if (error) reject(error)
  8. else resolve(paths)
  9. })
  10. })
  11. }
  12. export function sortObjectAlphabetically (obj) {
  13. return Object.fromEntries(
  14. Object.entries(obj).sort(([keyA], [keyB]) => keyA.localeCompare(keyB)),
  15. )
  16. }
  17. export function omit (object, key) {
  18. const copy = { ...object }
  19. delete copy[key]
  20. return copy
  21. }
  22. export async function getLocales (pathPattern) {
  23. const paths = await getPaths(pathPattern)
  24. return Object.fromEntries(await Promise.all(paths.map(async filePath => {
  25. const pluginName = path.basename(path.join(filePath, '..', '..'))
  26. const { default: locale } = await import(pathToFileURL(filePath))
  27. return [pluginName, locale]
  28. })))
  29. }