1.0.4 • Published 1 year ago

@endevr-io/wsl-api v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@endevr-io/wsl-api

GitHub release (latest SemVer) GitHub top language npm bundle size (scoped) GitHub Sponsors GitHub

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
ParamTypeDescription
distributionStringdistribution to export
pathStringpath/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
ParamTypeDescription
distributionStringdistribution to import
locationStringfolder path to import to
pathStringpath/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
ParamTypeDescription
distributionStringdistribution 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
ParamTypeDefaultDescription
rawBooleanfalseoption 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
ParamTypeDefaultDescription
rawBooleanfalseoption 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
ParamTypeDefaultDescription
rawBooleanfalseoption 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
ParamTypeDefaultDescription
rawBooleanfalseoption 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
ParamTypeDefaultDescription
rawBooleanfalseoption 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
ParamTypeDefaultDescription
rawBooleanfalseoption 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
ParamTypeDefaultDescription
pathStringdisk path to mount
nameString | Nulloption to mount with name
bareBooleanfalseoption to attach the drive but not mount it
typeString | Nulloption to specify the filesystem type
partitionNumber | Nulloption to specify the partition ID
optionsString | Nulloption 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
ParamTypeDefaultDescription
distributionString | Nulldistribution to run, or the default if not specified
userString | Nullthe 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
ParamTypeDefaultDescription
distributionString | Nulldistribution to run, or the default if not specified
userString | Nullthe 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
ParamTypeDescription
distributionStringdistribution 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
ParamTypeDescription
versionNumberversion 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
ParamTypeDescription
distributionStringdistribution to set
versionNumberversion 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
ParamTypeDefaultDescription
rawBooleanfalseoption 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
ParamTypeDescription
distributionStringdistribution 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
ParamTypeDescription
pathStringdisk 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
ParamTypeDescription
distributionStringdistribution 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
ParamTypeDefaultDescription
rollbackBooleanfalseoption 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

ParamTypeDefaultDescription
commandStringcommand to run, already is wrapped with ""
longProcessBooleanfalseoption if to run a process with a single response or a long running process with callbacks for the response
stdoutCallbackfunction | Booleanfalseoptional callback for the stdout event, longProcess must be true, returns a buffer
stderrCallbackfunction | Booleanfalseoptional callback for the stderr event, longProcess must be true, returns a buffer
errorCallbackfunction | Booleanfalseoptional callback for the error event, longProcess must be true, returns a buffer
closeCallbackfunction | Booleanfalseoptional 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

ParamTypeDefaultDescription
pathStringpath to the file in WSL/Linux format, ie /mnt/c/Path/To/File or /home/user/path/to/file
longProcessBooleanfalseoption if to run a process with a single response or a long running process with callbacks for the response
stdoutCallbackfunction | Booleanfalseoptional callback for the stdout event, returns a buffer
stderrCallbackfunction | Booleanfalseoptional callback for the stderr event, returns a buffer
errorCallbackfunction | Booleanfalseoptional callback for the error event, returns a buffer
closeCallbackfunction | Booleanfalseoptional 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

ParamTypeDefaultDescription
commandStringcommand to run, already is wrapped with ""
distributionString | Nulloption to specify the distribution or use the default distribution
userString | Nulloption to specify the user or use the default user
longProcessBooleanfalseoption if to run a process with a single response or a long running process with callbacks for the response
stdoutCallbackfunction | Booleanfalseoptional callback for the stdout event, returns a buffer
stderrCallbackfunction | Booleanfalseoptional callback for the stderr event, returns a buffer
errorCallbackfunction | Booleanfalseoptional callback for the error event, returns a buffer
closeCallbackfunction | Booleanfalseoptional 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

ParamTypeDefaultDescription
pathStringpath to the file in WSL/Linux format, ie /mnt/c/Path/To/File or /home/user/path/to/file
distributionString | Nulloption to specify the distribution or use the default distribution
userString | Nulloption to specify the user or use the default user
longProcessBooleanfalseoption if to run a process with a single response or a long running process with callbacks for the response
stdoutCallbackfunction | Booleanfalseoptional callback for the stdout event, returns a buffer
stderrCallbackfunction | Booleanfalseoptional callback for the stderr event, returns a buffer
errorCallbackfunction | Booleanfalseoptional callback for the error event, returns a buffer
closeCallbackfunction | Booleanfalseoptional 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
ParamTypeDescription
distributionStringdistribution 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
ParamTypeDescription
distributionStringdistribution 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
ParamTypeDescription
distributionStringdistribution 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
ParamTypeDescription
distributionStringdistribution 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
ParamTypeDefaultDescription
distributionStringdistribution to wait for running
pollNumber1000option 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
ParamTypeDefaultDescription
distributionStringdistribution to wait for not running
pollNumber1000option 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
ParamTypeDefaultDescription
pollNumber1000option 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
ParamTypeDefaultDescription
distributionStringdistribution to wait for installing
pollNumber1000option 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

NameType
nameString
defaultBoolean

ListOnlineObject : Object

Kind: global typedef
Properties

NameType
nameString
friendlyString

ListVerboseObject : Object

Kind: global typedef
Properties

NameType
defaultBoolean
nameString
stateString
versionNumber

StatusObject : Object

Kind: global typedef
Properties

NameType
defaultDistributionString
defaultVersionNumber
lastUpdatedDate
automaticUpdatesBoolean
kernelVersionString

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

License

MIT

1.0.4

1 year ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago