diff --git a/lib/bin.js b/lib/bin.js index a7503e7ac..fc369c0b2 100644 --- a/lib/bin.js +++ b/lib/bin.js @@ -4,6 +4,13 @@ import { exec } from './index.js'; import { log } from './log.js'; async function main () { + if (process.env.CHDIR && + process.env.CHDIR !== process.cwd()) { + // allow to override cwd by CHDIR env var + // https://github.com/resin-io/etcher/pull/1713 + process.chdir(process.env.CHDIR); + } + await exec(process.argv.slice(2)); } diff --git a/test/test-50-chdir-env-var/main.js b/test/test-50-chdir-env-var/main.js new file mode 100644 index 000000000..05f91ebfe --- /dev/null +++ b/test/test-50-chdir-env-var/main.js @@ -0,0 +1,32 @@ +#!/usr/bin/env node + +'use strict'; + +const path = require('path'); +const assert = require('assert'); +const utils = require('../utils.js'); + +assert(!module.parent); +assert(__dirname === process.cwd()); + +const target = process.argv[2] || 'host'; +const input = './test-x-index.js'; +const output = './run-time/test-output.exe'; + +let right; + +utils.pkg.sync([ + '--target', target, + '--output', output, input +], { env: { + CHDIR: 'source', + PATH: process.env.PATH +} }); + +right = utils.spawn.sync( + './' + path.basename(output), [], + { cwd: path.dirname('source/' + output) } +); + +assert.equal(right, 'ok\n'); +utils.vacuum.sync(path.dirname('source/' + output)); diff --git a/test/test-50-chdir-env-var/source/test-x-index.js b/test/test-50-chdir-env-var/source/test-x-index.js new file mode 100644 index 000000000..faea9b6d8 --- /dev/null +++ b/test/test-50-chdir-env-var/source/test-x-index.js @@ -0,0 +1,3 @@ +'use strict'; + +console.log('ok');