import { resolve } from 'node:path';
import { createRequire } from 'node:module';
import dayjs from 'dayjs';
import intel from 'intel';
import { toArray } from '../support/util.js';
import {
  removeFile,
  copyFile,
  removeFileSync,
  copyFileSync
} from './fileUtil.js';
const require = createRequire(import.meta.url);
const log = intel.getLogger('browsertime');

const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;

export function loadPrePostScriptsSync(scripts) {
  return toArray(scripts).map(script => {
    // if the script is an AsyncFunction, return it.
    if (script instanceof AsyncFunction) {
      return script;
    }
    try {
      if (script.endsWith('.js')) {
        const name = script + '.cjs';
        copyFileSync(script, name);
        const data = require(resolve(name));
        removeFileSync(name);
        return data;
      } else {
        return require(resolve(script));
      }
    } catch (error) {
      throw new Error(
        "Couldn't run pre/post script file: " + resolve(script) + ' ' + error
      );
    }
  });
}

export function loadScriptSync(script) {
  if (script instanceof AsyncFunction) {
    return script;
  }
  try {
    if (script.endsWith('.js')) {
      const name = script + '.cjs';
      copyFileSync(script, name);
      const data = require(resolve(name));
      removeFileSync(name);
      return data;
    } else {
      return require(resolve(script));
    }
  } catch {
    // assume the script is a valid inline script by default
    return script;
  }
}

export async function loadScript(script, throwError) {
  if (script) {
    // if the script is an AsyncFunction, return it.
    if (script instanceof AsyncFunction) {
      return script;
    }
    try {
      if (script.endsWith('.js')) {
        const name = script + '.cjs';
        await copyFile(script, name);
        const data = require(resolve(name));
        await removeFile(name);
        return data;
      } else {
        return require(resolve(script));
      }
    } catch (error) {
      // assume the script is a valid inline script by default
      if (throwError) {
        log.error(
          'Could not parse user script %s with error %s',
          script,
          error
        );
        throw error;
      }
    }
  }
  return script;
}
export function timestamp() {
  return dayjs().format();
}

@@ -9,16 +9,27 @@
  resolve
+, join, sep
  %7D from 
@@ -41,16 +41,42 @@
 :path';%0A
+import os from 'node:os';%0A
 import %7B
@@ -224,24 +224,18 @@
 rt %7B
-%0A  remove
+ copy
 File,
-%0A 
  cop
@@ -243,51 +243,91 @@
 File
-,%0A  removeFileSync,%0A  copyFile
+Sync %7D from './fileUtil.js';%0Aimport %7B realpathSync, mkdtempSync, rmdir
 Sync
-%0A
+ 
 %7D from '
 ./fi
@@ -318,36 +318,30 @@
 ync %7D from '
-./fileUtil.j
+node:f
 s';%0Aconst re
@@ -505,16 +505,94 @@
 uctor;%0A%0A
+function getTmp() %7B%0A  return mkdtempSync(realpathSync(os.tmpdir()) + sep);%0A%7D%0A%0A
 export f
@@ -836,32 +836,62 @@
 sWith('.js')) %7B%0A
+        const dir = getTmp();%0A
         const na
@@ -891,39 +891,44 @@
 onst name = 
-script + '
+join(dir, 'tmp
 .cjs'
+)
 ;%0A        co
@@ -1007,34 +1007,49 @@
        r
-emoveFileSync(name
+mdirSync(dir, %7B recursive: true %7D
 );%0A     
@@ -1418,24 +1418,52 @@
 h('.js')) %7B%0A
+      const dir = getTmp();%0A
       const 
@@ -1465,39 +1465,44 @@
 onst name = 
-script + '
+join(dir, 'tmp
 .cjs'
+)
 ;%0A      copy
@@ -1579,26 +1579,41 @@
    r
-emoveFileSync(name
+mdirSync(dir, %7B recursive: true %7D
 );%0A 
@@ -2019,32 +2019,62 @@
 sWith('.js')) %7B%0A
+        const dir = getTmp();%0A
         const na
@@ -2082,23 +2082,28 @@
 e = 
-script + '
+join(dir, 'tmp
 .cjs'
+)
 ;%0A  
@@ -2195,29 +2195,42 @@
     
-await removeFile(name
+rmdirSync(dir, %7B recursive: true %7D
 );%0A 

Use OS tmp dir for cjs hack (#1869)