Skip to content

Commit

Permalink
Ensure parent directory exists when using --output on CLI (#1530)
Browse files Browse the repository at this point in the history
* Ensure parent directory exists when using --output on CLI

Resolves #962

* Add test

* bump

---------

Co-authored-by: Fábio Santos <fabiosantosart@gmail.com>
  • Loading branch information
RyanZim and fabiosantoscode authored Oct 16, 2024
1 parent 97f9619 commit 142f56a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ export async function run_cli({ program, packageJson, fs, path }) {
return;
}
} else if (program.output) {
fs.mkdirSync(path.dirname(program.output), { recursive: true });
fs.writeFileSync(program.output, result.code);
if (options.sourceMap && options.sourceMap.url !== "inline" && result.map) {
fs.writeFileSync(program.output + ".map", result.map);
Expand Down
10 changes: 10 additions & 0 deletions test/mocha/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,14 @@ describe("bin/terser", function() {
done();
});
});
it("Should output even when parent directory doesn't exist", function(done) {
var outputFile = "tmp/does-not-exist/out.js";
var command = tersercmd + " test/input/defaults/input.js -o " + outputFile;

exec(command, function (err) {
if (err) throw err;
assert.strictEqual(fs.readFileSync(outputFile, "utf8"), "if(true){console.log(1+2)}")
done();
});
});
});

0 comments on commit 142f56a

Please sign in to comment.