Skip to content

Commit

Permalink
Add v7 folder
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Mar 18, 2024
1 parent 52a123b commit ed09d11
Show file tree
Hide file tree
Showing 11 changed files with 1,028 additions and 24 deletions.
73 changes: 49 additions & 24 deletions .vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,60 @@ export default defineConfig({
{text: 'Home', link: '/'},
{text: 'Docs', link: '/getting-started'},
{
text: '7.x',
text: 'v8',
items: [
{text: 'Releases', link: 'https://github.com/google/zx/releases'},
{text: 'v7', link: '/v7/api'},
],
},
],

sidebar: [
{
text: 'Docs',
items: [
{text: 'Getting Started', link: '/getting-started'},
{text: 'Process Promise', link: '/process-promise'},
{text: 'API Reference', link: '/api'},
{text: 'Configuration', link: '/configuration'},
{text: 'CLI Usage', link: '/cli'},
],
},
{
text: 'FAQ',
link: '/faq',
items: [
{text: 'Quotes', link: '/quotes'},
{text: 'TypeScript', link: '/typescript'},
{text: 'Markdown Scripts', link: '/markdown-scripts'},
{text: 'Known Issues', link: '/known-issues'},
],
},
],
sidebar: {
'/': [
{
text: 'Docs',
items: [
{text: 'Getting Started', link: '/getting-started'},
{text: 'Process Promise', link: '/process-promise'},
{text: 'API Reference', link: '/api'},
{text: 'Configuration', link: '/configuration'},
{text: 'CLI Usage', link: '/cli'},
],
},
{
text: 'FAQ',
link: '/faq',
items: [
{text: 'Quotes', link: '/quotes'},
{text: 'TypeScript', link: '/typescript'},
{text: 'Markdown Scripts', link: '/markdown-scripts'},
{text: 'Known Issues', link: '/known-issues'},
],
},
],

'/v7/': [
{
text: 'Docs (v7)',
items: [
{text: 'Getting Started', link: '/v7/getting-started'},
{text: 'Process Promise', link: '/v7/process-promise'},
{text: 'API Reference', link: '/v7/api'},
{text: 'Configuration', link: '/v7/configuration'},
{text: 'CLI Usage', link: '/v7/cli'},
],
},
{
text: 'FAQ',
link: '/v7/faq',
items: [
{text: 'Quotes', link: '/v7/quotes'},
{text: 'TypeScript', link: '/v7/typescript'},
{text: 'Markdown Scripts', link: '/v7/markdown-scripts'},
{text: 'Known Issues', link: '/v7/known-issues'},
],
},
],
},

socialLinks: [
{icon: 'github', link: 'https://github.com/google/zx'},
Expand Down
206 changes: 206 additions & 0 deletions v7/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
::: warning
This is documentation for zx v7, which is no longer actively maintained.

For up-to-date documentation, see the [latest version](/api) (v8).
:::

# API Reference

## cd()

Changes the current working directory.

```js
cd('/tmp')
await $`pwd` // => /tmp
```

Like `echo`, in addition to `string` arguments, `cd` accepts and trims
trailing newlines from `ProcessOutput` enabling common idioms like:

```js
cd(await $`mktemp -d`)
```

## fetch()

A wrapper around the [node-fetch](https://www.npmjs.com/package/node-fetch)
package.

```js
const resp = await fetch('https://medv.io')
```

## question()

A wrapper around the [readline](https://nodejs.org/api/readline.html) package.

```js
const bear = await question('What kind of bear is best? ')
```

## sleep()

A wrapper around the `setTimeout` function.

```js
await sleep(1000)
```

## echo()

A `console.log()` alternative which can take [ProcessOutput](#processoutput).

```js
const branch = await $`git branch --show-current`

echo`Current branch is ${branch}.`
// or
echo('Current branch is', branch)
```

## stdin()

Returns the stdin as a string.

```js
const content = JSON.parse(await stdin())
```

## within()

Creates a new async context.

```js
await $`pwd` // => /home/path

within(async () => {
cd('/tmp')

setTimeout(async () => {
await $`pwd` // => /tmp
}, 1000)
})

await $`pwd` // => /home/path
```

```js
await $`node --version` // => v20.2.0

const version = await within(async () => {
$.prefix += 'export NVM_DIR=$HOME/.nvm; source $NVM_DIR/nvm.sh; nvm use 16;'

return $`node --version`
})

echo(version) // => v16.20.0
```

## retry()

Retries a callback for a few times. Will return after the first
successful attempt, or will throw after specifies attempts count.

```js
const p = await retry(10, () => $`curl https://medv.io`)

// With a specified delay between attempts.
const p = await retry(20, '1s', () => $`curl https://medv.io`)

// With an exponential backoff.
const p = await retry(30, expBackoff(), () => $`curl https://medv.io`)
```

## spinner()

Starts a simple CLI spinner.

```js
await spinner(() => $`long-running command`)

// With a message.
await spinner('working...', () => $`sleep 99`)
```

## glob()

The [globby](https://github.com/sindresorhus/globby) package.

```js
const packages = await glob(['package.json', 'packages/*/package.json'])
```

## which()

The [which](https://github.com/npm/node-which) package.

```js
const node = await which('node')
```

## argv

The [minimist](https://www.npmjs.com/package/minimist) package.

A minimist-parsed version of the `process.argv` as `argv`.

```js
if (argv.someFlag) {
echo('yes')
}
```

Use minimist options to customize the parsing:

```js
const myCustomArgv = minimist(process.argv.slice(2), {
boolean: [
'force',
'help',
],
alias: {
h: 'help',
},
})
```

## chalk

The [chalk](https://www.npmjs.com/package/chalk) package.

```js
console.log(chalk.blue('Hello world!'))
```

## fs

The [fs-extra](https://www.npmjs.com/package/fs-extra) package.

```js
const {version} = await fs.readJson('./package.json')
```

## os

The [os](https://nodejs.org/api/os.html) package.

```js
await $`cd ${os.homedir()} && mkdir example`
```

## path

The [path](https://nodejs.org/api/path.html) package.

```js
await $`mkdir ${path.join(basedir, 'output')}`
```

## yaml

The [yaml](https://www.npmjs.com/package/yaml) package.

```js
console.log(YAML.parse('foo: bar').foo)
```
91 changes: 91 additions & 0 deletions v7/cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
::: warning
This is documentation for zx v7, which is no longer actively maintained.

For up-to-date documentation, see the [latest version](/api) (v8).
:::

# CLI Usage

Zx provides a CLI for running scripts. It is installed with the package and can be used as `zx` executable.

```sh
zx script.mjs
```

## No extensions

If script does not have a file extension (like `.git/hooks/pre-commit`), zx
assumes that it is
an [ESM](https://nodejs.org/api/modules.html#modules_module_createrequire_filename)
module.

```bash
zx docs/markdown.md
```

## Remote scripts

If the argument to the `zx` executable starts with `https://`, the file will be
downloaded and executed.

```bash
zx https://medv.io/game-of-life.js
```

## Scripts from stdin

The `zx` supports executing scripts from stdin.

```js
zx << 'EOF'
await $`pwd`
EOF
```

## --eval

Evaluate the following argument as a script.

```bash
cat package.json | zx --eval 'const v = JSON.parse(await stdin()).version; echo(v)'
```

## --install

```js
// script.mjs:
import sh from 'tinysh'

sh.say('Hello, world!')
```

Add `--install` flag to the `zx` command to install missing dependencies
automatically.

```bash
zx --install script.mjs
```

You can also specify needed version by adding comment with `@` after
the import.

```js
import sh from 'tinysh' // @^1
```

## __filename & __dirname

In [ESM](https://nodejs.org/api/esm.html) modules, Node.js does not provide
`__filename` and `__dirname` globals. As such globals are really handy in scripts,
zx provides these for use in `.mjs` files (when using the `zx` executable).

## require()

In [ESM](https://nodejs.org/api/modules.html#modules_module_createrequire_filename)
modules, the `require()` function is not defined.
The `zx` provides `require()` function, so it can be used with imports in `.mjs`
files (when using `zx` executable).

```js
const {version} = require('./package.json')
```

0 comments on commit ed09d11

Please sign in to comment.