cli.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env node
  2. const commander = require('commander');
  3. const inquirer = require('inquirer'); //命令行答询
  4. const ora = require('ora'); //命令行中加载状态标识
  5. const chalk = require('chalk'); //命令行输出字符颜色
  6. const fs = require('fs');
  7. const path = require("path");
  8. const serverHandler = require('../server/serverHandler');
  9. const clientHandler = require('../client/clientHandler');
  10. const projectPackage = require('../package.json');
  11. // 工具版本号
  12. commander.version(projectPackage.version);
  13. commander
  14. .command('server <configPath>')
  15. .description('start the cros server')
  16. .action(function (configPath) {
  17. let serverConfig;
  18. if(path.isAbsolute(configPath)){
  19. if(!fs.existsSync(configPath)){
  20. console.log(chalk.red(`[cros] The configuration file does not exist`));
  21. return 0;
  22. }
  23. serverConfig = JSON.parse(fs.readFileSync(configPath).toString());
  24. }else{
  25. let truePath = path.resolve('./', configPath);
  26. if(!fs.existsSync(truePath)){
  27. console.log(chalk.red(`[cros] The configuration file does not exist`));
  28. return 0;
  29. }
  30. serverConfig = JSON.parse(fs.readFileSync(truePath).toString());
  31. }
  32. let serverHandlerIns = new serverHandler(serverConfig);
  33. serverHandlerIns.start();
  34. });
  35. commander
  36. .command('client <configPath>')
  37. .description('start the cros client')
  38. .action(function (configPath) {
  39. let serverConfig;
  40. if(path.isAbsolute(configPath)){
  41. if(!fs.existsSync(configPath)){
  42. console.log(chalk.red(`[cros] The configuration file does not exist`));
  43. return 0;
  44. }
  45. serverConfig = JSON.parse(fs.readFileSync(configPath).toString());
  46. }else{
  47. let truePath = path.resolve('./', configPath);
  48. if(!fs.existsSync(truePath)){
  49. console.log(chalk.red(`[cros] The configuration file does not exist`));
  50. return 0;
  51. }
  52. serverConfig = JSON.parse(fs.readFileSync(truePath).toString());
  53. }
  54. let clientHandlerIns = new clientHandler(serverConfig);
  55. clientHandlerIns.start();
  56. });
  57. commander.parse(process.argv);