From 287ed0b1e3d64ff9a286683779ee2c9369b76d81 Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Fri, 13 Aug 2021 23:47:13 +0200 Subject: [PATCH] Separate test files and example files --- README.md | 2 +- examples/basics.mjs | 42 ----------------------------- examples/markdown.md | 4 +-- test.mjs | 4 +-- {examples => tests}/interactive.mjs | 0 {examples => tests}/no-extension | 0 {examples => tests}/typescript.ts | 2 +- zx.mjs | 15 +++++------ 8 files changed, 13 insertions(+), 56 deletions(-) delete mode 100755 examples/basics.mjs rename {examples => tests}/interactive.mjs (100%) rename {examples => tests}/no-extension (100%) rename {examples => tests}/typescript.ts (93%) diff --git a/README.md b/README.md index de0e7db7d1..e08d5ad0e9 100644 --- a/README.md +++ b/README.md @@ -354,7 +354,7 @@ zx examples/markdown.md The `zx` can compile `.ts` scripts to `.mjs` and execute them. ```bash -zx examples/typescript.ts +zx script.ts ``` In TypeScript file include the `zx` package to import types: diff --git a/examples/basics.mjs b/examples/basics.mjs deleted file mode 100755 index 368762b402..0000000000 --- a/examples/basics.mjs +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env zx - -// Copyright 2021 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -await $`ls -1 | wc -l` - -let branch = await $`git branch --show-current` -await $`echo ${branch} | wc` // The new line trimmed from stdout. - -let foo = `hi; echo 'oops'` -await $`echo ${foo} | wc` // Vars properly quoted. - -let path = await import('path') // We can use import, -let {name} = require(path.join(__dirname, '..', 'package.json')) // and require. - -console.log(chalk.black.bgCyanBright(name)) // The chalk is available without import. - -try { - await $`exit 42` // ProcessOutput will be thrown on non-zero exit codes. -} catch (p) { - console.log(p.exitCode) -} - -let {exitCode} = await nothrow($`exit 42`) // Change behavior to no-throw. -exitCode = await $`exit 42`.exitCode // Or wait on exitCode itself. - -// Use combinations of pipe() and nothrow(): -await $`find ./examples -type f -print0` - .pipe(nothrow($`xargs -0 grep ${'missing' + 'part'}`)) - .pipe($`wc -l`) diff --git a/examples/markdown.md b/examples/markdown.md index 5ee6b66cb1..c9ddb26174 100755 --- a/examples/markdown.md +++ b/examples/markdown.md @@ -5,7 +5,7 @@ by zx. Try to run `zx examples/markdown.md`. ```js await $`whoami` -await $`ls -la ${__dirname}` +await $`echo ${__dirname}` ``` The `__filename` will be pointed to **markdown.md**: @@ -17,7 +17,7 @@ console.log(chalk.yellowBright(__filename)) We can use imports here as well: ```js -await import('./basics.mjs') +await import('chalk') ``` A bash code (with `bash` or `sh` language tags) also will be executed: diff --git a/test.mjs b/test.mjs index a543643be5..1f03bdd933 100755 --- a/test.mjs +++ b/test.mjs @@ -82,7 +82,7 @@ import path from 'path' } { // Scripts with no extension are working - await $`node zx.mjs examples/no-extension` + await $`node zx.mjs tests/no-extension` } { // require() is working from stdin @@ -94,7 +94,7 @@ import path from 'path' } { // TypeScript scripts are working - await $`node zx.mjs examples/typescript.ts` + await $`node zx.mjs tests/typescript.ts` } { // Quiet mode is working diff --git a/examples/interactive.mjs b/tests/interactive.mjs similarity index 100% rename from examples/interactive.mjs rename to tests/interactive.mjs diff --git a/examples/no-extension b/tests/no-extension similarity index 100% rename from examples/no-extension rename to tests/no-extension diff --git a/examples/typescript.ts b/tests/typescript.ts similarity index 93% rename from examples/typescript.ts rename to tests/typescript.ts index 7a22407cae..7a4f2a2967 100755 --- a/examples/typescript.ts +++ b/tests/typescript.ts @@ -14,7 +14,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {ProcessOutput, ProcessPromise} from '..' +import {ProcessOutput, ProcessPromise} from '../index' void async function () { let p: ProcessPromise = $`cat` diff --git a/zx.mjs b/zx.mjs index 84729790d2..c94711e8b8 100755 --- a/zx.mjs +++ b/zx.mjs @@ -198,14 +198,13 @@ async function compile(input) { let tsc = $`npm_config_yes=true npx -p typescript tsc --target esnext --lib esnext --module esnext --moduleResolution node ${input}` $.verbose = v - let i = 0, color = [chalk.magentaBright, chalk.cyanBright, chalk.yellowBright, - chalk.greenBright, chalk.blueBright][new Date().getMinutes() % 5], - interval = setInterval(() => { - process.stdout.write(' ' - + color(['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'][i++ % 10]) - + '\r' - ) - }, 100) + let i = 0 + let interval = setInterval(() => { + process.stdout.write(' ' + + ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'][i++ % 10] + + '\r' + ) + }, 100) try { await tsc