<commit_before>/* eslint global-require: 0 */
/* eslint import/no-dynamic-require: 0 */
const s3 = require('s3');
const request = require('request');
const Promise = require('bluebird');
const path = require('path');
const fs = require('fs-plus');


let s3Client = null;
let packageVersion = null;
let fullVersion = null;

module.exports = (grunt) => {
  const {spawn} = require('./task-helpers')(grunt);

  function populateVersion() {
    return new Promise((resolve, reject) => {
      const json = grunt.config.get('appJSON')
      const cmd = 'git';
      const args = ['rev-parse', '--short', 'HEAD'];
      spawn({cmd, args}, (error, {stdout} = {}) => {
        if (error) {
          return reject();
        }
        const commitHash = stdout ? stdout.trim() : null;
        packageVersion = json.version;
        if (packageVersion.indexOf('-') > 0) {
          fullVersion = packageVersion;
        } else {
          fullVersion = `${packageVersion}-${commitHash}`;
        }
        return resolve();
      });
    });
  }

  function postToSlack(msg) {
    if (!process.env.NYLAS_INTERNAL_HOOK_URL) {
      return Promise.resolve();
    }
    return new Promise((resolve, reject) =>
      request.post({
        url: process.env.NYLAS_INTERNAL_HOOK_URL,
        json: {
          username: "Edgehill Builds",
          text: msg,
        },
      }
      , (error) => {
        return error ? reject(error) : resolve();
      })
    );
  }

  function put(localSource, destName, options = {}) {
    grunt.log.writeln(`>> Uploading ${localSource} to S3…`);

    const write = grunt.log.writeln;
    let lastPc = 0;

    const params = {
      Key: destName,
      ACL: "public-read",
      Bucket: "edgehill",
    };
    Object.assign(params, options);

    return new Promise((resolve, reject) => {
      const uploader = s3Client.uploadFile({
        localFile: localSource,
        s3Params: params,
      });
      uploader.on("error", err => reject(err));
      uploader.on("progress", () => {
        const pc = Math.round((uploader.progressAmount / uploader.progressTotal) * 100.0);
        if (pc !== lastPc) {
          lastPc = pc;
          write(`>> Uploading ${destName} ${pc}%`);
          return;
        }
      });
      uploader.on("end", data => resolve(data));
    });
  }

  function uploadToS3(filepath, key) {
    grunt.log.writeln(`>> Uploading ${filepath} to ${key}…`);
    return put(filepath, key).then((data) => {
      const msg = `N1 release asset uploaded: <${data.Location}|${key}>`;
      return postToSlack(msg).then(() => Promise.resolve(data));
    });
  }

  grunt.registerTask("publish", "Publish Nylas build", function publish() {
    const done = this.async();

    populateVersion()
    .then(() => {
      // find files to publish
      const {shouldPublishBuild} = require('./task-helpers')(grunt);
      const outputDir = grunt.config.get('outputDir');
      const uploads = [];

      if (process.platform === 'darwin') {
        uploads.push({
          source: `${outputDir}/N1.zip`,
          key: `${fullVersion}/${process.platform}/${process.<KEY>`,
        });
      } else if (process.platform === 'win32') {
        uploads.push({
          source: `${outputDir}/RELEASES`,
          key: `${fullVersion}/${process.platform}/${process.arch}/RELEASES`,
        });
        uploads.push({
          source: `${outputDir}/N1Setup.exe`,
          key: `${fullVersion}/${process.platform}/${process.arch}/N1Setup.exe`,
        });
        uploads.push({
          source: `${outputDir}/Nylas-${packageVersion}-full.nupkg`,
          key: `${fullVersion}/${process.platform}/${process.arch}/nylas-${packageVersion}-full.nupkg`,
        });
      } else if (process.platform === 'linux') {
        const files = fs.readdirSync(outputDir);
        for (const file of files) {
          if (path.extname(file) === '.deb') {
            uploads.push({
              source: file,
              key: `${fullVersion}/${process.platform}-deb/${process.arch}/N1.deb`,
              options: {ContentType: "application/x-deb"},
            });
          }
          if (path.extname(file) === '.rpm') {
            uploads.push({
              source: file,
              key: `${fullVersion}/${process.platform}-rpm/${process.arch}/N1.rpm`,
              options: {ContentType: "application/x-rpm"},
            });
          }
        }
      } else {
        grunt.fail.fatal(`Unsupported platform: '${process.platform}'`);
      }

      // configure environment
      if (!shouldPublishBuild()) {
        grunt.log.writeln(`>> Not publishing builds…`);
        grunt.log.writeln(`>> Would have uploaded the following assets: ${JSON.stringify(uploads, null, 2)}`);<commit_msg>fix(build) Fix publish task for linux<commit_after>/* eslint global-require: 0 */
/* eslint import/no-dynamic-require: 0 */
const s3 = require('s3');
const request = require('request');
const Promise = require('bluebird');
const path = require('path');
const fs = require('fs-plus');


let s3Client = null;
let packageVersion = null;
let fullVersion = null;

module.exports = (grunt) => {
  const {spawn} = require('./task-helpers')(grunt);

  function populateVersion() {
    return new Promise((resolve, reject) => {
      const json = grunt.config.get('appJSON')
      const cmd = 'git';
      const args = ['rev-parse', '--short', 'HEAD'];
      spawn({cmd, args}, (error, {stdout} = {}) => {
        if (error) {
          return reject();
        }
        const commitHash = stdout ? stdout.trim() : null;
        packageVersion = json.version;
        if (packageVersion.indexOf('-') > 0) {
          fullVersion = packageVersion;
        } else {
          fullVersion = `${packageVersion}-${commitHash}`;
        }
        return resolve();
      });
    });
  }

  function postToSlack(msg) {
    if (!process.env.NYLAS_INTERNAL_HOOK_URL) {
      return Promise.resolve();
    }
    return new Promise((resolve, reject) =>
      request.post({
        url: process.env.NYLAS_INTERNAL_HOOK_URL,
        json: {
          username: "Edgehill Builds",
          text: msg,
        },
      }
      , (error) => {
        return error ? reject(error) : resolve();
      })
    );
  }

  function put(localSource, destName, options = {}) {
    grunt.log.writeln(`>> Uploading ${localSource} to S3…`);

    const write = grunt.log.writeln;
    let lastPc = 0;

    const params = {
      Key: destName,
      ACL: "public-read",
      Bucket: "edgehill",
    };
    Object.assign(params, options);

    return new Promise((resolve, reject) => {
      const uploader = s3Client.uploadFile({
        localFile: localSource,
        s3Params: params,
      });
      uploader.on("error", err => reject(err));
      uploader.on("progress", () => {
        const pc = Math.round((uploader.progressAmount / uploader.progressTotal) * 100.0);
        if (pc !== lastPc) {
          lastPc = pc;
          write(`>> Uploading ${destName} ${pc}%`);
          return;
        }
      });
      uploader.on("end", data => resolve(data));
    });
  }

  function uploadToS3(filepath, key) {
    grunt.log.writeln(`>> Uploading ${filepath} to ${key}…`);
    return put(filepath, key).then((data) => {
      const msg = `N1 release asset uploaded: <${data.Location}|${key}>`;
      return postToSlack(msg).then(() => Promise.resolve(data));
    });
  }

  grunt.registerTask("publish", "Publish Nylas build", function publish() {
    const done = this.async();

    populateVersion()
    .then(() => {
      // find files to publish
      const {shouldPublishBuild} = require('./task-helpers')(grunt);
      const outputDir = grunt.config.get('outputDir');
      const uploads = [];

      if (process.platform === 'darwin') {
        uploads.push({
          source: `${outputDir}/N1.zip`,
          key: `${fullVersion}/${process.platform}/${process.arch}/N1.zip`,
        });
      } else if (process.platform === 'win32') {
        uploads.push({
          source: `${outputDir}/RELEASES`,
          key: `${fullVersion}/${process.platform}/${process.arch}/RELEASES`,
        });
        uploads.push({
          source: `${outputDir}/N1Setup.exe`,
          key: `${fullVersion}/${process.platform}/${process.arch}/N1Setup.exe`,
        });
        uploads.push({
          source: `${outputDir}/Nylas-${packageVersion}-full.nupkg`,
          key: `${fullVersion}/${process.platform}/${process.arch}/nylas-${packageVersion}-full.nupkg`,
        });
      } else if (process.platform === 'linux') {
        const files = fs.readdirSync(outputDir);
        for (const file of files) {
          if (path.extname(file) === '.deb') {
            uploads.push({
              source: `${outputDir}/${file}`,
              key: `${fullVersion}/${process.platform}-deb/${process.arch}/N1.deb`,
              options: {ContentType: "application/x-deb"},
            });
          }
          if (path.extname(file) === '.rpm') {
            uploads.push({
              source: `${outputDir}/${file}`,
              key: `${fullVersion}/${process.platform}-rpm/${process.arch}/N1.rpm`,
              options: {ContentType: "application/x-rpm"},
            });
          }
        }
      } else {
        grunt.fail.fatal(`Unsupported platform: '${process.platform}'`);
      }

      // configure environment
      if (!shouldPublishBuild()) {
        grunt.log.writeln(`>> Not publishing builds…`);
        grunt.log.writeln(`>> Would have uploaded the following assets: ${JSON.stringify(uploads, null, 2)}`);
{'fraction_non_alphanumeric': 0.1749029482740531, 'fraction_numerical': 0.005141118455566048, 'mean_word_length': 2.31432545201669, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5295101', 'n_tokens_mistral': 2916, 'n_tokens_neox': 2882, 'n_words': 578}