Skip to content

Commit 3614aff

Browse files
committed
feat: introduce --ext-override option to process non-std (non js/ts like) file extensions
resolves #1104
1 parent 82a5e0b commit 3614aff

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

man/zx.1

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ prefer locally installed packages bins
2525
evaluate script
2626
.SS --ext=<.mjs>
2727
default extension
28+
.SS --ext-override
29+
override script extensions
2830
.SS --install, -i
2931
install dependencies
3032
.SS --registry=<URL>

src/cli.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export function printUsage() {
6565
--cwd=<path> set current directory
6666
--eval=<js>, -e evaluate script
6767
--ext=<.mjs> default extension
68+
--ext-override override script extensions
6869
--install, -i install dependencies
6970
--registry=<URL> npm registry, defaults to https://registry.npmjs.org/
7071
--version, -v print current zx version
@@ -80,7 +81,7 @@ export function printUsage() {
8081
// prettier-ignore
8182
export const argv: minimist.ParsedArgs = parseArgv(process.argv.slice(2), {
8283
string: ['shell', 'prefix', 'postfix', 'eval', 'cwd', 'ext', 'registry', 'env'],
83-
boolean: ['version', 'help', 'quiet', 'verbose', 'install', 'repl', 'experimental', 'prefer-local'],
84+
boolean: ['version', 'help', 'quiet', 'verbose', 'install', 'repl', 'experimental', 'prefer-local', 'ext-override'],
8485
alias: { e: 'eval', i: 'install', v: 'version', h: 'help', l: 'prefer-local', 'env-file': 'env' },
8586
stopEarly: true,
8687
parseBoolean: true,
@@ -180,7 +181,7 @@ async function readScript() {
180181
}
181182

182183
const { ext, base, dir } = path.parse(tempPath || scriptPath)
183-
if (ext === '') {
184+
if (ext === '' || argv.extOverride) {
184185
tempPath = getFilepath(dir, base)
185186
}
186187
if (ext === '.md') {

test/cli.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ describe('cli', () => {
223223
)
224224
})
225225

226+
test('scripts with non standard extension (override)', async () => {
227+
const o =
228+
await $`node build/cli.js --ext-override test/fixtures/non-std-ext.zx`
229+
assert.ok(o.stdout.trim().endsWith('zx/test/fixtures/non-std-ext.zx.mjs'))
230+
})
231+
226232
test22('scripts from stdin with explicit extension', async () => {
227233
const out =
228234
await $`node --experimental-strip-types build/cli.js --ext='.ts' <<< 'const foo: string = "bar"; console.log(foo)'`

test/export.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ 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')
6768
assert.equal(typeof cli.argv.h, 'boolean', 'cli.argv.h')
6869
assert.equal(typeof cli.argv.help, 'boolean', 'cli.argv.help')
6970
assert.equal(typeof cli.argv.i, 'boolean', 'cli.argv.i')

test/fixtures/non-std-ext.zx

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env zx
2+
3+
// Copyright 2024 Google LLC
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// https://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
console.log(chalk.yellowBright`If file has non-std ext and 'ext-override' option specified, zx assumes it's ESM.`)
18+
await $`pwd`
19+
console.log('__filename =', __filename)

0 commit comments

Comments
 (0)