{"size": 1788, "ext": "js", "max_stars_repo_path": "src/plugins/js-builder.js", "max_stars_repo_name": "orsa-actual/orsa", "max_stars_repo_stars_event_min_datetime": "2020-06-19T18:43:21.000Z", "max_stars_repo_stars_event_max_datetime": "2020-06-28T16:19:05.000Z", "max_issues_repo_path": "src/plugins/js-builder.js", "max_issues_repo_name": "orsa-actual/orsa", "max_issues_repo_issues_event_min_datetime": "2020-01-23T05:17:28.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-12T23:31:30.000Z", "max_forks_repo_path": "src/plugins/js-builder.js", "max_forks_repo_name": "orsa-actual/orsa", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 7, "max_issues_count": 29, "max_forks_count": null, "avg_line_length": 23.5263157895, "max_line_length": 98, "alphanum_fraction": 0.5156599553}
const { exec } = require('child-process-promise');

const DEFAULT_BUILD_COMMAND = 'npm install';
const DEFAULT_TEST_COMMAND = 'npm test';

module.exports = async (
  { javascriptBuildCommand, javascriptTestCommand }, { store, logger },
) => {
  /* eslint-disable indent, comma-dangle */
  const { data: { projects } } = await store.query(
`query {
  projects {
    id
  }
}
`
  );
  /* eslint-enable indent, comma-dangle */

  for (const { id } of projects) {
    const project = store.getById(id);

    try {
      logger.log('Javascript builder', `Building ${project.name}`);
      const {
        stdout,
        stderr,
      } = await exec(
        javascriptBuildCommand || DEFAULT_BUILD_COMMAND, { cwd: project.transient.path },
      );
      store.update({
        ...project,
        build: {
          stdout,
          stderr,
        },
      });
    } catch (error) {
      logger.error('Javascript builder', `Error building ${project.name} - ${error.toString()}`);
      store.update({
        ...project,
        build: {
          error: error.toString(),
        },
      });
    }

    if (!project.build.error) {
      try {
        logger.log('Javascript builder', `Testing ${project.name}`);
        const {
          stdout,
          stderr,
        } = await exec(
          javascriptTestCommand || DEFAULT_TEST_COMMAND, { cwd: project.transient.path },
        );
        store.update({
          ...project,
          test: {
            stdout,
            stderr,
          },
        });
      } catch (error) {
        logger.error('Javascript builder', `Error testing ${project.name} - ${error.toString()}`);
        store.update({
          ...project,
          test: {
            error: error.toString(),
          },
        });
      }
    }
  }
};
