def setup(): # add node and npm from mozbuild to front of system path npm, _ = nodeutil.find_npm_executable() if not npm: exit(EX_CONFIG, "could not find npm executable") path = os.path.abspath(os.path.join(npm, os.pardir)) os.environ["PATH"] = "{}:{}".format(path, os.environ["PATH"])
def npm(command_context, args): from mozbuild.nodeutil import find_npm_executable # Avoid logging the command command_context.log_manager.terminal_handler.setLevel(logging.CRITICAL) import os # Add node and npm from mozbuild to front of system path # # This isn't pretty, but npm currently executes itself with # `#!/usr/bin/env node`, which means it just uses the node in the # current PATH. As a result, stuff gets built wrong and installed # in the wrong places and probably other badness too without this: npm_path, _ = find_npm_executable() if not npm_path: exit(-1, "could not find npm executable") path = os.path.abspath(os.path.dirname(npm_path)) os.environ["PATH"] = "{}:{}".format(path, os.environ["PATH"]) return command_context.run_process( [npm_path, "--scripts-prepend-node-path=auto"] + args, pass_thru=True, # Avoid eating npm output/error messages ensure_exit_code=False, # Don't throw on non-zero exit code. )
def npm(self, args): from mozbuild.nodeutil import find_npm_executable # Avoid logging the command self.log_manager.terminal_handler.setLevel(logging.CRITICAL) npm_path, _ = find_npm_executable() return self.run_process( [npm_path, "--scripts-prepend-node-path=auto"] + args, pass_thru=True, # Avoid eating npm output/error messages ensure_exit_code=False, # Don't throw on non-zero exit code. )
def check_node_executables_valid(): node_path, version = find_node_executable() if not node_path: print(NODE_NOT_FOUND_MESSAGE) return False if not version: print(NODE_MACHING_VERSION_NOT_FOUND_MESSAGE % NODE_MIN_VERSION) return False npm_path, version = find_npm_executable() if not npm_path: print(NPM_NOT_FOUND_MESSAGE) return False if not version: print(NPM_MACHING_VERSION_NOT_FOUND_MESSAGE % NPM_MIN_VERSION) return False return True
def package_setup( package_root, package_name, should_update=False, should_clobber=False, no_optional=False, ): """Ensure `package_name` at `package_root` is installed. When `should_update` is true, clobber, install, and produce a new "package-lock.json" file. This populates `package_root/node_modules`. """ orig_project_root = get_project_root() orig_cwd = os.getcwd() if should_update: should_clobber = True try: set_project_root(package_root) sys.path.append(os.path.dirname(__file__)) # npm sometimes fails to respect cwd when it is run using check_call so # we manually switch folders here instead. project_root = get_project_root() os.chdir(project_root) if should_clobber: node_modules_path = os.path.join(project_root, "node_modules") print("Clobbering %s..." % node_modules_path) if sys.platform.startswith("win") and have_winrm(): process = subprocess.Popen(["winrm", "-rf", node_modules_path]) process.wait() else: mozfileremove(node_modules_path) npm_path, _ = find_npm_executable() if not npm_path: return 1 node_path, _ = find_node_executable() if not node_path: return 1 extra_parameters = ["--loglevel=error"] if no_optional: extra_parameters.append("--no-optional") package_lock_json_path = os.path.join(get_project_root(), "package-lock.json") if should_update: cmd = [npm_path, "install"] mozfileremove(package_lock_json_path) else: cmd = [npm_path, "ci"] # On non-Windows, ensure npm is called via node, as node may not be in the # path. if platform.system() != "Windows": cmd.insert(0, node_path) # Ensure that bare `node` and `npm` in scripts, including post-install scripts, finds the # binary we're invoking with. Without this, it's easy for compiled extensions to get # mismatched versions of the Node.js extension API. extra_parameters.append("--scripts-prepend-node-path") cmd.extend(extra_parameters) print('Installing %s for mach using "%s"...' % (package_name, " ".join(cmd))) result = call_process(package_name, cmd) if not result: return 1 bin_path = os.path.join(get_project_root(), "node_modules", ".bin", package_name) print("\n%s installed successfully!" % package_name) print("\nNOTE: Your local %s binary is at %s\n" % (package_name, bin_path)) finally: set_project_root(orig_project_root) os.chdir(orig_cwd)
def eslint_setup(should_clobber=False): """Ensure eslint is optimally configured. This command will inspect your eslint configuration and guide you through an interactive wizard helping you configure eslint for optimal use on Mozilla projects. """ orig_cwd = os.getcwd() sys.path.append(os.path.dirname(__file__)) # npm sometimes fails to respect cwd when it is run using check_call so # we manually switch folders here instead. project_root = get_project_root() os.chdir(project_root) if should_clobber: node_modules_path = os.path.join(project_root, "node_modules") print("Clobbering node_modules...") if sys.platform.startswith('win') and have_winrm(): process = subprocess.Popen(['winrm', '-rf', node_modules_path]) process.wait() else: mozfileremove(node_modules_path) npm_path, version = find_npm_executable() if not npm_path: return 1 node_path, _ = find_node_executable() if not node_path: return 1 extra_parameters = ["--loglevel=error"] package_lock_json_path = os.path.join(get_project_root(), "package-lock.json") package_lock_json_tmp_path = os.path.join(tempfile.gettempdir(), "package-lock.json.tmp") # If we have an npm version newer than 5.8.0, just use 'ci', as that's much # simpler and does exactly what we want. npm_is_older_version = version < StrictVersion("5.8.0").version if npm_is_older_version: cmd = [node_path, npm_path, "install"] shutil.copy2(package_lock_json_path, package_lock_json_tmp_path) else: cmd = [node_path, npm_path, "ci"] cmd.extend(extra_parameters) print("Installing eslint for mach using \"%s\"..." % (" ".join(cmd))) result = call_process("eslint", cmd) if npm_is_older_version: shutil.move(package_lock_json_tmp_path, package_lock_json_path) if not result: return 1 eslint_path = os.path.join(get_project_root(), "node_modules", ".bin", "eslint") print("\nESLint and approved plugins installed successfully!") print("\nNOTE: Your local eslint binary is at %s\n" % eslint_path) os.chdir(orig_cwd)
def package_setup(package_root, package_name, should_clobber=False, no_optional=False): """Ensure `package_name` at `package_root` is installed. This populates `package_root/node_modules`. """ orig_project_root = get_project_root() orig_cwd = os.getcwd() try: set_project_root(package_root) sys.path.append(os.path.dirname(__file__)) # npm sometimes fails to respect cwd when it is run using check_call so # we manually switch folders here instead. project_root = get_project_root() os.chdir(project_root) if should_clobber: node_modules_path = os.path.join(project_root, "node_modules") print("Clobbering %s..." % node_modules_path) if sys.platform.startswith('win') and have_winrm(): process = subprocess.Popen(['winrm', '-rf', node_modules_path]) process.wait() else: mozfileremove(node_modules_path) npm_path, version = find_npm_executable() if not npm_path: return 1 node_path, _ = find_node_executable() if not node_path: return 1 extra_parameters = ["--loglevel=error"] if no_optional: extra_parameters.append('--no-optional') package_lock_json_path = os.path.join(get_project_root(), "package-lock.json") package_lock_json_tmp_path = os.path.join(tempfile.gettempdir(), "package-lock.json.tmp") # If we have an npm version newer than 5.8.0, just use 'ci', as that's much # simpler and does exactly what we want. npm_is_older_version = version < StrictVersion("5.8.0").version if npm_is_older_version: cmd = [npm_path, "install"] shutil.copy2(package_lock_json_path, package_lock_json_tmp_path) else: cmd = [npm_path, "ci"] # On non-Windows, ensure npm is called via node, as node may not be in the # path. if platform.system() != "Windows": cmd.insert(0, node_path) cmd.extend(extra_parameters) # Ensure that bare `node` and `npm` in scripts, including post-install scripts, finds the # binary we're invoking with. Without this, it's easy for compiled extensions to get # mismatched versions of the Node.js extension API. path = os.environ.get('PATH', '').split(os.pathsep) node_dir = os.path.dirname(node_path) if node_dir not in path: path = [node_dir] + path print("Installing %s for mach using \"%s\"..." % (package_name, " ".join(cmd))) result = call_process(package_name, cmd, append_env={'PATH': os.pathsep.join(path)}) if npm_is_older_version: shutil.move(package_lock_json_tmp_path, package_lock_json_path) if not result: return 1 bin_path = os.path.join(get_project_root(), "node_modules", ".bin", package_name) print("\n%s installed successfully!" % package_name) print("\nNOTE: Your local %s binary is at %s\n" % (package_name, bin_path)) finally: set_project_root(orig_project_root) os.chdir(orig_cwd)