From 4367423b3b27f7ec263604a61dba777a47c363c3 Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Mon, 23 Dec 2024 17:26:41 +0300 Subject: [PATCH 01/17] feat: add support env files --- src/cli.ts | 11 +++++++++-- src/util.ts | 40 ++++++++++++++++++++++++++++++++++++++++ test/cli.test.js | 36 ++++++++++++++++++++++++++++++++++++ test/util.test.js | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 2 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 74d2b60853..bdf7becd8d 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -27,7 +27,7 @@ import { parseArgv, } from './index.js' import { installDeps, parseDeps } from './deps.js' -import { randomId } from './util.js' +import { prepareEnv, randomId } from './util.js' import { createRequire } from './vendor.js' const EXT = '.mjs' @@ -66,6 +66,7 @@ export function printUsage() { --version, -v print current zx version --help, -h print help --repl start repl + --env= path to env file --experimental enables experimental features (deprecated) ${chalk.italic('Full documentation:')} ${chalk.underline('https://google.github.io/zx/')} @@ -74,7 +75,7 @@ export function printUsage() { // prettier-ignore export const argv = parseArgv(process.argv.slice(2), { - string: ['shell', 'prefix', 'postfix', 'eval', 'cwd', 'ext', 'registry'], + string: ['shell', 'prefix', 'postfix', 'eval', 'cwd', 'ext', 'registry', 'env'], boolean: ['version', 'help', 'quiet', 'verbose', 'install', 'repl', 'experimental', 'prefer-local'], alias: { e: 'eval', i: 'install', v: 'version', h: 'help', l: 'prefer-local' }, stopEarly: true, @@ -86,6 +87,12 @@ export async function main() { await import('./globals.js') argv.ext = normalizeExt(argv.ext) if (argv.cwd) $.cwd = argv.cwd + if (argv.env) { + const processRootDir = + argv?.cwd ?? path.dirname(new URL(import.meta.url).pathname) + const envPath = path.resolve(processRootDir, argv.env) + process.env = prepareEnv(envPath, process.env) + } if (argv.verbose) $.verbose = true if (argv.quiet) $.quiet = true if (argv.shell) $.shell = argv.shell diff --git a/src/util.ts b/src/util.ts index a79186ad97..a049b3bcfe 100644 --- a/src/util.ts +++ b/src/util.ts @@ -357,3 +357,43 @@ export const toCamelCase = (str: string) => export const parseBool = (v: string): boolean | string => ({ true: true, false: false })[v] ?? v + +// prettier-ignore +export const parseEnvFromFile = ( + content: string, + ): NodeJS.ProcessEnv => { + return content + .split(/\r?\n/) + .reduce((acc, line) => { + const cleanedLine = line.replace(/[\r?\n]+/g, '').trim(); + const key = cleanedLine.split("=", 1)?.[0]; + + if (!key) return acc; + + const value = cleanedLine.slice(key.length + 1); + + if (!value) return acc; + + acc[key] = value; + + return acc; + }, {}); + }; + +export const prepareEnv = ( + filepath: string, + env: NodeJS.ProcessEnv = process.env +): NodeJS.ProcessEnv => { + try { + const content = fs.readFileSync(path.resolve(filepath), { + encoding: 'utf8', + }) + + return { + ...env, + ...parseEnvFromFile(content), + } + } catch (e) { + return env + } +} diff --git a/test/cli.test.js b/test/cli.test.js index 0d724006b8..3bc68ca497 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -150,6 +150,42 @@ describe('cli', () => { assert.ok(p.stderr.endsWith(cwd + '\n')) }) + test('supports `--env` options with file', async () => { + const env = tmpfile( + '.env', + `FOO=BAR + BAR=FOO+=` + ) + const file = tmpfile( + 'index.mjs', + 'console.log(process.env.FOO);\nconsole.log(process.env.BAR)' + ) + + const out = await $`node build/cli.js --env=${env} ${file}` + fs.remove(env) + fs.remove(file) + assert.equal(out.stdout, 'BAR\nFOO+=\n') + }) + + test('supports `--env` and `--cwd` options and with file', async () => { + const cwd = path.resolve(fileURLToPath(import.meta.url), '../../temp') + fs.mkdirSync(cwd, { recursive: true }) + const env = tmpfile( + '.env', + `FOO=BAR + BAR=FOO+=` + ) + const file = tmpfile( + 'index.mjs', + 'console.log(process.env.FOO);\nconsole.log(process.env.BAR)' + ) + + const out = await $`node build/cli.js --cwd=${cwd} --env=${env} ${file}` + fs.remove(env) + fs.remove(file) + assert.equal(out.stdout, 'BAR\nFOO+=\n') + }) + test('scripts from https 200', async () => { const resp = await fs.readFile(path.resolve('test/fixtures/echo.http')) const port = await getPort() diff --git a/test/util.test.js b/test/util.test.js index 7eef6914dd..5cac614ffe 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -139,3 +139,35 @@ describe('util', () => { assert.equal(toCamelCase('kebab-input-str'), 'kebabInputStr') }) }) + +test('parseEnvFromFile()', () => { + assert.deepEqual(parseEnvFromFile('ENV=value1\nENV2=value24=f]fdsd'), { + ENV: 'value1', + ENV2: 'value24=f]fdsd', + }) + assert.deepEqual(parseEnvFromFile('ENV=value1\nENV2=value24'), { + ENV: 'value1', + ENV2: 'value24', + }) + assert.deepEqual(parseEnvFromFile(''), {}) +}) + +describe('prepareEnv()', () => { + test('handles correct proccess.env', () => { + const file = tempfile('.env', 'ENV=value1\nENV2=value24=f]fdsd') + const env = prepareEnv(file) + assert.equal(env.ENV, 'value1') + assert.equal(env.ENV2, 'value24=f]fdsd') + assert.ok(env.NODE_VERSION !== '') + }) + + test('handles correct some env', () => { + const file = tempfile('.env', 'ENV=value1\nENV2=value24=f]fdsd') + const env = prepareEnv(file, { version: '1.0.0', name: 'zx' }) + assert.equal(env.ENV, 'value1') + assert.equal(env.ENV2, 'value24=f]fdsd') + assert.equal(env.ENV2, 'value24=f]fdsd') + assert.equal(env.version, '1.0.0') + assert.equal(env.name, 'zx') + }) +}) From 0773da85f543110af62b0b824830d2ccdb63aa2f Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Mon, 23 Dec 2024 18:00:55 +0300 Subject: [PATCH 02/17] feat: update size limit --- .size-limit.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.size-limit.json b/.size-limit.json index 79c6a868d3..1a0cf6010d 100644 --- a/.size-limit.json +++ b/.size-limit.json @@ -2,14 +2,14 @@ { "name": "zx/core", "path": ["build/core.cjs", "build/util.cjs", "build/vendor-core.cjs"], - "limit": "75 kB", + "limit": "76 kB", "brotli": false, "gzip": false }, { "name": "zx/index", "path": "build/*.{js,cjs}", - "limit": "804 kB", + "limit": "805 kB", "brotli": false, "gzip": false }, @@ -30,7 +30,7 @@ { "name": "all", "path": "build/*", - "limit": "840 kB", + "limit": "842 kB", "brotli": false, "gzip": false } From b4e20466ff361b0e028f669b06c16251f5ec8965 Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Mon, 23 Dec 2024 18:03:37 +0300 Subject: [PATCH 03/17] chore: update test --- test/util.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/util.test.js b/test/util.test.js index 5cac614ffe..f7f1745f00 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -29,6 +29,8 @@ import { tempfile, preferLocalBin, toCamelCase, + parseEnvFromFile, + prepareEnv, } from '../build/util.js' describe('util', () => { From 148900e0c6787f7cd7e1346677805bc4c8c674d5 Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Mon, 23 Dec 2024 22:51:44 +0300 Subject: [PATCH 04/17] fix: review --- src/cli.ts | 8 +++----- src/util.ts | 35 +++++++++++------------------------ test/cli.test.js | 35 +++++++++++++++++------------------ test/util.test.js | 29 ++++++++++++----------------- 4 files changed, 43 insertions(+), 64 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index bdf7becd8d..15f40f40b3 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -27,7 +27,7 @@ import { parseArgv, } from './index.js' import { installDeps, parseDeps } from './deps.js' -import { prepareEnv, randomId } from './util.js' +import { readEnvFromFile, randomId } from './util.js' import { createRequire } from './vendor.js' const EXT = '.mjs' @@ -88,10 +88,8 @@ export async function main() { argv.ext = normalizeExt(argv.ext) if (argv.cwd) $.cwd = argv.cwd if (argv.env) { - const processRootDir = - argv?.cwd ?? path.dirname(new URL(import.meta.url).pathname) - const envPath = path.resolve(processRootDir, argv.env) - process.env = prepareEnv(envPath, process.env) + const envPath = path.resolve($.cwd ?? '', argv.env) + $.env = readEnvFromFile(envPath, process.env) } if (argv.verbose) $.verbose = true if (argv.quiet) $.quiet = true diff --git a/src/util.ts b/src/util.ts index a049b3bcfe..62030c8281 100644 --- a/src/util.ts +++ b/src/util.ts @@ -359,41 +359,28 @@ export const parseBool = (v: string): boolean | string => ({ true: true, false: false })[v] ?? v // prettier-ignore -export const parseEnvFromFile = ( +export const parseDotenv = ( content: string, ): NodeJS.ProcessEnv => { return content .split(/\r?\n/) .reduce((acc, line) => { - const cleanedLine = line.replace(/[\r?\n]+/g, '').trim(); - const key = cleanedLine.split("=", 1)?.[0]; - - if (!key) return acc; - - const value = cleanedLine.slice(key.length + 1); - - if (!value) return acc; - - acc[key] = value; - + const [k,v] = line.split(/(? v.trim()) + if (k && v) acc[k] = v return acc; }, {}); }; -export const prepareEnv = ( +export const readEnvFromFile = ( filepath: string, env: NodeJS.ProcessEnv = process.env ): NodeJS.ProcessEnv => { - try { - const content = fs.readFileSync(path.resolve(filepath), { - encoding: 'utf8', - }) - - return { - ...env, - ...parseEnvFromFile(content), - } - } catch (e) { - return env + const content = fs.readFileSync(path.resolve(filepath), { + encoding: 'utf8', + }) + + return { + ...env, + ...parseDotenv(content), } } diff --git a/test/cli.test.js b/test/cli.test.js index 3bc68ca497..c308fa4c44 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -154,36 +154,35 @@ describe('cli', () => { const env = tmpfile( '.env', `FOO=BAR - BAR=FOO+=` - ) - const file = tmpfile( - 'index.mjs', - 'console.log(process.env.FOO);\nconsole.log(process.env.BAR)' + BAR=FOO+` ) + const file = ` + console.log((await $\`echo $FOO\`).stdout); + console.log((await $\`echo $BAR\`).stdout) + ` - const out = await $`node build/cli.js --env=${env} ${file}` + const out = await $`node build/cli.js --env=${env} <<< ${file}` fs.remove(env) - fs.remove(file) - assert.equal(out.stdout, 'BAR\nFOO+=\n') + assert.equal(out.stdout, 'BAR\n\nFOO+\n\n') }) test('supports `--env` and `--cwd` options and with file', async () => { - const cwd = path.resolve(fileURLToPath(import.meta.url), '../../temp') - fs.mkdirSync(cwd, { recursive: true }) const env = tmpfile( '.env', `FOO=BAR - BAR=FOO+=` - ) - const file = tmpfile( - 'index.mjs', - 'console.log(process.env.FOO);\nconsole.log(process.env.BAR)' + BAR=FOO+` ) + const dir = tmpdir() + const file = ` + console.log((await $\`echo $FOO\`).stdout); + console.log((await $\`echo $BAR\`).stdout) + ` - const out = await $`node build/cli.js --cwd=${cwd} --env=${env} ${file}` + const out = + await $`node build/cli.js --cwd=${dir} --env=${env} <<< ${file}` fs.remove(env) - fs.remove(file) - assert.equal(out.stdout, 'BAR\nFOO+=\n') + fs.remove(dir) + assert.equal(out.stdout, 'BAR\n\nFOO+\n\n') }) test('scripts from https 200', async () => { diff --git a/test/util.test.js b/test/util.test.js index f7f1745f00..1ad6f28421 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -29,8 +29,8 @@ import { tempfile, preferLocalBin, toCamelCase, - parseEnvFromFile, - prepareEnv, + parseDotenv, + readEnvFromFile, } from '../build/util.js' describe('util', () => { @@ -142,33 +142,28 @@ describe('util', () => { }) }) -test('parseEnvFromFile()', () => { - assert.deepEqual(parseEnvFromFile('ENV=value1\nENV2=value24=f]fdsd'), { - ENV: 'value1', - ENV2: 'value24=f]fdsd', - }) - assert.deepEqual(parseEnvFromFile('ENV=value1\nENV2=value24'), { +test('parseDotenv()', () => { + assert.deepEqual(parseDotenv('ENV=value1\nENV2=value24'), { ENV: 'value1', ENV2: 'value24', }) - assert.deepEqual(parseEnvFromFile(''), {}) + assert.deepEqual(parseDotenv(''), {}) }) -describe('prepareEnv()', () => { +describe('readEnvFromFile()', () => { test('handles correct proccess.env', () => { - const file = tempfile('.env', 'ENV=value1\nENV2=value24=f]fdsd') - const env = prepareEnv(file) + const file = tempfile('.env', 'ENV=value1\nENV2=value24') + const env = readEnvFromFile(file) assert.equal(env.ENV, 'value1') - assert.equal(env.ENV2, 'value24=f]fdsd') + assert.equal(env.ENV2, 'value24') assert.ok(env.NODE_VERSION !== '') }) test('handles correct some env', () => { - const file = tempfile('.env', 'ENV=value1\nENV2=value24=f]fdsd') - const env = prepareEnv(file, { version: '1.0.0', name: 'zx' }) + const file = tempfile('.env', 'ENV=value1\nENV2=value24') + const env = readEnvFromFile(file, { version: '1.0.0', name: 'zx' }) assert.equal(env.ENV, 'value1') - assert.equal(env.ENV2, 'value24=f]fdsd') - assert.equal(env.ENV2, 'value24=f]fdsd') + assert.equal(env.ENV2, 'value24') assert.equal(env.version, '1.0.0') assert.equal(env.name, 'zx') }) From e36e23db564ee3d0696322ca0cbddb09dc8e91b4 Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Mon, 23 Dec 2024 22:55:00 +0300 Subject: [PATCH 05/17] chore: update size limit --- .size-limit.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.size-limit.json b/.size-limit.json index 1a0cf6010d..afce6307ae 100644 --- a/.size-limit.json +++ b/.size-limit.json @@ -2,14 +2,14 @@ { "name": "zx/core", "path": ["build/core.cjs", "build/util.cjs", "build/vendor-core.cjs"], - "limit": "76 kB", + "limit": "76.5 kB", "brotli": false, "gzip": false }, { "name": "zx/index", "path": "build/*.{js,cjs}", - "limit": "805 kB", + "limit": "808 kB", "brotli": false, "gzip": false }, @@ -30,7 +30,7 @@ { "name": "all", "path": "build/*", - "limit": "842 kB", + "limit": "844.5 kB", "brotli": false, "gzip": false } From ec953dc247c8426aefdb269d3585245c869cb134 Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Mon, 23 Dec 2024 22:56:16 +0300 Subject: [PATCH 06/17] fix: update env path --- src/cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli.ts b/src/cli.ts index 15f40f40b3..23c281e8f3 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -88,7 +88,7 @@ export async function main() { argv.ext = normalizeExt(argv.ext) if (argv.cwd) $.cwd = argv.cwd if (argv.env) { - const envPath = path.resolve($.cwd ?? '', argv.env) + const envPath = path.resolve($.cwd ?? process.cwd(), argv.env) $.env = readEnvFromFile(envPath, process.env) } if (argv.verbose) $.verbose = true From d51dc87367fac11daa1241c581cfc486d13d7974 Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Mon, 23 Dec 2024 22:57:42 +0300 Subject: [PATCH 07/17] chore: update size limit --- .size-limit.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.size-limit.json b/.size-limit.json index afce6307ae..d8ea85b783 100644 --- a/.size-limit.json +++ b/.size-limit.json @@ -2,7 +2,7 @@ { "name": "zx/core", "path": ["build/core.cjs", "build/util.cjs", "build/vendor-core.cjs"], - "limit": "76.5 kB", + "limit": "77 kB", "brotli": false, "gzip": false }, From 441d7ea4d7c3289e6a433e7fd1662267f131b3ce Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Mon, 23 Dec 2024 23:28:41 +0300 Subject: [PATCH 08/17] chore: update man --- man/zx.1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/man/zx.1 b/man/zx.1 index b6f23a9c47..8279582e1d 100644 --- a/man/zx.1 +++ b/man/zx.1 @@ -31,6 +31,8 @@ install dependencies npm registry, defaults to https://registry.npmjs.org/ .SS --repl start repl +.SS --env= +path to env file .SS --version, -v print current zx version .SS --help, -h From ddaaad0d67a97253beae134a2faee83adda9ce70 Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Mon, 23 Dec 2024 23:28:56 +0300 Subject: [PATCH 09/17] chore: update size limit --- .size-limit.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.size-limit.json b/.size-limit.json index d8ea85b783..eedbac486c 100644 --- a/.size-limit.json +++ b/.size-limit.json @@ -9,7 +9,7 @@ { "name": "zx/index", "path": "build/*.{js,cjs}", - "limit": "808 kB", + "limit": "805 kB", "brotli": false, "gzip": false }, @@ -30,7 +30,7 @@ { "name": "all", "path": "build/*", - "limit": "844.5 kB", + "limit": "842 kB", "brotli": false, "gzip": false } From 2116b19b8603ffed8fe9bc6e877776f9e9c91645 Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Mon, 23 Dec 2024 23:29:22 +0300 Subject: [PATCH 10/17] fix: replace split by limit --- src/util.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.ts b/src/util.ts index 62030c8281..6bee2306f1 100644 --- a/src/util.ts +++ b/src/util.ts @@ -365,7 +365,7 @@ export const parseDotenv = ( return content .split(/\r?\n/) .reduce((acc, line) => { - const [k,v] = line.split(/(? v.trim()) + const [k,v] = line.trim().split('=', 2) if (k && v) acc[k] = v return acc; }, {}); From c1df238e8c44367659a044bd11debcac9ce09cb9 Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Tue, 24 Dec 2024 09:47:14 +0300 Subject: [PATCH 11/17] refactor: update parseDotenv --- src/util.ts | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/util.ts b/src/util.ts index 6bee2306f1..77fd921476 100644 --- a/src/util.ts +++ b/src/util.ts @@ -358,18 +358,14 @@ export const toCamelCase = (str: string) => export const parseBool = (v: string): boolean | string => ({ true: true, false: false })[v] ?? v -// prettier-ignore -export const parseDotenv = ( - content: string, - ): NodeJS.ProcessEnv => { - return content - .split(/\r?\n/) - .reduce((acc, line) => { - const [k,v] = line.trim().split('=', 2) - if (k && v) acc[k] = v - return acc; - }, {}); - }; +export const parseDotenv = (content: string): NodeJS.ProcessEnv => { + return content.split(/\r?\n/).reduce((acc, line) => { + const [k] = line.split('=', 1) + const v = line.slice(k.length + 1) + if (k && v) acc[k] = v + return acc + }, {}) +} export const readEnvFromFile = ( filepath: string, From 8af8cbecb93eaf757c88807f610c743deb8139ad Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Tue, 24 Dec 2024 09:51:06 +0300 Subject: [PATCH 12/17] docs: update docs --- docs/cli.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/cli.md b/docs/cli.md index 1c00b717b2..1a2818dd00 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -118,6 +118,19 @@ Set the current working directory. zx --cwd=/foo/bar script.mjs ``` +## --env +Specify a env file. + +```bash +zx --env=/path/to/some.env script.mjs +``` + +Also works with `--cwd` options. + +```bash +zx --cwd=/foo/bar --env=/path/to/some.env script.mjs +``` + ## --ext Override the default (temp) script extension. Default is `.mjs`. From 665c157e47200253216711362451eace66a988a5 Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Tue, 24 Dec 2024 10:33:22 +0300 Subject: [PATCH 13/17] fix: add line trim --- src/util.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/util.ts b/src/util.ts index 77fd921476..b3fc5a5938 100644 --- a/src/util.ts +++ b/src/util.ts @@ -359,11 +359,11 @@ export const parseBool = (v: string): boolean | string => ({ true: true, false: false })[v] ?? v export const parseDotenv = (content: string): NodeJS.ProcessEnv => { - return content.split(/\r?\n/).reduce((acc, line) => { - const [k] = line.split('=', 1) - const v = line.slice(k.length + 1) - if (k && v) acc[k] = v - return acc + return content.split(/\r?\n/).reduce((r, line) => { + const [k] = line.trim().split('=', 1) + const v = line.trim().slice(k.length + 1) + if (k && v) r[k] = v + return r }, {}) } @@ -371,9 +371,7 @@ export const readEnvFromFile = ( filepath: string, env: NodeJS.ProcessEnv = process.env ): NodeJS.ProcessEnv => { - const content = fs.readFileSync(path.resolve(filepath), { - encoding: 'utf8', - }) + const content = fs.readFileSync(path.resolve(filepath), 'utf8') return { ...env, From ff6452741df62bcae303e1d4a6e57aab5895f1d7 Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Tue, 24 Dec 2024 10:34:09 +0300 Subject: [PATCH 14/17] test: add test for file reading error --- test/cli.test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/cli.test.js b/test/cli.test.js index c308fa4c44..aae9af2d4c 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -166,6 +166,8 @@ describe('cli', () => { assert.equal(out.stdout, 'BAR\n\nFOO+\n\n') }) + + test('supports `--env` and `--cwd` options and with file', async () => { const env = tmpfile( '.env', @@ -185,6 +187,20 @@ describe('cli', () => { assert.equal(out.stdout, 'BAR\n\nFOO+\n\n') }) + test('supports handling errors with the `--env` option.', async () => { + const file = ` + console.log((await $\`echo $FOO\`).stdout); + console.log((await $\`echo $BAR\`).stdout) + ` + try { + await $`node build/cli.js --env=./env <<< ${file}` + fs.remove(env) + assert.throw() + } catch (e) { + assert.equal(e.exitCode, 1) + } + }) + test('scripts from https 200', async () => { const resp = await fs.readFile(path.resolve('test/fixtures/echo.http')) const port = await getPort() From 300ed2f9f2c883d6477e49e8fe0888d3b6a17699 Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Tue, 24 Dec 2024 10:36:19 +0300 Subject: [PATCH 15/17] docs: update docs --- docs/cli.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cli.md b/docs/cli.md index 1a2818dd00..43ddbbd9e1 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -125,7 +125,7 @@ Specify a env file. zx --env=/path/to/some.env script.mjs ``` -Also works with `--cwd` options. +When cwd option is specified, it will used as base path: `--cwd='/foo/bar' --env='../.env'` → `/foo/.env` ```bash zx --cwd=/foo/bar --env=/path/to/some.env script.mjs From 37e954007601baba45db2169d895d50390b411c0 Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Tue, 24 Dec 2024 10:37:18 +0300 Subject: [PATCH 16/17] chore: prettify test --- test/cli.test.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/cli.test.js b/test/cli.test.js index aae9af2d4c..a248481193 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -166,8 +166,6 @@ describe('cli', () => { assert.equal(out.stdout, 'BAR\n\nFOO+\n\n') }) - - test('supports `--env` and `--cwd` options and with file', async () => { const env = tmpfile( '.env', @@ -193,11 +191,11 @@ describe('cli', () => { console.log((await $\`echo $BAR\`).stdout) ` try { - await $`node build/cli.js --env=./env <<< ${file}` - fs.remove(env) - assert.throw() + await $`node build/cli.js --env=./env <<< ${file}` + fs.remove(env) + assert.throw() } catch (e) { - assert.equal(e.exitCode, 1) + assert.equal(e.exitCode, 1) } }) From db6252074b1cb2881f04064127b8f11087f0cda8 Mon Sep 17 00:00:00 2001 From: Mikhail Avdeev Date: Tue, 24 Dec 2024 10:49:31 +0300 Subject: [PATCH 17/17] chore: delete dot --- test/cli.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/cli.test.js b/test/cli.test.js index a248481193..ebaa02f93b 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -166,7 +166,7 @@ describe('cli', () => { assert.equal(out.stdout, 'BAR\n\nFOO+\n\n') }) - test('supports `--env` and `--cwd` options and with file', async () => { + test('supports `--env` and `--cwd` options with file', async () => { const env = tmpfile( '.env', `FOO=BAR @@ -185,7 +185,7 @@ describe('cli', () => { assert.equal(out.stdout, 'BAR\n\nFOO+\n\n') }) - test('supports handling errors with the `--env` option.', async () => { + test('supports handling errors with the `--env` option', async () => { const file = ` console.log((await $\`echo $FOO\`).stdout); console.log((await $\`echo $BAR\`).stdout)