All files / api-build/utils utils.js

59.57% Statements 56/94
39.47% Branches 15/38
58.33% Functions 7/12
60% Lines 54/90
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 1631x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x   1x 1x 1x 1x   1x 1x 1x     1x 8x 8x         20x 1x         1x   1x   1x 1x     1x     1x 11x 11x   6x       1x                       1x 14x 14x 14x 5x 5x   9x   9x                                 1x 3x 3x               3x     1x                         1x 4x 4x 2x   2x     1x                           1x                       1x                  
const os = require('os');
const fs = require('fs');
const url = require('url');
const path = require('path');
const glob = require('glob');
const request = require('request-promise');
const CookieJar = require('tough-cookie').CookieJar;
const execSync = require('child_process').execSync;
const buildDocs = require('build-docs');
 
const console = require('./console');
const exit = require('./exit');
 
const host = process.env.BUILD_HOST || 'api.readme.build';
const protocol = process.env.BUILD_HOST ? 'http' : 'https'; // if overriding
const www = process.env.WWW_HOST || 'readme.build';
const version = process.env.BUILD_HOST ? '' : 'v1';
 
exports.WWW_URL = url.format({ host: www, protocol });
exports.BUILD_URL = url.format({ host, protocol, pathname: version });
exports.WS_URL = url.format({ host, protocol: 'ws', slashes: true });
 
// Sets up ~/.readme-build/ if it doesn't exist
exports.setupSharedDirectory = () => {
  const homePath = exports.sharedDirectoryPath();
  Iif (!fs.existsSync(homePath)) {
    fs.mkdirSync(homePath);
  }
};
 
exports.sharedDirectoryPath = () => path.join(process.env.HOME_DIR || os.homedir(), '.readme-build');
exports.credPath = path.join(exports.sharedDirectoryPath(), 'creds.json');
 
// Sets up ~/.readme-build/.cache
// Currently we just use this for the deploy zip (and delete the zip after deploy)
// But in the future we could do more stuff here
exports.cacheDir = () => {
  // Make sure shared directory is set up
  exports.setupSharedDirectory();
 
  const cachePath = path.join(exports.sharedDirectoryPath(), '.cache');
  Iif (!fs.existsSync(cachePath)) {
    fs.mkdirSync(cachePath);
  }
  return cachePath;
};
 
exports.fileExists = (file) => {
  try {
    return fs.statSync(file).isFile();
  } catch (err) {
    return false;
  }
};
 
exports.getAliasFile = (unknownAction) => {
  const files = glob.sync(path.join(__dirname, '../commands', '*.js'));
  let foundAction = false;
  for (const file of files) {
    const actionInfo = require(file);
    if (actionInfo.aliases && actionInfo.aliases.indexOf(unknownAction) >= 0) {
      foundAction = file.match(/(\w+).js/)[1];
    }
  }
  return foundAction;
};
 
exports.getJar = () => {
  const jar = request.jar();
  try {
    const s = fs.readFileSync(exports.credPath).toString();
    CookieJar.deserializeSync(s, jar._jar.store);
    return jar;
  } catch (e) {
    Iif (e.code !== 'ENOENT') throw e;
 
    Eif (process.env.NODE_ENV === 'testing') return undefined;
 
    console.error(`You must be logged in to perform that action:
 
  ${'api login'.green}
 
Don't have an account? Signup is free and takes 5 seconds!
 
  ${'api signup'.green}
    `);
 
    return exit(1);
  }
};
 
// fixes args like numbers=[1, 3, 2]
// Spaces confuse minimist
exports.parseArgs = (args) => {
  const parsed = [];
  for (const arg of args) {
    const stringArg = arg.toString();
    if (stringArg.indexOf('=') === -1) {
      parsed[parsed.length - 1] += stringArg;
    } else {
      parsed.push(arg);
    }
  }
  return parsed;
};
 
exports.getGitConfig = (config) => {
  let val;
  try {
    val = execSync(`git config --global ${config}`).toString().trim();
  } catch (e) {
    // hi
  }
  return val;
};
 
// Gets the current version of the sdk
// api-cli@version if `api run`
// api@version if in code
exports.getSDKVersion = (isCLI = false) => {
  const sdkVersion = require(path.join(__dirname, '../package.json')).version;
  if (isCLI) {
    return `api-cli@${sdkVersion}`;
  }
  return `api@${sdkVersion}`;
};
 
exports.makeUsername = () => {
  if (exports.getGitConfig('github.user')) {
    return exports.getGitConfig('github.user');
  }
 
  const name = exports.getGitConfig('user.name');
  if (!name) return undefined;
 
  const split = name.split(' ');
  split[0] = split[0][0]; // first character of first name
 
  return split.join('').replace(/[^\w]/g, '').toLowerCase();
};
 
exports.buildErrors = (baseDir = process.cwd()) => {
  const docs = buildDocs.parseDirectory(path.join(baseDir, 'endpoints'));
  const errors = {};
  for (const doc of docs) {
    if (doc.errors) {
      errors[doc.name] = doc.errors.toString();
    }
  }
  return errors;
};
 
// Get names of files still containing stub documentation
exports.getUnchangedDocs = (docs) => {
  const unchanged = [];
  for (const doc of docs) {
    if (doc.fullDescription && doc.fullDescription.indexOf('https://docs.readme.build/docs/writing-documentation') >= 0) {
      unchanged.push(doc.name);
    }
  }
  return unchanged;
};