const { Homedir, Hostname } = require("../../os.js")

let cache = {}

async function doInit() {
  cache["homedir"] = await Homedir()
  cache["hostname"] = await Hostname()
}

function getByCache(field) {
  return () => {
    const v = cache[field]
    if (v === null) {
      throw `Please using "await os.__init__()" for init os.${field} in @nanoapp/nstd/mock/os.`
    } else {
      return v
    }
  }
}

let { todo } = require("./util.js")

module.exports = todo("os", {
  homedir: getByCache("homedir"),
  hostname: getByCache("hostname"),
  __init__: doInit(),
})
