Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

85 строки
1.9 KiB

  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. 'use strict';
  8. var chalk = require('chalk');
  9. var execSync = require('child_process').execSync;
  10. var path = require('path');
  11. var execOptions = {
  12. encoding: 'utf8',
  13. stdio: [
  14. 'pipe', // stdin (default)
  15. 'pipe', // stdout (default)
  16. 'ignore', //stderr
  17. ],
  18. };
  19. function isProcessAReactApp(processCommand) {
  20. return /^node .*react-scripts\/scripts\/start\.js\s?$/.test(processCommand);
  21. }
  22. function getProcessIdOnPort(port) {
  23. return execSync('lsof -i:' + port + ' -P -t -sTCP:LISTEN', execOptions)
  24. .split('\n')[0]
  25. .trim();
  26. }
  27. function getPackageNameInDirectory(directory) {
  28. var packagePath = path.join(directory.trim(), 'package.json');
  29. try {
  30. return require(packagePath).name;
  31. } catch (e) {
  32. return null;
  33. }
  34. }
  35. function getProcessCommand(processId, processDirectory) {
  36. var command = execSync(
  37. 'ps -o command -p ' + processId + ' | sed -n 2p',
  38. execOptions
  39. );
  40. command = command.replace(/\n$/, '');
  41. if (isProcessAReactApp(command)) {
  42. const packageName = getPackageNameInDirectory(processDirectory);
  43. return packageName ? packageName : command;
  44. } else {
  45. return command;
  46. }
  47. }
  48. function getDirectoryOfProcessById(processId) {
  49. return execSync(
  50. 'lsof -p ' +
  51. processId +
  52. ' | awk \'$4=="cwd" {for (i=9; i<=NF; i++) printf "%s ", $i}\'',
  53. execOptions
  54. ).trim();
  55. }
  56. function getProcessForPort(port) {
  57. try {
  58. var processId = getProcessIdOnPort(port);
  59. var directory = getDirectoryOfProcessById(processId);
  60. var command = getProcessCommand(processId, directory);
  61. return (
  62. chalk.cyan(command) +
  63. chalk.grey(' (pid ' + processId + ')\n') +
  64. chalk.blue(' in ') +
  65. chalk.cyan(directory)
  66. );
  67. } catch (e) {
  68. return null;
  69. }
  70. }
  71. module.exports = getProcessForPort;