All files / dist/utils utils.js

73.98% Statements 91/123
60.87% Branches 28/46
78.95% Functions 15/19
74.36% Lines 87/117
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 2171x 1x 1x 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   4x       1x                       1x 15x 15x 15x       15x   15x                                   1x 6x 6x 6x 4x 4x 4x   6x     1x   4x 2x     2x 2x 2x         2x         1x 6x 6x 4x 4x     4x     6x     1x                         1x 5x 5x 3x   2x     1x                           1x                       1x 2x       1x 10x 10x 10x 24x     10x           1x 8x 8x 8x 12x 3x   9x       8x  
const os = require('os');
const fs = require('fs');
const url = require('url');
const path = require('path');
const glob = require('glob');
const stream = require('stream');
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 fileUtils = require('./file-utils');
 
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);
  }
};
 
// Converts command line arguments to a javascript object
// test=1 test2='hello' test3=@/path/to/file
// { test: 1, test2: 'hello', test3: BUFFER }
exports.parseArgs = args => {
  const data = {};
  const passedData = exports.fixMinimistSpaces(args);
  for (const arg of passedData) {
    const i = arg.indexOf('=');
    const parsedArg = [arg.slice(0, i), arg.slice(i + 1)];
    data[parsedArg[0]] = exports.convertArgToProperType(parsedArg[1]);
  }
  return data;
};
 
exports.convertArgToProperType = arg => {
  // It's a file
  if (arg.indexOf('@') === 0) {
    return fileUtils.file(arg.split('@')[1]);
  }
 
  let value = arg;
  try {
    value = JSON.parse(arg);
  } catch (e) {
    // Already in proper format
    // console.log(e);
  }
  return value;
};
 
// fixes args like numbers=[1, 3, 2]
// Spaces confuse minimist
exports.fixMinimistSpaces = args => {
  const parsed = [];
  for (const arg of args) {
    const stringArg = arg.toString();
    Iif (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 => {
  return docs.filter(doc => doc.fullDescription && doc.fullDescription.indexOf('https://docs.readme.build/docs/writing-documentation') >= 0).map(doc => doc.name);
};
 
// Parses response to make sure its the correct type
exports.parseResponse = response => {
  let parsedResponse = response.body;
  try {
    parsedResponse = JSON.parse(response.body, (k, v) => {
      return v && v.type === 'Buffer' ? Buffer.from(v.data) : v;
    });
  } catch (e) {/* response is a string */}
  return parsedResponse;
};
 
// Parses data to be sent for invoke
// body.data is stringified json
// Other is files
exports.parseData = data => {
  const files = {};
  const filelessData = {};
  for (const param in data) {
    if (data[param] instanceof stream || Buffer.isBuffer(data[param])) {
      files[param] = data[param];
    } else {
      filelessData[param] = data[param];
    }
  }
 
  return Object.assign({}, files, { data: JSON.stringify(filelessData) });
};