Skip to content

Commit 1e46b93

Browse files
committed
feat: add py-node library
1 parent caea27e commit 1e46b93

File tree

12 files changed

+998
-0
lines changed

12 files changed

+998
-0
lines changed

.github/workflows/tests.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [main, master]
6+
pull_request:
7+
branches: [main, master]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14"]
15+
16+
steps:
17+
- uses: actions/checkout@v5
18+
19+
- name: Set up Python ${{ matrix.python-version }}
20+
uses: actions/setup-python@v5
21+
with:
22+
python-version: ${{ matrix.python-version }}
23+
24+
- name: Install dependencies
25+
run: |
26+
python -m pip install --upgrade pip
27+
pip install -e .
28+
pip install -r requirements-dev.txt
29+
30+
- name: Run tests
31+
run: |
32+
pytest tests/ --cov=py_node --cov-report=xml
33+
34+
- name: Upload coverage to Codecov
35+
uses: codecov/codecov-action@v5
36+
with:
37+
file: ./coverage.xml
38+
flags: unittests
39+
name: codecov-umbrella
40+
41+
lint-format:
42+
runs-on: ubuntu-latest
43+
steps:
44+
- uses: actions/checkout@v5
45+
46+
- name: Set up Python
47+
uses: actions/setup-python@v5
48+
with:
49+
python-version: "3.10"
50+
51+
- name: Install dependencies
52+
run: |
53+
python -m pip install --upgrade pip
54+
pip install -e .
55+
pip install ruff
56+
57+
- name: Run linting
58+
run: |
59+
ruff check .
60+
61+
- name: Run format check
62+
run: |
63+
ruff format . --check

README-zh_CN.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# py-node
2+
3+
一个方便在Python中使用Node.js的工具库。
4+
5+
[![Tests](https://github.com/HogaStack/py-node/workflows/Tests/badge.svg)](https://github.com/HogaStack/py-node/actions)
6+
[![Coverage](https://codecov.io/gh/HogaStack/py-node/branch/main/graph/badge.svg)](https://codecov.io/gh/HogaStack/py-node)
7+
[![GitHub](https://shields.io/badge/license-MIT-informational)](https://github.com/HogaStack/py-node/blob/main/LICENSE)
8+
[![PyPI](https://img.shields.io/pypi/v/py-node.svg?color=dark-green)](https://pypi.org/project/py-node/)
9+
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
10+
11+
简体中文 | [English](./README.md)
12+
13+
## 安装
14+
15+
```bash
16+
pip install py-node
17+
```
18+
19+
## 使用方法
20+
21+
### 基本用法
22+
23+
```python
24+
from py_node import NodeManager
25+
26+
# 创建NodeManager实例
27+
# download_node=True 表示如果系统中没有Node.js则自动下载
28+
# node_version指定要下载的Node.js版本
29+
manager = NodeManager(download_node=True, node_version='18.17.0')
30+
31+
# 获取Node.js、npm和npx的路径
32+
node_path = manager.node_path # 下载的Node.js路径,如果使用系统Node.js则为None
33+
npm_path = manager.npm_path # npm路径
34+
npx_path = manager.npx_path # npx路径
35+
36+
# 获取环境变量(如果使用下载的Node.js)
37+
node_env = manager.node_env # 环境变量字典
38+
```
39+
40+
### 运行Node.js命令
41+
42+
```python
43+
import subprocess
44+
from py_node import NodeManager
45+
46+
manager = NodeManager(download_node=True, node_version='18.17.0')
47+
48+
# 运行Node.js命令
49+
result = subprocess.run(
50+
[manager.npm_path, 'init', '-y'],
51+
env=manager.node_env,
52+
capture_output=True,
53+
text=True
54+
)
55+
print(result.stdout)
56+
```
57+
58+
### 不自动下载Node.js
59+
60+
```python
61+
from py_node import NodeManager
62+
63+
# 如果系统中没有Node.js将会抛出异常
64+
manager = NodeManager(download_node=False, node_version='18.17.0')
65+
```
66+
67+
## 测试
68+
69+
本项目使用pytest进行测试,测试覆盖率达到100%。
70+
71+
### 运行测试
72+
73+
在conda环境中运行测试:
74+
75+
```bash
76+
# 激活conda环境
77+
conda activate py-node
78+
79+
# 安装依赖
80+
pip install -e .
81+
pip install -r requirements-dev.txt
82+
83+
# 运行测试
84+
pytest tests/ -v
85+
86+
# 运行测试并生成覆盖率报告
87+
pytest tests/ --cov=py_node --cov-report=term-missing
88+
```
89+
90+
### 测试结构
91+
92+
- `tests/test_node_manager.py` - NodeManager类的完整测试套件
93+
- `tests/conftest.py` - pytest配置
94+
- `tests/__init__.py` - 包初始化文件
95+
96+
所有代码路径都经过测试,包括:
97+
98+
- 正常执行路径
99+
- 错误处理场景
100+
- 平台特定行为
101+
- CLI模式日志
102+
- 缓存的Node.js使用

README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# py-node
2+
3+
A tool library for conveniently using Node.js in Python.
4+
5+
[![Tests](https://github.com/HogaStack/py-node/workflows/Tests/badge.svg)](https://github.com/HogaStack/py-node/actions)
6+
[![Coverage](https://codecov.io/gh/HogaStack/py-node/branch/main/graph/badge.svg)](https://codecov.io/gh/HogaStack/py-node)
7+
[![GitHub](https://shields.io/badge/license-MIT-informational)](https://github.com/HogaStack/py-node/blob/main/LICENSE)
8+
[![PyPI](https://img.shields.io/pypi/v/py-node.svg?color=dark-green)](https://pypi.org/project/py-node/)
9+
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
10+
11+
English | [简体中文](./README-zh_CN.md)
12+
13+
## Installation
14+
15+
```bash
16+
pip install py-node
17+
```
18+
19+
## Usage
20+
21+
### Basic Usage
22+
23+
```python
24+
from py_node import NodeManager
25+
26+
# Create NodeManager instance
27+
# download_node=True means automatically download Node.js if not found in system
28+
# node_version specifies the Node.js version to download
29+
manager = NodeManager(download_node=True, node_version='18.17.0')
30+
31+
# Get paths to Node.js, npm, and npx
32+
node_path = manager.node_path # Path to downloaded Node.js, None if using system Node.js
33+
npm_path = manager.npm_path # Path to npm
34+
npx_path = manager.npx_path # Path to npx
35+
36+
# Get environment variables (if using downloaded Node.js)
37+
node_env = manager.node_env # Environment variables dictionary
38+
```
39+
40+
### Running Node.js Commands
41+
42+
```python
43+
import subprocess
44+
from py_node import NodeManager
45+
46+
manager = NodeManager(download_node=True, node_version='18.17.0')
47+
48+
# Run Node.js commands
49+
result = subprocess.run(
50+
[manager.npm_path, 'init', '-y'],
51+
env=manager.node_env,
52+
capture_output=True,
53+
text=True
54+
)
55+
print(result.stdout)
56+
```
57+
58+
### Without Automatic Node.js Download
59+
60+
```python
61+
from py_node import NodeManager
62+
63+
# Will raise an exception if Node.js is not found in the system
64+
manager = NodeManager(download_node=False, node_version='18.17.0')
65+
```
66+
67+
## Testing
68+
69+
This project uses pytest for testing with 100% code coverage.
70+
71+
### Running Tests
72+
73+
To run the tests in the conda environment:
74+
75+
```bash
76+
# Activate the conda environment
77+
conda activate py-node
78+
79+
# Install dependencies
80+
pip install -e .
81+
pip install -r requirements-dev.txt
82+
83+
# Run tests
84+
pytest tests/ -v
85+
86+
# Run tests with coverage report
87+
pytest tests/ --cov=py_node --cov-report=term-missing
88+
```
89+
90+
### Test Structure
91+
92+
- `tests/test_node_manager.py` - Complete test suite for NodeManager class
93+
- `tests/conftest.py` - pytest configuration
94+
- `tests/__init__.py` - Package initialization
95+
96+
All code paths are tested including:
97+
98+
- Normal execution paths
99+
- Error handling scenarios
100+
- Platform-specific behaviors
101+
- CLI mode logging
102+
- Cached Node.js usage

py_node/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from .manager import logger, NodeManager
2+
3+
4+
__version__ = '0.1.0'
5+
6+
7+
__all__ = ['logger', 'NodeManager']

0 commit comments

Comments
 (0)