<commit_before>// https://developers.google.com/sheets/api/quickstart/nodejs
import fs from 'fs';
import readline from 'readline';
import GoogleAuth from 'google-auth-library';

import config from '../config';

const {
  googleScopes: GOOGLE_SCOPES,
  googleTokenDir: GOOGLE_TOKEN_DIR,
  googleTokenPath: GOOGLE_TOKEN_PATH,
  googleClientSecretPath: GOOGLE_CLIENT_SECRET_PATH,
  nodeEnv: NODE_ENV,
} = config;

function storeToken(token) {
  if (NODE_ENV === 'production') {
    try {
      fs.mkdirSync(GOOGLE_TOKEN_DIR);
    } catch (err) {
      if (err.code != 'EEXIST') {
        throw err;
      }
    }
  }

  fs.writeFile(GOOGLE_TOKEN_PATH, JSON.stringify(token));
  console.log(`Token stored to ${GOOGLE_TOKEN_PATH}`);
}

function getNewToken(oauth2Client, callback) {
  const authURL = oauth2Client.generateAuthUrl({
    access_type: 'offline',
    scope: GOOGLE_SCOPES,
  });

  console.log(`Authorize this app by visiting this url: ${authURL}`);

  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  });

  rl.question('Enter the code from that page here: ', (code) => {
    rl.close();

    oauth2Client.getToken(code, (err, token) => {
      if (err) {
        console.log(`Error while trying to retrieve access token: ${err}`);
        return;
      }

      oauth2Client.credentials = token;
      storeToken(token);
      callback(oauth2Client);
    });
  });
}

function authorizeApp(credentials, callback) {
  const clientSecret = credentials.installed.client_secret;
  const clientID = credentials.installed.client_id;
  const redirectURL = credentials.installed.redirect_uris[0];
  const auth = new GoogleAuth();
  const oauth2Client = new auth.OAuth2(clientID, clientSecret, redirectURL);

  fs.readFile(GOOGLE_TOKEN_PATH, (err, token) => {
    if (err) {
      getNewToken(oauth2Client, callback);
    } else {
      oauth2Client.credentials = JSON.parse(token);
      callback(oauth2Client);
    }
  });
}

export default function initAuth(callback) {
  fs.readFile(GOOGLE_CLIENT_SECRET_PATH, (err, content) => {
    if (err) {
      console.log(`Error loading client secret file: ${err}`);
      return;
    }

    authorizeApp(JSON.parse(content), callback);
  });
}
<commit_msg>make our linter happy<commit_after>// https://developers.google.com/sheets/api/quickstart/nodejs
import fs from 'fs';
import readline from 'readline';
import GoogleAuth from 'google-auth-library';

import config from '../config';

const {
  googleScopes: GOOGLE_SCOPES,
  googleTokenDir: GOOGLE_TOKEN_DIR,
  googleTokenPath: GOOGLE_TOKEN_PATH,
  googleClientSecretPath: GOOGLE_CLIENT_SECRET_PATH,
  nodeEnv: NODE_ENV,
} = config;

function storeToken(token) {
  if (NODE_ENV === 'production') {
    try {
      fs.mkdirSync(GOOGLE_TOKEN_DIR);
    } catch (err) {
      if (err.code !== 'EEXIST') {
        throw err;
      }
    }
  }

  fs.writeFile(GOOGLE_TOKEN_PATH, JSON.stringify(token));
  console.log(`Token stored to ${GOOGLE_TOKEN_PATH}`);
}

function getNewToken(oauth2Client, callback) {
  const authURL = oauth2Client.generateAuthUrl({
    access_type: 'offline',
    scope: GOOGLE_SCOPES,
  });

  console.log(`Authorize this app by visiting this url: ${authURL}`);

  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  });

  rl.question('Enter the code from that page here: ', (code) => {
    rl.close();

    oauth2Client.getToken(code, (err, token) => {
      if (err) {
        console.log(`Error while trying to retrieve access token: ${err}`);
        return;
      }

      oauth2Client.credentials = token;
      storeToken(token);
      callback(oauth2Client);
    });
  });
}

function authorizeApp(credentials, callback) {
  const clientSecret = credentials.installed.client_secret;
  const clientID = credentials.installed.client_id;
  const redirectURL = credentials.installed.redirect_uris[0];
  const auth = new GoogleAuth();
  const oauth2Client = new auth.OAuth2(clientID, clientSecret, redirectURL);

  fs.readFile(GOOGLE_TOKEN_PATH, (err, token) => {
    if (err) {
      getNewToken(oauth2Client, callback);
    } else {
      oauth2Client.credentials = JSON.parse(token);
      callback(oauth2Client);
    }
  });
}

export default function initAuth(callback) {
  fs.readFile(GOOGLE_CLIENT_SECRET_PATH, (err, content) => {
    if (err) {
      console.log(`Error loading client secret file: ${err}`);
      return;
    }

    authorizeApp(JSON.parse(content), callback);
  });
}

{'fraction_non_alphanumeric': 0.12852386237513874, 'fraction_numerical': 0.0048834628190899, 'mean_word_length': 3.683991683991684, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '7281515', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1476, 'n_words': 309}