Commit 33aaae4
Changed files (6)
tests/typescript.ts
@@ -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))
}()
index.d.ts
@@ -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
-}
index.mjs
@@ -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()
@@ -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,
-})
README.md
@@ -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`
test.mjs
@@ -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
zx.mjs
@@ -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) {
@@ -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)
@@ -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')