{"size": 1181, "ext": "js", "max_stars_repo_path": "tools/build-native.js", "max_stars_repo_name": "igorgo/jstp", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "tools/build-native.js", "max_issues_repo_name": "igorgo/jstp", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "tools/build-native.js", "max_forks_repo_name": "igorgo/jstp", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.8703703704, "max_line_length": 70, "alphanum_fraction": 0.6138865368}
#!/usr/bin/env node

'use strict';

const fs = require('fs');
const os = require('os');
const childProcess = require('child_process');

const EXIT_SUCCESS = 0;
const EXIT_FAIL = 1;

let action = process.argv.includes('--rebuild') ? 'rebuild' : 'build';
const jobs = `-j${process.env.JOBS || os.cpus().length}`;

fs.access('build', (error) => {
  if (error) {
    action = 'rebuild';
  }

  const nodeGyp = childProcess.spawn(
    'node-gyp', [action, jobs], { shell: true });
  const errorLines = [];

  nodeGyp.on('error', () => {
    handleBuildError(EXIT_FAIL);
  });

  nodeGyp.stdout.pipe(process.stdout);

  nodeGyp.stderr.on('data', (data) => {
    const line = data.toString();
    console.error(line);
    errorLines.push(line);
  });

  nodeGyp.on('exit', (code) => {
    if (errorLines.length > 0) {
      fs.writeFileSync('builderror.log', errorLines.join('\n'));
    }
    if (code !== EXIT_SUCCESS) {
      handleBuildError(code);
    }
  });
});

function handleBuildError(code) {
  if (process.env.CI) {
    process.exit(code);
  } else {
    console.warn('Could not build JSTP native extensions, ' +
      'JavaScript implementation will be used instead.');
  }
}
