#!/usr/bin/env node

const commandLineArgs = require('command-line-args');
const commandLineCommands = require('command-line-commands');
const path = require('path');
const ChildProcess = require('child_process');

const validCommands = [ null, 'run' ];
const { command, argv } = commandLineCommands(validCommands);
const optionDefinitions = [
  { name: 'jsdebugger', type: Boolean },
  { name: 'path', type: String, defaultOption: true },
];
const options = commandLineArgs(optionDefinitions, { argv: argv });

const DIST_DIR = path.join(__dirname, '..', 'dist');

const EXECUTABLE_DIR = process.platform === 'darwin' ?
                       path.join(DIST_DIR, 'Runtime.app', 'Contents', 'MacOS') :
                       path.join(DIST_DIR, 'runtime');

const EXECUTABLE = process.platform === 'win32' ?
                   path.join(EXECUTABLE_DIR, 'firefox.exe') :
                   path.join(EXECUTABLE_DIR, 'firefox');

let executableArgs = [
  '--app', path.join(__dirname, '..', 'application.ini'),
  options.path,
];

// The Mac and Linux runtimes accept either -jsdebugger or --jsdebugger,
// but Windows needs the former, so we use it for all platforms.
options.jsdebugger && executableArgs.push('-jsdebugger');

process.env.MOZ_NO_REMOTE = 1;

const childProcess = ChildProcess.spawn(EXECUTABLE, executableArgs, {
  stdio: 'inherit',
});
childProcess.on('close', code => process.exit(code));

@@ -126,24 +126,126 @@
 commands');%0A
+const fs = require('fs-extra');%0Aconst os = require('os');%0Aconst package = require('../package.json');%0A
 const path =
@@ -1027,41 +1027,30 @@
 );%0A%0A
-let executableArgs = %5B%0A  '--app',
+const applicationIni =
  pat
@@ -1091,16 +1091,223 @@
 on.ini')
+;%0Aconst profileDir = fs.mkdtempSync(path.join(os.tmpdir(), %60$%7Bpackage.name%7D-profile-%60));%0Aconsole.log(%60profileDir: $%7BprofileDir%7D%60);%0A%0Alet executableArgs = %5B%0A  '--app', applicationIni,%0A  '--profile', profileDir
 ,%0A  opti
@@ -1675,16 +1675,49 @@
  code =%3E
+ %7B%0A  fs.removeSync(profileDir);%0A 
  process
@@ -1727,11 +1727,14 @@
 it(code)
+;%0A%7D
 );%0A

use a temporary profile