node.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env python
  2. # Copyright 2017 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. from os import path as os_path
  6. import platform
  7. import subprocess
  8. import sys
  9. import os
  10. def GetBinaryPath():
  11. darwin_name = ('node-darwin-arm64' if platform.machine() == 'arm64' else
  12. 'node-darwin-x64')
  13. return os_path.join(os_path.dirname(__file__), *{
  14. 'Darwin': ('mac', darwin_name, 'bin', 'node'),
  15. 'Linux': ('linux', 'node-linux-x64', 'bin', 'node'),
  16. 'Windows': ('win', 'node.exe'),
  17. }[platform.system()])
  18. def RunNode(cmd_parts, stdout=None):
  19. cmd = [GetBinaryPath()] + cmd_parts
  20. process = subprocess.Popen(
  21. cmd, cwd=os.getcwd(), stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  22. universal_newlines=True)
  23. stdout, stderr = process.communicate()
  24. if process.returncode != 0:
  25. # Handle cases where stderr is empty, even though the command failed, for
  26. # example https://github.com/microsoft/TypeScript/issues/615
  27. err = stderr if len(stderr) > 0 else stdout
  28. raise RuntimeError('Command \'%s\' failed\n%s' % (' '.join(cmd), err))
  29. return stdout
  30. if __name__ == '__main__':
  31. RunNode(sys.argv[1:])