0
# Environment Management
1
2
Core functionality for creating, configuring, and managing isolated Node.js virtual environments. Provides comprehensive environment setup with cross-platform shell integration and activation scripts.
3
4
## Capabilities
5
6
### Environment Creation
7
8
Creates a complete Node.js virtual environment with proper directory structure, activation scripts, and environment isolation.
9
10
```python { .api }
11
def create_environment(env_dir, args):
12
"""
13
Create a new Node.js virtual environment.
14
15
Parameters:
16
env_dir (str): Target directory for the environment
17
args (argparse.Namespace): Parsed command-line arguments
18
19
Creates directory structure, installs Node.js and npm if requested,
20
sets up activation scripts for multiple shells, and configures
21
the environment for isolated Node.js development.
22
23
The created environment includes:
24
- Node.js installation (version specified in args)
25
- npm installation (if --with-npm specified)
26
- Shell activation scripts (bash, zsh, fish, PowerShell, cmd)
27
- Environment configuration and PATH management
28
- Integration with Python virtualenv (if detected)
29
"""
30
```
31
32
### Environment Directory Resolution
33
34
Determines the target directory for environment creation based on arguments and current context.
35
36
```python { .api }
37
def get_env_dir(args):
38
"""
39
Determine environment directory path from arguments.
40
41
Parameters:
42
args (argparse.Namespace): Parsed command-line arguments
43
44
Returns:
45
str: Absolute path to environment directory
46
47
Handles various scenarios:
48
- Explicit environment path specification
49
- Default environment naming
50
- Integration with existing Python virtualenv
51
- Current working directory considerations
52
"""
53
```
54
55
### Activation Script Installation
56
57
Installs shell activation and deactivation scripts for cross-platform environment management.
58
59
```python { .api }
60
def install_activate(env_dir, args):
61
"""
62
Install activation scripts for the Node.js environment.
63
64
Parameters:
65
env_dir (str): Environment directory path
66
args (argparse.Namespace): Configuration arguments
67
68
Creates activation scripts for multiple shells:
69
- activate (bash/zsh)
70
- activate.bat (Windows cmd)
71
- activate.ps1 (PowerShell)
72
- activate.fish (fish shell)
73
74
Also creates corresponding deactivation functionality and
75
configures environment variables for Node.js and npm.
76
"""
77
```
78
79
### Pre-deactivation Hook
80
81
Sets up hooks that run before environment deactivation for cleanup operations.
82
83
```python { .api }
84
def set_predeactivate_hook(env_dir):
85
"""
86
Set up pre-deactivation hook for environment cleanup.
87
88
Parameters:
89
env_dir (str): Environment directory path
90
91
Creates a hook script that runs before environment deactivation
92
to perform necessary cleanup operations and restore the previous
93
environment state properly.
94
"""
95
```
96
97
## Usage Examples
98
99
### Basic Environment Creation
100
101
```python
102
import nodeenv
103
from argparse import Namespace
104
105
# Create environment with default settings
106
args = Namespace(
107
node='latest',
108
with_npm=True,
109
jobs='2',
110
without_ssl=False,
111
source=False,
112
requirements=None
113
)
114
115
nodeenv.create_environment('/path/to/myenv', args)
116
```
117
118
### Environment with Custom Configuration
119
120
```python
121
import nodeenv
122
from argparse import Namespace
123
124
# Create environment with specific Node.js version and build options
125
args = Namespace(
126
node='16.20.0',
127
with_npm=True,
128
jobs='4',
129
without_ssl=False,
130
source=True, # Build from source
131
requirements='/path/to/requirements.txt',
132
mirror='https://npm.taobao.org/mirrors/node'
133
)
134
135
nodeenv.create_environment('/path/to/custom-env', args)
136
```