Skip to content

Commit cafb90d

Browse files
authoredMar 27, 2024··
feat: set $.verbose to false by default (#745)
* feat: set `$.verbose` to false by default closes #569 * chore: linting * chore: minor tweak ups
1 parent def0883 commit cafb90d

17 files changed

+102
-53
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ reports/
66
.stryker-tmp/
77
yarn.lock
88
test/fixtures/ts-project/package-lock.json
9+
test/fixtures/js-project/package-lock.json

‎package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"build:js": "node scripts/build-js.mjs --format=esm --entry=src/*.ts && npm run build:vendor",
4747
"build:vendor": "node scripts/build-js.mjs --format=esm --entry=src/vendor.ts --bundle=all --banner",
4848
"build:dts": "tsc --project tsconfig.prod.json && node scripts/build-dts.mjs",
49-
"test": "npm run build && node ./test/all.test.js",
49+
"test": "npm run build && npm run test:unit && npm run test:types",
50+
"test:unit": "node ./test/all.test.js",
5051
"test:types": "tsd",
5152
"coverage": "c8 -x build/vendor.js -x 'test/**' -x scripts --check-coverage npm test",
5253
"mutation": "stryker run",
@@ -61,7 +62,7 @@
6162
"@stryker-mutator/core": "^6.4.2",
6263
"@types/fs-extra": "^11.0.4",
6364
"@types/minimist": "^1.2.5",
64-
"@types/node": ">=20.11.19",
65+
"@types/node": ">=20.11.30",
6566
"@types/which": "^3.0.3",
6667
"@webpod/ps": "^0.0.0-beta.2",
6768
"c8": "^9.1.0",

‎src/cli.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,23 @@ function printUsage() {
5353

5454
const argv = minimist(process.argv.slice(2), {
5555
string: ['shell', 'prefix', 'eval'],
56-
boolean: ['version', 'help', 'quiet', 'install', 'repl', 'experimental'],
56+
boolean: [
57+
'version',
58+
'help',
59+
'quiet',
60+
'verbose',
61+
'install',
62+
'repl',
63+
'experimental',
64+
],
5765
alias: { e: 'eval', i: 'install', v: 'version', h: 'help' },
5866
stopEarly: true,
5967
})
6068

6169
await (async function main() {
6270
const globals = './globals.js'
6371
await import(globals)
72+
if (argv.verbose) $.verbose = true
6473
if (argv.quiet) $.verbose = false
6574
if (argv.shell) $.shell = argv.shell
6675
if (argv.prefix) $.prefix = argv.prefix

‎src/core.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ hook.enable()
8383
export const defaults: Options = {
8484
[processCwd]: process.cwd(),
8585
[syncExec]: false,
86-
verbose: true,
86+
verbose: false,
8787
env: process.env,
8888
sync: false,
8989
shell: true,
@@ -257,7 +257,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
257257
},
258258
stderr: (data) => {
259259
// Stderr should be printed regardless of piping.
260-
$.log({ kind: 'stderr', data, verbose: self.isVerbose() })
260+
$.log({ kind: 'stderr', data, verbose: !self.isQuiet() })
261261
},
262262
end: ({ error, stdout, stderr, stdall, status, signal }) => {
263263
self._resolved = true
@@ -425,9 +425,12 @@ export class ProcessPromise extends Promise<ProcessOutput> {
425425
return this
426426
}
427427

428+
isQuiet(): boolean {
429+
return this._quiet ?? this._snapshot.quiet
430+
}
431+
428432
isVerbose(): boolean {
429-
const { verbose, quiet } = this._snapshot
430-
return verbose && !(this._quiet ?? quiet)
433+
return this._snapshot.verbose && !this.isQuiet()
431434
}
432435

433436
timeout(d: Duration, signal = 'SIGTERM'): ProcessPromise {

‎test/cli.test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ describe('cli', () => {
2121
let promiseResolved = false
2222

2323
beforeEach(() => {
24-
$.verbose = false
2524
process.on('exit', () => {
2625
if (!promiseResolved) {
2726
console.error('Error: ProcessPromise never resolved.')
@@ -77,29 +76,30 @@ describe('cli', () => {
7776
})
7877

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

8685
test('supports `--shell` flag ', async () => {
8786
let shell = $.shell
8887
let p =
89-
await $`node build/cli.js --shell=${shell} <<< '$\`echo \${$.shell}\`'`
88+
await $`node build/cli.js --verbose --shell=${shell} <<< '$\`echo \${$.shell}\`'`
9089
assert.ok(p.stderr.includes(shell))
9190
})
9291

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

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

‎test/core.test.js

-6
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ import { ProcessPromise, ProcessOutput } from '../build/index.js'
2121
import '../build/globals.js'
2222

2323
describe('core', () => {
24-
beforeEach(() => {
25-
$.verbose = false
26-
})
27-
2824
test('only stdout is used during command substitution', async () => {
2925
let hello = await $`echo Error >&2; echo Hello`
3026
let len = +(await $`echo ${hello} | wc -c`)
@@ -340,7 +336,6 @@ describe('core', () => {
340336
resolve()
341337
}
342338

343-
$.verbose = false
344339
assert.equal($.verbose, false)
345340

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

366361
within(async () => {
367-
$.verbose = false
368362
cd('/tmp')
369363
setTimeout(async () => {
370364
assert.ok((await $`pwd`).stdout.trim().endsWith('/tmp'))

‎test/deps.test.js

-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ import { $ } from '../build/index.js'
1818
import { installDeps, parseDeps } from '../build/deps.js'
1919

2020
describe('deps', () => {
21-
beforeEach(() => {
22-
$.verbose = false
23-
})
24-
2521
test('installDeps() loader works via JS API', async () => {
2622
await installDeps({
2723
cpy: '9.0.1',

‎test/experimental.test.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,4 @@
1515
import { describe, before } from 'node:test'
1616
import '../build/globals.js'
1717

18-
describe('experimental', () => {
19-
before(() => {
20-
$.verbose = false
21-
})
22-
})
18+
describe('experimental', () => {})

‎test/extra.test.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@
1313
// limitations under the License.
1414

1515
import assert from 'node:assert'
16-
import fs from 'node:fs'
16+
import fs from 'node:fs/promises'
1717
import { test, describe } from 'node:test'
1818
import { globby } from 'globby'
1919

2020
describe('extra', () => {
2121
test('every file should have a license', async () => {
22-
const files = await globby(['**/*.{ts,js,mjs}'], { gitignore: true })
22+
const files = await globby(['**/*.{js,mjs,ts}'], {
23+
gitignore: true,
24+
onlyFiles: true,
25+
cwd: process.cwd(),
26+
followSymbolicLinks: false,
27+
})
2328
for (const file of files) {
24-
const content = fs.readFileSync(file).toString()
29+
const content = await fs.readFile(file, 'utf8')
2530
assert(
2631
content
2732
.replace(/\d{4}/g, 'YEAR')

‎test/fixtures/js-project/package-lock.json

+57
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/js-project/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"private": true,
33
"type": "module",
44
"dependencies": {
5-
"zx": "*"
5+
"zx": "file:../../.."
66
}
77
}

‎test/fixtures/ts-project/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"type": "module",
44
"dependencies": {
55
"typescript": "^5.0.0",
6-
"zx": "*"
6+
"zx": "file:../../.."
77
}
88
}

‎test/fixtures/ts-project/script.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414

1515
import { $, ProcessPromise } from 'zx'
1616

17-
let p: ProcessPromise = $`echo ts-script`
17+
let p: ProcessPromise = $({ verbose: true })`echo ts-script`
1818
await p

‎test/goods.test.js

-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ import { test, describe, beforeEach } from 'node:test'
1818
import '../build/globals.js'
1919

2020
describe('goods', () => {
21-
beforeEach(() => {
22-
$.verbose = false
23-
})
24-
2521
function zx(script) {
2622
return $`node build/cli.js --eval ${script}`.nothrow().timeout('5s')
2723
}

‎test/package.test.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import '../build/globals.js'
1818

1919
describe('package', () => {
2020
beforeEach(async () => {
21-
$.verbose = false
2221
const pack = await $`npm pack`
2322
await $`tar xf ${pack}`
2423
await $`rm ${pack}`.nothrow()
@@ -29,8 +28,6 @@ describe('package', () => {
2928
const out = await within(async () => {
3029
cd('test/fixtures/ts-project')
3130
await $`npm i`
32-
await $`rm -rf node_modules/zx`
33-
await $`mv ${pack} node_modules/zx`
3431
try {
3532
await $`npx tsc`
3633
} catch (err) {
@@ -45,10 +42,8 @@ describe('package', () => {
4542
const pack = path.resolve('package')
4643
const out = await within(async () => {
4744
cd('test/fixtures/js-project')
48-
await $`rm -rf node_modules`
49-
await $`mkdir node_modules`
50-
await $`mv ${pack} node_modules/zx`
51-
return $`node node_modules/zx/build/cli.js script.js`
45+
await $`npm i`
46+
return $`node node_modules/zx/build/cli.js --verbose script.js`
5247
})
5348
assert.match(out.stderr, /js-script/)
5449
})

‎test/win32.test.js

-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ import '../build/globals.js'
1919
const _describe = process.platform === 'win32' ? describe : describe.skip
2020

2121
_describe('win32', () => {
22-
beforeEach(() => {
23-
$.verbose = false
24-
})
25-
2622
test('should work with windows-specific commands', async () => {
2723
const p = await $`echo $0` // Bash is first by default.
2824
assert.match(p.stdout, /bash/)

0 commit comments

Comments
 (0)
Please sign in to comment.