-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatic version switching in powershell
- Loading branch information
Showing
11 changed files
with
265 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# AUTO Command - Node Version Switcher | ||
``` | ||
nvs auto | ||
nvs auto on | ||
nvs auto off | ||
``` | ||
When invoked with no parameters, `nvs auto` searches for the nearest `.node-version` file in the current directory or parent directories. If found, the version specified in the file is then added (if necessary) and used. If no `.node-version` file is found, then the default (linked) version, if any, is used. | ||
|
||
The `nvs auto on` command enables automatic switching as needed whenever the current shell's working directory changes; `nvs auto off` disables automatic switching in the current shell. (This feature is not supported in Windows Command Prompt.) | ||
|
||
A `.node-version` file must contain a single line with a valid NVS version string. A version string consists of a semantic version number, optionally preceded by a remote name, optionally followed by a processor architecture or bitness ("x86", "x64", "32", "64"), separated by slashes. Version labels ("lts", "latest") are not supported for use with the USE command. Examples: "4.6.0", "6.3.1/x86", "node/6.7.0/x64" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* global settings */ | ||
let fs = require('fs'); // Non-const enables test mocking | ||
const os = require('os'); | ||
const path = require('path'); | ||
const Error = require('./error'); | ||
|
||
const nvsVersion = require('./version'); | ||
let nvsUse = require('./use'); // Non-const enables test mocking | ||
let nvsAddRemove = require('./addRemove'); // Non-const enables test mocking | ||
|
||
/** | ||
* Searches for the nearest `.node-version` file in the current directory or parent directories. | ||
* If found, the version specified in the file is then added (if necessary) and used. If no | ||
* `.node-version` file is found, then the default (linked) version, if any, is used. | ||
*/ | ||
function autoSwitchAsync(cwd) { | ||
let version = null; | ||
let dir = cwd || process.cwd(); | ||
while (dir) { | ||
let versionFile = path.join(dir, '.node-version'); | ||
try { | ||
let versionString = fs.readFileSync(versionFile, 'utf8').trim(); | ||
try { | ||
version = nvsVersion.parse(versionString); | ||
} catch (e) { | ||
throw new Error('Failed to parse version in file: ' + versionFile, e); | ||
} | ||
} catch (e) { | ||
Error.throwIfNot(Error.ENOENT, e, 'Failed to read file: ' + versionFile); | ||
} | ||
|
||
let parentDir = path.dirname(dir); | ||
dir = (parentDir !== dir ? parentDir : null); | ||
} | ||
|
||
|
||
if (version) { | ||
let binPath = nvsUse.getVersionBinary(version); | ||
if (!binPath) { | ||
let versionString = | ||
version.remoteName + '/' + version.semanticVersion + '/' + version.arch; | ||
console.log('Adding: ' + versionString); | ||
|
||
return nvsAddRemove.addAsync(version).then(() => { | ||
return nvsUse.use(version); | ||
}); | ||
} | ||
} | ||
|
||
return Promise.resolve(nvsUse.use(version)); | ||
} | ||
|
||
/** | ||
* Enables or disables automatic version switching based on the presence of a | ||
* .node-version file in the current shell directory or a parent directory. | ||
* (This functionality requires support from the bootstrap shell script.) | ||
* | ||
* @param {any} enable | ||
*/ | ||
function enableAutoSwitch(enable) { | ||
if (/\.cmd/i.test(process.env['NVS_POSTSCRIPT'])) { | ||
throw new Error('Automatic switching is not supported from a Windows Command Prompt.' + | ||
os.EOL + 'Use PowerShell instead.'); | ||
} | ||
|
||
let psScriptFile = path.join(path.resolve(__dirname, '..'), 'nvs.ps1'); | ||
let shScriptFile = path.join(path.resolve(__dirname, '..'), 'nvs.sh'); | ||
|
||
require('./postScript').generate(null, { | ||
'.PS1': [ | ||
// Patch the function that is invoked every time PowerShell shows a prompt. | ||
// Export the function from the script using a dynamic module; this | ||
// does NOT require the script to be sourced. | ||
'if (-not $env:NVS_ORIGINAL_PROMPT) { $env:NVS_ORIGINAL_PROMPT = prompt }', | ||
'New-Module -Script {', | ||
enable | ||
? 'function prompt { . "' + psScriptFile + '" "prompt" }' | ||
: 'function prompt { Invoke-Expression $env:NVS_ORIGINAL_PROMPT }', | ||
'Export-ModuleMember -Function prompt', | ||
'} > $null', | ||
], | ||
'.SH': null // TODO: define cd(), pushd(), popd() functions | ||
}); | ||
} | ||
|
||
module.exports = { | ||
autoSwitchAsync, | ||
enableAutoSwitch, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.