Skip to content

Commit

Permalink
Register globals only in zx binary
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Aug 27, 2021
1 parent 303feea commit 33aaae4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 53 deletions.
15 changes: 1 addition & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,22 +370,9 @@ The `zx` can compile `.ts` scripts to `.mjs` and execute them.
zx script.ts
```
In TypeScript file include the `zx` package to import types:
```ts
import 'zx'
```
Or reference the `zx` package via:
```js
/// <reference types="zx"/>
```
Example:
```ts
#!/usr/bin/env zx
import 'zx'
import {$} from 'zx'

void async function () {
await $`ls -la`
Expand Down
16 changes: 0 additions & 16 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,3 @@ export const nothrow: nothrow
export const os: typeof _os
export const question: question
export const sleep: sleep

declare global {
var $: $
var argv: ParsedArgs
var cd: cd
var chalk: typeof _chalk
// @ts-ignore
var fetch: typeof _fetch
var fs: typeof _fs
var globby: typeof _globby.globby & typeof _globby
var glob: typeof _globby.globby & typeof _globby
var nothrow: nothrow
var os: typeof _os
var question: question
var sleep: sleep
}
32 changes: 17 additions & 15 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ export const globby = Object.assign(function globby(...args) {
}, globbyModule)
export const glob = globby

export function registerGlobals() {
Object.assign(global, {
$,
argv,
cd,
chalk,
fetch,
fs,
glob,
globby,
nothrow,
os,
question,
sleep,
})
}

export function $(pieces, ...args) {
let {verbose, cwd, shell, prefix} = $
let __from = (new Error().stack.split(/^\s*at\s/m)[2]).trim()
Expand Down Expand Up @@ -331,18 +348,3 @@ function exitCodeInfo(exitCode) {
159: 'Bad syscall',
}[exitCode]
}

Object.assign(global, {
$,
argv,
cd,
chalk,
fetch,
fs,
glob,
globby,
nothrow,
os,
question,
sleep,
})
3 changes: 2 additions & 1 deletion test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ import path from 'path'
}

{ // TypeScript scripts are working
await $`node zx.mjs tests/typescript.ts`
let {stderr} = await $`node zx.mjs tests/typescript.ts`
assert.match(stderr, /Hello from TypeScript/)
}

{ // Quiet mode is working
Expand Down
6 changes: 2 additions & 4 deletions tests/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {ProcessOutput, ProcessPromise} from '../index'
import {$, ProcessOutput, ProcessPromise, chalk} from '..'

void async function () {
let p: ProcessPromise<ProcessOutput> = $`cat`
p.pipe(process.stderr)

p.stdin.write('Hello, World!\n')
p.stdin.write('Hello from TypeScript!\n')
p.stdin.end()

let out: ProcessOutput = await p
console.log(chalk.red(out.exitCode))
}()
Expand Down
9 changes: 6 additions & 3 deletions zx.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import {tmpdir} from 'os'
import fs from 'fs-extra'
import {createRequire} from 'module'
import url from 'url'
import {$, fetch, ProcessOutput, argv} from './index.mjs'
import {$, fetch, ProcessOutput, argv, registerGlobals} from './index.mjs'

registerGlobals()

try {
if (argv.version || argv.v || argv.V) {
Expand Down Expand Up @@ -115,7 +117,7 @@ async function importPath(filepath, origin = filepath) {
}
if (ext === '.ts') {
let {dir, name} = parse(filepath)
let outFile = join(dir, name + '.mjs')
let outFile = join(dir, name + '.cjs')
await compile(filepath)
await fs.rename(join(dir, name + '.js'), outFile)
let wait = importPath(outFile, filepath)
Expand Down Expand Up @@ -195,13 +197,14 @@ function transformMarkdown(source) {
async function compile(input) {
let v = $.verbose
$.verbose = false
let tsc = $`npm_config_yes=true npx -p typescript tsc --target esnext --lib esnext --module esnext --moduleResolution node ${input}`
let tsc = $`npm_config_yes=true npx -p typescript tsc --target esnext --lib esnext --module commonjs --moduleResolution node ${input}`
$.verbose = v
let i= 0, spinner = setInterval(() => process.stdout.write(` ${'⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'[i++ % 10]}\r`), 100)
try {
await tsc
} catch (err) {
console.error(err.toString())
process.exit(1)
}
clearInterval(spinner)
process.stdout.write(' \r')
Expand Down

0 comments on commit 33aaae4

Please sign in to comment.