BVB Source Codes

parse-server Show commander.js Source code

Return Download parse-server: download commander.js Source code - Download parse-server Source code - Type:.js
  1. /* eslint-disable no-console */
  2. import { Command } from 'commander';
  3. import path from 'path';
  4. let _definitions;
  5. let _reverseDefinitions;
  6. let _defaults;
  7.  
  8. Command.prototype.loadDefinitions = function(definitions) {
  9.   _definitions = definitions;
  10.  
  11.   Object.keys(definitions).reduce((program, opt) => {
  12.     if (typeof definitions[opt] == "object") {
  13.       const additionalOptions = definitions[opt];
  14.       if (additionalOptions.required === true) {
  15.         return program.option(`--${opt} <${opt}>`, additionalOptions.help, additionalOptions.action);
  16.       } else {
  17.         return program.option(`--${opt} [${opt}]`, additionalOptions.help, additionalOptions.action);
  18.       }
  19.     }
  20.     return program.option(`--${opt} [${opt}]`);
  21.   }, this);
  22.  
  23.   _defaults = Object.keys(definitions).reduce((defs, opt) => {
  24.     if(_definitions[opt].default) {
  25.       defs[opt] = _definitions[opt].default;
  26.     }
  27.     return defs;
  28.   }, {});
  29.  
  30.   _reverseDefinitions = Object.keys(definitions).reduce((object, key) => {
  31.     let value = definitions[key];
  32.     if (typeof value == "object") {
  33.       value = value.env;
  34.     }
  35.     if (value) {
  36.       object[value] = key;
  37.     }
  38.     return object;
  39.   }, {});
  40.  
  41.    /* istanbul ignore next */
  42.   this.on('--help', function(){
  43.     console.log('  Configure From Environment:');
  44.     console.log('');
  45.     Object.keys(_reverseDefinitions).forEach((key) => {
  46.       console.log(`    $ ${key}='${_reverseDefinitions[key]}'`);
  47.     });
  48.     console.log('');
  49.   });
  50. };
  51.  
  52. function parseEnvironment(env = {}) {
  53.   return Object.keys(_reverseDefinitions).reduce((options, key) => {
  54.     if (env[key]) {
  55.       const originalKey = _reverseDefinitions[key];
  56.       let action = (option) => (option);
  57.       if (typeof _definitions[originalKey] === "object") {
  58.         action = _definitions[originalKey].action || action;
  59.       }
  60.       options[_reverseDefinitions[key]] = action(env[key]);
  61.     }
  62.     return options;
  63.   }, {});
  64. }
  65.  
  66. function parseConfigFile(program) {
  67.   let options = {};
  68.   if (program.args.length > 0) {
  69.     let jsonPath = program.args[0];
  70.     jsonPath = path.resolve(jsonPath);
  71.     const jsonConfig = require(jsonPath);
  72.     if (jsonConfig.apps) {
  73.       if (jsonConfig.apps.length > 1) {
  74.         throw 'Multiple apps are not supported';
  75.       }
  76.       options = jsonConfig.apps[0];
  77.     } else {
  78.       options = jsonConfig;
  79.     }
  80.     Object.keys(options).forEach((key) => {
  81.       const value = options[key];
  82.       if (!_definitions[key]) {
  83.         throw `error: unknown option ${key}`;
  84.       }
  85.       const action = _definitions[key].action;
  86.       if (action) {
  87.         options[key] = action(value);
  88.       }
  89.     });
  90.     console.log(`Configuration loaded from ${jsonPath}`)
  91.   }
  92.   return options;
  93. }
  94.  
  95. Command.prototype.setValuesIfNeeded = function(options) {
  96.   Object.keys(options).forEach((key) => {
  97.     if (!this.hasOwnProperty(key)) {
  98.       this[key] = options[key];
  99.     }
  100.   });
  101. };
  102.  
  103. Command.prototype._parse = Command.prototype.parse;
  104.  
  105. Command.prototype.parse = function(args, env) {
  106.   this._parse(args);
  107.   // Parse the environment first
  108.   const envOptions = parseEnvironment(env);
  109.   const fromFile = parseConfigFile(this);
  110.   // Load the env if not passed from command line
  111.   this.setValuesIfNeeded(envOptions);
  112.   // Load from file to override
  113.   this.setValuesIfNeeded(fromFile);
  114.   // Last set the defaults
  115.   this.setValuesIfNeeded(_defaults);
  116. };
  117.  
  118. Command.prototype.getOptions = function() {
  119.   return Object.keys(_definitions).reduce((options, key) => {
  120.     if (typeof this[key] !== 'undefined') {
  121.       options[key] = this[key];
  122.     }
  123.     return options;
  124.   }, {});
  125. };
  126.  
  127. export default new Command();
  128. /* eslint-enable no-console */
  129.  
downloadcommander.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top