Skip to content
This repository was archived by the owner on Jun 3, 2021. It is now read-only.

Files

Latest commit

 

History

History
53 lines (35 loc) · 1.93 KB

context-system.md

File metadata and controls

53 lines (35 loc) · 1.93 KB

Provides access to shell and OS processes.

You can access these tools on the @weex-cli/core, via const { system } = require('@weex-cli/core').

run

This is an async function.

Runs a shell command and returns the output as a string.

The first parameter commandLine is the shell command to run. It can have pipes! The second parameter is options, object. This is a promise wrapped around node.js child-process.exec api call.

You can also pass trim: true inside the options parameter to have the output automatically trim all starting and trailing spaces.

Should the process fail, an error will be thrown with properties such as:

property type purpose
code number the exit code
cmd string the command we asked to run
stderr string any information the process wrote to stderr
killed bool if the process was killed or not
signal number the signal number used to off the process (if killed)
const nodeVersion = context.system.run('node -v', { trim: true })

which

Returns the full path to a command on your system if located on your path.

const whereIsIt = context.system.which('npm')

startTimer

Returns a function. When this is called, the number of milliseconds will be returned.

const timer = context.system.startTimer()

// time passes...
console.log(`that just took ${timer()} ms.`)

Caveat: Due to the event loop scheduler in Node.JS, they don't guarantee millisecond accuracy when invoking async functions. For that reason, expect a up to a 4ms overage.

Note that this lag doesn't apply to synchronous code.