@endevr-io/wsl-api
v1.0.4
Published
Node wrapper and utilities for the WSL CLI
Downloads
94
Readme
@endevr-io/wsl-api
Node wrapper and utilities for the WSL CLI. Uses node child_process with no other dependencies
Installation
You can use npm or yarn to install this package into your project
npm install @endevr-io/wsl-api
yarn add @endevr-io/wsl-api
Functions
Typedefs
exportDistribution(distribution, path) ⇒ Promise
Exports a specified distribution to a path/file name
Kind: global function
Returns: Promise - resolves promise from execPromise
Throws:
- TypeError throws if distribution is not a string or path is not a string
- Error throws if distribution could not be exported
| Param | Type | Description | | --- | --- | --- | | distribution | String | distribution to export | | path | String | path/file name to export to |
Example
const wsl = require('@endevr-io/wsl-api')
await wsl.exportDistribution('Ubuntu', 'C:\\endevr\\Ubuntu.tar.gz')
importDistribution(distribution, location, path) ⇒ Promise
Imports a new distribution to a location from a path/file name
Kind: global function
Returns: Promise - resolves promise from execPromise
Throws:
- TypeError throws if distribution is not a string or location is not a string or path is not a string
- Error throws if distribution could not be imported
| Param | Type | Description | | --- | --- | --- | | distribution | String | distribution to import | | location | String | folder path to import to | | path | String | path/file name to import from |
Example
const wsl = require('@endevr-io/wsl-api')
await wsl.importDistribution('Custom', 'C:\\endevr', 'C:\\endevr\\Ubuntu.tar.gz')
install() ⇒ Promise
Install WSL and Ubuntu
Kind: global function
Returns: Promise - resolves promise from execPromise
Throws:
- Error throws if WSL and Ubuntu could not be installed
Example
const wsl = require('@endevr-io/wsl-api')
await wsl.install()
installDistribution(distribution) ⇒ Promise
Install official distribution
Kind: global function
Returns: Promise - resolves promise from execPromise
Throws:
- TypeError throws if distribution is not a string
- Error throws if distribution could not be installed
| Param | Type | Description | | --- | --- | --- | | distribution | String | distribution to install |
Example
const wsl = require('@endevr-io/wsl-api')
await wsl.installDistribution('Ubuntu')
list([raw]) ⇒ Promise | Array.<ListObject> | String | Null
Lists distributions
Kind: global function
Returns: Promise | Array.<ListObject> | String | Null - returns an array of distribution objects, a string of the raw output, or null
Throws:
- TypeError throws if raw is not a boolean
| Param | Type | Default | Description | | --- | --- | --- | --- | | [raw] | Boolean | false | option to return a string of the raw output |
Example
const wsl = require('@endevr-io/wsl-api')
const list = await wsl.list()
console.log(list)
// [
// { name: 'Ubuntu', default: true },
// { name: 'Debian', default: false }
// ]
listAll([raw]) ⇒ Promise | Array.<ListObject> | String | Null
Lists all distributions
Kind: global function
Returns: Promise | Array.<ListObject> | String | Null - returns an array of distribution objects, a string of the raw output, or null
Throws:
- TypeError throws if raw is not a boolean
| Param | Type | Default | Description | | --- | --- | --- | --- | | [raw] | Boolean | false | option to return a string of the raw output |
Example
const wsl = require('@endevr-io/wsl-api')
const listAll = await wsl.listAll()
console.log(listAll)
// [
// { name: 'Ubuntu', default: true },
// { name: 'Debian', default: false }
// ]
listOnline([raw]) ⇒ Promise | Array.<ListOnlineObject> | String | Null
Lists official online distributions that can be installed
Kind: global function
Returns: Promise | Array.<ListOnlineObject> | String | Null - returns an array of distribution objects, a string of the raw output, or null
Throws:
- TypeError throws if raw is not a boolean
- Error throws if unable to list online
| Param | Type | Default | Description | | --- | --- | --- | --- | | [raw] | Boolean | false | option to return a string of the raw output |
Example
const wsl = require('@endevr-io/wsl-api')
const listOnline = await wsl.listOnline()
console.log(listOnline)
// [
// { name: 'Ubuntu', friendly: 'Ubuntu' },
// { name: 'Debian', friendly: 'Debian GNU/Linux' },
// { name: 'kali-linux', friendly: 'Kali Linux Rolling' },
// { name: 'openSUSE-42', friendly: 'openSUSE Leap 42' },
// { name: 'SLES-12', friendly: 'SUSE Linux Enterprise Server v12' },
// { name: 'Ubuntu-16.04', friendly: 'Ubuntu 16.04 LTS' },
// { name: 'Ubuntu-18.04', friendly: 'Ubuntu 18.04 LTS' },
// { name: 'Ubuntu-20.04', friendly: 'Ubuntu 20.04 LTS' }
// ]
listQuiet([raw]) ⇒ Promise | Array.<String> | String | Null
Lists quiet distributions
Kind: global function
Returns: Promise | Array.<String> | String | Null - returns an array of distribution names, a string of the raw output, or null
Throws:
- TypeError throws if raw is not a boolean
| Param | Type | Default | Description | | --- | --- | --- | --- | | [raw] | Boolean | false | option to return a string of the raw output |
Example
const wsl = require('@endevr-io/wsl-api')
const listQuiet = await wsl.listQuiet()
console.log(listQuiet)
// [ 'Ubuntu', 'Debian' ]
listRunning([raw]) ⇒ Promise | Array.<ListObject> | String | Null
Lists running distributions
Kind: global function
Returns: Promise | Array.<ListObject> | String | Null - returns an array of distribution objects, a string of the raw output, or null
Throws:
- TypeError throws if raw is not a boolean
| Param | Type | Default | Description | | --- | --- | --- | --- | | [raw] | Boolean | false | option to return a string of the raw output |
Example
const wsl = require('@endevr-io/wsl-api')
const listRunning = await wsl.listRunning()
console.log(listRunning)
// [ { name: 'Ubuntu', default: true } ]
listVerbose([raw]) ⇒ Promise | Array.<ListVerboseObject> | String | Null
Lists verbose distributions
Kind: global function
Returns: Promise | Array.<ListVerboseObject> | String | Null - returns an array of distribution objects, a string of the raw output, or null
Throws:
- TypeError throws if raw is not a boolean
| Param | Type | Default | Description | | --- | --- | --- | --- | | [raw] | Boolean | false | option to return a string of the raw output |
Example
const wsl = require('@endevr-io/wsl-api')
const listVerbose = await wsl.listVerbose()
console.log(listVerbose)
// [
// { default: true, name: 'Ubuntu', state: 'Stopped', version: 2 },
// { default: false, name: 'Debian', state: 'Stopped', version: 2 }
// ]
mount(path, [name], [bare], [type], [partition], [options]) ⇒ Promise
Mounts a disk to all distributions
Kind: global function
Returns: Promise - resolves promise from execPromise
Throws:
- TypeError throws if path is not a string, name is not string or null, bare is not boolean, type is not string or null, partition is not number or null, or options is not a string or null
- Error throws if disk could not be mounted
| Param | Type | Default | Description | | --- | --- | --- | --- | | path | String | | disk path to mount | | [name] | String | Null | | option to mount with name | | [bare] | Boolean | false | option to attach the drive but not mount it | | [type] | String | Null | | option to specify the filesystem type | | [partition] | Number | Null | | option to specify the partition ID | | [options] | String | Null | | option with the mounting options, they are already encased with "" |
Example
const wsl = require('@endevr-io/wsl-api')
await wsl.mount('\\\\.\\PHYSICALDRIVE3', 'F', false, null, null, 'data=ordered')
runDistribution([distribution], [user])
Runs a distribution with an optional user. The distribution will be attached to this process. Do not await a response
Kind: global function
Throws:
- TypeError throws if distribution is not a string
- TypeError throws if user is not a string
- Error throws if the distribution could not be run
| Param | Type | Default | Description | | --- | --- | --- | --- | | [distribution] | String | Null | | distribution to run, or the default if not specified | | [user] | String | Null | | the user to run as |
Example
const wsl = require('@endevr-io/wsl-api')
wsl.runDistribution('Ubuntu', 'endevr')
runDistributionDetached([distribution], [user])
Runs a detached distribution with an optional user. The distribution will be detached from this process in it's own window
Kind: global function
Throws:
- TypeError throws if distribution is not a string
- TypeError throws if user is not a string
- Error throws if the distribution could not be run detached
| Param | Type | Default | Description | | --- | --- | --- | --- | | [distribution] | String | Null | | distribution to run, or the default if not specified | | [user] | String | Null | | the user to run as |
Example
const wsl = require('@endevr-io/wsl-api')
wsl.runDistributionDetached('Ubuntu', 'endevr')
setDefaultDistribution(distribution) ⇒ Promise
Sets the default distribution
Kind: global function
Returns: Promise - resolves promise from execPromise
Throws:
- TypeError throws if distribution is not a string
- Error throws if default distribution could not be set
| Param | Type | Description | | --- | --- | --- | | distribution | String | distribution to set |
Example
const wsl = require('@endevr-io/wsl-api')
await wsl.setDefaultDistribution('Ubuntu')
setDefaultVersion(version) ⇒ Promise
Sets the default WSL version
Kind: global function
Returns: Promise - resolves promise from execPromise
Throws:
- TypeError throws if version is not a number
- Error throws if version is not 1 or 2
- Error throws if default version could not be set
| Param | Type | Description | | --- | --- | --- | | version | Number | version to set |
Example
const wsl = require('@endevr-io/wsl-api')
await wsl.setDefaultVersion(2)
setVersion(distribution, version) ⇒ Promise
Sets the WSL version of a distribution
Kind: global function
Returns: Promise - resolves promise from execPromise
Throws:
- TypeError throws if distribution is not a string
- TypeError throws if version is not a number
- Error throws if version is not 1 or 2
- Error throws if distribution version could not be set
| Param | Type | Description | | --- | --- | --- | | distribution | String | distribution to set | | version | Number | version to set |
Example
const wsl = require('@endevr-io/wsl-api')
await wsl.setVersion('Ubuntu', 2)
shutdown() ⇒ Promise
Shutdown all distributions
Kind: global function
Returns: Promise - resolves promise from execPromise
Throws:
- Error throws if distributions could not be shutdown
Example
const wsl = require('@endevr-io/wsl-api')
await wsl.shutdown()
status([raw]) ⇒ Promise | StatusObject | String | Null
Lists the status of WSL
Kind: global function
Returns: Promise | StatusObject | String | Null - returns a status object, a string of the raw output, or null
Throws:
- TypeError throws if raw is not a boolean
| Param | Type | Default | Description | | --- | --- | --- | --- | | [raw] | Boolean | false | option to return a string of the raw output |
Example
const wsl = require('@endevr-io/wsl-api')
const status = await wsl.status()
console.log(status)
// {
// defaultDistribution: 'Ubuntu',
// defaultVersion: 2,
// lastUpdated: 2021-12-01T06:00:00.000Z,
// automaticUpdates: true,
// kernelVersion: '5.10.60.1'
// }
terminate(distribution) ⇒ Promise
Terminates or stops specified distribution
Kind: global function
Returns: Promise - resolves promise from execPromise
Throws:
- TypeError throws if distribution is not a string
- Error throws if distribution could not be terminated
| Param | Type | Description | | --- | --- | --- | | distribution | String | distribution to terminate |
Example
const wsl = require('@endevr-io/wsl-api')
await wsl.terminate('Ubuntu')
unmount(path) ⇒ Promise
Unmounts a disk to all distributions
Kind: global function
Returns: Promise - resolves promise from execPromise
Throws:
- TypeError throws if path is not a string
- Error throws if disk could not be unmounted
| Param | Type | Description | | --- | --- | --- | | path | String | disk path to unmount |
Example
const wsl = require('@endevr-io/wsl-api')
await wsl.unmount('\\\\.\\PHYSICALDRIVE3')
unregister(distribution) ⇒ Promise
Unregisters or uninstalls specified distribution
Kind: global function
Returns: Promise - resolves promise from execPromise
Throws:
- TypeError throws if distribution is not a string
- Error throws if distribution could not be unregistered
| Param | Type | Description | | --- | --- | --- | | distribution | String | distribution to unregister |
Example
const wsl = require('@endevr-io/wsl-api')
await wsl.unregister('Ubuntu')
update([rollback]) ⇒ Promise
Update the WSL kernel
Kind: global function
Returns: Promise - resolves promise from execPromise
Throws:
- TypeError throws if rollback is not a boolean
- Error throws if unable to update WSL
| Param | Type | Default | Description | | --- | --- | --- | --- | | [rollback] | Boolean | false | option to rollback to previous version |
Example
const wsl = require('@endevr-io/wsl-api')
await wsl.update()
isSupported() ⇒ Boolean
Checks if WSL is supported in the OS, must be Windows 10 build >= 19041 or Windows 11
Kind: global function
Returns: Boolean - returns truthy of is WSL supported or false on error
Example
const wsl = require('@endevr-io/wsl-api')
const isSupported = wsl.isSupported()
console.log(isSupported)
// true
~~runBashCommand(command, [longProcess], [stdoutCallback], [stderrCallback], [errorCallback], [closeCallback]) ⇒ Promise | String~~
Deprecated
Runs a bash command on the default distribution
Kind: global function
Returns: Promise | String - returns promise from spawnBashPromise if longProcess is true or string from execPromise if longProcess is false
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| command | String | | command to run, already is wrapped with "" |
| [longProcess] | Boolean | false | option if to run a process with a single response or a long running process with callbacks for the response |
| [stdoutCallback] | function | Boolean | false | optional callback for the stdout event, longProcess
must be true, returns a buffer |
| [stderrCallback] | function | Boolean | false | optional callback for the stderr event, longProcess
must be true, returns a buffer |
| [errorCallback] | function | Boolean | false | optional callback for the error event, longProcess
must be true, returns a buffer |
| [closeCallback] | function | Boolean | false | optional callback for the close event, longProcess
must be true, returns a close code |
Example
const wsl = require('@endevr-io/wsl-api')
const ls = await wsl.runBashCommand('ls')
console.log(ls)
function callback (data) {
console.log(data.toString())
}
await wsl.runBashCommand('apt update', true, callback)
~~runBashFile(path, [longProcess], [stdoutCallback], [stderrCallback], [errorCallback], [closeCallback]) ⇒ Promise | String~~
Deprecated
Runs a bash file on the default distribution
:warning: Make sure that the file uses Linux line endings (LF) and NOT Windows line endings (CRLF)
Kind: global function
Returns: Promise | String - returns promise from spawnBashPromise if longProcess is true or string from execPromise if longProcess is false
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| path | String | | path to the file in WSL/Linux format, ie /mnt/c/Path/To/File
or /home/user/path/to/file
|
| [longProcess] | Boolean | false | option if to run a process with a single response or a long running process with callbacks for the response |
| [stdoutCallback] | function | Boolean | false | optional callback for the stdout event, returns a buffer |
| [stderrCallback] | function | Boolean | false | optional callback for the stderr event, returns a buffer |
| [errorCallback] | function | Boolean | false | optional callback for the error event, returns a buffer |
| [closeCallback] | function | Boolean | false | optional callback for the close event, returns a close code |
Example
const wsl = require('@endevr-io/wsl-api')
const short = await wsl.runBashFile('/mnt/c/shortprocess.sh')
console.log(short)
function callback (data) {
console.log(data.toString())
}
await wsl.runBashFile('/mnt/c/longprocess.sh', true, callback)
runWSLCommand(command, [distribution], [user], [longProcess], [stdoutCallback], [stderrCallback], [errorCallback], [closeCallback]) ⇒ Promise | String
Runs a command on the default or specified distribution. Must be a simple single command. Use runBashCommand
for multiple or complex commands or run a bash file with runWSLFile
or runBashFile
Kind: global function
Returns: Promise | String - returns promise from spawnBashPromise if longProcess is true or string from execPromise if longProcess is false
| Param | Type | Default | Description | | --- | --- | --- | --- | | command | String | | command to run, already is wrapped with "" | | [distribution] | String | Null | | option to specify the distribution or use the default distribution | | [user] | String | Null | | option to specify the user or use the default user | | [longProcess] | Boolean | false | option if to run a process with a single response or a long running process with callbacks for the response | | [stdoutCallback] | function | Boolean | false | optional callback for the stdout event, returns a buffer | | [stderrCallback] | function | Boolean | false | optional callback for the stderr event, returns a buffer | | [errorCallback] | function | Boolean | false | optional callback for the error event, returns a buffer | | [closeCallback] | function | Boolean | false | optional callback for the close event, returns a close code |
Example
const wsl = require('@endevr-io/wsl-api')
const ls = await wsl.runWSLCommand('ls', 'Ubuntu', 'endevr')
console.log(ls)
function callback (data) {
console.log(data.toString())
}
await wsl.runWSLCommand('apt update', 'Ubuntu', 'endevr', true, callback)
runWSLFile(path, [distribution], [user], [longProcess], [stdoutCallback], [stderrCallback], [errorCallback], [closeCallback]) ⇒ Promise | String
Runs a bash file on the default or specified istribution
:warning: Make sure that the file uses Linux line endings (LF) and NOT Windows line endings (CRLF)
Kind: global function
Returns: Promise | String - returns promise from spawnBashPromise if longProcess is true or string from execPromise if longProcess is false
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| path | String | | path to the file in WSL/Linux format, ie /mnt/c/Path/To/File
or /home/user/path/to/file
|
| [distribution] | String | Null | | option to specify the distribution or use the default distribution |
| [user] | String | Null | | option to specify the user or use the default user |
| [longProcess] | Boolean | false | option if to run a process with a single response or a long running process with callbacks for the response |
| [stdoutCallback] | function | Boolean | false | optional callback for the stdout event, returns a buffer |
| [stderrCallback] | function | Boolean | false | optional callback for the stderr event, returns a buffer |
| [errorCallback] | function | Boolean | false | optional callback for the error event, returns a buffer |
| [closeCallback] | function | Boolean | false | optional callback for the close event, returns a close code |
Example
const wsl = require('@endevr-io/wsl-api')
const short = await wsl.runWSLFile('/mnt/c/shortprocess.sh', 'Ubuntu', 'endevr')
console.log(short)
function callback (data) {
console.log(data.toString())
}
await wsl.runWSLFile('/mnt/c/longprocess.sh', 'Ubuntu', 'endevr', true, callback)
isInstalled(distribution) ⇒ Boolean
Checks if distribution is installed or exists
Kind: global function
Returns: Boolean - returns truthy of if the distribution exists
Throws:
- TypeError throws if distribution is not a string
| Param | Type | Description | | --- | --- | --- | | distribution | String | distribution to check for installed |
Example
const wsl = require('@endevr-io/wsl-api')
const isInstalled = await wsl.isInstalled('Ubuntu')
console.log(isInstalled)
// true
isRunning(distribution) ⇒ Boolean
Checks if distribution is currently running
Kind: global function
Returns: Boolean - returns truthy of if the distribution is running
Throws:
- TypeError throws if distribution is not a string
| Param | Type | Description | | --- | --- | --- | | distribution | String | distribution to check for running |
Example
const wsl = require('@endevr-io/wsl-api')
const isRunning = await wsl.isRunning('Ubuntu')
console.log(isRunning)
// false
isNotRunning(distribution) ⇒ Boolean
Checks if distribution is currently not running
Kind: global function
Returns: Boolean - returns truthy of if the distribution is not running
Throws:
- TypeError throws if distribution is not a string
| Param | Type | Description | | --- | --- | --- | | distribution | String | distribution to check for not running |
Example
const wsl = require('@endevr-io/wsl-api')
const isNotRunning = await wsl.isNotRunning('Ubuntu')
console.log(isNotRunning)
// true
isAnyRunning() ⇒ Boolean
Checks if any distribution is currently running, can also use const isAnyRunning = wsl.listRunning().length >= 1
Kind: global function
Returns: Boolean - returns truthy of if any distribution is running
Example
const wsl = require('@endevr-io/wsl-api')
const isAnyRunning = await wsl.isAnyRunning('Ubuntu')
console.log(isAnyRunning)
// true
isInstalling(distribution) ⇒ Boolean
Checks if distribution is currently installing
Kind: global function
Returns: Boolean - returns truthy of if the distribution is installing
Throws:
- TypeError throws if distribution is not a string
| Param | Type | Description | | --- | --- | --- | | distribution | String | distribution to check for installing |
Example
const wsl = require('@endevr-io/wsl-api')
const isInstalling = await wsl.isInstalling('Ubuntu')
console.log(isInstalling)
// false
waitForRunning(distribution, [poll]) ⇒ Promise
Checks every poll interval and returns a promise when the distribution is running
Kind: global function
Returns: Promise - returns when the distribution is running
Throws:
- TypeError throws if distribution is not a string or poll is not a number
| Param | Type | Default | Description | | --- | --- | --- | --- | | distribution | String | | distribution to wait for running | | [poll] | Number | 1000 | option poll interval |
Example
const wsl = require('@endevr-io/wsl-api')
wsl.runDistribution('Ubuntu', 'endevr')
await wsl.waitForRunning('Ubuntu')
console.log('Ubuntu has started')
// Ubuntu has started
waitForNotRunning(distribution, [poll]) ⇒ Promise
Checks every poll interval and returns a promise when the distribution is not running
Kind: global function
Returns: Promise - returns when the distribution is not running
Throws:
- TypeError throws if distribution is not a string or poll is not a number
| Param | Type | Default | Description | | --- | --- | --- | --- | | distribution | String | | distribution to wait for not running | | [poll] | Number | 1000 | option poll interval |
Example
const wsl = require('@endevr-io/wsl-api')
await wsl.terminate('Ubuntu')
await wsl.waitForNotRunning('Ubuntu')
console.log('Ubuntu has stopped')
// Ubuntu has stopped
waitForAllNotRunning([poll]) ⇒ Promise
Checks every poll interval and returns a promise when all distributions are not running
Kind: global function
Returns: Promise - returns when the distribution is not running
Throws:
- TypeError throws if distribution is not a string or poll is not a number
| Param | Type | Default | Description | | --- | --- | --- | --- | | [poll] | Number | 1000 | option poll interval |
Example
const wsl = require('@endevr-io/wsl-api')
wsl.runDistribution('Ubuntu', 'endevr')
await wsl.terminate('Ubuntu')
await wsl.waitForAllNotRunning()
console.log('All distributions have stopped')
// All distributions have stopped
waitForInstalling(distribution, [poll]) ⇒ Promise
Checks every poll interval and returns a promise when the distribution is installing
Kind: global function
Returns: Promise - returns when the distribution is installing
Throws:
- TypeError throws if distribution is not a string or poll is not a number
| Param | Type | Default | Description | | --- | --- | --- | --- | | distribution | String | | distribution to wait for installing | | [poll] | Number | 1000 | option poll interval |
Example
const wsl = require('@endevr-io/wsl-api')
await wsl.installDistribution('Ubuntu')
await wsl.waitForInstalling('Ubuntu')
console.log('Ubuntu is installing')
// Ubuntu is installing
ListObject : Object
Kind: global typedef
Properties
| Name | Type | | --- | --- | | name | String | | default | Boolean |
ListOnlineObject : Object
Kind: global typedef
Properties
| Name | Type | | --- | --- | | name | String | | friendly | String |
ListVerboseObject : Object
Kind: global typedef
Properties
| Name | Type | | --- | --- | | default | Boolean | | name | String | | state | String | | version | Number |
StatusObject : Object
Kind: global typedef
Properties
| Name | Type | | --- | --- | | defaultDistribution | String | | defaultVersion | Number | | lastUpdated | Date | | automaticUpdates | Boolean | | kernelVersion | String |
Contributing
Pull requests are welcome for bug fixes or feature requests.
Sponsors
Support this project and possibly other open-source projects by becoming a sponsor. Higher tier sponsor will appear here with a logo and link to your website. Become a sponsor