Skip to content

Commit e4b8e48

Browse files
committed
refactor(cli): use ext option to override non-js-like script extensions
1 parent 3614aff commit e4b8e48

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

docs/cli.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ assumes that it is
1313
an [ESM](https://nodejs.org/api/modules.html#modules_module_createrequire_filename)
1414
module unless the `--ext` option is specified.
1515

16+
## Non-standard extension
17+
`zx` internally loads scripts via `import` API, so you can use any extension supported by the runtime (nodejs, deno, bun) or apply a [custom loader](https://nodejs.org/api/cli.html#--experimental-loadermodule).
18+
However, if the script has a non-js-like extension (`/^\.[mc]?[jt]sx?$/`) and the `--ext` is specified, it will be used.
1619

20+
```bash
21+
zx script.zx # Unknown file extension "\.zx"
22+
zx --ext=mjs script.zx # OK
23+
```
24+
25+
## Markdown
1726
```bash
1827
zx docs/markdown.md
1928
```
@@ -89,10 +98,10 @@ Enable verbose mode.
8998

9099
## `--shell`
91100

92-
Specify a custom shell binary.
101+
Specify a custom shell binary path. By default, zx refers to `bash`.
93102

94103
```bash
95-
zx --shell=/bin/bash script.mjs
104+
zx --shell=/bin/another/sh script.mjs
96105
```
97106

98107
## `--prefer-local, -l`
@@ -131,7 +140,7 @@ When `cwd` option is specified, it will be used as base path:
131140

132141
## `--ext`
133142

134-
Override the default (temp) script extension. Default is `.mjs`.
143+
Overrides the default script extension (`.mjs`).
135144

136145
## `--version, -v`
137146

man/zx.1

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ prefer locally installed packages bins
2424
.SS --eval=<js>, -e
2525
evaluate script
2626
.SS --ext=<.mjs>
27-
default extension
28-
.SS --ext-override
29-
override script extensions
27+
script extension
3028
.SS --install, -i
3129
install dependencies
3230
.SS --registry=<URL>

src/cli.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { randomId, bufToString } from './util.js'
3535
import { createRequire, type minimist } from './vendor.js'
3636

3737
const EXT = '.mjs'
38+
const EXT_RE = /^\.[mc]?[jt]sx?$/
3839

3940
isMain() &&
4041
main().catch((err) => {
@@ -64,8 +65,7 @@ export function printUsage() {
6465
--prefer-local, -l prefer locally installed packages bins
6566
--cwd=<path> set current directory
6667
--eval=<js>, -e evaluate script
67-
--ext=<.mjs> default extension
68-
--ext-override override script extensions
68+
--ext=<.mjs> script extension
6969
--install, -i install dependencies
7070
--registry=<URL> npm registry, defaults to https://registry.npmjs.org/
7171
--version, -v print current zx version
@@ -81,7 +81,7 @@ export function printUsage() {
8181
// prettier-ignore
8282
export const argv: minimist.ParsedArgs = parseArgv(process.argv.slice(2), {
8383
string: ['shell', 'prefix', 'postfix', 'eval', 'cwd', 'ext', 'registry', 'env'],
84-
boolean: ['version', 'help', 'quiet', 'verbose', 'install', 'repl', 'experimental', 'prefer-local', 'ext-override'],
84+
boolean: ['version', 'help', 'quiet', 'verbose', 'install', 'repl', 'experimental', 'prefer-local'],
8585
alias: { e: 'eval', i: 'install', v: 'version', h: 'help', l: 'prefer-local', 'env-file': 'env' },
8686
stopEarly: true,
8787
parseBoolean: true,
@@ -181,7 +181,7 @@ async function readScript() {
181181
}
182182

183183
const { ext, base, dir } = path.parse(tempPath || scriptPath)
184-
if (ext === '' || argv.extOverride) {
184+
if (ext === '' || (argv.ext && !EXT_RE.test(ext))) {
185185
tempPath = getFilepath(dir, base)
186186
}
187187
if (ext === '.md') {

test/cli.test.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,15 @@ describe('cli', () => {
223223
)
224224
})
225225

226-
test('scripts with non standard extension (override)', async () => {
226+
test('scripts with non standard extension', async () => {
227227
const o =
228-
await $`node build/cli.js --ext-override test/fixtures/non-std-ext.zx`
228+
await $`node build/cli.js --ext='.mjs' test/fixtures/non-std-ext.zx`
229229
assert.ok(o.stdout.trim().endsWith('zx/test/fixtures/non-std-ext.zx.mjs'))
230+
231+
await assert.rejects(
232+
$`node build/cli.js test/fixtures/non-std-ext.zx`,
233+
/Unknown file extension "\.zx"/
234+
)
230235
})
231236

232237
test22('scripts from stdin with explicit extension', async () => {

test/export.test.js

-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ describe('cli', () => {
6464
assert.equal(typeof cli.argv, 'object', 'cli.argv')
6565
assert.equal(typeof cli.argv._, 'object', 'cli.argv._')
6666
assert.equal(typeof cli.argv.experimental, 'boolean', 'cli.argv.experimental')
67-
assert.equal(typeof cli.argv.extOverride, 'boolean', 'cli.argv.extOverride')
6867
assert.equal(typeof cli.argv.h, 'boolean', 'cli.argv.h')
6968
assert.equal(typeof cli.argv.help, 'boolean', 'cli.argv.help')
7069
assert.equal(typeof cli.argv.i, 'boolean', 'cli.argv.i')

0 commit comments

Comments
 (0)