"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = exports.ShellSandbox = void 0;var _child_process = _interopRequireDefault(require("child_process"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}






const exec = (cmd, reporter, hash) => {
  return new Promise((resolve, reject) => {
    reporter.log(`run ${cmd}`);
    const proc = _child_process.default.exec(cmd);
    proc.stdout.on('data', chunk => {
      reporter.output(hash, 'stdout', chunk);
    });
    proc.stderr.on('data', chunk => {
      reporter.output(hash, 'stderr', chunk);
    });
    proc.on('close', code => {
      resolve();
    });
    proc.on('error', err => {
      reject(err);
    });
  });
};

class ShellSandbox {






  constructor(reporter, rootPath) {_defineProperty(this, "rootPath", void 0);_defineProperty(this, "filetypes", { sh: 'sh', shell: 'sh' });_defineProperty(this, "reporter", void 0);
    this.rootPath = rootPath;
    this.reporter = reporter;
  }
  async run(code, hash, lang, meta) {
    lang = this.filetypes[lang];
    if (!lang) {
      return false;
    }
    if (code.startsWith('#! ')) {
      await exec(code, this.reporter, hash);
    } else {
      for (let line of code.split('\n')) {
        line = line.trimLeft();
        if (line.startsWith('$ ')) {
          line = line.slice(2);
        }
        await exec(line, this.reporter, hash);
      }
    }
    return true;
  }}exports.ShellSandbox = ShellSandbox;


const plugin = () => {
  return {
    name: 'shell',
    sandbox: async (reporter, rootPath) => new ShellSandbox(reporter, rootPath) };

};var _default =

plugin;exports.default = _default;