Skip to content

Commit

Permalink
feat: set $.verbose to false by default (#745)
Browse files Browse the repository at this point in the history
* feat: set `$.verbose` to false by default

closes #569

* chore: linting

* chore: minor tweak ups
  • Loading branch information
antongolub committed Mar 27, 2024
1 parent def0883 commit cafb90d
Show file tree
Hide file tree
Showing 17 changed files with 102 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,3 +6,4 @@ reports/
.stryker-tmp/
yarn.lock
test/fixtures/ts-project/package-lock.json
test/fixtures/js-project/package-lock.json
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -46,7 +46,8 @@
"build:js": "node scripts/build-js.mjs --format=esm --entry=src/*.ts && npm run build:vendor",
"build:vendor": "node scripts/build-js.mjs --format=esm --entry=src/vendor.ts --bundle=all --banner",
"build:dts": "tsc --project tsconfig.prod.json && node scripts/build-dts.mjs",
"test": "npm run build && node ./test/all.test.js",
"test": "npm run build && npm run test:unit && npm run test:types",
"test:unit": "node ./test/all.test.js",
"test:types": "tsd",
"coverage": "c8 -x build/vendor.js -x 'test/**' -x scripts --check-coverage npm test",
"mutation": "stryker run",
Expand All @@ -61,7 +62,7 @@
"@stryker-mutator/core": "^6.4.2",
"@types/fs-extra": "^11.0.4",
"@types/minimist": "^1.2.5",
"@types/node": ">=20.11.19",
"@types/node": ">=20.11.30",
"@types/which": "^3.0.3",
"@webpod/ps": "^0.0.0-beta.2",
"c8": "^9.1.0",
Expand Down
11 changes: 10 additions & 1 deletion src/cli.ts
Expand Up @@ -53,14 +53,23 @@ function printUsage() {

const argv = minimist(process.argv.slice(2), {
string: ['shell', 'prefix', 'eval'],
boolean: ['version', 'help', 'quiet', 'install', 'repl', 'experimental'],
boolean: [
'version',
'help',
'quiet',
'verbose',
'install',
'repl',
'experimental',
],
alias: { e: 'eval', i: 'install', v: 'version', h: 'help' },
stopEarly: true,
})

await (async function main() {
const globals = './globals.js'
await import(globals)
if (argv.verbose) $.verbose = true
if (argv.quiet) $.verbose = false
if (argv.shell) $.shell = argv.shell
if (argv.prefix) $.prefix = argv.prefix
Expand Down
11 changes: 7 additions & 4 deletions src/core.ts
Expand Up @@ -83,7 +83,7 @@ hook.enable()
export const defaults: Options = {
[processCwd]: process.cwd(),
[syncExec]: false,
verbose: true,
verbose: false,
env: process.env,
sync: false,
shell: true,
Expand Down Expand Up @@ -257,7 +257,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
},
stderr: (data) => {
// Stderr should be printed regardless of piping.
$.log({ kind: 'stderr', data, verbose: self.isVerbose() })
$.log({ kind: 'stderr', data, verbose: !self.isQuiet() })
},
end: ({ error, stdout, stderr, stdall, status, signal }) => {
self._resolved = true
Expand Down Expand Up @@ -425,9 +425,12 @@ export class ProcessPromise extends Promise<ProcessOutput> {
return this
}

isQuiet(): boolean {
return this._quiet ?? this._snapshot.quiet
}

isVerbose(): boolean {
const { verbose, quiet } = this._snapshot
return verbose && !(this._quiet ?? quiet)
return this._snapshot.verbose && !this.isQuiet()
}

timeout(d: Duration, signal = 'SIGTERM'): ProcessPromise {
Expand Down
12 changes: 6 additions & 6 deletions test/cli.test.js
Expand Up @@ -21,7 +21,6 @@ describe('cli', () => {
let promiseResolved = false

beforeEach(() => {
$.verbose = false
process.on('exit', () => {
if (!promiseResolved) {
console.error('Error: ProcessPromise never resolved.')
Expand Down Expand Up @@ -77,29 +76,30 @@ describe('cli', () => {
})

test('supports `--quiet` flag', async () => {
let p = await $`node build/cli.js test/fixtures/markdown.md`
let p = await $`node build/cli.js --quiet test/fixtures/markdown.md`
assert.ok(!p.stderr.includes('ignore'), 'ignore was printed')
assert.ok(p.stderr.includes('hello'), 'no hello')
assert.ok(!p.stderr.includes('hello'), 'no hello')
assert.ok(p.stdout.includes('world'), 'no world')
})

test('supports `--shell` flag ', async () => {
let shell = $.shell
let p =
await $`node build/cli.js --shell=${shell} <<< '$\`echo \${$.shell}\`'`
await $`node build/cli.js --verbose --shell=${shell} <<< '$\`echo \${$.shell}\`'`
assert.ok(p.stderr.includes(shell))
})

test('supports `--prefix` flag ', async () => {
let prefix = 'set -e;'
let p =
await $`node build/cli.js --prefix=${prefix} <<< '$\`echo \${$.prefix}\`'`
await $`node build/cli.js --verbose --prefix=${prefix} <<< '$\`echo \${$.prefix}\`'`
assert.ok(p.stderr.includes(prefix))
})

test('scripts from https', async () => {
$`cat ${path.resolve('test/fixtures/echo.http')} | nc -l 8080`
let out = await $`node build/cli.js http://127.0.0.1:8080/echo.mjs`
let out =
await $`node build/cli.js --verbose http://127.0.0.1:8080/echo.mjs`
assert.match(out.stderr, /test/)
})

Expand Down
6 changes: 0 additions & 6 deletions test/core.test.js
Expand Up @@ -21,10 +21,6 @@ import { ProcessPromise, ProcessOutput } from '../build/index.js'
import '../build/globals.js'

describe('core', () => {
beforeEach(() => {
$.verbose = false
})

test('only stdout is used during command substitution', async () => {
let hello = await $`echo Error >&2; echo Hello`
let len = +(await $`echo ${hello} | wc -c`)
Expand Down Expand Up @@ -340,7 +336,6 @@ describe('core', () => {
resolve()
}

$.verbose = false
assert.equal($.verbose, false)

within(() => {
Expand All @@ -364,7 +359,6 @@ describe('core', () => {
let pwd = await $`pwd`

within(async () => {
$.verbose = false
cd('/tmp')
setTimeout(async () => {
assert.ok((await $`pwd`).stdout.trim().endsWith('/tmp'))
Expand Down
4 changes: 0 additions & 4 deletions test/deps.test.js
Expand Up @@ -18,10 +18,6 @@ import { $ } from '../build/index.js'
import { installDeps, parseDeps } from '../build/deps.js'

describe('deps', () => {
beforeEach(() => {
$.verbose = false
})

test('installDeps() loader works via JS API', async () => {
await installDeps({
cpy: '9.0.1',
Expand Down
6 changes: 1 addition & 5 deletions test/experimental.test.js
Expand Up @@ -15,8 +15,4 @@
import { describe, before } from 'node:test'
import '../build/globals.js'

describe('experimental', () => {
before(() => {
$.verbose = false
})
})
describe('experimental', () => {})
11 changes: 8 additions & 3 deletions test/extra.test.js
Expand Up @@ -13,15 +13,20 @@
// limitations under the License.

import assert from 'node:assert'
import fs from 'node:fs'
import fs from 'node:fs/promises'
import { test, describe } from 'node:test'
import { globby } from 'globby'

describe('extra', () => {
test('every file should have a license', async () => {
const files = await globby(['**/*.{ts,js,mjs}'], { gitignore: true })
const files = await globby(['**/*.{js,mjs,ts}'], {
gitignore: true,
onlyFiles: true,
cwd: process.cwd(),
followSymbolicLinks: false,
})
for (const file of files) {
const content = fs.readFileSync(file).toString()
const content = await fs.readFile(file, 'utf8')
assert(
content
.replace(/\d{4}/g, 'YEAR')
Expand Down
57 changes: 57 additions & 0 deletions test/fixtures/js-project/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/fixtures/js-project/package.json
Expand Up @@ -2,6 +2,6 @@
"private": true,
"type": "module",
"dependencies": {
"zx": "*"
"zx": "file:../../.."
}
}
2 changes: 1 addition & 1 deletion test/fixtures/ts-project/package.json
Expand Up @@ -3,6 +3,6 @@
"type": "module",
"dependencies": {
"typescript": "^5.0.0",
"zx": "*"
"zx": "file:../../.."
}
}
2 changes: 1 addition & 1 deletion test/fixtures/ts-project/script.ts
Expand Up @@ -14,5 +14,5 @@

import { $, ProcessPromise } from 'zx'

let p: ProcessPromise = $`echo ts-script`
let p: ProcessPromise = $({ verbose: true })`echo ts-script`
await p
4 changes: 0 additions & 4 deletions test/goods.test.js
Expand Up @@ -18,10 +18,6 @@ import { test, describe, beforeEach } from 'node:test'
import '../build/globals.js'

describe('goods', () => {
beforeEach(() => {
$.verbose = false
})

function zx(script) {
return $`node build/cli.js --eval ${script}`.nothrow().timeout('5s')
}
Expand Down
9 changes: 2 additions & 7 deletions test/package.test.js
Expand Up @@ -18,7 +18,6 @@ import '../build/globals.js'

describe('package', () => {
beforeEach(async () => {
$.verbose = false
const pack = await $`npm pack`
await $`tar xf ${pack}`
await $`rm ${pack}`.nothrow()
Expand All @@ -29,8 +28,6 @@ describe('package', () => {
const out = await within(async () => {
cd('test/fixtures/ts-project')
await $`npm i`
await $`rm -rf node_modules/zx`
await $`mv ${pack} node_modules/zx`
try {
await $`npx tsc`
} catch (err) {
Expand All @@ -45,10 +42,8 @@ describe('package', () => {
const pack = path.resolve('package')
const out = await within(async () => {
cd('test/fixtures/js-project')
await $`rm -rf node_modules`
await $`mkdir node_modules`
await $`mv ${pack} node_modules/zx`
return $`node node_modules/zx/build/cli.js script.js`
await $`npm i`
return $`node node_modules/zx/build/cli.js --verbose script.js`
})
assert.match(out.stderr, /js-script/)
})
Expand Down
4 changes: 0 additions & 4 deletions test/win32.test.js
Expand Up @@ -19,10 +19,6 @@ import '../build/globals.js'
const _describe = process.platform === 'win32' ? describe : describe.skip

_describe('win32', () => {
beforeEach(() => {
$.verbose = false
})

test('should work with windows-specific commands', async () => {
const p = await $`echo $0` // Bash is first by default.
assert.match(p.stdout, /bash/)
Expand Down

0 comments on commit cafb90d

Please sign in to comment.