nvm-vanilla 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -17,4 +17,10 @@ nvm exec <version> <command>
17
17
 
18
18
  nvm alias <name> <version>
19
19
  nvm unalias <name>
20
- ```
20
+ ```
21
+
22
+ ### For Windows
23
+
24
+ ```
25
+ Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
26
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nvm-vanilla",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
package/src/bin.js CHANGED
@@ -15,6 +15,12 @@ const evalCommandSet = new Set(['use', 'autoload', 'env']);
15
15
  const main = async () => {
16
16
  // process.stderr.write(JSON.stringify(process.argv) + '\n'); // debug
17
17
 
18
+ await promisify(fs.appendFile)(path.resolve(__dirname, '.history'), JSON.stringify({
19
+ argv: process.argv,
20
+ cwd: process.cwd(),
21
+ time: Date.now(),
22
+ }));
23
+
18
24
  const homeDir = os.homedir();
19
25
 
20
26
  const baseDir = path.join(homeDir, '.nvm-vanilla');
@@ -41,7 +47,7 @@ const main = async () => {
41
47
  case 'env': {
42
48
  const content = await promisify(fs.readFile)(path.resolve(
43
49
  __dirname,
44
- process.platform == 'win32' ? 'nvm.ps1' : 'nvm.sh'
50
+ process.env.PSModulePath ? 'nvm.ps1' : 'nvm.sh'
45
51
  ), 'utf-8');
46
52
  process.stdout.write(content);
47
53
  break;
@@ -52,10 +58,11 @@ const main = async () => {
52
58
  }
53
59
  case 'autoload': {
54
60
  const targetVersion = await detect(baseDir);
55
- if (targetVersion) {
61
+ try {
62
+ if (!targetVersion) throw '';
56
63
  await use(baseDir, targetVersion);
57
- } else {
58
- console.log(':');
64
+ } catch (_) {
65
+ console.log(process.env.PSModulePath ? ';' : ':');
59
66
  }
60
67
  break;
61
68
  }
package/src/index.js CHANGED
@@ -359,15 +359,13 @@ const use = async (baseDir, version, evalFlag = true) => {
359
359
  if (evalFlag) {
360
360
  for (const key in env) {
361
361
  const value = env[key];
362
- let command = (key === 'PATH' || true)
363
- ? `export ${key}=${value}`
364
- : `export ${key}=\${${key}:-${value}}`;
362
+
363
+ // shell
364
+ let command = `export ${key}=${value}`;
365
365
 
366
366
  // powershell
367
- if (process.platform === 'win32') {
368
- command = (key === 'PATH' || true)
369
- ? `$env:${key}="${value}"`
370
- : `if not defined ${key} set ${key}=${value}`;
367
+ if (process.env.PSModulePath) {
368
+ command = `$env:${key}="${value}"`;
371
369
  }
372
370
 
373
371
  process.stdout.write(
@@ -384,10 +382,10 @@ const use = async (baseDir, version, evalFlag = true) => {
384
382
  };
385
383
 
386
384
  const list = async (baseDir) => {
387
- const nameList = await promisify(fs.readdir)(baseDir);
385
+ const nameList = await promisify(fs.readdir)(baseDir).catch(() => []);
388
386
 
389
387
  let versionList = await Promise.all(nameList.map(async name => {
390
- const version = getLocalNodeVersion(baseDir, name).catch(() => {});
388
+ const version = await getLocalNodeVersion(baseDir, name).catch(() => {});
391
389
  if (!version) return;
392
390
  return 'node@' + name + ' (' + version + ')';
393
391
  }));
package/src/init.js CHANGED
@@ -16,7 +16,7 @@ const insert = async (profilePath, line) => {
16
16
  };
17
17
 
18
18
  const init = async () => {
19
- if (process.platform === 'win32') {
19
+ if (process.env.PSModulePath) {
20
20
  const profilePath = path.resolve(os.homedir(), 'Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1');
21
21
  const line = 'nvm-vanilla --eval env | Out-String | Invoke-Expression';
22
22
  await insert(profilePath, line);
package/src/nvm.ps1 CHANGED
@@ -1,8 +1,41 @@
1
1
  function nvm {
2
- $result = nvm-vanilla --eval @args 2>$null
2
+ $result = nvm-vanilla --eval @args
3
3
  if ($result) {
4
4
  Invoke-Expression ($result -join ";")
5
5
  } else {
6
6
  nvm-vanilla @args
7
7
  }
8
8
  }
9
+
10
+ $global:_nvmAutoloadDir = ""
11
+
12
+ function _nvmAutoloadHook {
13
+ # 只在交互式 shell 中运行
14
+ if (-not [Environment]::UserInteractive) { return }
15
+
16
+ $currentDir = (Get-Location).Path
17
+
18
+ # 如果目录没变,跳过
19
+ if ($global:_nvmAutoloadDir -eq $currentDir) { return }
20
+
21
+ nvm autoload
22
+
23
+ $global:_nvmAutoloadDir = $currentDir
24
+ }
25
+
26
+ # 集成到 prompt 中
27
+ $originalPrompt = Get-Item function:prompt -ErrorAction SilentlyContinue
28
+ if ($originalPrompt) {
29
+ $originalPrompt = $originalPrompt.ScriptBlock
30
+ }
31
+
32
+ function global:prompt {
33
+ _nvmAutoloadHook
34
+
35
+ # 调用原始 prompt
36
+ if ($originalPrompt) {
37
+ & $originalPrompt
38
+ } else {
39
+ "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
40
+ }
41
+ }