"use strict";

// src/migrations/20230203084900_ensure_static_root_exists.js
var assert = require("assert");
var fsp = require("fs/promises");
var path = require("path");
exports.default = async function() {
  assert.ok(process.env.DATA_MOUNT_POINT, "DATA_MOUNT_POINT is undefined");
  for (const namespaceDir of await listDirectories(process.env.DATA_MOUNT_POINT)) {
    for (const componentNameDir of await listDirectories(namespaceDir)) {
      for (const versionDir of await listDirectories(componentNameDir)) {
        const manifestPath = path.resolve(versionDir, "manifest.json");
        const manifest = await tryReadManifest(manifestPath);
        if (!manifest)
          continue;
        if ("staticFiles" in manifest && "locationRoot" in manifest.staticFiles) {
          const staticFiles = path.resolve(path.join(versionDir, manifest.staticFiles.locationRoot));
          if (await pathExists(staticFiles) == false) {
            await fsp.mkdir(staticFiles, { recursive: true });
          }
        }
      }
    }
  }
};
async function listDirectories(filePath) {
  return (await fsp.readdir(filePath, { withFileTypes: true })).filter((dirent) => dirent.isDirectory()).map((dirent) => path.resolve(filePath, dirent.name));
}
async function tryReadManifest(manifestPath) {
  try {
    return JSON.parse(await fsp.readFile(manifestPath, { encoding: "utf-8" }));
  } catch (_e) {
    return null;
  }
}
async function pathExists(path2) {
  try {
    await fsp.stat(path2);
    return true;
  } catch (error) {
    if (error.code === "ENOENT") {
      return false;
    } else {
      throw error;
    }
  }
}
