const { execute } = require('@pown/cli')
const { extract } = require('@pown/modules')

const { sub } = require('../sub')

async function exec(line) {
  const { loadableModules, loadableCommands } = await extract() // NOTE: assume this is coming from the cache so it must be fast

  const executeOptions = {
    loadableModules: loadableModules,
    loadableCommands: loadableCommands,

    inlineCommands: sub,

    file: '', // TODO: set to the file who called the function

    env: process.env,
  }

  await execute(line, executeOptions)
}

async function sh(strings, ...args) {
  const result = []

  for (const [index, string] of strings.entries()) {
    result.push(
      string,
      index < args.length ? args[index]?.toString?.() || '' : ''
    )
  }

  const cmd = result.join('')

  return await exec(cmd)
}

async function shq(strings, ...args) {
  const result = []

  for (const [index, string] of strings.entries()) {
    result.push(
      string,
      index < args.length
        ? `'${args[index]?.toString?.().replace(/'/g, String.raw`\'`) || ''}'`
        : ''
    )
  }

  const cmd = result.join('')

  return await exec(cmd)
}

module.exports = {
  sh,
  shq,
}

@@ -113,16 +113,66 @@
 ../sub')
+%0Aconst %7B options %7D = require('../globals/options')
 %0A%0Aasync 
@@ -552,43 +552,242 @@
 %0A%0A  
-await execute(line, executeOptions)
+if (options.expand) %7B%0A    console.warn(%60$ $%7Bline%7D%60)%0A  %7D%0A%0A  if (options.exit) %7B%0A    await execute(line, executeOptions)%0A  %7D%0A  else %7B%0A    try %7B%0A      await execute(line, executeOptions)%0A    %7D catch (e) %7B%0A      console.error(e)%0A    %7D%0A  %7D
 %0A%7D%0A%0A

Use options.