{"size": 1102, "ext": "js", "max_stars_repo_path": "src/Script.js", "max_stars_repo_name": "abigailjs/abigail-plugin-parse", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/Script.js", "max_issues_repo_name": "abigailjs/abigail-plugin-parse", "max_issues_repo_issues_event_min_datetime": "2016-04-02T09:30:35.000Z", "max_issues_repo_issues_event_max_datetime": "2016-06-13T07:16:14.000Z", "max_forks_repo_path": "src/Script.js", "max_forks_repo_name": "abigailjs/abigail-plugin-parse", "max_forks_repo_forks_event_min_datetime": "2021-04-09T22:29:46.000Z", "max_forks_repo_forks_event_max_datetime": "2021-04-09T22:29:46.000Z"}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": 1, "avg_line_length": 29.0, "max_line_length": 98, "alphanum_fraction": 0.6215970962}
import { parse, quote } from 'shell-quote';
import which from 'which';
import npmRunPath from 'npm-run-path';
import { relative as relativePaths } from 'path';

export const whichOptions = { path: npmRunPath() };
export function setPath(cwd) {
  whichOptions.path = npmRunPath({ cwd });
}

export default class Script {
  constructor(name, raw, options = {}, meta = {}) {
    this.name = name;
    this.raw = raw;
    this.meta = meta;
    this.parsed = parse(raw);
    this.canSpawn = this.parsed.reduce((can, op) => can && typeof op === 'string', true);

    if (options.require) {
      this.transformRawScript(options.require);
    }
  }

  transformRawScript(moduleName) {
    const bin = this.parsed[0];
    try {
      const resolvedBin = relativePaths(process.cwd(), which.sync(bin, whichOptions));
      const isNodeCLI = resolvedBin.split('/').slice(-2, -1)[0] === '.bin';
      if (isNodeCLI) {
        this.parsed = ['node', '--require', moduleName, resolvedBin].concat(this.parsed.slice(1));
        this.raw = quote(this.parsed);
      }
    } catch (error) {
      // ignore
    }
  }
}
