All files / api-build/commands link.js

89.66% Statements 26/29
75% Branches 6/8
100% Functions 1/1
89.66% Lines 26/29
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                              1x             1x   1x 1x 1x   1x   1x   1x   7x   7x   7x   7x     7x 4x 4x 4x 4x 4x 4x       3x 3x 3x   3x 3x 3x 3x                
/* Link file format:
 
  ~/.readme-build/links.json
 
  {
    services: { // Services linked globally that are available
      math: '~/Desktop/math/',
    },
    directories: { // Services that the consumer have linked
      '~/Desktop/consumer/': ['math'],
    },
  }
*/
 
 
module.exports.usage = `Run services in code from local computer
 
Usage:
  api link # Allows service to be run locally from code
  api link math # Allows service math to be run from current directory
`;
 
module.exports.category = 'utility';
 
const utils = require('../utils/utils');
const path = require('path');
const fs = require('fs');
 
const packageJson = require('../lib/package-json');
 
module.exports.baseLinks = { services: {}, directories: {} };
 
module.exports.run = (args) => {
  // Creates .readme-build in the home directory if it doesn't exist
  utils.setupSharedDirectory();
 
  const linksPath = path.join(utils.sharedDirectoryPath(), 'links.json');
 
  const pjsonName = packageJson().get('name');
 
  const links = utils.fileExists(linksPath) ? require(linksPath) : module.exports.baseLinks;
 
  // Setting up linked service `api link`
  if (args.length === 1) {
    links.services[pjsonName] = process.cwd();
    try {
      fs.writeFileSync(linksPath, JSON.stringify(links));
      console.log('Local link added!');
      const greenString = `api link ${pjsonName}`.green;
      console.log(`Run ${greenString} to use in another project`);
    } catch (err) {
      console.error(err);
    }
  } else Eif (links.services[args[1]]) { // check we know about service trying to be linked
    Eif (!links.directories[process.cwd()]) {
      links.directories[process.cwd()] = [];
    }
    links.directories[process.cwd()].push(args[1]);
    try {
      fs.writeFileSync(linksPath, JSON.stringify(links));
      console.log(`Link added! Calls to ${args[1]} will be made locally.`);
    } catch (err) {
      console.error(err);
    }
  } else {
    console.log(`You need to run ${'`api link`'.green} in your service directory first!`); // TODO: come up with better message
  }
};